/
DbTab.cpp
549 lines (429 loc) · 14.4 KB
/
DbTab.cpp
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
/*
* Copyright © 2014 Volker Knollmann
*
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License, Version 2,
* as published by Sam Hocevar. See the COPYING file or visit
* http://www.wtfpl.net/ for more details.
*
* This program comes without any warranty. Use it at your own risk or
* don't use it at all.
*/
#include <stdint.h> // for int64_t
#include <algorithm> // for max
#include <cstddef> // for size_t, std
#include <memory> // for unique_ptr, make_unique
#include <Sloppy/CSV.h> // for CSV_Table, CSV_Value, CSV_Value::Type
#include <Sloppy/String.h> // for estring
#include <Sloppy/Utils.h> // for trimAndCheckString
#include "TabRow.h" // for TabRow
#include "Transaction.h" // for Transaction
#include "DbTab.h"
using namespace std;
namespace SqliteOverlay
{
DbTab::DbTab(const SqliteDatabase& _db, const string& _tabName, bool forceNameCheck)
: CommonTabularClass (_db, _tabName, false, forceNameCheck),
cachedSelectSql{"SELECT rowid FROM " + tabName + " WHERE "}
{
}
//----------------------------------------------------------------------------
int DbTab::insertRow(const ColumnValueClause& ic) const
{
SqlStatement stmt = ic.getInsertStmt(db, tabName);
db.get().execNonQuery(stmt);
// insert succeeded, otherwise the commands
// above would have thrown an exception
return db.get().getLastInsertId();
}
//----------------------------------------------------------------------------
int DbTab::insertRow() const
{
const ColumnValueClause empty;
return insertRow(empty);
}
//----------------------------------------------------------------------------
TabRow DbTab::operator [](const int id) const
{
return TabRow(db, tabName, id, true);
}
//----------------------------------------------------------------------------
TabRow DbTab::operator [](const WhereClause& w) const
{
return TabRow(db, tabName, w);
}
//----------------------------------------------------------------------------
optional<TabRow> DbTab::get2(const int id) const
{
if (!hasRowId(id)) return optional<TabRow>{};
return TabRow(db, tabName, id, true);
}
//----------------------------------------------------------------------------
optional<TabRow> DbTab::get2(const WhereClause& w) const
{
return getSingleRowByWhereClause2(w);
}
//----------------------------------------------------------------------------
TabRow DbTab::getSingleRowByColumnValueNull(const string& col) const
{
try
{
const int id = db.get().execScalarQuery<int>(cachedSelectSql + col + " IS NULL");
return TabRow(db, tabName, id, true);
}
catch (SqlStatementCreationError)
{
throw;
}
}
//----------------------------------------------------------------------------
optional<TabRow> DbTab::getSingleRowByColumnValueNull2(const string& col) const
{
//
// No error handling here... BUSY etc. should be handled by the caller
//
auto stmt = db.get().prepStatement(cachedSelectSql + col + " IS NULL");
stmt.step();
if (!stmt.hasData()) return optional<TabRow>{};
return TabRow(db, tabName, stmt.get<int>(0), true);
}
//----------------------------------------------------------------------------
vector<TabRow> DbTab::getRowsByWhereClause(const WhereClause& w) const
{
if (w.isEmpty())
{
throw std::invalid_argument("getRowsByWhereClause() called with empty WHERE");
}
try
{
auto stmt = w.getSelectStmt(db, tabName, false);
return statementResultsToVector(stmt);
}
catch (SqlStatementCreationError)
{
return vector<TabRow>{};
}
}
//----------------------------------------------------------------------------
vector<TabRow> DbTab::getRowsByWhereClause(const string& w) const
{
if (w.empty())
{
throw std::invalid_argument("getRowsByWhereClause() called with empty WHERE");
}
try
{
auto stmt = db.get().prepStatement(cachedSelectSql + w);
return statementResultsToVector(stmt);
}
catch (SqlStatementCreationError)
{
return vector<TabRow>{};
}
}
//----------------------------------------------------------------------------
vector<TabRow> DbTab::getRowsByColumnValueNull(const string& col) const
{
if (col.empty())
{
throw std::invalid_argument("getRowsByColumnValueNull() called with empty colum name");
}
try
{
SqlStatement stmt = db.get().prepStatement(cachedSelectSql + col + " IS NULL");
return statementResultsToVector(stmt);
}
catch (SqlStatementCreationError)
{
return vector<TabRow>{};
}
}
//----------------------------------------------------------------------------
vector<TabRow> DbTab::getAllRows() const
{
const string sql = "SELECT rowid FROM " + tabName;
auto stmt = db.get().prepStatement(sql);
return statementResultsToVector(stmt);
}
//----------------------------------------------------------------------------
TabRow DbTab::getSingleRowByWhereClause(const WhereClause& w) const
{
try
{
return TabRow(db, tabName, w);
}
catch (std::invalid_argument&)
{
throw NoDataException();
}
}
//----------------------------------------------------------------------------
TabRow DbTab::getSingleRowByWhereClause(const string& w) const
{
const string sql = cachedSelectSql + w + " LIMIT 1";
auto stmt = db.get().prepStatement(sql);
stmt.step();
if (!(stmt.hasData()))
{
throw NoDataException();
}
return TabRow(db, tabName, stmt.get<int>(0), true);
}
//----------------------------------------------------------------------------
optional<TabRow> DbTab::getSingleRowByWhereClause2(const WhereClause& w) const
{
auto stmt = w.getSelectStmt(db.get(), tabName, false);
stmt.step();
if (!(stmt.hasData()))
{
return std::nullopt;
}
return TabRow(db, tabName, stmt.get<int>(0), true);
}
//----------------------------------------------------------------------------
optional<TabRow> DbTab::getSingleRowByWhereClause2(const string& w) const
{
const string sql = cachedSelectSql + w + " LIMIT 1";
auto stmt = db.get().prepStatement(sql);
stmt.step();
if (!(stmt.hasData()))
{
return std::nullopt;
}
return TabRow(db, tabName, stmt.get<int>(0), true);
}
//----------------------------------------------------------------------------
int DbTab::deleteRowsByWhereClause(const WhereClause& where) const
{
if (where.isEmpty())
{
throw std::invalid_argument("deleteRowsByWhereClause(): called with empty WHERE");
}
auto stmt = where.getDeleteStmt(db, tabName);
db.get().execNonQuery(stmt);
// delete succeeded
return db.get().getRowsAffected();
}
//----------------------------------------------------------------------------
int DbTab::clear() const
{
SqlStatement stmt = db.get().prepStatement("DELETE FROM " + tabName);
db.get().execNonQuery(stmt);
// delete succeeded
return db.get().getRowsAffected();
}
//----------------------------------------------------------------------------
bool DbTab::hasRowId(int id) const
{
SqlStatement stmt = db.get().prepStatement(cachedSelectSql + " rowid=" + to_string(id));
stmt.step();
return stmt.hasData();
}
//----------------------------------------------------------------------------
vector<int> DbTab::checkConstraint(const string& colName, Sloppy::ValueConstraint c, int firstRowId) const
{
vector<int> result;
Sloppy::estring sql = "SELECT rowid,%1 FROM %2 WHERE rowid >= %3 ORDER BY rowid ASC";
sql.arg(colName);
sql.arg(tabName);
sql.arg(firstRowId);
SqlStatement stmt = db.get().prepStatement(sql);
for (stmt.step() ; stmt.hasData() ; stmt.step())
{
optional<string> val;
try
{
val = stmt.get<std::string>(1);
}
catch (NullValueException)
{
val.reset();
}
if (!Sloppy::checkConstraint(val, c))
{
result.push_back(stmt.get<int>(0));
}
}
return result;
}
//----------------------------------------------------------------------------
TabRowIterator DbTab::tabRowIterator(int minRowId, int maxRowId)
{
return TabRowIterator(db.get(), tabName, minRowId, maxRowId);
}
//----------------------------------------------------------------------------
TabRowIterator DbTab::tabRowIterator(const WhereClause& w)
{
return TabRowIterator(db.get(), tabName, w);
}
//----------------------------------------------------------------------------
int DbTab::importCSV(const Sloppy::CSV_Table& csvTab, TransactionType tt) const
{
if (csvTab.empty()) return 0;
if (!csvTab.hasHeaders())
{
throw std::invalid_argument{"DbTab::importCSV(): called with invalid CSV data (no column names)"};
}
// prepare an SQL string that contains all column names
// and a bind parameter for each column
//
// quote column names to provide a little more resistance against
// ugly / unsecure / dangerous strings provided by untrusted users
Sloppy::estring sql = "INSERT INTO " + tabName +
" (\"" + csvTab.getHeader(0) + "\"%1) VALUES (?%2)";
string colNames;
string qMarks;
for (size_t colIdx = 1; colIdx < csvTab.nCols(); ++colIdx)
{
colNames += ",\"" + csvTab.getHeader(colIdx) + "\"";
qMarks += ",?";
}
sql.arg(colNames);
sql.arg(qMarks);
auto stmt = db.get().prepStatement(sql);
// lock the table
auto trans = db.get().startTransaction(tt);
// iterate over all data rows and import data one-by-one
std::for_each(csvTab.cbegin(), csvTab.cend(), [&stmt](const Sloppy::CSV_Row& row) {
stmt.reset(true);
// Note: SQLite bind parameters are 1-based while the
// CSV columns are 0-based!
size_t colIdx{1};
std::for_each(row.cbegin(), row.cend(), [&](const Sloppy::CSV_Value& val) {
switch (val.valueType())
{
case Sloppy::CSV_Value::Type::Long:
stmt.bind(colIdx, val.get<int64_t>());
break;
case Sloppy::CSV_Value::Type::String:
stmt.bind(colIdx, val.get<string>());
break;
case Sloppy::CSV_Value::Type::Double:
stmt.bind(colIdx, val.get<double>());
break;
case Sloppy::CSV_Value::Type::Null:
stmt.bindNull(colIdx);
break;
}
++colIdx;
});
// the actual insertion
stmt.step();
});
trans.commit();
return csvTab.size();
}
//----------------------------------------------------------------------------
void DbTab::addColumn_exec(const string& colName, ColumnDataType colType, const string& constraints) const
{
string cn{colName};
if (!Sloppy::trimAndCheckString(cn))
{
throw std::invalid_argument("Called with empty column name");
}
string ts{};
switch (colType)
{
case ColumnDataType::Integer:
ts = "INTEGER";
break;
case ColumnDataType::Float:
ts = "FLOAT";
break;
case ColumnDataType::Text:
ts = "TEXT";
break;
case ColumnDataType::Blob:
ts = "BLOB";
break;
}
// no checking for empty "ts" here, because SQLite allows
// column declarations with empty type
//
// we skip any consistency checks here; this function is private and it's assumed
// that any checks have been performed before by the associated public functions
//
// construct an "alter table" sql query for inserting the column
string sql = "ALTER TABLE " + tabName + " ADD COLUMN " + cn + " " + ts;
// add any constraints
if (!(constraints.empty())) sql += " " + constraints;
// actually add the column
SqlStatement stmt = db.get().prepStatement(sql);
stmt.step();
}
//----------------------------------------------------------------------------
vector<TabRow> DbTab::statementResultsToVector(SqlStatement& stmt) const
{
vector<TabRow> result;
while (stmt.dataStep()) {
result.emplace_back(db, tabName, stmt.get<int>(0), true);
}
return result;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
TabRowIterator::TabRowIterator(const SqliteDatabase& _db, const string& _tabName, int minRowId, int maxRowId)
:db{_db}, tabName{_tabName}
{
WhereClause w;
if (minRowId > 0)
{
w.addCol("rowid", ">=", minRowId);
}
if (maxRowId > 0)
{
w.addCol("rowid", "<=", maxRowId);
}
init(w);
}
//----------------------------------------------------------------------------
TabRowIterator::TabRowIterator(const SqliteDatabase& _db, const string& _tabName, const WhereClause& w)
:db{_db}, tabName{_tabName}
{
init(w);
}
//----------------------------------------------------------------------------
bool TabRowIterator::operator++()
{
curRow.reset(); // delete the old TabRow object
stmt.step();
if (stmt.hasData())
{
curRow = make_unique<TabRow>(db.get(), tabName, rowid(), true);
}
return stmt.hasData();
}
//----------------------------------------------------------------------------
TabRow& TabRowIterator::operator*() const
{
if (!stmt.hasData())
{
throw NoDataException("TabRowIterator: trying to de-reference empty / exhausted SQL statement");
}
return *curRow;
}
//----------------------------------------------------------------------------
TabRow* TabRowIterator::operator->() const
{
if (!stmt.hasData())
{
throw NoDataException("TabRowIterator: trying to de-reference empty / exhausted SQL statement");
}
return curRow.get();
}
//----------------------------------------------------------------------------
void TabRowIterator::init(const WhereClause& w)
{
if (w.isEmpty())
{
stmt = db.get().prepStatement("SELECT rowid FROM " + tabName);
} else {
stmt = w.getSelectStmt(db, tabName, false);
}
// call `step()` and creates a new TabRow instance, if necessary
operator++();
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
}