-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
175 lines (138 loc) · 3.46 KB
/
Copy pathdatabase.cpp
File metadata and controls
175 lines (138 loc) · 3.46 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
/*
*
* (C) 2012 Markus Dittrich
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License Version 3 as published by the Free Software Foundation.
*
* This program 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 Version 3 for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include <iostream>
#include <sqlcipher.h>
#include <string>
#include <vector>
#include "database.hpp"
using std::cout;
using std::endl;
using std::string;
using std::vector;
////////////////////////////////////////////////////////////////////////
//
// public interface
//
////////////////////////////////////////////////////////////////////////
DataBase::DataBase(string databaseName, string password, bool verbose) :
db_(nullptr),
success_{true},
verbose_{verbose} {
if (openDatabase_(databaseName, password) != 0) {
success_ = false;
}
}
DataBase::~DataBase() {
close();
sqlite3_shutdown();
}
//
// main workhorse - interacts with database
//
QueryResult DataBase::query(string query) {
// re-initialize success
success_ = true;
sqlite3_stmt* statement;
vector<vector<string>> results;
if(sqlite3_prepare_v2(db_, query.c_str(), -1, &statement, 0)
== SQLITE_OK) {
int cols = sqlite3_column_count(statement);
int result = 0;
while(true) {
result = sqlite3_step(statement);
if(result == SQLITE_ROW) {
vector<string> values;
for(int col = 0; col < cols; col++) {
const char* content = reinterpret_cast<const char*>(
sqlite3_column_text(statement, col));
if (content) {
values.push_back(content);
}
}
results.push_back(values);
} else {
break;
}
}
sqlite3_finalize(statement);
}
string error = sqlite3_errmsg(db_);
if (verbose_ && error != "not an error") {
cout << query << " " << error << endl;
success_ = false;
}
return results;
}
//
// check if table exists in database; returns true on success
// and false otherwise
//
bool
DataBase::hasTable(string tableName)
{
string checkQuery{"SELECT 1 FROM "};
checkQuery += tableName;
checkQuery += ";";
QueryResult result = query(checkQuery);
return !result.empty();
}
//
// function for checking if a database query was
// successful
//
bool
DataBase::success()
{
return success_;
}
//
// close database
//
void
DataBase::close()
{
sqlite3_close(db_);
}
///////////////////////////////////////////////////////////////////////
//
// private interface
//
///////////////////////////////////////////////////////////////////////
//
// open database; return 0 on success and 1 on failure
//
int
DataBase::openDatabase_(string name, string password)
{
sqlite3_initialize();
int rc = sqlite3_open_v2(name.c_str(), &db_, SQLITE_OPEN_READWRITE |
SQLITE_OPEN_CREATE, NULL);
if (rc != SQLITE_OK)
{
sqlite3_close(db_);
return 1;
}
if (sqlite3_key(db_, reinterpret_cast<const void*>(password.c_str()),
password.size()) != SQLITE_OK)
{
return 1;
}
return 0;
}