-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhlib.cpp
More file actions
607 lines (495 loc) · 12.8 KB
/
hlib.cpp
File metadata and controls
607 lines (495 loc) · 12.8 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//---------------------------------------------------------- -
//Copyright(c) 2020. Tawanda M.Nyoni(hkay dot tee at outlook dot com)
//
////VERSION !.0.0
//
//
//This file is part of the Hlib library which is released
//under the Creative Commons Attribution Non - Commercial
//2.0 Generic license(CC BY - NC 2.0).
//
//See accompanying file CC - BY - NC - 2.0.txt
//
//Below is a short summary of what this license means.
//
//Under this license, you are free to :
//------------------------------------
//SHARE : copy and redistribute the material in any medium or format.
// ADAPT : remix, transform, and build upon the material.
//
// Under the following terms :
//--------------------------
//ATTRIBUTION : You must give appropriate credit, provide a link to the
// license, and indicate if changes were made.You may do so in any
// reasonable manner, but not in any way that suggests the licensor endorses
// you or your use.
// NON - COMMERCIAL : You may not use the material for commercial purposes.For
// a commercial license contact the copyright holder using the contact
// information provided in this file.
// NO ADDITIONAL RESTRICTIONS : You may not apply legal terms or technological
// measures that legally restrict others from doing anything the license
// permits.
//----------------------------------------------------------------------------------
#include "hlib.h"
#include "sqlite3.h"
#include "picosha2.h"
using table = std::vector<std::map<std::string, std::string>>;
class hlib::hbase::hbase_impl {
friend hbase;
bool connected_;
sqlite3* db_;
public:
hbase_impl() :
connected_(false),
db_(nullptr) {}
~hbase_impl() {
if (db_) {
// close database
sqlite3_close(db_);
db_ = nullptr;
}
}
std::string sqlite_error(int error_code) {
std::string error = sqlite3_errstr(error_code);
if (error == "not an error") error.clear();
if (error.length() > 0) error[0] = toupper(error[0]);
return error;
}
std::string sqlite_error() {
if (db_) {
std::string error = sqlite3_errmsg(db_);
if (error == "not an error") error.clear();
if (error.length() > 0) error[0] = toupper(error[0]);
return error;
}
else {
return "Database not open"; return false;
}
}
bool sqlite_query(const std::string& query,
table& table,
std::string& error) {
table.clear();
if (db_) {
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, query.c_str(), -1, &statement, 0) == SQLITE_OK) {
const int columns = sqlite3_column_count(statement);
while (true) {
if (sqlite3_step(statement) == SQLITE_ROW) {
std::map<std::string, std::string> values;
for (int column = 0; column < columns; column++) {
std::string column_name, value;
// get column name
char* ccColumn = (char*)sqlite3_column_name(statement, column);
if (ccColumn) {
column_name = ccColumn;
// get data
char* ccData = (char*)sqlite3_column_text(statement, column);
if (ccData)
value = ccData;
values.insert(std::make_pair(column_name, value));
}
}
table.push_back(values);
}
else
break;
}
sqlite3_finalize(statement);
}
else {
error = sqlite_error();
return false;
}
return true;
}
else {
error = "Database not open";
return false;
}
}
std::string type_to_string(hbase::column_type_ type) {
std::string _type;
switch (type)
{
case column_type_::blob_:
_type = "BLOB";
break;
case column_type_::float_:
_type = "FLOAT";
break;
case column_type_::integer_:
_type = "INTEGER";
break;
case column_type_::text_:
default:
_type = "TEXT";
break;
}
return _type;
}
std::string constraint_to_string(constraint_ constraint) {
std::string _constraint;
switch (constraint)
{
case constraint_::not_null:
_constraint = "NOT NULL";
break;
default:
_constraint = "NULL";
break;
}
return _constraint;
}
};
bool hlib::hbase::connect(const file_& file,
std::vector<table_>& tables,
std::string& error) {
if (d_.connected_)
return true;
int error_code = 0;
if (file.password.empty()) {
error_code = sqlite3_open_v2(file.name.c_str(), &d_.db_,
SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL);
error_code = sqlite3_exec(d_.db_, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL);
}
else {
error_code = sqlite3_open_v2(file.name.c_str(), &d_.db_,
SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL);
sqlite3_stmt* stm = nullptr;
const char* pzTail = nullptr;
// key the database
auto pragma_string = "PRAGMA key = '" + file.password + "';";
sqlite3_prepare(d_.db_, pragma_string.c_str(), -1, &stm, &pzTail);
sqlite3_step(stm);
sqlite3_finalize(stm);
// test if key is correct
error_code = sqlite3_exec(d_.db_, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL);
}
if (error_code != SQLITE_OK) {
// an error occured
error = d_.sqlite_error(error_code);
// close database
sqlite3_close(d_.db_);
d_.db_ = nullptr;
return false;
}
// create tables
table table;
for (const auto& table_ : tables) {
std::string sql = "CREATE TABLE " + table_.name + "(";
for (const auto& col : table_.columns)
sql += col.name + " " + d_.type_to_string(col.type) + " " + d_.constraint_to_string(col.constraint) + ",";
std::string composite_key;
size_t count_keys = 1;
for (const auto key : table_.primary_key) {
composite_key += key;
if (count_keys < table_.primary_key.size()) {
composite_key += ',';
}
count_keys++;
}
sql += "PRIMARY KEY (" + composite_key + "));";
if (!d_.sqlite_query(sql, table, error))
if (error.find("already exists") == std::string::npos)
return false;
sql.clear();
}
d_.connected_ = true;
return true;
}
bool hlib::hbase::insert_row(std::vector<field_>& row,
const std::string& table_name,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
size_t index = 1;
std::string colums;
std::string values;
for (const auto& field : row) {
colums += field.name;
values += "'"+ field.value +"'";
if (index < row.size()) {
colums += ",";
values += ",";
}
index++;
}
std::string sql = "INSERT INTO " + table_name + "(";
sql += (colums + ") VALUES (" + values + ");");
table table;
if (!d_.sqlite_query(sql, table, error))
return false;
return true;
}
bool hlib::hbase::delete_row(const field_& field,
const std::string& table_name,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
table table_;
std::string sql = "DELETE FROM " + table_name;
sql += " WHERE " + field.name + " = '" + field.value + "';";
if (!d_.sqlite_query(sql ,table_, error))
return false;
return true;
}
bool hlib::hbase::count_records(const field_& field,
const std::string& table_name,
size_t& records,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
table table_;
std::string sql = "SELECT COUNT(*) FROM " + table_name;
sql += " WHERE " + field.name + " = '" + field.value + "';";
if (!d_.sqlite_query(sql, table_, error))
return false;
try {
if (!table_.empty())
records = std::atoi(table_[0].at("COUNT(*)").c_str());
else {
error = "The table is empty!";
return false;
}
return true;
}
catch (const std::exception & e) {
error = e.what();
return false;
}
}
bool hlib::hbase::count_records(const std::string& table_name,
size_t& records,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
table table_;
std::string sql = "SELECT COUNT(*) FROM '" + table_name + "';";
if (!d_.sqlite_query(sql, table_, error))
return false;
try {
if (!table_.empty())
records = std::atoi(table_[0].at("COUNT(*)").c_str());
else {
error = "The table is empty!";
return false;
}
return true;
}
catch (const std::exception & e) {
error = e.what();
return false;
}
}
bool hlib::hbase::get_records(table& records,
const std::vector<field_>& compound_keys,
const std::string& table_name,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
std::string keys;
int indx = 1;
for (auto key : compound_keys) {
keys += key.name + " = '" + key.value +"'";
if (indx < compound_keys.size())
keys += " AND ";
indx++;
}
table table_;
std::string sql = "SELECT * FROM " + table_name;
sql += " WHERE " + keys + ";";
if (!d_.sqlite_query(sql, table_, error))
return false;
if (!table_.empty()) {
for (const auto& row : table_)
records.push_back(row);
return true;
}
else {
error = "the table is empty!";
return false;
}
}
bool hlib::hbase::get_records(table& records,
const std::string& table_name,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
table table_;
const std::string sql = "SELECT * FROM '" + table_name + "';";
if (!d_.sqlite_query(sql, table_, error))
return false;
if (!table_.empty()) {
for (const auto row : table_)
records.push_back(row);
return true;
}
else {
error = "the table is empty!";
return false;
}
}
bool hlib::hbase::get_records_with_sort_by(table& records,
const field_& sort_by_field,
const std::string& table_name,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
table table_;
std::string sql = "SELECT * FROM " + table_name;
sql += " ORDER BY '" + sort_by_field.name + "';";
if (!d_.sqlite_query(sql, table_, error))
return false;
if (!table_.empty()) {
for (const auto& row : table_)
records.push_back(row);
return true;
}
else {
error = "the table is empty!";
return false;
}
}
bool hlib::hbase::get_records_with_and_sort_by(table& records,
const std::vector<field_>& compound_keys,
const field_& sort_by_field,
const std::string& table_name,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
std::string keys;
int indx = 1;
for (auto key : compound_keys) {
keys += key.name + " = '" + key.value + "'";
if (indx < compound_keys.size())
keys += " AND ";
indx++;
}
table table_;
std::string sql = "SELECT * FROM " + table_name;
sql += " WHERE " + keys + " ORDER BY '" + sort_by_field.name + "';";
if (!d_.sqlite_query(sql, table_, error))
return false;
if (!table_.empty()) {
for (const auto& row : table_)
records.push_back(row);
return true;
}
else {
error = "the table is empty!";
return false;
}
}
bool hlib::hbase::get_records_using_custom_query(table& records,
const std::string& custom_query_statement,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
table table_;
if (!d_.sqlite_query(custom_query_statement, table_, error))
return false;
if (!table_.empty()) {
for (const auto& row : table_)
records.push_back(row);
return true;
}
else {
error = "the table is empty!";
return false;
}
}
bool hlib::hbase::get_records_with_or_sort_by(table& records,
const std::vector<field_>& compound_keys,
const field_& sort_by_field,
const std::string& table_name,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
std::string keys;
int indx = 1;
for (auto key : compound_keys) {
keys += key.name + " = '" + key.value + "'";
if (indx < compound_keys.size())
keys += " OR ";
indx++;
}
table table_;
std::string sql = "SELECT * FROM " + table_name;
sql += " WHERE " + keys + " ORDER BY '" + sort_by_field.name + "';";
if (!d_.sqlite_query(sql, table_, error))
return false;
if (!table_.empty()) {
for (const auto& row : table_)
records.push_back(row);
return true;
}
else {
error = "the table is empty!";
return false;
}
}
bool hlib::hbase::custom_query(const std::string& custom_query_, std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
table table_;
if (!d_.sqlite_query(custom_query_, table_, error))
return false;
if (!table_.empty())
return true;
else {
error = "the table is empty!";
return false;
}
}
bool hlib::hbase::update_record(const field_ field,
std::vector<field_>& row_update,
const std::string& table_name,
std::string& error) {
if (!d_.connected_) {
error = "Not connected to database";
return false;
}
size_t index = 1;
std::string fields;
for (auto field_ : row_update) {
fields += field_.name + " = '" + field_.value + "'";
if (index < row_update.size())
fields += ",";
index++;
}
table table_;
std::string sql = "UPDATE " + table_name + " SET ";
sql += fields + " WHERE " + field.name + " = '" + field.value +"';";
;
if (!d_.sqlite_query(sql, table_, error))
return false;
return true;
}
hlib::hbase::hbase() :
d_(*new hbase_impl()) {}
hlib::hbase::~hbase() {
delete& d_;
}