-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqlite_driver.hpp
1059 lines (923 loc) · 32 KB
/
sqlite_driver.hpp
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: sqlite_driver.hpp
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef SQLITE_DRIVER_HPP
#define SQLITE_DRIVER_HPP
#include <climits>
#include <cstring>
#include <sqlite3.h>
#include <algorithm>
#include <vector>
#include "driver.hpp"
namespace vgi { namespace dbconn { namespace dbd { namespace sqlite {
enum class open_flag : int
{
READONLY = SQLITE_OPEN_READONLY,
READWRITE = SQLITE_OPEN_READWRITE,
CREATE = SQLITE_OPEN_CREATE,
DELETEONCLOSE = SQLITE_OPEN_DELETEONCLOSE,
EXCLUSIVE = SQLITE_OPEN_EXCLUSIVE,
#ifdef SQLITE_OPEN_AUTOPROXY
AUTOPROXY = SQLITE_OPEN_AUTOPROXY,
#endif
#ifdef SQLITE_OPEN_URI
URI = SQLITE_OPEN_URI,
#endif
#ifdef SQLITE_OPEN_MEMORY
MEMORY = SQLITE_OPEN_MEMORY,
#endif
MAIN_DB = SQLITE_OPEN_MAIN_DB,
TEMP_DB = SQLITE_OPEN_TEMP_DB,
TRANSIENT_DB = SQLITE_OPEN_TRANSIENT_DB,
MAIN_JOURNAL = SQLITE_OPEN_MAIN_JOURNAL,
TEMP_JOURNAL = SQLITE_OPEN_TEMP_JOURNAL,
SUBJOURNAL = SQLITE_OPEN_SUBJOURNAL,
MASTER_JOURNAL = SQLITE_OPEN_MASTER_JOURNAL,
NOMUTEX = SQLITE_OPEN_NOMUTEX,
FULLMUTEX = SQLITE_OPEN_FULLMUTEX,
SHAREDCACHE = SQLITE_OPEN_SHAREDCACHE,
PRIVATECACHE = SQLITE_OPEN_PRIVATECACHE,
#ifdef SQLITE_OPEN_WAL
WAL = SQLITE_OPEN_WAL
#endif
};
constexpr open_flag operator|(open_flag l, open_flag r) { return open_flag(utils::base_type(l) | utils::base_type(r)); }
enum class config_flag : int
{
#ifdef SQLITE_CONFIG_LOG
LOG = SQLITE_CONFIG_LOG,
#endif
#ifdef SQLITE_CONFIG_URI
URI = SQLITE_CONFIG_URI,
#endif
#ifdef SQLITE_CONFIG_PCACHE2
PCACHE2 = SQLITE_CONFIG_PCACHE2,
#endif
#ifdef SQLITE_CONFIG_GETPCACHE2
GETPCACHE2 = SQLITE_CONFIG_GETPCACHE2,
#endif
#ifdef SQLITE_CONFIG_COVERING_INDEX_SCAN
COVERING_INDEX = SQLITE_CONFIG_COVERING_INDEX_SCAN,
#endif
#ifdef SQLITE_CONFIG_SQLLOG
SQLLOG = SQLITE_CONFIG_SQLLOG,
#endif
#ifdef SQLITE_CONFIG_MMAP_SIZE
MMAP_SIZE = SQLITE_CONFIG_MMAP_SIZE,
#endif
#ifdef SQLITE_CONFIG_WIN32_HEAPSIZE
WIN32_HEAPSIZE = SQLITE_CONFIG_WIN32_HEAPSIZE,
#endif
#ifdef SQLITE_CONFIG_PCACHE_HDRSZ
PCACHE_HDRSZ = SQLITE_CONFIG_PCACHE_HDRSZ,
#endif
#ifdef SQLITE_CONFIG_PMASZ
PMASZ = SQLITE_CONFIG_PMASZ,
#endif
#ifdef SQLITE_CONFIG_STMTJRNL_SPILL
STMTJRNL_SPILL = SQLITE_CONFIG_STMTJRNL_SPILL,
#endif
SINGLETHREAD = SQLITE_CONFIG_SINGLETHREAD,
MULTITHREAD = SQLITE_CONFIG_MULTITHREAD,
SERIALIZED = SQLITE_CONFIG_SERIALIZED,
MALLOC = SQLITE_CONFIG_MALLOC,
GETMALLOC = SQLITE_CONFIG_GETMALLOC,
SCRATCH = SQLITE_CONFIG_SCRATCH,
PAGECACHE = SQLITE_CONFIG_PAGECACHE,
HEAP = SQLITE_CONFIG_HEAP,
MEMSTATUS = SQLITE_CONFIG_MEMSTATUS,
MUTEX = SQLITE_CONFIG_MUTEX,
GETMUTEX = SQLITE_CONFIG_GETMUTEX,
LOOKASIDE = SQLITE_CONFIG_LOOKASIDE,
PCACHE = SQLITE_CONFIG_PCACHE,
GETPCACHE = SQLITE_CONFIG_GETPCACHE,
// extra flag for functions other than sqlite3_config
SOFT_HEAP_LIMIT,
};
enum class db_config_flag : int
{
#ifdef SQLITE_DBCONFIG_MAINDBNAME
MAINDBNAME = SQLITE_DBCONFIG_MAINDBNAME,
#endif
#ifdef SQLITE_DBCONFIG_ENABLE_FKEY
ENABLE_FKEY = SQLITE_DBCONFIG_ENABLE_FKEY,
#endif
#ifdef SQLITE_DBCONFIG_ENABLE_TRIGGER
ENABLE_TRIGGER = SQLITE_DBCONFIG_ENABLE_TRIGGER,
#endif
#ifdef SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
ENABLE_FTS3_TOKENIZER = SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,
#endif
#ifdef SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION
ENABLE_LOAD_EXTENSION = SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,
#endif
#ifdef SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE
NO_CKPT_ON_CLOSE = SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,
#endif
LOOKASIDE = SQLITE_DBCONFIG_LOOKASIDE
};
static const char* decode_errcode(int c)
{
switch (c)
{
case SQLITE_OK : return "Successful result";
case SQLITE_ERROR : return "SQL error or missing database";
case SQLITE_INTERNAL : return "Internal logic error in SQLite";
case SQLITE_PERM : return "Access permission denied";
case SQLITE_ABORT : return "Callback routine requested an abort";
case SQLITE_BUSY : return "The database file is locked";
case SQLITE_LOCKED : return "A table in the database is locked";
case SQLITE_NOMEM : return "A malloc() failed";
case SQLITE_READONLY : return "Attempt to write a readonly database";
case SQLITE_INTERRUPT : return "Operation terminated by sqlite3_interrupt()";
case SQLITE_IOERR : return "Some kind of disk I/O error occurred";
case SQLITE_CORRUPT : return "The database disk image is malformed";
case SQLITE_NOTFOUND : return "Unknown opcode in sqlite3_file_control()";
case SQLITE_FULL : return "Insertion failed because database is full";
case SQLITE_CANTOPEN : return "Unable to open the database file";
case SQLITE_PROTOCOL : return "Database lock protocol error";
case SQLITE_EMPTY : return "Database is empty";
case SQLITE_SCHEMA : return "The database schema changed";
case SQLITE_TOOBIG : return "String or BLOB exceeds size limit";
case SQLITE_CONSTRAINT: return "Abort due to constraint violation";
case SQLITE_MISMATCH : return "Data type mismatch";
case SQLITE_MISUSE : return "Library used incorrectly";
case SQLITE_NOLFS : return "Uses OS features not supported on host";
case SQLITE_AUTH : return "Authorization denied";
case SQLITE_FORMAT : return "Auxiliary database format error";
case SQLITE_RANGE : return "2nd parameter to sqlite3_bind out of range";
case SQLITE_NOTADB : return "File opened that is not a database file";
#ifdef SQLITE_NOTICE
case SQLITE_NOTICE : return "Notifications from sqlite3_log()";
#endif
#ifdef SQLITE_WARNING
case SQLITE_WARNING : return "Warnings from sqlite3_log()";
#endif
case SQLITE_ROW : return "sqlite3_step() has another row ready";
case SQLITE_DONE : return "sqlite3_step() has finished executing";
}
return "UNKNOWN";
}
// forward declaration
class driver;
class statement;
class connection;
//=====================================================================================
/**
* result_set - is a class that implements dbi::iresult_set interface and
* represents results of SQL query execution with collection of helper function
* to make the retrieval of the results of a query execution an easy task.
* result_set object cannot be instantiated directly, only via statement execute()
* function call. result_set objects automatically cancel executed
* queries and destroy allocated memory as they go out of scope.
*/
class result_set : public dbi::iresult_set
{
public:
void clear()
{
stepped = false;
name2index.clear();
row_cnt = 0;
column_cnt = 0;
affected_rows = 0;
}
virtual bool has_data()
{
return column_count();
}
virtual bool more_results()
{
return (stmts_index > 0 && (stmts_index < sqlite_stmts.size() || (stmts_index == sqlite_stmts.size() && stepped)));
}
virtual size_t row_count() const
{
return std::abs(row_cnt);
}
virtual size_t rows_affected() const
{
return affected_rows;
}
virtual size_t column_count() const
{
return column_cnt;
}
virtual std::string column_name(size_t col_idx)
{
validate();
return sqlite3_column_name(sqlite_stmt, col_idx);
}
virtual int column_index(const std::string& col_name)
{
auto it = name2index.find(col_name);
if (it != name2index.end())
return it->second;
return -1;
}
virtual bool prev()
{
throw std::runtime_error(std::string(__FUNCTION__).append(": Cursors are not supported by database"));
}
virtual bool first()
{
throw std::runtime_error(std::string(__FUNCTION__).append(": Cursors are not supported by database"));
}
virtual bool last()
{
throw std::runtime_error(std::string(__FUNCTION__).append(": Cursors are not supported by database"));
}
virtual bool next()
{
validate();
if (stepped && column_cnt > 0)
{
stepped = false;
return true;
}
auto res = sqlite3_step(sqlite_stmt);
switch (res)
{
case SQLITE_DONE:
affected_rows = (row_cnt > 0 ? row_cnt : sqlite3_changes(sqlite_conn));
sqlite3_reset(sqlite_stmt);
if (stmts_index > 0 && stmts_index < sqlite_stmts.size())
{
name2index.clear();
row_cnt = column_cnt = 0;
sqlite_stmt = sqlite_stmts[stmts_index++];
int tmp = affected_rows;
next();
stepped = true;
affected_rows = tmp;
}
break;
case SQLITE_ROW:
{
row_cnt += 1;
if (0 == column_cnt)
{
column_cnt = sqlite3_column_count(sqlite_stmt);
for (auto i = 0; i < column_cnt; ++i)
name2index[sqlite3_column_name(sqlite_stmt, i)] = i;
}
}
return true;
case SQLITE_BUSY:
cancel();
break;
default:
cancel();
throw std::runtime_error(std::string(__FUNCTION__).append(": ").append(decode_errcode(res)));
}
return false;
}
virtual bool is_null(size_t col_idx)
{
validate();
return (SQLITE_NULL == sqlite3_column_type(sqlite_stmt, col_idx));
}
virtual int16_t get_short(size_t col_idx)
{
return get_slint<int16_t>(col_idx);
}
virtual uint16_t get_ushort(size_t col_idx)
{
return get_slint<uint16_t>(col_idx);
}
virtual int32_t get_int(size_t col_idx)
{
return get_slint<int32_t>(col_idx);
}
virtual uint32_t get_uint(size_t col_idx)
{
return get_slint<uint32_t>(col_idx);
}
virtual int64_t get_long(size_t col_idx)
{
return get_slint64<int64_t>(col_idx);
}
virtual uint64_t get_ulong(size_t col_idx)
{
return get_slint64<uint64_t>(col_idx);
}
virtual float get_float(size_t col_idx)
{
return static_cast<float>(get_double(col_idx));
}
virtual double get_double(size_t col_idx)
{
validate();
return sqlite3_column_double(sqlite_stmt, col_idx);
}
virtual bool get_bool(size_t col_idx)
{
return get_slint<bool>(col_idx);
}
virtual char get_char(size_t col_idx)
{
validate();
return *sqlite3_column_text(sqlite_stmt, col_idx);
}
virtual std::string get_string(size_t col_idx)
{
validate();
auto start = reinterpret_cast<const char*>(sqlite3_column_text(sqlite_stmt, col_idx));
return std::move(std::string(start, sqlite3_column_bytes(sqlite_stmt, col_idx)));
}
virtual int get_date(size_t col_idx)
{
auto s = get_string(col_idx);
s.erase(std::remove(s.begin(), s.end(), '-'), s.end());
return std::stoi(s);
}
virtual double get_time(size_t col_idx)
{
auto s = get_string(col_idx);
s.erase(std::remove(s.begin(), s.end(), ':'), s.end());
return std::stod(s);
}
virtual time_t get_datetime(size_t col_idx)
{
std::string s = get_string(col_idx);
std::memset(&stm, 0, sizeof(stm));
std::sscanf(s.c_str(), "%04d-%02d-%02d %02d:%02d:%02d", &stm.tm_year, &stm.tm_mon, &stm.tm_mday, &stm.tm_hour, &stm.tm_min, &stm.tm_sec);
stm.tm_year -= 1900;
stm.tm_mon -= 1;
stm.tm_isdst = -1;
return mktime(&stm);
}
virtual char16_t get_u16char(size_t col_idx)
{
validate();
return *(reinterpret_cast<const char16_t*>(sqlite3_column_text16(sqlite_stmt, col_idx)));
}
virtual std::u16string get_u16string(size_t col_idx)
{
validate();
auto start = reinterpret_cast<const char16_t*>(sqlite3_column_text16(sqlite_stmt, col_idx));
sqlite3_column_bytes16(sqlite_stmt, col_idx);
return std::u16string(start);
}
virtual std::vector<uint8_t> get_binary(size_t col_idx)
{
validate();
auto data = sqlite3_column_blob(sqlite_stmt, col_idx);
std::vector<uint8_t> t(sqlite3_column_bytes(sqlite_stmt, col_idx));
std::memcpy(reinterpret_cast<void*>(t.data()), data, t.size());
return std::move(t);
}
bool cancel()
{
clear();
if (nullptr != sqlite_conn)
sqlite3_interrupt(sqlite_conn);
if (nullptr != sqlite_stmt)
return (SQLITE_OK == sqlite3_reset(sqlite_stmt));
return true;
}
private:
friend class statement;
result_set(std::vector<sqlite3_stmt*>& stmts) : sqlite_stmts(stmts) {}
result_set(const result_set& rs) = delete;
result_set& operator=(const result_set& rs) = delete;
template<typename T>
T get_slint(size_t col_idx)
{
validate();
return static_cast<T>(sqlite3_column_int(sqlite_stmt, col_idx));
}
template<typename T>
T get_slint64(size_t col_idx)
{
validate();
return static_cast<T>(sqlite3_column_int64(sqlite_stmt, col_idx));
}
void validate()
{
if (sqlite_stmt == nullptr)
throw std::runtime_error(std::string(__FUNCTION__).append(": Invalid result state object state"));
}
private:
bool stepped = false;
long row_cnt = 0;
long column_cnt = 0;
size_t stmts_index = 0;
size_t affected_rows = 0;
sqlite3* sqlite_conn = nullptr;
sqlite3_stmt* sqlite_stmt = nullptr;
struct tm stm;
std::vector<sqlite3_stmt*>& sqlite_stmts;
std::map<std::string, int> name2index;
}; // result_set
//=====================================================================================
/**
* sqlite driver class based on sqlite3 C++ library. This class cannot be
* used directly and is intended to be used via wrapper dbd::driver class
*/
class driver : public idriver
{
public:
dbi::connection get_connection(const std::string& server);
driver& version(long& ver)
{
std::lock_guard<utils::spin_lock> lg(lock);
ver = sqlite3_libversion_number();
return *this;
}
driver& version_string(std::string& ver)
{
std::lock_guard<utils::spin_lock> lg(lock);
ver = sqlite3_libversion();
return *this;
}
driver& max_connections(unsigned int conn_num)
{
std::lock_guard<utils::spin_lock> lg(lock);
max_conn = conn_num;
return *this;
}
template <typename... T>
driver& config(config_flag flag, T... t)
{
int ret = SQLITE_OK;
std::lock_guard<utils::spin_lock> lg(lock);
if (flag == config_flag::SOFT_HEAP_LIMIT)
{
if (sizeof...(t) != 1)
ret = SQLITE_MISUSE;
else
{
int p = {t...};
sqlite3_soft_heap_limit(p);
}
}
else
{
if (conn_cnt > 0)
throw std::runtime_error(std::string(__FUNCTION__).append(": This function must be used before any connections are opened"));
sqlite3_shutdown();
ret = sqlite3_config(utils::base_type(flag), t...);
sqlite3_initialize();
}
if (SQLITE_OK != ret)
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set config parameter: ").append(std::to_string(utils::base_type(flag))).append(": ").append(decode_errcode(ret)));
return *this;
}
protected:
friend class connection;
driver(const driver&) = delete;
driver(driver&&) = delete;
driver& operator=(const driver&) = delete;
driver& operator=(driver&&) = delete;
driver()
{
}
void upd_conn_count(int change)
{
std::lock_guard<utils::spin_lock> lg(lock);
conn_cnt += change;
}
bool is_max_conn()
{
std::lock_guard<utils::spin_lock> lg(lock);
return conn_cnt == max_conn;
}
private:
unsigned int max_conn = UINT_MAX;
unsigned int conn_cnt = 0;
utils::spin_lock lock;
}; // driver
//=====================================================================================
/**
* connection - is a class that implements dbi::iconnection interface and
* represents native database connection handle, in case of sqlite it's a C++
* wrap around around sqlite3.
* connection object cannot be instantiated directly, only via driver
* get_connection() function call. connection objects automatically delete the
* native connection handle they manage as soon as they themselves are destroyed.
*/
class connection : public dbi::iconnection
{
public:
virtual ~connection()
{
disconnect();
}
connection(connection&& conn)
: sqlite_conn(conn.sqlite_conn), is_utf16(conn.is_utf16),
is_autocommit(conn.is_autocommit), oflag(conn.oflag),
vfsname(std::move(conn.vfsname)), server(std::move(conn.server))
{
conn.sqlite_conn = nullptr;
}
connection& operator=(connection&& conn)
{
if (this != &conn)
{
disconnect();
sqlite_conn = conn.sqlite_conn;
conn.sqlite_conn = nullptr;
is_utf16 = conn.is_utf16;
is_autocommit = conn.is_autocommit;
oflag = conn.oflag;
vfsname = std::move(conn.vfsname);
server = std::move(conn.server);
}
return *this;
}
template <typename... T>
connection& config(db_config_flag flag, T... t)
{
if (sqlite_conn == nullptr)
throw std::runtime_error(std::string(__FUNCTION__).append(": This function must be used after connection is opened"));
int ret = sqlite3_db_config(sqlite_conn, utils::base_type(flag), t...);
if (SQLITE_OK != ret)
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set config parameter: ").append(std::to_string(utils::base_type(flag))).append(": ").append(decode_errcode(ret)));
return *this;
}
virtual bool connect()
{
if (drv->is_max_conn())
throw std::runtime_error(std::string(__FUNCTION__).append(": Can't open new connections, please revise max connections"));
if (true == connected())
disconnect();
if (oflag == 0)
{
if (SQLITE_OK != (is_utf16 ? sqlite3_open16(server.c_str(), &sqlite_conn) : sqlite3_open(server.c_str(), &sqlite_conn)))
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to connect: ").append(server));
}
else
{
if (SQLITE_OK != sqlite3_open_v2(server.c_str(), &sqlite_conn, oflag, (vfsname.empty() ? nullptr : vfsname.c_str())))
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to connect: ").append(server));
}
drv->upd_conn_count(1);
return alive();
}
virtual void disconnect()
{
if (nullptr != sqlite_conn)
{
sqlite3_close(sqlite_conn);
sqlite_conn = nullptr;
drv->upd_conn_count(-1);
}
};
virtual bool connected() const
{
return (nullptr != sqlite_conn);
}
virtual bool alive() const
{
return (nullptr != sqlite_conn);
}
virtual void autocommit(bool ac)
{
if (nullptr != sqlite_conn)
{
if (ac != is_autocommit)
{
is_autocommit = ac;
if (ac)
sqlite_exec("rollback transaction;");
else
sqlite_exec("begin transaction;");
}
}
}
virtual void commit()
{
if (false == is_autocommit)
{
sqlite_exec("commit transaction;");
sqlite_exec("begin transaction;");
}
}
virtual void rollback()
{
if (false == is_autocommit)
{
sqlite_exec("rollback transaction;");
sqlite_exec("begin transaction;");
}
}
virtual dbi::istatement* get_statement(dbi::iconnection& iconn);
connection& flags(open_flag flag)
{
oflag = utils::base_type(flag);
return *this;
}
connection& vfs(const std::string& name)
{
vfsname = name;
return *this;
}
connection& utf16(bool flag)
{
is_utf16 = flag;
return *this;
}
sqlite3* native_connection() const
{
return sqlite_conn;
}
private:
void sqlite_exec(const std::string& sql)
{
char* err = nullptr;
if (SQLITE_OK != sqlite3_exec(sqlite_conn, sql.c_str(), nullptr, nullptr, &err))
{
std::string s = (err ? err : "unknown error");
sqlite3_free(err);
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to execute: ").append(sql).append(": ").append(s));
}
}
private:
friend class driver;
friend class statement;
connection() = delete;
connection(const connection&) = delete;
connection& operator=(const connection&) = delete;
connection(driver* drv, const std::string& server)
: drv(drv), server(server)
{
}
private:
driver* drv = nullptr;
sqlite3* sqlite_conn = nullptr;
bool is_utf16 = false;
bool is_autocommit = true;
int oflag = 0;
std::string vfsname;
std::string server;
}; // connection
dbi::connection driver::get_connection(const std::string& server)
{
return create_connection(new connection(this, server));
}
//=====================================================================================
/**
* statement - is a class that implements dbi::istatement interface and
* represents native database statement structure, in case of sqlite it's a C++
* wrap around sqlite3_stmt that also contains result set structure.
* statement object cannot be instantiated directly, only via connection
* get_statement() function call. statement objects automatically cancel executed
* queries and destroy result set as they go out of scope.
*/
class statement : public dbi::istatement
{
public:
~statement()
{
cancel();
}
virtual bool cancel()
{
bool res = rs.cancel();
for (auto stmt : sqlite_stmts)
{
if (SQLITE_OK != sqlite3_finalize(stmt))
res = false;
}
sqlite_stmts.clear();
rs.sqlite_stmt = nullptr;
rs.stmts_index = 0;
return res;
}
virtual dbi::iresult_set* execute()
{
rs.cancel();
return fetch();
}
virtual dbi::iresult_set* execute(const std::string& cmd, bool usecursor = false, bool scrollable = false)
{
cancel();
command = cmd;
command.erase(std::find_if(command.rbegin(), command.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), command.end());
size_t plen = 0;
while (command.length() > 0 && plen != command.length())
{
command.erase(command.begin(), std::find_if(command.begin(), command.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
sqlite_stmts.push_back(nullptr);
prepare(command, &sqlite_stmts[sqlite_stmts.size() - 1]);
plen = command.length();
command = tail;
}
return fetch();
}
virtual void prepare(const std::string& cmd)
{
if (cmd.empty())
throw std::runtime_error(std::string(__FUNCTION__).append(": SQL command is not set"));
if (false == conn.alive())
throw std::runtime_error(std::string(__FUNCTION__).append(": Database connection is dead"));
cancel();
sqlite_stmts.resize(1);
prepare(cmd, &sqlite_stmts[0]);
}
virtual void call(const std::string& cmd)
{
throw std::runtime_error(std::string(__FUNCTION__).append(": Stored procedures are not supported by database"));
}
virtual int proc_retval()
{
throw std::runtime_error(std::string(__FUNCTION__).append(": Stored procedures are not supported by database"));
}
virtual void set_null(size_t param_idx)
{
validate();
if (SQLITE_OK != sqlite3_bind_null(sqlite_stmts.front(), param_idx + 1))
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set null at index ").append(std::to_string(param_idx)));
}
virtual void set_short(size_t param_idx, int16_t val)
{
set_slint(param_idx, val);
}
virtual void set_ushort(size_t param_idx, uint16_t val)
{
set_slint(param_idx, val);
}
virtual void set_int(size_t param_idx, int32_t val)
{
set_slint(param_idx, val);
}
virtual void set_uint(size_t param_idx, uint32_t val)
{
set_int(param_idx, val);
}
virtual void set_long(size_t param_idx, int64_t val)
{
set_slint64(param_idx, val);
}
virtual void set_ulong(size_t param_idx, uint64_t val)
{
set_slint64(param_idx, val);
}
virtual void set_float(size_t param_idx, float val)
{
set_double(param_idx, val);
}
virtual void set_double(size_t param_idx, double val)
{
validate();
if (SQLITE_OK != sqlite3_bind_double(sqlite_stmts.front(), param_idx + 1, val))
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set double at index ").append(std::to_string(param_idx)));
}
virtual void set_bool(size_t param_idx, bool val)
{
set_slint(param_idx, val);
}
virtual void set_char(size_t param_idx, char val)
{
set_string(param_idx, std::string(1, val));
}
virtual void set_string(size_t param_idx, const std::string& val)
{
validate();
if (SQLITE_OK != sqlite3_bind_text(sqlite_stmts.front(), param_idx + 1, val.c_str(), val.size(), SQLITE_TRANSIENT))
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set string at index ").append(std::to_string(param_idx)));
}
virtual void set_date(size_t param_idx, int val)
{
auto yr = val / 10000;
auto mon = (val % 10000) / 100;
auto day = val % 100;
std::vector<char> dt(11);
std::sprintf(dt.data(), "%4d-%02d-%02d", yr, mon, day);
set_string(param_idx, dt.data());
}
virtual void set_time(size_t param_idx, double val)
{
auto t = static_cast<int>(val);
auto hr = t / 10000;
auto min = (t % 10000) / 100;
auto sec = t % 100;
auto ms = static_cast<int>(floor((val - t) * 1000 + 0.5));
std::vector<char> dt(13);
std::sprintf(dt.data(), "%02d:%02d:%02d.%03d", hr, min, sec, ms);
set_string(param_idx, dt.data());
}
virtual void set_datetime(size_t param_idx, time_t val)
{
#if defined(_WIN32) || defined(_WIN64)
::localtime_s(&stm, &val);
#else
::localtime_r(&val, &stm);
#endif
stm.tm_year += 1900;
std::vector<char> dt(20);
std::sprintf(dt.data(), "%04d-%02d-%02d %02d:%02d:%02d", stm.tm_year, stm.tm_mon + 1, stm.tm_mday, stm.tm_hour, stm.tm_min, stm.tm_sec);
set_string(param_idx, dt.data());
}
virtual void set_u16char(size_t param_idx, char16_t val)
{
set_u16string(param_idx, std::u16string(1, val));
}
virtual void set_u16string(size_t param_idx, const std::u16string& val)
{
validate();
if (SQLITE_OK != sqlite3_bind_text16(sqlite_stmts.front(), param_idx + 1, val.data(), val.size() * sizeof(char16_t), SQLITE_TRANSIENT))
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set text16 at index ").append(std::to_string(param_idx)));
}
virtual void set_binary(size_t param_idx, const std::vector<uint8_t>& val)
{
validate();
if (SQLITE_OK != sqlite3_bind_blob(sqlite_stmts.front(), param_idx + 1, val.data(), val.size(), SQLITE_TRANSIENT))
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set blob at index ").append(std::to_string(param_idx)));
}
private:
friend class connection;
statement() = delete;
statement(const statement&) = delete;
statement& operator=(const statement&) = delete;
statement(connection& conn) : conn(conn), rs(sqlite_stmts)
{
rs.sqlite_conn = conn.sqlite_conn;
}
template<typename T>
void set_slint(size_t param_idx, T val)
{
validate();
int ret = sqlite3_bind_int(sqlite_stmts.front(), param_idx + 1, val);
if (SQLITE_OK != ret)
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set int at index ").append(std::to_string(param_idx)).append(": ").append(decode_errcode(ret)));
}
template<typename T>
void set_slint64(size_t param_idx, T val)
{
validate();
int ret = sqlite3_bind_int64(sqlite_stmts.front(), param_idx + 1, val);
if (SQLITE_OK != ret)
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to set int64 at index ").append(std::to_string(param_idx)).append(": ").append(decode_errcode(ret)));
}
void prepare(const std::string& cmd, sqlite3_stmt** stmtptr)
{
auto ret = sqlite3_prepare_v2(conn.sqlite_conn, cmd.c_str(), cmd.length(), stmtptr, &tail);
if (SQLITE_OK != ret)
throw std::runtime_error(std::string(__FUNCTION__).append(": Failed to prepare command, error code: ").append(decode_errcode(ret)));
}
dbi::iresult_set* fetch()
{
size_t rows_affected = 0;
int failed_cnt = 0;