-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbDatabase.cpp
More file actions
258 lines (195 loc) · 5.69 KB
/
fbDatabase.cpp
File metadata and controls
258 lines (195 loc) · 5.69 KB
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
246
247
248
249
250
251
252
/* $Id: fbDatabase.cpp,v 1.18 2008/04/19 20:37:29 laffer1 Exp $ */
#include "fbDatabase.h"
/**
* fbDatabase
* main database access object
* @author Byron Heads
* @date MARCH 16, 2008
*/
fbDatabase::fbDatabase(fbErrorLogger* log, string path):errlog(log), cs(), db(errlog), row(0)
{
db.connect(path);
errlog->debug(NONE, "fbDatabase.this");
}
fbDatabase::fbDatabase(fbErrorLogger* log, const char* path):errlog(log), cs(), db(errlog), row(0)
{
db.connect(path);
errlog->debug(NONE, "fbDatabase.this");
}
fbDatabase::~fbDatabase()
{
db.close();
errlog->debug(NONE, "fbDatabase.~this");
}
bool fbDatabase::addBackupJob(string& desc, fbDate& date, fbTime& time, string& path, Repeat_type rt, int rv)
{
char buff[1024];
snprintf(buff, sizeof(buff)-1, "insert into backup (desc, date, time, repeatmode, repeatval, disk) values (\'%s\',%ld,%ld,%d,%d,\'%s\');",
desc.c_str(), date.getJulian(), time.getTicks(), rt, rv, path.c_str());
string cmd = buff;
errlog->debug(NONE, cmd);
if(!db.exe(cmd))
{
return false;
errlog->warn(UNKNOWN, "Failed to add Backup Job..");
}
//backup (ID INTEGER PRIMARY KEY, desc TEXT, date INTEGER, time INTEGER, repeatmode INTEGER, repeatval INTEGER, disk TEXT);
return true;
}
bool fbDatabase::addRestoreJob(string& tarfile, string& dest)
{
char buff[4096];
snprintf(buff, sizeof(buff)-1, "insert into restore (tarfile, path) values (\'%s\', \'%s\');", tarfile.c_str(), dest.c_str());
string cmd = buff;
errlog->debug(NONE, cmd);
if(!db.exe(cmd))
{
return false;
errlog->warn(UNKNOWN, "Failed to add Restore Job..");
}
//restore (ID INTEGER PRIMARY KEY, tarfile TEXT, path TEXT);
return true;
}
bool fbDatabase::addRepo(string& desc, fbDate& date, fbTime& time, string& path, string& tarfile)
{
char buff[1024];
snprintf(buff, sizeof(buff)-1, "insert into repo (desc, date, time, path, tarfile) values (\'%s\', %ld, %ld, \'%s\', \'%s\');",
desc.c_str(), date.getJulian(), time.getTicks(), path.c_str(), tarfile.c_str());
string cmd = buff;
errlog->debug(NONE, cmd);
if(!db.exe(cmd))
{
return false;
errlog->warn(UNKNOWN, "Failed to add Restore Job..");
}
//repo (ID INTEGER PRIMARY KEY, desc TEXT, date INTEGER, time INTEGER, path TEXT, tarfile TEXT);
return true;
}
bool fbDatabase::queryBackups()
{
char buff[1024];
fbDate date;
fbTime time;
snprintf(buff, sizeof(buff)-1, "select * from backup where date <= %ld AND time <= %ld;",
date.getJulian(), time.getTicks());
string cmd = buff;
errlog->debug(NONE, cmd);
db.query(cmd);
errlog->debug(NONE, "Query Done");
row = 0;
return true;
}
bool fbDatabase::queryRestore()
{
string cmd = "select * from restore;";
errlog->debug(NONE, cmd);
db.query(cmd);
errlog->debug(NONE, "Query Done");
row = 0;
return true;
}
bool fbDatabase::queryRepo()
{
string cmd = "select * from repo;";
errlog->debug(NONE, cmd);
db.query(cmd);
errlog->debug(NONE, "Query Done");
row = 0;
return true;
}
string & fbDatabase::getBackupList()
{
string *out;
errlog->debug(NONE, "fbDatabase: Getting List");
string cmd = "select * from backup;";
errlog->debug(NONE, cmd);
db.query(cmd);
out = new string;
for ( int i = 0; i < db.rows(); i++ )
{
int index = db.cols() * i;
out->append(db.table[index++].c_str()); // id
out->append("\t");
out->append(db.table[index++].c_str()); //date
out->append("\t");
out->append(db.table[index++].c_str()); // time
out->append("\t");
out->append(db.table[index++].c_str()); // rt
out->append("\t");
out->append(db.table[index++].c_str()); // rv
out->append("\t");
out->append(db.table[index++].c_str()); // path
out->append("\n");
}
db.queryDone();
return *out;
}
bool fbDatabase::getBackupRow(string& desc, fbDate& date, fbTime& time, string& path,
Repeat_type* rt, int* rv, int* id)
{
errlog->debug(NONE, "fbDatabase: Getting Backup Rows");
if(row >= db.rows())
{
errlog->debug(NONE, "fbDatabase: No Backup Rows To Get");
db.queryDone();
return false;
}
int index = db.cols() * row;
errlog->debug(NONE, "fbDatabase: Found a ROW!");
*id = atoi(db.table[index++].c_str());
desc = db.table[index++];
date.setJulian(atol(db.table[index++].c_str()));
time.setTicks(atol(db.table[index++].c_str()));
*rt = (Repeat_type)atoi(db.table[index++].c_str());
*rv = atoi(db.table[index++].c_str());
path = db.table[index++];
++row;
errlog->debug(NONE, "fbDatabase: Got Row");
return true;
}
bool fbDatabase::getRestoreRow(string& tarfile, string& path, int* id)
{
errlog->debug(NONE, "fbDatabase: Getting Restore Rows");
if(row >= db.rows())
{
errlog->debug(NONE, "fbDatabase: No Restore Rows To Get");
db.queryDone();
return false;
}
int index = db.cols() * row;
errlog->debug(NONE, "fbDatabase: Found a ROW!");
*id = atoi(db.table[index++].c_str());
tarfile = db.table[index++];
path = db.table[index++];
++row;
errlog->debug(NONE, "fbDatabase: Got Row");
return true;
}
bool fbDatabase::getRepoRow(string& desc, fbDate& date, fbTime& time, string& path, string& tarfile, int* id)
{
errlog->debug(NONE, "fbDatabase: Getting Repo Rows");
if(row >= db.rows())
{
errlog->debug(NONE, "fbDatabase: No Repo Rows To Get");
db.queryDone();
return false;
}
int index = db.cols() * row;
errlog->debug(NONE, "fbDatabase: Found a ROW!");
*id = atoi(db.table[index++].c_str());
desc = db.table[index++];
date.setJulian(atol(db.table[index++].c_str()));
time.setTicks(atol(db.table[index++].c_str()));
path = db.table[index++];
tarfile = db.table[index++];
++row;
errlog->debug(NONE, "fbDatabase: Got Row");
return true;
}
bool fbDatabase::deleteRow(const char* table, int id)
{
char buff[1024];
snprintf(buff, sizeof(buff)-1, "DELETE FROM %s WHERE ID = %d;", table, id);
string cmd = buff;
return db.exe(cmd);
}