Newer
Older
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
SLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SLS; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "sls.hh"
#include "database.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_();
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
}
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_() {
}