-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdb_startup.cpp
245 lines (190 loc) · 7.62 KB
/
db_startup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "db_startup.hpp"
#include "../model-sys/structures.hpp"
#include "../model-sys/protocol.hpp"
#include "../model-sys/access_level.hpp"
#include "../model-app/oneliners.hpp"
#include "base_dao.hpp"
#include "protocol_dao.hpp"
#include "access_level_dao.hpp"
#include "../data-app/oneliners_dao.hpp"
#include "../logging.hpp"
// Needed for Initializing and checking users data is setup
// On startup.
#include "session_stats_dao.hpp"
#include "security_dao.hpp"
#include "users_dao.hpp"
#include "libSqliteWrapped.h"
#include <cassert>
DbStartup::DbStartup()
{
}
DbStartup::~DbStartup()
{
}
/**
* @brief Setup for Database and Tables
*/
bool DbStartup::initDatabaseTables()
{
// Setup Users Database name and path
USERS_DATABASE = GLOBAL_DATA_PATH;
#ifdef _WIN32
USERS_DATABASE.append("\\");
#else
USERS_DATABASE.append("/");
#endif
USERS_DATABASE.append("xrm_users.sqlite3");
Logging &log = Logging::getInstance();
// Setup isolated scope for smart pointers and clean up.
{
// Check and Setup users database if tables are not setup
SQLW::Database user_database(USERS_DATABASE);
// Link to users dao for data access object
UsersDao user_dao(user_database);
// Link to security dao for data access object
SecurityDao security_dao(user_database);
// Verify if the security table exists.
// Security must be present before user because of foreign key.
if(!security_dao.doesTableExist())
{
log.write<Logging::CONSOLE_LOG>("doesn't exist (security table).");
// Setup database Param, cache sizes etc..
if(!security_dao.firstTimeSetupParams())
{
log.write<Logging::ERROR_LOG>("unable to execute firstTimeSetupParams (security table).");
return(false);
}
// Setup create users table and indexes.
if(!security_dao.createTable())
{
log.write<Logging::ERROR_LOG>("unable to create (security table).");
return(false);
}
log.write<Logging::CONSOLE_LOG>("security table created successfully.");
}
// Verify if the user table exists.
if(!user_dao.doesTableExist())
{
log.write<Logging::CONSOLE_LOG>("doesn't exist (user table).");
// Setup database Param, cache sizes etc..
if(!user_dao.firstTimeSetupParams())
{
log.write<Logging::ERROR_LOG>("unable to execute firstTimeSetupParams (user table).");
return(false);
}
// Setup create users table and indexes.
if(!user_dao.createTable())
{
log.write<Logging::ERROR_LOG>("unable to create (user table).");
return(false);
}
log.write<Logging::CONSOLE_LOG>("user table created successfully.");
}
// Check Table setup for Session Stats
SessionStatsDao session_stat_dao(user_database);
// Verify if the user table exists.
if(!session_stat_dao.doesTableExist())
{
log.write<Logging::CONSOLE_LOG>("doesn't exist (session stats table).");
// Setup database Param, cache sies etc..
if(!session_stat_dao.firstTimeSetupParams())
{
log.write<Logging::ERROR_LOG>("unable to execute firstTimeSetupParams (session stats table).");
return(false);
}
// Setup create users table and indexes.
if(!session_stat_dao.createTable())
{
log.write<Logging::ERROR_LOG>("unable to create (session stats table).");
return(false);
}
log.write<Logging::CONSOLE_LOG>("session stats table created successfully.");
}
// Link to Access Level dao for data access object
AccessLevelDao access_dao(user_database);
// Verify if the access_level table exists.
if(!access_dao.doesTableExist())
{
log.write<Logging::CONSOLE_LOG>("doesn't exist (access_level table).");
// Setup database Param, cache sizes etc..
if(!access_dao.firstTimeSetupParams())
{
log.write<Logging::ERROR_LOG>("unable to execute firstTimeSetupParams (access_level table).");
return(false);
}
// Setup create users table and indexes.
if(!access_dao.createTable())
{
log.write<Logging::ERROR_LOG>("unable to create (access_level table).");
return(false);
}
log.write<Logging::CONSOLE_LOG>("access_level table created successfully.");
// Check and Setup default Access Levels.
access_level_ptr level = std::make_shared<AccessLevel>();
// Set Initial Defaults for Not Validated Level
// the reest are populated on Class Defaults.
level->sName = "Not Validated";
level->sStartMenu ="top";
level->iLevel = 10;
level->iTimeLimit = 120;
level->bTimeLimit = true;
access_dao.insertRecord(level);
// Validated User
level.reset();
level = std::make_shared<AccessLevel>();
level->sName = "Validated User";
level->sStartMenu ="top";
level->iLevel = 20;
level->iTimeLimit = 1440;
level->bTimeLimit = true;
access_dao.insertRecord(level);
// Administrator (time Limit false by default)
level.reset();
level = std::make_shared<AccessLevel>();
level->sName = "Sysop";
level->sStartMenu ="top";
level->iLevel = 255;
level->iTimeLimit = 1440;
access_dao.insertRecord(level);
}
protocols_ptr prots = std::make_shared<Protocols>();
ProtocolDao protdb(prots, GLOBAL_DATA_PATH);
if(!protdb.fileExists())
{
log.write<Logging::CONSOLE_LOG>("Protocol configuration doesn't exist.");
// Create Genric Protocol Entry to Test File Creation
Protocol p1("Sexyz", "D", "Z", "C:\\TESTPATH\\", "--Test", false, false);
prots->protocols.push_back(p1);
protdb.saveConfig(prots);
log.write<Logging::CONSOLE_LOG>("Protocol configuration created successfully");
}
OnelinerDao oneLineDao(user_database);
if(!oneLineDao.doesTableExist())
{
log.write<Logging::CONSOLE_LOG>("doesn't exist (oneliner table).");
// Setup database Param, cache sizes etc..
if(!oneLineDao.firstTimeSetupParams())
{
log.write<Logging::ERROR_LOG>("unable to execute firstTimeSetupParams (oneliner table).");
return(false);
}
// Setup create users table and indexes.
if(!oneLineDao.createTable())
{
log.write<Logging::ERROR_LOG>("unable to create (oneliner table).");
return(false);
}
log.write<Logging::CONSOLE_LOG>("oneliner table created successfully.");
// Insert a default record the first time the table
// is created only.
oneliner_ptr one = std::make_shared<Oneliners>();
one->iUserId = 1;
one->sText = "Welcome to a new system running Oblivion/2 XRM";
one->sUserInitials = "MF";
one->sUserName = "Mercyful Fate";
//one->dtDatePosted
oneLineDao.insertRecord(one);
}
}
return true;
}