-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_orm.hpp
More file actions
12437 lines (10562 loc) · 446 KB
/
sqlite_orm.hpp
File metadata and controls
12437 lines (10562 loc) · 446 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
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
#pragma once
#if defined(_MSC_VER)
#if defined(min)
__pragma(push_macro("min"))
#undef min
#define __RESTORE_MIN__
#endif
#if defined(max)
__pragma(push_macro("max"))
#undef max
#define __RESTORE_MAX__
#endif
#endif // defined(_MSC_VER)
#include <ciso646> // due to #166
#if __cplusplus >= 201703L // use of C++17 or higher
// Enables use of std::optional in SQLITE_ORM.
#define SQLITE_ORM_OPTIONAL_SUPPORTED
#endif
#pragma once
#include <system_error> // std::error_code, std::system_error
#include <string> // std::string
#include <sqlite3.h>
#include <stdexcept>
#include <sstream> // std::ostringstream
namespace sqlite_orm {
enum class orm_error_code {
not_found = 1,
type_is_not_mapped_to_storage,
trying_to_dereference_null_iterator,
too_many_tables_specified,
incorrect_set_fields_specified,
column_not_found,
table_has_no_primary_key_column,
cannot_start_a_transaction_within_a_transaction,
no_active_transaction,
incorrect_journal_mode_string,
invalid_collate_argument_enum,
failed_to_init_a_backup,
};
}
namespace sqlite_orm {
class orm_error_category : public std::error_category {
public:
const char *name() const noexcept override final {
return "ORM error";
}
std::string message(int c) const override final {
switch(static_cast<orm_error_code>(c)) {
case orm_error_code::not_found:
return "Not found";
case orm_error_code::type_is_not_mapped_to_storage:
return "Type is not mapped to storage";
case orm_error_code::trying_to_dereference_null_iterator:
return "Trying to dereference null iterator";
case orm_error_code::too_many_tables_specified:
return "Too many tables specified";
case orm_error_code::incorrect_set_fields_specified:
return "Incorrect set fields specified";
case orm_error_code::column_not_found:
return "Column not found";
case orm_error_code::table_has_no_primary_key_column:
return "Table has no primary key column";
case orm_error_code::cannot_start_a_transaction_within_a_transaction:
return "Cannot start a transaction within a transaction";
case orm_error_code::no_active_transaction:
return "No active transaction";
case orm_error_code::invalid_collate_argument_enum:
return "Invalid collate_argument enum";
case orm_error_code::failed_to_init_a_backup:
return "Failed to init a backup";
default:
return "unknown error";
}
}
};
class sqlite_error_category : public std::error_category {
public:
const char *name() const noexcept override final {
return "SQLite error";
}
std::string message(int c) const override final {
return sqlite3_errstr(c);
}
};
inline const orm_error_category &get_orm_error_category() {
static orm_error_category res;
return res;
}
inline const sqlite_error_category &get_sqlite_error_category() {
static sqlite_error_category res;
return res;
}
template<typename... T>
std::string get_error_message(sqlite3 *db, T &&... args) {
std::ostringstream stream;
using unpack = int[];
static_cast<void>(unpack{0, (static_cast<void>(static_cast<void>(stream << args)), 0)...});
stream << sqlite3_errmsg(db);
return stream.str();
}
template<typename... T>
[[noreturn]] void throw_error(sqlite3 *db, T &&... args) {
throw std::system_error(std::error_code(sqlite3_errcode(db), get_sqlite_error_category()),
get_error_message(db, std::forward<T>(args)...));
}
}
namespace std {
template<>
struct is_error_code_enum<sqlite_orm::orm_error_code> : std::true_type {};
inline std::error_code make_error_code(sqlite_orm::orm_error_code errorCode) {
return std::error_code(static_cast<int>(errorCode), sqlite_orm::get_orm_error_category());
}
}
#pragma once
#include <map> // std::map
#include <string> // std::string
#include <regex> // std::regex, std::regex_match
#include <memory> // std::make_unique, std::unique_ptr
#include <vector> // std::vector
#include <cctype> // std::toupper
namespace sqlite_orm {
using int64 = sqlite_int64;
using uint64 = sqlite_uint64;
// numeric and real are the same for c++
enum class sqlite_type {
INTEGER,
TEXT,
BLOB,
REAL,
};
/**
* @param str case doesn't matter - it is uppercased before comparing.
*/
inline std::unique_ptr<sqlite_type> to_sqlite_type(const std::string &str) {
auto asciiStringToUpper = [](std::string &s) {
std::transform(s.begin(), s.end(), s.begin(), [](char c) {
return std::toupper(c);
});
};
auto upperStr = str;
asciiStringToUpper(upperStr);
static std::map<sqlite_type, std::vector<std::regex>> typeMap = {
{sqlite_type::INTEGER,
{
std::regex("INT"),
std::regex("INT.*"),
std::regex("TINYINT"),
std::regex("SMALLINT"),
std::regex("MEDIUMINT"),
std::regex("BIGINT"),
std::regex("UNSIGNED BIG INT"),
std::regex("INT2"),
std::regex("INT8"),
}},
{sqlite_type::TEXT,
{
std::regex("CHARACTER\\([[:digit:]]+\\)"),
std::regex("VARCHAR\\([[:digit:]]+\\)"),
std::regex("VARYING CHARACTER\\([[:digit:]]+\\)"),
std::regex("NCHAR\\([[:digit:]]+\\)"),
std::regex("NATIVE CHARACTER\\([[:digit:]]+\\)"),
std::regex("NVARCHAR\\([[:digit:]]+\\)"),
std::regex("CLOB"),
std::regex("TEXT"),
}},
{sqlite_type::BLOB,
{
std::regex("BLOB"),
}},
{sqlite_type::REAL,
{
std::regex("REAL"),
std::regex("DOUBLE"),
std::regex("DOUBLE PRECISION"),
std::regex("FLOAT"),
std::regex("NUMERIC"),
std::regex("DECIMAL\\([[:digit:]]+,[[:digit:]]+\\)"),
std::regex("BOOLEAN"),
std::regex("DATE"),
std::regex("DATETIME"),
}},
};
for(auto &p: typeMap) {
for(auto &r: p.second) {
if(std::regex_match(upperStr, r)) {
return std::make_unique<sqlite_type>(p.first);
}
}
}
return {};
}
}
#pragma once
#include <tuple> // std::tuple, std::get
#include <type_traits> // std::false_type, std::true_type
// #include "static_magic.h"
#include <type_traits> // std::false_type, std::true_type, std::integral_constant
namespace sqlite_orm {
// got from here
// https://stackoverflow.com/questions/37617677/implementing-a-compile-time-static-if-logic-for-different-string-types-in-a-co
namespace internal {
template<typename T, typename F>
auto static_if(std::true_type, T t, F) {
return std::move(t);
}
template<typename T, typename F>
auto static_if(std::false_type, T, F f) {
return std::move(f);
}
template<bool B, typename T, typename F>
auto static_if(T t, F f) {
return static_if(std::integral_constant<bool, B>{}, std::move(t), std::move(f));
}
template<bool B, typename T>
auto static_if(T t) {
return static_if(std::integral_constant<bool, B>{}, t, [](auto &&...) {});
}
}
}
namespace sqlite_orm {
// got from here http://stackoverflow.com/questions/25958259/how-do-i-find-out-if-a-tuple-contains-a-type
namespace tuple_helper {
template<typename T, typename Tuple>
struct has_type;
template<typename T>
struct has_type<T, std::tuple<>> : std::false_type {};
template<typename T, typename U, typename... Ts>
struct has_type<T, std::tuple<U, Ts...>> : has_type<T, std::tuple<Ts...>> {};
template<typename T, typename... Ts>
struct has_type<T, std::tuple<T, Ts...>> : std::true_type {};
template<typename T, typename Tuple>
using tuple_contains_type = typename has_type<T, Tuple>::type;
template<size_t N, class... Args>
struct iterator {
template<class L>
void operator()(const std::tuple<Args...> &t, const L &l, bool reverse = true) {
if(reverse) {
l(std::get<N>(t));
iterator<N - 1, Args...>()(t, l, reverse);
} else {
iterator<N - 1, Args...>()(t, l, reverse);
l(std::get<N>(t));
}
}
};
template<class... Args>
struct iterator<0, Args...> {
template<class L>
void operator()(const std::tuple<Args...> &t, const L &l, bool /*reverse*/ = true) {
l(std::get<0>(t));
}
};
template<size_t N>
struct iterator<N> {
template<class L>
void operator()(const std::tuple<> &, const L &, bool /*reverse*/ = true) {
//..
}
};
template<size_t N, size_t I, class L, class R>
void move_tuple_impl(L &lhs, R &rhs) {
std::get<I>(lhs) = std::move(std::get<I>(rhs));
internal::static_if<std::integral_constant<bool, N != I + 1>{}>([](auto &lhs, auto &rhs) {
move_tuple_impl<N, I + 1>(lhs, rhs);
})(lhs, rhs);
}
}
namespace internal {
template<size_t N, class L, class R>
void move_tuple(L &lhs, R &rhs) {
using bool_type = std::integral_constant<bool, N != 0>;
static_if<bool_type{}>([](auto &lhs, auto &rhs) {
tuple_helper::move_tuple_impl<N, 0>(lhs, rhs);
})(lhs, rhs);
}
template<class L, class... Args>
void iterate_tuple(const std::tuple<Args...> &t, const L &l) {
using tuple_type = std::tuple<Args...>;
tuple_helper::iterator<std::tuple_size<tuple_type>::value - 1, Args...>()(t, l, false);
}
template<typename... input_t>
using tuple_cat_t = decltype(std::tuple_cat(std::declval<input_t>()...));
template<class... Args>
struct conc_tuple {
using type = tuple_cat_t<Args...>;
};
template<class T, template<class> class C>
struct count_tuple;
template<template<class> class C>
struct count_tuple<std::tuple<>, C> {
static constexpr const int value = 0;
};
template<class H, class... Args, template<class> class C>
struct count_tuple<std::tuple<H, Args...>, C> {
static constexpr const int value = C<H>::value + count_tuple<std::tuple<Args...>, C>::value;
};
}
}
#pragma once
#include <string> // std::string
#include <memory> // std::shared_ptr, std::unique_ptr
#include <vector> // std::vector
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
#include <optional> // std::optional
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
namespace sqlite_orm {
/**
* This class accepts c++ type and transfers it to sqlite name (int -> INTEGER, std::string -> TEXT)
*/
template<class T, typename Enable = void>
struct type_printer;
struct integer_printer {
inline const std::string &print() {
static const std::string res = "INTEGER";
return res;
}
};
struct text_printer {
inline const std::string &print() {
static const std::string res = "TEXT";
return res;
}
};
struct real_printer {
inline const std::string &print() {
static const std::string res = "REAL";
return res;
}
};
struct blob_printer {
inline const std::string &print() {
static const std::string res = "BLOB";
return res;
}
};
// Note unsigned/signed char and simple char used for storing integer values, not char values.
template<>
struct type_printer<unsigned char, void> : public integer_printer {};
template<>
struct type_printer<signed char, void> : public integer_printer {};
template<>
struct type_printer<char, void> : public integer_printer {};
template<>
struct type_printer<unsigned short int, void> : public integer_printer {};
template<>
struct type_printer<short, void> : public integer_printer {};
template<>
struct type_printer<unsigned int, void> : public integer_printer {};
template<>
struct type_printer<int, void> : public integer_printer {};
template<>
struct type_printer<unsigned long, void> : public integer_printer {};
template<>
struct type_printer<long, void> : public integer_printer {};
template<>
struct type_printer<unsigned long long, void> : public integer_printer {};
template<>
struct type_printer<long long, void> : public integer_printer {};
template<>
struct type_printer<bool, void> : public integer_printer {};
template<>
struct type_printer<std::string, void> : public text_printer {};
template<>
struct type_printer<std::wstring, void> : public text_printer {};
template<>
struct type_printer<const char *, void> : public text_printer {};
template<>
struct type_printer<float, void> : public real_printer {};
template<>
struct type_printer<double, void> : public real_printer {};
template<class T>
struct type_printer<std::shared_ptr<T>, void> : public type_printer<T> {};
template<class T>
struct type_printer<std::unique_ptr<T>, void> : public type_printer<T> {};
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
template<class T>
struct type_printer<std::optional<T>, void> : public type_printer<T> {};
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
template<>
struct type_printer<std::vector<char>, void> : public blob_printer {};
}
#pragma once
namespace sqlite_orm {
namespace internal {
enum class collate_argument {
binary,
nocase,
rtrim,
};
}
}
#pragma once
#include <string> // std::string
#include <tuple> // std::tuple, std::make_tuple
#include <sstream> // std::stringstream
#include <type_traits> // std::is_base_of, std::false_type, std::true_type
#include <ostream> // std::ostream
namespace sqlite_orm {
namespace constraints {
/**
* AUTOINCREMENT constraint class.
*/
struct autoincrement_t {
operator std::string() const {
return "AUTOINCREMENT";
}
};
struct primary_key_base {
enum class order_by {
unspecified,
ascending,
descending,
};
order_by asc_option = order_by::unspecified;
operator std::string() const {
std::string res = "PRIMARY KEY";
switch(this->asc_option) {
case order_by::ascending:
res += " ASC";
break;
case order_by::descending:
res += " DESC";
break;
default:
break;
}
return res;
}
};
/**
* PRIMARY KEY constraint class.
* Cs is parameter pack which contains columns (member pointers and/or function pointers). Can be empty when
* used withen `make_column` function.
*/
template<class... Cs>
struct primary_key_t : primary_key_base {
using order_by = primary_key_base::order_by;
std::tuple<Cs...> columns;
primary_key_t(decltype(columns) c) : columns(std::move(c)) {}
using field_type = void; // for column iteration. Better be deleted
using constraints_type = std::tuple<>;
primary_key_t<Cs...> asc() const {
auto res = *this;
res.asc_option = order_by::ascending;
return res;
}
primary_key_t<Cs...> desc() const {
auto res = *this;
res.asc_option = order_by::descending;
return res;
}
};
/**
* UNIQUE constraint class.
*/
struct unique_t {
operator std::string() const {
return "UNIQUE";
}
};
/**
* DEFAULT constraint class.
* T is a value type.
*/
template<class T>
struct default_t {
using value_type = T;
value_type value;
operator std::string() const {
return "DEFAULT";
}
};
#if SQLITE_VERSION_NUMBER >= 3006019
/**
* FOREIGN KEY constraint class.
* Cs are columns which has foreign key
* Rs are column which C references to
* Available in SQLite 3.6.19 or higher
*/
template<class A, class B>
struct foreign_key_t;
enum class foreign_key_action {
none, // not specified
no_action,
restrict_,
set_null,
set_default,
cascade,
};
inline std::ostream &operator<<(std::ostream &os, foreign_key_action action) {
switch(action) {
case decltype(action)::no_action:
os << "NO ACTION";
break;
case decltype(action)::restrict_:
os << "RESTRICT";
break;
case decltype(action)::set_null:
os << "SET NULL";
break;
case decltype(action)::set_default:
os << "SET DEFAULT";
break;
case decltype(action)::cascade:
os << "CASCADE";
break;
case decltype(action)::none:
break;
}
return os;
}
struct on_update_delete_base {
const bool update; // true if update and false if delete
operator std::string() const {
if(this->update) {
return "ON UPDATE";
} else {
return "ON DELETE";
}
}
};
/**
* F - foreign key class
*/
template<class F>
struct on_update_delete_t : on_update_delete_base {
using foreign_key_type = F;
const foreign_key_type &fk;
on_update_delete_t(decltype(fk) fk_, decltype(update) update, foreign_key_action action_) :
on_update_delete_base{update}, fk(fk_), _action(action_) {}
foreign_key_action _action = foreign_key_action::none;
foreign_key_type no_action() const {
auto res = this->fk;
if(update) {
res.on_update._action = foreign_key_action::no_action;
} else {
res.on_delete._action = foreign_key_action::no_action;
}
return res;
}
foreign_key_type restrict_() const {
auto res = this->fk;
if(update) {
res.on_update._action = foreign_key_action::restrict_;
} else {
res.on_delete._action = foreign_key_action::restrict_;
}
return res;
}
foreign_key_type set_null() const {
auto res = this->fk;
if(update) {
res.on_update._action = foreign_key_action::set_null;
} else {
res.on_delete._action = foreign_key_action::set_null;
}
return res;
}
foreign_key_type set_default() const {
auto res = this->fk;
if(update) {
res.on_update._action = foreign_key_action::set_default;
} else {
res.on_delete._action = foreign_key_action::set_default;
}
return res;
}
foreign_key_type cascade() const {
auto res = this->fk;
if(update) {
res.on_update._action = foreign_key_action::cascade;
} else {
res.on_delete._action = foreign_key_action::cascade;
}
return res;
}
operator bool() const {
return this->_action != decltype(this->_action)::none;
}
};
template<class... Cs, class... Rs>
struct foreign_key_t<std::tuple<Cs...>, std::tuple<Rs...>> {
using columns_type = std::tuple<Cs...>;
using references_type = std::tuple<Rs...>;
using self = foreign_key_t<columns_type, references_type>;
columns_type columns;
references_type references;
on_update_delete_t<self> on_update;
on_update_delete_t<self> on_delete;
static_assert(std::tuple_size<columns_type>::value == std::tuple_size<references_type>::value,
"Columns size must be equal to references tuple");
foreign_key_t(columns_type columns_, references_type references_) :
columns(std::move(columns_)), references(std::move(references_)),
on_update(*this, true, foreign_key_action::none), on_delete(*this, false, foreign_key_action::none) {}
foreign_key_t(const self &other) :
columns(other.columns), references(other.references), on_update(*this, true, other.on_update._action),
on_delete(*this, false, other.on_delete._action) {}
self &operator=(const self &other) {
this->columns = other.columns;
this->references = other.references;
this->on_update = {*this, true, other.on_update._action};
this->on_delete = {*this, false, other.on_delete._action};
return *this;
}
using field_type = void; // for column iteration. Better be deleted
using constraints_type = std::tuple<>;
template<class L>
void for_each_column(const L &) {}
template<class... Opts>
constexpr bool has_every() const {
return false;
}
};
/**
* Cs can be a class member pointer, a getter function member pointer or setter
* func member pointer
* Available in SQLite 3.6.19 or higher
*/
template<class... Cs>
struct foreign_key_intermediate_t {
using tuple_type = std::tuple<Cs...>;
tuple_type columns;
foreign_key_intermediate_t(tuple_type columns_) : columns(std::move(columns_)) {}
template<class... Rs>
foreign_key_t<std::tuple<Cs...>, std::tuple<Rs...>> references(Rs... references) {
return {std::move(this->columns), std::make_tuple(std::forward<Rs>(references)...)};
}
};
#endif
struct collate_t {
internal::collate_argument argument = internal::collate_argument::binary;
collate_t(internal::collate_argument argument_) : argument(argument_) {}
operator std::string() const {
std::string res = "COLLATE " + this->string_from_collate_argument(this->argument);
return res;
}
static std::string string_from_collate_argument(internal::collate_argument argument) {
switch(argument) {
case decltype(argument)::binary:
return "BINARY";
case decltype(argument)::nocase:
return "NOCASE";
case decltype(argument)::rtrim:
return "RTRIM";
}
throw std::system_error(std::make_error_code(orm_error_code::invalid_collate_argument_enum));
}
};
template<class T>
struct is_constraint : std::false_type {};
template<>
struct is_constraint<autoincrement_t> : std::true_type {};
template<class... Cs>
struct is_constraint<primary_key_t<Cs...>> : std::true_type {};
template<>
struct is_constraint<unique_t> : std::true_type {};
template<class T>
struct is_constraint<default_t<T>> : std::true_type {};
template<class C, class R>
struct is_constraint<foreign_key_t<C, R>> : std::true_type {};
template<>
struct is_constraint<collate_t> : std::true_type {};
template<class... Args>
struct constraints_size;
template<>
struct constraints_size<> {
static constexpr const int value = 0;
};
template<class H, class... Args>
struct constraints_size<H, Args...> {
static constexpr const int value = is_constraint<H>::value + constraints_size<Args...>::value;
};
}
#if SQLITE_VERSION_NUMBER >= 3006019
/**
* FOREIGN KEY constraint construction function that takes member pointer as argument
* Available in SQLite 3.6.19 or higher
*/
template<class... Cs>
constraints::foreign_key_intermediate_t<Cs...> foreign_key(Cs... columns) {
return {std::make_tuple(std::forward<Cs>(columns)...)};
}
#endif
/**
* UNIQUE constraint builder function.
*/
inline constraints::unique_t unique() {
return {};
}
inline constraints::autoincrement_t autoincrement() {
return {};
}
template<class... Cs>
inline constraints::primary_key_t<Cs...> primary_key(Cs... cs) {
using ret_type = constraints::primary_key_t<Cs...>;
return ret_type(std::make_tuple(cs...));
}
template<class T>
constraints::default_t<T> default_value(T t) {
return {std::move(t)};
}
inline constraints::collate_t collate_nocase() {
return {internal::collate_argument::nocase};
}
inline constraints::collate_t collate_binary() {
return {internal::collate_argument::binary};
}
inline constraints::collate_t collate_rtrim() {
return {internal::collate_argument::rtrim};
}
namespace internal {
/**
* FOREIGN KEY traits. Common case
*/
template<class T>
struct is_foreign_key : std::false_type {};
/**
* FOREIGN KEY traits. Specialized case
*/
template<class C, class R>
struct is_foreign_key<constraints::foreign_key_t<C, R>> : std::true_type {};
/**
* PRIMARY KEY traits. Common case
*/
template<class T>
struct is_primary_key : public std::false_type {};
/**
* PRIMARY KEY traits. Specialized case
*/
template<class... Cs>
struct is_primary_key<constraints::primary_key_t<Cs...>> : public std::true_type {};
}
}
#pragma once
#include <type_traits> // std::false_type, std::true_type
#include <memory> // std::shared_ptr, std::unique_ptr
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
#include <optional> // std::optional
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
namespace sqlite_orm {
/**
* This is class that tells `sqlite_orm` that type is nullable. Nullable types
* are mapped to sqlite database as `NULL` and not-nullable are mapped as `NOT NULL`.
* Default nullability status for all types is `NOT NULL`. So if you want to map
* custom type as `NULL` (for example: boost::optional) you have to create a specialiation
* of type_is_nullable for your type and derive from `std::true_type`.
*/
template<class T>
struct type_is_nullable : public std::false_type {
bool operator()(const T &) const {
return true;
}
};
/**
* This is a specialization for std::shared_ptr. std::shared_ptr is nullable in sqlite_orm.
*/
template<class T>
struct type_is_nullable<std::shared_ptr<T>> : public std::true_type {
bool operator()(const std::shared_ptr<T> &t) const {
return static_cast<bool>(t);
}
};
/**
* This is a specialization for std::unique_ptr. std::unique_ptr is nullable too.
*/
template<class T>
struct type_is_nullable<std::unique_ptr<T>> : public std::true_type {
bool operator()(const std::unique_ptr<T> &t) const {
return static_cast<bool>(t);
}
};
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
/**
* This is a specialization for std::optional. std::optional is nullable.
*/
template<class T>
struct type_is_nullable<std::optional<T>> : public std::true_type {
bool operator()(const std::optional<T> &t) const {
return t.has_value();
}
};
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
}
#pragma once
#include <memory> // std::unique_ptr
#include <string> // std::string
#include <sstream> // std::stringstream
// #include "constraints.h"
namespace sqlite_orm {
namespace internal {
template<class T>
std::string serialize(const T &t);
/**
* This class is used in tuple interation to know whether tuple constains `default_value_t`
* constraint class and what it's value if it is
*/
struct default_value_extractor {
template<class A>
std::unique_ptr<std::string> operator()(const A &) {
return {};
}
template<class T>
std::unique_ptr<std::string> operator()(const constraints::default_t<T> &t) {
return std::make_unique<std::string>(serialize(t.value));
}
};
}
}
#pragma once
#include <type_traits> // std::false_type, std::true_type
namespace sqlite_orm {
namespace internal {
/**