Newer
Older
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
#include "sls.hh"
#include "database.hh"
#include "error.hh"
// *****************************************************************************
// Public Method
// *****************************************************************************
Database::Database() : db_(0) {}
void Database::open(const string &path) {
if (path.empty())
throw Error(ERR_DB, "DB filename is empty");
if (db_)
throw Error(ERR_DB, "DB already open");
// Try to open db
if (sqlite3_open(path.c_str(), &db_)) {
string msg = (string) "Can't open database: " + sqlite3_errmsg(db_);
sqlite3_close(db_);
db_ = 0;
throw Error(ERR_DB, msg);
}
// load structs
load_hosts_();
load_groups_();
load_users_();
load_scripts_();
load_aliases_();
}
void Database::close() {
if (!db_)
throw Error(ERR_DB, "DB not open");
// save structs
save_hosts_();
save_groups_();
save_users_();
save_scripts_();
save_aliases_();
// close db
sqlite3_close(db_);
db_ = 0;
}
// *****************************************************************************
// Protected Method
// *****************************************************************************
void Database::exec(const string &sql) {
if (sql.empty())
throw Error(ERR_DB, "SQL command is empty");
if (!db_)
throw Error(ERR_DB, "DB is not open");
char *ErrMsg = 0;
if (sqlite3_exec(db_, sql.c_str(), 0, 0, &ErrMsg) != SQLITE_OK) {
string msg = (string) "SQL error: %s\n" + ErrMsg;
sqlite3_free(ErrMsg);
throw Error(ERR_DB, msg);
}
}
void Database::load_hosts_() {
// create host table if not exists
exec("BEGIN TRANSACTION;");
exec("CREATE TABLE IF NOT EXISTS 'host' ( 'login' TEXT, 'pass' TEXT, 'ipv4' TEXT, 'level' NUMERIC );");
exec("COMMIT;");
}
void Database::load_groups_() {
// create group table if not exists
exec("BEGIN TRANSACTION;");
exec("CREATE TABLE IF NOT EXISTS 'group' ( 'name' TEXT, 'hosts' TEXT );");
exec("COMMIT;");
}
void Database::load_users_() {
// create user table if not exists
exec("BEGIN TRANSACTION;");
exec("CREATE TABLE IF NOT EXISTS 'user' ('login' TEXT, 'pass' TEXT, 'level' NUMERIC, 'priv' NUMERIC );");
exec("COMMIT;");
}
void Database::load_scripts_() {
// create script table if not exists
exec("BEGIN TRANSACTION;");
exec("CREATE TABLE IF NOT EXISTS 'script' ( 'name' TEXT, 'path' TEXT, 'level' NUMERIC );");
exec("COMMIT;");
}
void Database::load_aliases_() {
// create alias table if not exists
exec("BEGIN TRANSACTION;");
exec("CREATE TABLE IF NOT EXISTS 'alias' ( 'name' TEXT, 'value' TEXT );");
exec("COMMIT;");
}
void Database::save_hosts_() {
}
void Database::save_groups_() {
}
void Database::save_users_() {
}
void Database::save_scripts_() {
}
void Database::save_aliases_() {
}