-
Notifications
You must be signed in to change notification settings - Fork 45
/
pods.d
1389 lines (1303 loc) · 53.3 KB
/
pods.d
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
/**
DDBC - D DataBase Connector - abstraction layer for RDBMS access, with interface similar to JDBC.
Source file ddbc/drivers/pgsqlddbc.d.
DDBC library attempts to provide implementation independent interface to different databases.
Set of supported RDBMSs can be extended by writing Drivers for particular DBs.
JDBC documentation can be found here:
$(LINK http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/package-summary.html)$(BR)
This module contains implementation POD utilities.
----
import ddbc;
import std.stdio;
// prepare database connectivity
auto conn = createConnection("sqlite:ddbctest.sqlite");
scope(exit) conn.close();
Statement stmt = conn.createStatement();
scope(exit) stmt.close();
// fill database with test data
stmt.executeUpdate("DROP TABLE IF EXISTS user");
stmt.executeUpdate("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR(255) NOT NULL, flags int null)");
stmt.executeUpdate(`INSERT INTO user (id, name, flags) VALUES (1, "John", 5), (2, "Andrei", 2), (3, "Walter", 2), (4, "Rikki", 3), (5, "Iain", 0), (6, "Robert", 1)`);
// our POD object
struct User {
long id;
string name;
int flags;
}
writeln("reading all user table rows");
foreach(e; stmt.select!User) {
writeln("id:", e.id, " name:", e.name, " flags:", e.flags);
}
writeln("reading user table rows with where and order by");
foreach(e; stmt.select!User.where("id < 6").orderBy("name desc")) {
writeln("id:", e.id, " name:", e.name, " flags:", e.flags);
}
writeln("reading user table rows with where and order by with limit and offset");
foreach(e; stmt.select!User.where("id < 6").orderBy("name desc").limit("3").offset("1")) {
writeln("id:", e.id, " name:", e.name, " flags:", e.flags);
}
----
Copyright: Copyright 2013
License: $(LINK www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
Author: Vadim Lopatin
*/
module ddbc.pods;
import std.algorithm;
import std.traits;
import std.typecons;
import std.conv;
import std.datetime;
import std.string;
import std.variant;
static if (__traits(compiles, () { import std.logger; } )) {
import std.logger;
} else {
import std.experimental.logger;
}
static import std.ascii;
import ddbc.core;
alias Nullable!byte Byte;
alias Nullable!ubyte Ubyte;
alias Nullable!short Short;
alias Nullable!ushort Ushort;
alias Nullable!int Int;
alias Nullable!uint Uint;
alias Nullable!long Long;
alias Nullable!ulong Ulong;
alias Nullable!float Float;
alias Nullable!double Double;
alias Nullable!SysTime NullableSysTime;
alias Nullable!DateTime NullableDateTime;
alias Nullable!Date NullableDate;
alias Nullable!TimeOfDay NullableTimeOfDay;
/// Wrapper around string, to distinguish between Null and NotNull fields: string is NotNull, String is Null -- same interface as in Nullable
// Looks ugly, but I tried `typedef string String`, but it is deprecated; `alias string String` cannot be distinguished from just string. How to define String better?
struct String
{
string _value;
/**
Returns $(D true) if and only if $(D this) is in the null state.
*/
@property bool isNull() const pure nothrow @safe
{
return _value is null;
}
/**
Forces $(D this) to the null state.
*/
void nullify()
{
_value = null;
}
alias _value this;
}
enum PropertyMemberType : int {
BOOL_TYPE, // bool
BYTE_TYPE, // byte
SHORT_TYPE, // short
INT_TYPE, // int
LONG_TYPE, // long
UBYTE_TYPE, // ubyte
USHORT_TYPE, // ushort
UINT_TYPE, // uint
ULONG_TYPE, // ulong
NULLABLE_BYTE_TYPE, // Nullable!byte
NULLABLE_SHORT_TYPE, // Nullable!short
NULLABLE_INT_TYPE, // Nullable!int
NULLABLE_LONG_TYPE, // Nullable!long
NULLABLE_UBYTE_TYPE, // Nullable!ubyte
NULLABLE_USHORT_TYPE,// Nullable!ushort
NULLABLE_UINT_TYPE, // Nullable!uint
NULLABLE_ULONG_TYPE, // Nullable!ulong
FLOAT_TYPE, // float
DOUBLE_TYPE, // double
NULLABLE_FLOAT_TYPE, // Nullable!float
NULLABLE_DOUBLE_TYPE,// Nullable!double
STRING_TYPE, // string
NULLABLE_STRING_TYPE, // nullable string - String struct
SYSTIME_TYPE,
DATETIME_TYPE, // std.datetime.DateTime
DATE_TYPE, // std.datetime.Date
TIME_TYPE, // std.datetime.TimeOfDay
NULLABLE_SYSTIME_TYPE,
NULLABLE_DATETIME_TYPE, // Nullable!std.datetime.DateTime
NULLABLE_DATE_TYPE, // Nullable!std.datetime.Date
NULLABLE_TIME_TYPE, // Nullable!std.datetime.TimeOfDay
BYTE_ARRAY_TYPE, // byte[]
UBYTE_ARRAY_TYPE, // ubyte[]
}
/// converts camel case MyEntityName to my_entity_name
string camelCaseToUnderscoreDelimited(immutable string s) {
string res;
bool lastLower = false;
static import std.ascii;
foreach(ch; s) {
if (ch >= 'A' && ch <= 'Z') {
if (lastLower) {
lastLower = false;
res ~= "_";
}
res ~= std.ascii.toLower(ch);
} else if (ch >= 'a' && ch <= 'z') {
lastLower = true;
res ~= ch;
} else {
res ~= ch;
}
}
return res;
}
unittest {
static assert(camelCaseToUnderscoreDelimited("User") == "user");
static assert(camelCaseToUnderscoreDelimited("MegaTableName") == "mega_table_name");
}
template isSupportedSimpleType(T, string m) {
alias typeof(__traits(getMember, T, m)) ti;
static if (is(ti == function)) {
static if (is(ReturnType!(ti) == bool)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == byte)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == short)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == int)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == long)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == ubyte)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == ushort)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == uint)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == ulong)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == float)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == double)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!byte)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!short)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!int)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!long)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!ubyte)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!ushort)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!uint)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!ulong)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!float)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!double)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == string)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == String)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == SysTime)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == DateTime)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Date)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == TimeOfDay)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!SysTime)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!DateTime)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!Date)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == Nullable!TimeOfDay)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == byte[])) {
enum bool isSupportedSimpleType = true;
} else static if (is(ReturnType!(ti) == ubyte[])) {
enum bool isSupportedSimpleType = true;
} else static if (true) {
enum bool isSupportedSimpleType = false;
}
} else static if (is(ti == bool)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == byte)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == short)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == int)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == long)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == ubyte)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == ushort)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == uint)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == ulong)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == float)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == double)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!byte)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!short)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!int)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!long)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!ubyte)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!ushort)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!uint)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!ulong)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!float)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!double)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == string)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == String)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == SysTime)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == DateTime)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Date)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == TimeOfDay)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!SysTime)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!DateTime)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!Date)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == Nullable!TimeOfDay)) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == byte[])) {
enum bool isSupportedSimpleType = true;
} else static if (is(ti == ubyte[])) {
enum bool isSupportedSimpleType = true;
} else static if (true) {
enum bool isSupportedSimpleType = false;
}
}
PropertyMemberType getPropertyType(ti)() {
//pragma(msg, T.stringof);
//alias typeof(T) ti;
static if (is(ti == bool)) {
return PropertyMemberType.BOOL_TYPE;
} else static if (is(ti == byte)) {
return PropertyMemberType.BYTE_TYPE;
} else static if (is(ti == short)) {
return PropertyMemberType.SHORT_TYPE;
} else static if (is(ti == int)) {
return PropertyMemberType.INT_TYPE;
} else static if (is(ti == long)) {
return PropertyMemberType.LONG_TYPE;
} else static if (is(ti == ubyte)) {
return PropertyMemberType.UBYTE_TYPE;
} else static if (is(ti == ushort)) {
return PropertyMemberType.USHORT_TYPE;
} else static if (is(ti == uint)) {
return PropertyMemberType.UINT_TYPE;
} else static if (is(ti == ulong)) {
return PropertyMemberType.ULONG_TYPE;
} else static if (is(ti == float)) {
return PropertyMemberType.FLOAT_TYPE;
} else static if (is(ti == double)) {
return PropertyMemberType.DOUBLE_TYPE;
} else static if (is(ti == Nullable!byte)) {
return PropertyMemberType.NULLABLE_BYTE_TYPE;
} else static if (is(ti == Nullable!short)) {
return PropertyMemberType.NULLABLE_SHORT_TYPE;
} else static if (is(ti == Nullable!int)) {
return PropertyMemberType.NULLABLE_INT_TYPE;
} else static if (is(ti == Nullable!long)) {
return PropertyMemberType.NULLABLE_LONG_TYPE;
} else static if (is(ti == Nullable!ubyte)) {
return PropertyMemberType.NULLABLE_UBYTE_TYPE;
} else static if (is(ti == Nullable!ushort)) {
return PropertyMemberType.NULLABLE_USHORT_TYPE;
} else static if (is(ti == Nullable!uint)) {
return PropertyMemberType.NULLABLE_UINT_TYPE;
} else static if (is(ti == Nullable!ulong)) {
return PropertyMemberType.NULLABLE_ULONG_TYPE;
} else static if (is(ti == Nullable!float)) {
return PropertyMemberType.NULLABLE_FLOAT_TYPE;
} else static if (is(ti == Nullable!double)) {
return PropertyMemberType.NULLABLE_DOUBLE_TYPE;
} else static if (is(ti == string)) {
return PropertyMemberType.STRING_TYPE;
} else static if (is(ti == String)) {
return PropertyMemberType.NULLABLE_STRING_TYPE;
} else static if (is(ti == SysTime)) {
return PropertyMemberType.SYSTIME_TYPE;
} else static if (is(ti == DateTime)) {
return PropertyMemberType.DATETIME_TYPE;
} else static if (is(ti == Date)) {
return PropertyMemberType.DATE_TYPE;
} else static if (is(ti == TimeOfDay)) {
return PropertyMemberType.TIME_TYPE;
} else static if (is(ti == Nullable!SysTime)) {
return PropertyMemberType.NULLABLE_SYSTIME_TYPE;
} else static if (is(ti == Nullable!DateTime)) {
return PropertyMemberType.NULLABLE_DATETIME_TYPE;
} else static if (is(ti == Nullable!Date)) {
return PropertyMemberType.NULLABLE_DATE_TYPE;
} else static if (is(ti == Nullable!TimeOfDay)) {
return PropertyMemberType.NULLABLE_TIME_TYPE;
} else static if (is(ti == byte[])) {
return PropertyMemberType.BYTE_ARRAY_TYPE;
} else static if (is(ti == ubyte[])) {
return PropertyMemberType.UBYTE_ARRAY_TYPE;
} else static if (true) {
static assert (false, "has unsupported type " ~ ti.stringof);
}
}
PropertyMemberType getPropertyMemberType(T, string m)() {
alias typeof(__traits(getMember, T, m)) ti;
static if (is(ti == bool)) {
return PropertyMemberType.BOOL_TYPE;
} else static if (is(ti == byte)) {
return PropertyMemberType.BYTE_TYPE;
} else static if (is(ti == short)) {
return PropertyMemberType.SHORT_TYPE;
} else static if (is(ti == int)) {
return PropertyMemberType.INT_TYPE;
} else static if (is(ti == long)) {
return PropertyMemberType.LONG_TYPE;
} else static if (is(ti == ubyte)) {
return PropertyMemberType.UBYTE_TYPE;
} else static if (is(ti == ushort)) {
return PropertyMemberType.USHORT_TYPE;
} else static if (is(ti == uint)) {
return PropertyMemberType.UINT_TYPE;
} else static if (is(ti == ulong)) {
return PropertyMemberType.ULONG_TYPE;
} else static if (is(ti == float)) {
return PropertyMemberType.FLOAT_TYPE;
} else static if (is(ti == double)) {
return PropertyMemberType.DOUBLE_TYPE;
} else static if (is(ti == Nullable!byte)) {
return PropertyMemberType.NULLABLE_BYTE_TYPE;
} else static if (is(ti == Nullable!short)) {
return PropertyMemberType.NULLABLE_SHORT_TYPE;
} else static if (is(ti == Nullable!int)) {
return PropertyMemberType.NULLABLE_INT_TYPE;
} else static if (is(ti == Nullable!long)) {
return PropertyMemberType.NULLABLE_LONG_TYPE;
} else static if (is(ti == Nullable!ubyte)) {
return PropertyMemberType.NULLABLE_UBYTE_TYPE;
} else static if (is(ti == Nullable!ushort)) {
return PropertyMemberType.NULLABLE_USHORT_TYPE;
} else static if (is(ti == Nullable!uint)) {
return PropertyMemberType.NULLABLE_UINT_TYPE;
} else static if (is(ti == Nullable!ulong)) {
return PropertyMemberType.NULLABLE_ULONG_TYPE;
} else static if (is(ti == Nullable!float)) {
return PropertyMemberType.NULLABLE_FLOAT_TYPE;
} else static if (is(ti == Nullable!double)) {
return PropertyMemberType.NULLABLE_DOUBLE_TYPE;
} else static if (is(ti == string)) {
return PropertyMemberType.STRING_TYPE;
} else static if (is(ti == String)) {
return PropertyMemberType.NULLABLE_STRING_TYPE;
} else static if (is(ti == SysTime)) {
return PropertyMemberType.SYSTIME_TYPE;
} else static if (is(ti == DateTime)) {
return PropertyMemberType.DATETIME_TYPE;
} else static if (is(ti == Date)) {
return PropertyMemberType.DATE_TYPE;
} else static if (is(ti == TimeOfDay)) {
return PropertyMemberType.TIME_TYPE;
} else static if (is(ti == Nullable!SysTime)) {
return PropertyMemberType.NULLABLE_SYSTIME_TYPE;
} else static if (is(ti == Nullable!DateTime)) {
return PropertyMemberType.NULLABLE_DATETIME_TYPE;
} else static if (is(ti == Nullable!Date)) {
return PropertyMemberType.NULLABLE_DATE_TYPE;
} else static if (is(ti == Nullable!TimeOfDay)) {
return PropertyMemberType.NULLABLE_TIME_TYPE;
} else static if (is(ti == byte[])) {
return PropertyMemberType.BYTE_ARRAY_TYPE;
} else static if (is(ti == ubyte[])) {
return PropertyMemberType.UBYTE_ARRAY_TYPE;
} else static if (true) {
static assert (false, "Member " ~ m ~ " of class " ~ T.stringof ~ " has unsupported type " ~ ti.stringof);
}
}
string getPropertyReadCode(T, string m)() {
return "entity." ~ m;
}
string getPropertyReadCode(alias T)() {
return "entity." ~ T.stringof;
}
static immutable bool[] ColumnTypeCanHoldNulls =
[
false, //BOOL_TYPE // bool
false, //BYTE_TYPE, // byte
false, //SHORT_TYPE, // short
false, //INT_TYPE, // int
false, //LONG_TYPE, // long
false, //UBYTE_TYPE, // ubyte
false, //USHORT_TYPE, // ushort
false, //UINT_TYPE, // uint
false, //ULONG_TYPE, // ulong
true, //NULLABLE_BYTE_TYPE, // Nullable!byte
true, //NULLABLE_SHORT_TYPE, // Nullable!short
true, //NULLABLE_INT_TYPE, // Nullable!int
true, //NULLABLE_LONG_TYPE, // Nullable!long
true, //NULLABLE_UBYTE_TYPE, // Nullable!ubyte
true, //NULLABLE_USHORT_TYPE,// Nullable!ushort
true, //NULLABLE_UINT_TYPE, // Nullable!uint
true, //NULLABLE_ULONG_TYPE, // Nullable!ulong
false,//FLOAT_TYPE, // float
false,//DOUBLE_TYPE, // double
true, //NULLABLE_FLOAT_TYPE, // Nullable!float
true, //NULLABLE_DOUBLE_TYPE,// Nullable!double
false, //STRING_TYPE // string -- treat as @NotNull by default
true, //NULLABLE_STRING_TYPE // String
false, //SYSTIME_TYPE
false, //DATETIME_TYPE, // std.datetime.DateTime
false, //DATE_TYPE, // std.datetime.Date
false, //TIME_TYPE, // std.datetime.TimeOfDay
true, //NULLABLE_SYSTIME_TYPE
true, //NULLABLE_DATETIME_TYPE, // Nullable!std.datetime.DateTime
true, //NULLABLE_DATE_TYPE, // Nullable!std.datetime.Date
true, //NULLABLE_TIME_TYPE, // Nullable!std.datetime.TimeOfDay
true, //BYTE_ARRAY_TYPE, // byte[]
true, //UBYTE_ARRAY_TYPE, // ubyte[]
];
bool isColumnTypeNullableByDefault(T, string m)() {
return ColumnTypeCanHoldNulls[getPropertyMemberType!(T,m)];
}
static immutable string[] ColumnTypeKeyIsSetCode =
[
"(%s != 0)", //BOOL_TYPE // bool
"(%s != 0)", //BYTE_TYPE, // byte
"(%s != 0)", //SHORT_TYPE, // short
"(%s != 0)", //INT_TYPE, // int
"(%s != 0)", //LONG_TYPE, // long
"(%s != 0)", //UBYTE_TYPE, // ubyte
"(%s != 0)", //USHORT_TYPE, // ushort
"(%s != 0)", //UINT_TYPE, // uint
"(%s != 0)", //ULONG_TYPE, // ulong
"(!%s.isNull)", //NULLABLE_BYTE_TYPE, // Nullable!byte
"(!%s.isNull)", //NULLABLE_SHORT_TYPE, // Nullable!short
"(!%s.isNull)", //NULLABLE_INT_TYPE, // Nullable!int
"(!%s.isNull)", //NULLABLE_LONG_TYPE, // Nullable!long
"(!%s.isNull)", //NULLABLE_UBYTE_TYPE, // Nullable!ubyte
"(!%s.isNull)", //NULLABLE_USHORT_TYPE,// Nullable!ushort
"(!%s.isNull)", //NULLABLE_UINT_TYPE, // Nullable!uint
"(!%s.isNull)", //NULLABLE_ULONG_TYPE, // Nullable!ulong
"(%s != 0)",//FLOAT_TYPE, // float
"(%s != 0)",//DOUBLE_TYPE, // double
"(!%s.isNull)", //NULLABLE_FLOAT_TYPE, // Nullable!float
"(!%s.isNull)", //NULLABLE_DOUBLE_TYPE,// Nullable!double
"(%s !is null)", //STRING_TYPE // string
"(%s !is null)", //NULLABLE_STRING_TYPE // String
"(%s != SysTime())", //SYSTIME_TYPE, // std.datetime.systime : SysTime
"(%s != DateTime())", //DATETIME_TYPE, // std.datetime.DateTime
"(%s != Date())", //DATE_TYPE, // std.datetime.Date
"(%s != TimeOfDay())", //TIME_TYPE, // std.datetime.TimeOfDay
"(!%s.isNull)", //NULLABLE_SYSTIME_TYPE, // Nullable!std.datetime.systime.SysTime
"(!%s.isNull)", //NULLABLE_DATETIME_TYPE, // Nullable!std.datetime.DateTime
"(!%s.isNull)", //NULLABLE_DATE_TYPE, // Nullable!std.datetime.Date
"(!%s.isNull)", //NULLABLE_TIME_TYPE, // Nullable!std.datetime.TimeOfDay
"(%s !is null)", //BYTE_ARRAY_TYPE, // byte[]
"(%s !is null)", //UBYTE_ARRAY_TYPE, // ubyte[]
];
string getColumnTypeKeyIsSetCode(T, string m)() {
return substituteParam(ColumnTypeKeyIsSetCode[getPropertyMemberType!(T,m)()], getPropertyReadCode!(T,m)());
}
static immutable string[] ColumnTypeIsNullCode =
[
"(false)", //BOOL_TYPE // bool
"(false)", //BYTE_TYPE, // byte
"(false)", //SHORT_TYPE, // short
"(false)", //INT_TYPE, // int
"(false)", //LONG_TYPE, // long
"(false)", //UBYTE_TYPE, // ubyte
"(false)", //USHORT_TYPE, // ushort
"(false)", //UINT_TYPE, // uint
"(false)", //ULONG_TYPE, // ulong
"(%s.isNull)", //NULLABLE_BYTE_TYPE, // Nullable!byte
"(%s.isNull)", //NULLABLE_SHORT_TYPE, // Nullable!short
"(%s.isNull)", //NULLABLE_INT_TYPE, // Nullable!int
"(%s.isNull)", //NULLABLE_LONG_TYPE, // Nullable!long
"(%s.isNull)", //NULLABLE_UBYTE_TYPE, // Nullable!ubyte
"(%s.isNull)", //NULLABLE_USHORT_TYPE,// Nullable!ushort
"(%s.isNull)", //NULLABLE_UINT_TYPE, // Nullable!uint
"(%s.isNull)", //NULLABLE_ULONG_TYPE, // Nullable!ulong
"(false)",//FLOAT_TYPE, // float
"(false)",//DOUBLE_TYPE, // double
"(%s.isNull)", //NULLABLE_FLOAT_TYPE, // Nullable!float
"(%s.isNull)", //NULLABLE_DOUBLE_TYPE,// Nullable!double
"(%s is null)", //STRING_TYPE // string
"(%s is null)", //NULLABLE_STRING_TYPE // String
"(false)", //SYSTIME_TYPE
"(false)", //DATETIME_TYPE, // std.datetime.DateTime
"(false)", //DATE_TYPE, // std.datetime.Date
"(false)", //TIME_TYPE, // std.datetime.TimeOfDay
"(%s.isNull)", //NULLABLE_SYSTIME_TYPE
"(%s.isNull)", //NULLABLE_DATETIME_TYPE, // Nullable!std.datetime.DateTime
"(%s.isNull)", //NULLABLE_DATE_TYPE, // Nullable!std.datetime.Date
"(%s.isNull)", //NULLABLE_TIME_TYPE, // Nullable!std.datetime.TimeOfDay
"(%s is null)", //BYTE_ARRAY_TYPE, // byte[]
"(%s is null)", //UBYTE_ARRAY_TYPE, // ubyte[]
];
string getColumnTypeIsNullCode(T, string m)() {
return substituteParam(ColumnTypeIsNullCode[getPropertyMemberType!(T,m)()], getPropertyReadCode!(T,m)());
}
static immutable string[] ColumnTypeSetNullCode =
[
"bool nv;", // BOOL_TYPE // bool
"byte nv = 0;", //BYTE_TYPE, // byte
"short nv = 0;", //SHORT_TYPE, // short
"int nv = 0;", //INT_TYPE, // int
"long nv = 0;", //LONG_TYPE, // long
"ubyte nv = 0;", //UBYTE_TYPE, // ubyte
"ushort nv = 0;", //USHORT_TYPE, // ushort
"uint nv = 0;", //UINT_TYPE, // uint
"ulong nv = 0;", //ULONG_TYPE, // ulong
"Nullable!byte nv;", //NULLABLE_BYTE_TYPE, // Nullable!byte
"Nullable!short nv;", //NULLABLE_SHORT_TYPE, // Nullable!short
"Nullable!int nv;", //NULLABLE_INT_TYPE, // Nullable!int
"Nullable!long nv;", //NULLABLE_LONG_TYPE, // Nullable!long
"Nullable!ubyte nv;", //NULLABLE_UBYTE_TYPE, // Nullable!ubyte
"Nullable!ushort nv;", //NULLABLE_USHORT_TYPE,// Nullable!ushort
"Nullable!uint nv;", //NULLABLE_UINT_TYPE, // Nullable!uint
"Nullable!ulong nv;", //NULLABLE_ULONG_TYPE, // Nullable!ulong
"float nv = 0;",//FLOAT_TYPE, // float
"double nv = 0;",//DOUBLE_TYPE, // double
"Nullable!float nv;", //NULLABLE_FLOAT_TYPE, // Nullable!float
"Nullable!double nv;", //NULLABLE_DOUBLE_TYPE,// Nullable!double
"string nv;", //STRING_TYPE // string
"string nv;", //NULLABLE_STRING_TYPE // String
"SysTime nv;", //SYSTIME_TYPE
"DateTime nv;", //DATETIME_TYPE, // std.datetime.DateTime
"Date nv;", //DATE_TYPE, // std.datetime.Date
"TimeOfDay nv;", //TIME_TYPE, // std.datetime.TimeOfDay
"Nullable!SysTime nv;", //NULLABLE_SYSTIME_TYPE
"Nullable!DateTime nv;", //NULLABLE_DATETIME_TYPE, // Nullable!std.datetime.DateTime
"Nullable!Date nv;", //NULLABLE_DATE_TYPE, // Nullable!std.datetime.Date
"Nullable!TimeOfDay nv;", //NULLABLE_TIME_TYPE, // Nullable!std.datetime.TimeOfDay
"byte[] nv = null;", //BYTE_ARRAY_TYPE, // byte[]
"ubyte[] nv = null;", //UBYTE_ARRAY_TYPE, // ubyte[]
];
static immutable string[] ColumnTypePropertyToVariant =
[
"Variant(%s)", //BOOL_TYPE // bool
"Variant(%s)", //BYTE_TYPE, // byte
"Variant(%s)", //SHORT_TYPE, // short
"Variant(%s)", //INT_TYPE, // int
"Variant(%s)", //LONG_TYPE, // long
"Variant(%s)", //UBYTE_TYPE, // ubyte
"Variant(%s)", //USHORT_TYPE, // ushort
"Variant(%s)", //UINT_TYPE, // uint
"Variant(%s)", //ULONG_TYPE, // ulong
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_BYTE_TYPE, // Nullable!byte
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_SHORT_TYPE, // Nullable!short
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_INT_TYPE, // Nullable!int
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_LONG_TYPE, // Nullable!long
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_UBYTE_TYPE, // Nullable!ubyte
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_USHORT_TYPE,// Nullable!ushort
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_UINT_TYPE, // Nullable!uint
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_ULONG_TYPE, // Nullable!ulong
"Variant(%s)",//FLOAT_TYPE, // float
"Variant(%s)",//DOUBLE_TYPE, // double
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_FLOAT_TYPE, // Nullable!float
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_DOUBLE_TYPE,// Nullable!double
"Variant(%s)", //STRING_TYPE // string
"Variant(%s)", //NULLABLE_STRING_TYPE // String
"Variant(%s)", //SYSTIME_TYPE
"Variant(%s)", //DATETIME_TYPE, // std.datetime.DateTime
"Variant(%s)", //DATE_TYPE, // std.datetime.Date
"Variant(%s)", //TIME_TYPE, // std.datetime.TimeOfDay
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_SYSTIME_TYPE
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_DATETIME_TYPE, // Nullable!std.datetime.DateTime
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_DATE_TYPE, // Nullable!std.datetime.Date
"(%s.isNull ? Variant(null) : Variant(%s.get()))", //NULLABLE_TIME_TYPE, // Nullable!std.datetime.TimeOfDay
"Variant(%s)", //BYTE_ARRAY_TYPE, // byte[]
"Variant(%s)", //UBYTE_ARRAY_TYPE, // ubyte[]
];
static immutable string[] ColumnTypeDatasetReaderCode =
[
"r.getBoolean(index)", //BOOL_TYPE, // bool
"r.getByte(index)", //BYTE_TYPE, // byte
"r.getShort(index)", //SHORT_TYPE, // short
"r.getInt(index)", //INT_TYPE, // int
"r.getLong(index)", //LONG_TYPE, // long
"r.getUbyte(index)", //UBYTE_TYPE, // ubyte
"r.getUshort(index)", //USHORT_TYPE, // ushort
"r.getUint(index)", //UINT_TYPE, // uint
"r.getUlong(index)", //ULONG_TYPE, // ulong
"Nullable!byte(r.getByte(index))", //NULLABLE_BYTE_TYPE, // Nullable!byte
"Nullable!short(r.getShort(index))", //NULLABLE_SHORT_TYPE, // Nullable!short
"Nullable!int(r.getInt(index))", //NULLABLE_INT_TYPE, // Nullable!int
"Nullable!long(r.getLong(index))", //NULLABLE_LONG_TYPE, // Nullable!long
"Nullable!ubyte(r.getUbyte(index))", //NULLABLE_UBYTE_TYPE, // Nullable!ubyte
"Nullable!ushort(r.getUshort(index))", //NULLABLE_USHORT_TYPE,// Nullable!ushort
"Nullable!uint(r.getUint(index))", //NULLABLE_UINT_TYPE, // Nullable!uint
"Nullable!ulong(r.getUlong(index))", //NULLABLE_ULONG_TYPE, // Nullable!ulong
"r.getFloat(index)",//FLOAT_TYPE, // float
"r.getDouble(index)",//DOUBLE_TYPE, // double
"Nullable!float(r.getFloat(index))", //NULLABLE_FLOAT_TYPE, // Nullable!float
"Nullable!double(r.getDouble(index))", //NULLABLE_DOUBLE_TYPE,// Nullable!double
"r.getString(index)", //STRING_TYPE // string
"r.getString(index)", //NULLABLE_STRING_TYPE // String
"r.getSysTime(index)", //SYSTIME_TYPE
"r.getDateTime(index)", //DATETIME_TYPE, // std.datetime.DateTime
"r.getDate(index)", //DATE_TYPE, // std.datetime.Date
"r.getTime(index)", //TIME_TYPE, // std.datetime.TimeOfDay
"Nullable!SysTime(r.getSysTime(index))", //NULLABLE_SYSTIME_TYPE
"Nullable!DateTime(r.getDateTime(index))", //NULLABLE_DATETIME_TYPE, // Nullable!std.datetime.DateTime
"Nullable!Date(r.getDate(index))", //NULLABLE_DATE_TYPE, // Nullable!std.datetime.Date
"Nullable!TimeOfDay(r.getTime(index))", //NULLABLE_TIME_TYPE, // Nullable!std.datetime.TimeOfDay
"r.getBytes(index)", //BYTE_ARRAY_TYPE, // byte[]
"r.getUbytes(index)", //UBYTE_ARRAY_TYPE, // ubyte[]
];
string getColumnTypeDatasetReadCode(T, string m)() {
return ColumnTypeDatasetReaderCode[getPropertyMemberType!(T,m)()];
}
string getVarTypeDatasetReadCode(T)() {
return ColumnTypeDatasetReaderCode[getPropertyType!T];
}
string getPropertyWriteCode(T, string m)() {
//immutable PropertyMemberKind kind = getPropertyMemberKind!(T, m)();
immutable string nullValueCode = ColumnTypeSetNullCode[getPropertyMemberType!(T,m)()];
immutable string datasetReader = "(!r.isNull(index) ? " ~ getColumnTypeDatasetReadCode!(T, m)() ~ " : nv)";
return nullValueCode ~ "entity." ~ m ~ " = " ~ datasetReader ~ ";";
}
string getPropertyWriteCode(T)() {
//immutable PropertyMemberKind kind = getPropertyMemberKind!(T, m)();
immutable string nullValueCode = ColumnTypeSetNullCode[getPropertyType!T];
immutable string datasetReader = "(!r.isNull(index) ? " ~ getVarTypeDatasetReadCode!T ~ " : nv)";
return nullValueCode ~ "a = " ~ datasetReader ~ ";";
}
/// returns array of field names
string[] getColumnNamesForType(T)() if (__traits(isPOD, T)) {
string[] res;
foreach(m; FieldNameTuple!T) {
static if (__traits(compiles, (typeof(__traits(getMember, T, m))))){
// skip non-public members
static if (__traits(getProtection, __traits(getMember, T, m)) == "public") {
static if (isSupportedSimpleType!(T, m)) {
res ~= m;
}
}
}
}
return res;
}
string getColumnReadCode(T, string m)() {
return "{" ~ getPropertyWriteCode!(T,m)() ~ "index++;}\n";
}
string getAllColumnsReadCode(T)() {
string res = "int index = 1;\n";
foreach(m; FieldNameTuple!T) {
static if (__traits(compiles, (typeof(__traits(getMember, T, m))))){
// skip non-public members
static if (__traits(getProtection, __traits(getMember, T, m)) == "public") {
static if (isSupportedSimpleType!(T, m)) {
res ~= getColumnReadCode!(T, m);
}
}
}
}
return res;
}
string getAllColumnsReadCode(T, fieldList...)() {
string res = "int index = 1;\n";
foreach(m; fieldList) {
res ~= getColumnReadCode!(T, m);
}
return res;
}
unittest {
struct User1 {
long id;
string name;
int flags;
}
//pragma(msg, "nullValueCode = " ~ ColumnTypeSetNullCode[getPropertyMemberType!(User, "id")()]);
//pragma(msg, "datasetReader = " ~ getColumnTypeDatasetReadCode!(User, "id")());
//pragma(msg, "getPropertyWriteCode: " ~ getPropertyWriteCode!(User, "id"));
//pragma(msg, "getAllColumnsReadCode:\n" ~ getAllColumnsReadCode!(User));
//static assert(getPropertyWriteCode!(User, "id") == "long nv = 0;entity.id = (!r.isNull(index) ? r.getLong(index) : nv);");
}
unittest {
struct User1 {
long id;
string name;
int flags;
}
static assert(getPropertyMemberType!(User1, "id")() == PropertyMemberType.LONG_TYPE);
static assert(getPropertyMemberType!(User1, "name")() == PropertyMemberType.STRING_TYPE);
//pragma(msg, "getPropertyMemberType unit test passed");
}
/// returns table name for struct type
string getTableNameForType(T)() if (__traits(isPOD, T)) {
return camelCaseToUnderscoreDelimited(T.stringof);
}
unittest {
struct User1 {
long id;
string name;
int flags;
}
static assert(getTableNameForType!User1() == "user1");
}
/// returns "SELECT <field list> FROM <table name>"
string generateSelectSQL(T)() {
return "SELECT " ~ join(getColumnNamesForType!(T)(), ",") ~ " FROM " ~ getTableNameForType!(T)();
}
unittest {
struct User1 {
long id;
string name;
int flags;
}
static assert(generateSelectSQL!User1() == "SELECT id,name,flags FROM user1");
}
string joinFieldList(fieldList...)() {
string res;
foreach(f; fieldList) {
if (res.length)
res ~= ",";
res ~= f;
}
return res;
}
/// returns "SELECT <field list> FROM <table name>"
string generateSelectSQL(T, fieldList...)() {
string res = "SELECT ";
res ~= joinFieldList!fieldList;
res ~= " FROM " ~ getTableNameForType!(T)();
return res;
}
unittest {
//pragma(msg, "column names: " ~ join(getColumnNamesForType!(User)(), ","));
//pragma(msg, "select SQL: " ~ generateSelectSQL!(User)());
}
/// returns "SELECT <field list> FROM <table name>"
string generateSelectForGetSQL(T)() {
string res = generateSelectSQL!T();
res ~= " WHERE id=";
return res;
}
string generateSelectForGetSQLWithFilter(T)() {
string res = generateSelectSQL!T();
res ~= " WHERE ";
return res;
}
T get(T)(Statement stmt, long id) {
T entity;
static immutable getSQL = generateSelectForGetSQL!T();
ResultSet r;
r = stmt.executeQuery(getSQL ~ to!string(id));
r.next();
mixin(getAllColumnsReadCode!T());
return entity;
}
T get(T)(Statement stmt, string filter) {
T entity;
static immutable getSQL = generateSelectForGetSQLWithFilter!T();
ResultSet r;
r = stmt.executeQuery(getSQL ~ filter);
r.next();
mixin(getAllColumnsReadCode!T());
return entity;
}
string getColumnTypeDatasetReadCodeByName(T, string m)() {
PropertyMemberType pmt = getPropertyMemberType!(T,m)();
final switch(pmt) with (PropertyMemberType) {
case BOOL_TYPE:
return `r.getBoolean("` ~ m ~ `")`;
case BYTE_TYPE:
return `r.getByte("` ~ m ~ `")`;
case SHORT_TYPE:
return `r.getShort("` ~ m ~ `")`;
case INT_TYPE:
return `r.getInt("` ~ m ~ `")`;
case LONG_TYPE:
return `r.getLong("` ~ m ~ `")`;
case UBYTE_TYPE:
return `r.getUbyte("` ~ m ~ `")`;
case USHORT_TYPE:
return `r.getUshort("` ~ m ~ `")`;
case UINT_TYPE:
return `r.getUint("` ~ m ~ `")`;
case ULONG_TYPE:
return `r.getUlong("` ~ m ~ `")`;
case FLOAT_TYPE:
return `r.getFloat("` ~ m ~ `")`;
case DOUBLE_TYPE:
return `r.getDouble("` ~ m ~ `")`;
case STRING_TYPE:
return `r.getString("` ~ m ~ `")`;
case DATE_TYPE:
return `r.getDate("` ~ m ~ `")`;
case TIME_TYPE:
return `r.getTime("` ~ m ~ `")`;
case SYSTIME_TYPE:
return `r.getSysTime("` ~ m ~ `")`;
case DATETIME_TYPE:
return `r.getDateTime("` ~ m ~ `")`;
case BYTE_ARRAY_TYPE:
return `r.getBytes("` ~ m ~ `")`;
case UBYTE_ARRAY_TYPE:
return `r.getUbytes("` ~ m ~ `")`;
case NULLABLE_BYTE_TYPE:
return `Nullable!byte(r.getByte("` ~ m ~ `"))`;
case NULLABLE_SHORT_TYPE:
return `Nullable!short(r.getShort("` ~ m ~ `"))`;
case NULLABLE_INT_TYPE:
return `Nullable!int(r.getInt("` ~ m ~ `"))`;
case NULLABLE_LONG_TYPE:
return `Nullable!long(r.getLong("` ~ m ~ `"))`;
case NULLABLE_UBYTE_TYPE:
return `Nullable!ubyte(r.getUbyte("` ~ m ~ `"))`;
case NULLABLE_USHORT_TYPE:
return `Nullable!ushort(r.getUshort("` ~ m ~ `"))`;
case NULLABLE_UINT_TYPE:
return `Nullable!uint(r.getUint("` ~ m ~ `"))`;
case NULLABLE_ULONG_TYPE:
return `Nullable!ulong(r.getUlong("` ~ m ~ `"))`;
case NULLABLE_FLOAT_TYPE:
return `Nullable!float(r.getFloat("` ~ m ~ `"))`;
case NULLABLE_DOUBLE_TYPE:
return `Nullable!double(r.getDouble("` ~ m ~ `"))`;
case NULLABLE_STRING_TYPE:
return `r.getString("` ~ m ~ `")`;
case NULLABLE_DATE_TYPE:
return `Nullable!Date(r.getDate("` ~ m ~ `"))`;
case NULLABLE_TIME_TYPE:
return `Nullable!Time(r.getTime("` ~ m ~ `"))`;
case NULLABLE_SYSTIME_TYPE:
return `Nullable!SysTime(r.getSysTime("` ~ m ~ `"))`;
case NULLABLE_DATETIME_TYPE:
return `Nullable!DateTime(r.getDateTime("` ~ m ~ `"))`;
}
}
string getPropertyWriteCodeByName(T, string m)() {
immutable string nullValueCode = ColumnTypeSetNullCode[getPropertyMemberType!(T,m)()];
immutable string propertyWriter = nullValueCode ~ "entity." ~ m ~ " = " ~ getColumnTypeDatasetReadCodeByName!(T, m)() ~ ";\n";
return propertyWriter ~ "if (r.wasNull) entity." ~ m ~ " = nv;";
}
string getColumnReadCodeByName(T, string m)() {
return "{" ~ getPropertyWriteCodeByName!(T,m)() ~ "}\n";
}
string getAllColumnsReadCodeByName(T)() {
string res;
foreach(m; FieldNameTuple!T) {
static if (__traits(compiles, (typeof(__traits(getMember, T, m))))){
// skip non-public members
static if (__traits(getProtection, __traits(getMember, T, m)) == "public") {
static if (isSupportedSimpleType!(T, m)) {
res ~= getColumnReadCodeByName!(T, m);
}
}
}
}
return res;
}
/**
* Extract a row from the result set as the specified type.
* Requires that next has already been checked.
* Can be used for example to extract rows from executing a PreparedStatement.
*/
T get(T)(ResultSet r) {
T entity;
mixin(getAllColumnsReadCodeByName!T());
return entity;
}
/// range for select query
struct select(T, fieldList...) if (__traits(isPOD, T)) {
T entity;
private Statement stmt;
private ResultSet r;
static immutable selectSQL = generateSelectSQL!(T, fieldList)();
string whereCondSQL;
string orderBySQL;