-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
122 lines (94 loc) · 2.01 KB
/
Copy pathdatabase.cpp
File metadata and controls
122 lines (94 loc) · 2.01 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
#include "database.h"
#include "logger.h"
#include "utils.h"
#include "config.h"
#include <sys/stat.h>
#include <sys/types.h>
Database* Database::self = NULL;
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
for(i = 0; i<argc; i++){
LOG(LOG_INFO, "%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
return 0;
}
Database::Database() : db(NULL), cbData(NULL)
{
}
Database::~Database()
{
if (cbData) {
delete cbData;
}
}
bool Database::Init()
{
if (!self) {
self = new Database();
self->cbData = new CallbackData();
}
if (!self || !self->cbData) {
LOG(LOG_ERROR, "Initializing Database\n");
return false;
}
return self->Open();
}
void Database::Destroy()
{
if (self) {
self->Close();
delete self;
}
}
bool Database::Open()
{
int rc = SQLITE_ERROR;
std::string db_file_name;
std::string _db_file_path;
Utils::GetCurrDateTimeMs(db_file_name);
db_file_name += ".db";
_db_file_path = config.db_file_path;
if (mkdir(_db_file_path.c_str(), 0755)) {
LOG(LOG_ERROR, "Opening DB\n");
return false;
}
if (_db_file_path[_db_file_path.length()-1] != '/') {
_db_file_path += "/";
}
_db_file_path += db_file_name;
rc = sqlite3_open(_db_file_path.c_str(), &db);
if (rc != SQLITE_OK) {
LOG(LOG_ERROR, "%s\n", sqlite3_errmsg(db));
return false;
}
return true;
}
void Database::Close()
{
int rc = SQLITE_ERROR;
rc = sqlite3_close(db);
if (rc != SQLITE_OK) {
LOG(LOG_ERROR, "%s\n", sqlite3_errmsg(db));
}
}
bool Database::Exec(const char *query, void *data)
{
int rc = SQLITE_ERROR;
char *zErrMsg = NULL;
cbData->dbObject = this;
cbData->data = data;
rc = sqlite3_exec(db, query, callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
LOG(LOG_ERROR, "%s\n", sqlite3_errmsg(db));
return false;
}
return true;
}
bool Database::Update(const char *query, void *data)
{
if (!self) {
LOG(LOG_ERROR, "Database Object Missing\n");
return false;
}
return self->Exec(query, data);
}