-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.cpp
1659 lines (1456 loc) · 44.9 KB
/
builder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
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
/* gSAFE - LIB
general Sql dAtabase FrontEnd
http://hyperprog.com/gsafe/
(C) 2005-2024 Péter Deák ([email protected])
License: Apache 2.0
*/
#include <string.h>
#include "builder.h"
#include "dconsole.h"
#define FIELD_GET 1
#define FIELD_SET_V 2
#define FIELD_SET_E 3
#define COND_ZZ 0
#define COND_FF 1
#define COND_FV 2
#define COND_FE 3
#define COND_FB 4
#define COND_SPEC_F 5
#define COND_SPEC_V 6
#define COND_SPEC_VF 7
#define COND_GRP 8
#define COND_SORT 9
QMap<QString,QString> HSqlBuilder::spec_conds = QMap<QString,QString>();
HSqlBuilderCondition cond(HSqlBuilder_ConditionRelation r)
{
return HSqlBuilderCondition(r);
}
HSqlBuilderCondition not_cond(HSqlBuilder_ConditionRelation r)
{
return HSqlBuilderCondition(r,"opposite=true");
}
HSqlBuilder db_query(QString tablename,QString alias)
{
return HSqlBuilder(Select,tablename,alias);
}
HSqlBuilder db_insert(QString tablename,QString alias)
{
return HSqlBuilder(Insert,tablename,alias);
}
HSqlBuilder db_update(QString tablename,QString alias)
{
return HSqlBuilder(Update,tablename,alias);
}
HSqlBuilder db_delete(QString tablename,QString alias)
{
return HSqlBuilder(Delete,tablename,alias);
}
// Sql dialect table ////////////////////////////////////////////////////////////
const char *dialectTablePuzzles[] = {//Empty string is the closer element !
"<<<current_timestamp>>>",
"<<<datetype_null>>>",
"<<<numeric_null>>>",
"<<<longtext_type>>>",
"<<<regex>>>",
"",
};
const char *dialectTableDialects[] = //Empty string is the closer element !
{"nochange" , "sqlite" , "pgsql" , "mysql" , ""};
const char *dialectTableResolve[][4] = {
{"<<<current_timestamp>>>", "CURRENT_TIMESTAMP", "now()" , "CURRENT_TIMESTAMP" },
{"<<<datetype_null>>>" , "NULL" , "NULL" , "NULL" },
{"<<<numeric_null>>>" , "NULL" , "NULL" , "NULL" },
{"<<<longtext_type>>>" , "longtext" , "text" , "longtext" },
{"<<<regex>>>" , "regexp" , "~" , "regexp" },
};
// HSqlBuilderField ////////////////////////////////////////////////////////////
HSqlBuilderField::HSqlBuilderField()
{
type = FIELD_GET;
table = "";
name = "";
alias = "";
vt = Quoted;
value_expression = "";
options = "";
}
HSqlBuilderField::~HSqlBuilderField()
{
}
bool HSqlBuilderField::isGetType(void)
{
if(type == FIELD_GET)
return true;
return false;
}
bool HSqlBuilderField::isSetType(void)
{
if(type == FIELD_SET_V || type == FIELD_SET_E)
return true;
return false;
}
QString HSqlBuilderField::getVisibleName(void)
{
if(alias.isEmpty())
return name;
return alias;
}
HSqlBuilderField& HSqlBuilderField::get(const QString fieldName,const QString aliasName,const QString optionsString)
{
get_withtable("",fieldName,aliasName,optionsString);
return *this;
}
HSqlBuilderField& HSqlBuilderField::get(const QString fieldSpec[2],const QString aliasName,const QString optionsString)
{
get_withtable(fieldSpec[0],fieldSpec[1],aliasName,optionsString);
return *this;
}
HSqlBuilderField& HSqlBuilderField::get_withtable(const QString tableName,const QString fieldName,const QString aliasName,const QString optionsString)
{
type = FIELD_GET;
name = fieldName;
table = tableName;
alias = aliasName;
options = optionsString;
return *this;
}
HSqlBuilderField& HSqlBuilderField::set_fv(const QString fieldName,HSqlBuilder_FieldValueType vType,QVariant value,const QString optionsString)
{
type = FIELD_SET_V;
table = "";
name = fieldName;
alias = "";
vt = vType;
value_expression = value;
options = optionsString;
return *this;
}
HSqlBuilderField& HSqlBuilderField::set_fe(const QString fieldName,const QString expression,const QString optionsString)
{
type = FIELD_SET_E;
table = "";
name = fieldName;
alias = "";
vt = Unquoted;
value_expression = expression;
options = optionsString;
return *this;
}
QString HSqlBuilderField::local_cmd_Get(void)
{
if(type != FIELD_GET)
return QString();
QString str;
if(table.isEmpty())
str = name;
else
str = table + "." + name;
QMap<QString,QString> opts = HSqlBuilder::genOptions(options);
if(!opts.value("function","").isEmpty())
{
QString modstr;
modstr = opts.value("function","") + QString("(");
if(!opts.value("more_args_before","").isEmpty())
modstr.append(opts.value("more_args_before","") + ",");
modstr.append(str);
if(!opts.value("more_args","").isEmpty())
modstr.append(QString(",") + opts.value("more_args","") + ",");
if(!opts.value("more_args_after","").isEmpty())
modstr.append(QString(",") + opts.value("more_args_after","") + ",");
modstr.append(")");
str = modstr;
}
if(!alias.isEmpty())
str += " AS " + alias;
return str;
}
QString HSqlBuilderField::local_cmd_Key(void)
{
if(type != FIELD_SET_V && type != FIELD_SET_E)
return QString();
return name;
}
QString HSqlBuilderField::local_cmd_Val(HSqlBuilder *builder,bool vmm,QString dialect)
{
if(type != FIELD_SET_V && type != FIELD_SET_E)
return QString();
QString v = "";
QMap<QString,QString> opts = HSqlBuilder::genOptions(options);
if(type == FIELD_SET_V)
{
if(vmm)
{
v = builder->addValueForBind(value_expression);
}
else
{
v = value_expression.toString();
if(vt == Quoted)
v = QString("\'%1\'").arg(value_expression.toString());
}
}
if(type == FIELD_SET_E)
{
v = value_expression.toString();
if(opts.value("dialected_element","no") == "yes")
v = HSqlBuilder::translateDialect(v,dialect);
if(vt == Quoted)
v = QString("\'%1\'").arg(value_expression.toString());
}
if(!opts.value("function","").isEmpty())
{
QString modfsql;
modfsql = opts.value("function","") + QString("(");
if(!opts.value("more_args_before","").isEmpty())
modfsql.append(opts.value("more_args_before","") + ",");
modfsql.append(v);
if(!opts.value("more_args","").isEmpty())
modfsql.append(QString(",") + opts.value("more_args","") + ",");
if(!opts.value("more_args_after","").isEmpty())
modfsql.append(QString(",") + opts.value("more_args_after","") + ",");
modfsql.append(")");
v = modfsql;
}
return v;
}
QString HSqlBuilderField::json_string(void)
{
QString jstr = "";
if(isGetType())
{
jstr += "{";
jstr += QString("\"table\": \"%1\",").arg(table);
jstr += QString("\"name\": \"%1\",").arg(name);
jstr += QString("\"alias\": \"%1\",").arg(alias);
jstr += QString("\"options\": \"%1\"").arg(options);
jstr += "}";
}
if(isSetType())
{
jstr += "{";
if(type == FIELD_SET_V)
jstr += QString("\"type\": \"set_val\",");
if(type == FIELD_SET_E)
jstr += QString("\"type\": \"set_expr\",");
if(vt == Quoted)
jstr += QString("\"valtype\": \"quoted\",");
if(vt == Unquoted)
jstr += QString("\"valtype\": \"unquoted\",");
jstr += QString("\"name\": \"%1\",").arg(name);
jstr += QString("\"value\": \"%1\",").arg(strToJsonStr(value_expression.toString()));
jstr += QString("\"options\": \"%1\"").arg(options);
jstr += "}";
}
return jstr;
}
// HSqlBuilderCondition ////////////////////////////////////////////////////////////
HSqlBuilderCondition::HSqlBuilderCondition(HSqlBuilder_ConditionRelation r,QString opts)
{
ct = COND_GRP;
relation = r;
vt = Unquoted;
cname = "";
value_expression = "";
op = "";
field1 = "";
field2 = "";
table1 = "";
table2 = "";
options = opts;
sub_conds.clear();
}
HSqlBuilderCondition::~HSqlBuilderCondition()
{
}
HSqlBuilderCondition& HSqlBuilderCondition::ff(const QString fieldName1,const QString fieldName2,const QString operation,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FF;
newcond.op = operation;
newcond.field1 = fieldName1;
newcond.table1 = "";
newcond.field2 = fieldName2;
newcond.table2 = "";
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::ff(const QString fieldSpec1[2],const QString fieldSpec2[2],const QString operation,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FF;
newcond.op = operation;
newcond.table1 = fieldSpec1[0];
newcond.field1 = fieldSpec1[1];
newcond.table2 = fieldSpec2[0];
newcond.field2 = fieldSpec2[1];
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::ff(const QString tableName1,const QString fieldName1,const QString tableName2,const QString fieldName2,const QString operation,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FF;
newcond.op = operation;
newcond.table1 = tableName1;
newcond.field1 = fieldName1;
newcond.table2 = tableName2;
newcond.field2 = fieldName2;
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::fv(const QString fieldName,HSqlBuilder_FieldValueType vType,const QString value,const QString operation,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FV;
newcond.vt = vType;
newcond.value_expression = value;
newcond.op = operation;
newcond.table1 = "";
newcond.field1 = fieldName;
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::fv(const QString fieldSpec[2],HSqlBuilder_FieldValueType vType,const QString value,const QString operation,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FV;
newcond.vt = vType;
newcond.value_expression = value;
newcond.op = operation;
newcond.table1 = fieldSpec[0];
newcond.field1 = fieldSpec[1];
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::fe(const QString fieldName,const QString expression,const QString operation,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FE;
newcond.value_expression = expression;
newcond.op = operation;
newcond.table1 = "";
newcond.field1 = fieldName;
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::fe(const QString fieldSpec[2],const QString expression,const QString operation,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FE;
newcond.value_expression = expression;
newcond.op = operation;
newcond.table1 = fieldSpec[0];
newcond.field1 = fieldSpec[1];
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::fb(const QString fieldName,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FB;
newcond.table1 = "";
newcond.field1 = fieldName;
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::fb(const QString fieldSpec[2],const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_FB;
newcond.table1 = fieldSpec[0];
newcond.field1 = fieldSpec[1];
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::spec_f(const QString condSpec,const QString fieldName,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_SPEC_F;
newcond.cname = condSpec;
newcond.table1 = "";
newcond.field1 = fieldName;
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::spec_f(const QString condSpec,const QString fieldSpec[2],const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_SPEC_F;
newcond.cname = condSpec;
newcond.table1 = fieldSpec[0];
newcond.field1 = fieldSpec[1];
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::spec_v(const QString condSpec,const HSqlBuilder_FieldValueType vType,const QString value,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_SPEC_V;
newcond.cname = condSpec;
newcond.vt = vType;
newcond.value_expression = value;
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::spec_fv(const QString condSpec,const QString fieldName,HSqlBuilder_FieldValueType vType,const QString value,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_SPEC_VF;
newcond.cname = condSpec;
newcond.vt = vType;
newcond.value_expression = value;
newcond.table1 = "";
newcond.field1 = fieldName;
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::spec_fv(const QString condSpec,const QString fieldSpec[2],HSqlBuilder_FieldValueType vType,const QString value,const QString optionsString)
{
HSqlBuilderCondition newcond;
newcond.ct = COND_SPEC_VF;
newcond.cname = condSpec;
newcond.vt = vType;
newcond.value_expression = value;
newcond.table1 = fieldSpec[0];
newcond.field1 = fieldSpec[1];
newcond.options = optionsString;
add(newcond);
return *this;
}
HSqlBuilderCondition& HSqlBuilderCondition::add(HSqlBuilderCondition cond)
{
ct = COND_GRP;
sub_conds.push_back(cond);
return *this;
}
bool HSqlBuilderCondition::isGrpCond(void)
{
if(ct == COND_GRP)
return true;
return false;
}
bool HSqlBuilderCondition::isEmpty()
{
if((ct == COND_GRP || ct == COND_ZZ) && sub_conds.count() == 0)
return true;
return false;
}
HSqlBuilder_ConditionRelation HSqlBuilderCondition::topRelation(void)
{
return relation;
}
QString HSqlBuilderCondition::local_cmd(HSqlBuilder *builder,bool top,bool vmm,QString dialect)
{
QString local_cmd = "";
if(ct == COND_ZZ)
return local_cmd;
QMap<QString,QString> opts = HSqlBuilder::genOptions(options);
if(opts.value("opposite","") == "true")
local_cmd += "NOT ";
if(ct == COND_GRP)
{
QString sub_cmd = "";
QList<HSqlBuilderCondition>::iterator ci = sub_conds.begin();
while(ci != sub_conds.end())
{
if(!sub_cmd.isEmpty())
sub_cmd += relation == And ? " AND " : " OR ";
sub_cmd += ci->local_cmd(builder,false,vmm,dialect);
++ci;
}
if(!top)
local_cmd += "(" + sub_cmd + ")";
else
local_cmd += sub_cmd;
}
if(ct == COND_FF)
{
QString f1,f2;
f1 = (table1.isEmpty() ? "" : table1 + ".") + field1;
f2 = (table2.isEmpty() ? "" : table2 + ".") + field2;
if(!opts.value("f1function","").isEmpty())
f1 = opts.value("f1function") + "(" + f1 + ")";
if(!opts.value("f2function","").isEmpty())
f2 = opts.value("f2function") + "(" + f2 + ")";
local_cmd += f1 + " " + op + " " + f2;
}
if(ct == COND_FV)
{
QString v,f;
if(vmm)
{
v = builder->addValueForBind(value_expression);
}
else
{
if(vt == Quoted)
v = QString("\'%1\'").arg(value_expression);
if(vt == Unquoted)
v = value_expression;
}
f = (table1.isEmpty() ? "" : table1 + ".") + field1;
if(!opts.value("vfunction","").isEmpty())
v = opts.value("vfunction") + "(" + v + ")";
if(!opts.value("ffunction","").isEmpty())
f = opts.value("ffunction") + "(" + f + ")";
local_cmd += f + " " + op + " " + v;
}
if(ct == COND_FE)
{
QString e,f;
e = value_expression;
f = (table1.isEmpty() ? "" : table1 + ".") + field1;
if(!opts.value("efunction","").isEmpty())
e = opts.value("efunction") + "(" + e + ")";
if(!opts.value("ffunction","").isEmpty())
f = opts.value("ffunction") + "(" + f + ")";
local_cmd += f + " " + op + " " + e;
}
if(ct == COND_FB)
{
QString f;
f = (table1.isEmpty() ? "" : table1 + ".") + field1;
if(!opts.value("ffunction","").isEmpty())
f = opts.value("ffunction") + "(" + f + ")";
local_cmd += f;
}
if(ct == COND_SPEC_VF || ct == COND_SPEC_F || ct == COND_SPEC_V)
{
QString v,f,t;
t = HSqlBuilder::special_cond_template(cname);
if(ct == COND_SPEC_VF || ct == COND_SPEC_V)
{
if(vmm)
{
v = builder->addValueForBind(value_expression);
}
else
{
if(vt == Quoted)
v = QString("\'%1\'").arg(value_expression);
if(vt == Unquoted)
v = value_expression;
}
if(!opts.value("vfunction","").isEmpty())
v = opts.value("vfunction") + "(" + v + ")";
t.replace(QString("__VALUE__"),v);
}
if(ct == COND_SPEC_VF || ct == COND_SPEC_F)
{
f = (table1.isEmpty() ? "" : table1 + ".") + field1;
if(!opts.value("ffunction","").isEmpty())
f = opts.value("ffunction") + "(" + f + ")";
t.replace(QString("__FIELD__"),f);
}
local_cmd += t;
}
return local_cmd;
}
QString HSqlBuilderCondition::json_string(void)
{
QString cstr = "";
if(ct == COND_ZZ)
return cstr;
if(ct == COND_GRP)
{
QMap<QString,QString> opts = HSqlBuilder::genOptions(options);
cstr += "{";
if(opts.value("opposite","") == "true")
{
if(relation == And) cstr += QString("\"relation\": \"nand\",");
if(relation == Or) cstr += QString("\"relation\": \"nor\",");
}
else
{
if(relation == And) cstr += QString("\"relation\": \"and\",");
if(relation == Or) cstr += QString("\"relation\": \"or\",");
}
cstr += QString("\"type\": \"sub\",");
cstr += QString("\"subcond\": [");
QList<HSqlBuilderCondition>::iterator ci = sub_conds.begin();
QString cas = "";
while(ci != sub_conds.end())
{
QString s = ci->json_string();
if(!s.isEmpty())
{
if(!cas.isEmpty())
cas += ",";
cas += s;
}
++ci;
}
cstr += cas + "]}";
return cstr;
}
cstr += "{";
if(ct == COND_FF) cstr += QString("\"type\": \"ff\",");
if(ct == COND_FV) cstr += QString("\"type\": \"fv\",");
if(ct == COND_FE) cstr += QString("\"type\": \"fe\",");
if(ct == COND_FB) cstr += QString("\"type\": \"fb\",");
if(ct == COND_SPEC_F) cstr += QString("\"type\": \"spec_f\",");
if(ct == COND_SPEC_V) cstr += QString("\"type\": \"spec_v\",");
if(ct == COND_SPEC_VF) cstr += QString("\"type\": \"spec_vf\",");
if(vt == Quoted) cstr += QString("\"valtype\": \"quoted\",");
if(vt == Unquoted) cstr += QString("\"valtype\": \"unquoted\",");
if(ct == COND_SPEC_F || ct == COND_SPEC_V || ct == COND_SPEC_VF)
cstr += QString("\"template\": \"%1\",").arg(cname);
else
cstr += QString("\"op\": \"%1\",").arg(op);
cstr += QString("\"value\": \"%1\",").arg(strToJsonStr(value_expression));
cstr += QString("\"field1\": \"%1\",").arg(field1);
cstr += QString("\"table1\": \"%1\",").arg(table1);
cstr += QString("\"field2\": \"%1\",").arg(field2);
cstr += QString("\"table2\": \"%1\",").arg(table2);
cstr += QString("\"options\": \"%1\"").arg(options);
cstr += "}";
return cstr;
}
QString HSqlBuilderCondition::json_string_top(void)
{
QString cstr = "";
if(ct == COND_ZZ)
return cstr;
if(ct == COND_GRP)
{
cstr += "[";
QList<HSqlBuilderCondition>::iterator ci = sub_conds.begin();
QString cas = "";
while(ci != sub_conds.end())
{
QString s = ci->json_string();
if(!s.isEmpty())
{
if(!cas.isEmpty())
cas += ",";
cas += s;
}
++ci;
}
cstr += cas + "]";
return cstr;
}
return cstr;
}
// HSqlBuilderJoin ///////////////////////////////////////////////////////////////
HSqlBuilderJoin::HSqlBuilderJoin()
{
empty = true;
jtype = Inner;
totable = "";
toalias = "";
jcond = HSqlBuilderCondition(And);
}
HSqlBuilderJoin::~HSqlBuilderJoin()
{
}
HSqlBuilderJoin& HSqlBuilderJoin::join(const QString toTable,const QString toAlias,HSqlBuilderCondition joinCond)
{
empty = false;
jtype = Inner;
totable = toTable;
toalias = toAlias;
jcond = joinCond;
return *this;
}
HSqlBuilderJoin& HSqlBuilderJoin::join_opt(const QString toTable,const QString toAlias,HSqlBuilderCondition joinCond)
{
empty = false;
jtype = LeftOuter;
totable = toTable;
toalias = toAlias;
jcond = joinCond;
return *this;
}
bool HSqlBuilderJoin::isEmpty()
{
return empty;
}
QString HSqlBuilderJoin::local_cmd(HSqlBuilder *builder,bool vmm,QString dialect)
{
if(empty)
return "";
QString jstr = "";
QString jcondstr = "";
if(jtype == Inner)
jstr += QString("INNER JOIN ");
if(jtype == LeftOuter)
jstr += QString("LEFT OUTER JOIN ");
jstr += totable;
if(!toalias.isEmpty())
jstr += QString(" AS %1").arg(toalias);
jcondstr = jcond.local_cmd(builder,true,vmm,dialect);
if(!jcondstr.isEmpty())
jstr += QString(" ON %1").arg(jcondstr);
return jstr;
}
QString HSqlBuilderJoin::json_string(void)
{
QString joinstring = "";
if(!jcond.isGrpCond() || empty)
return joinstring;
joinstring += "{";
if(jtype == Inner)
joinstring += QString("\"type\": \"inner\",");
if(jtype == LeftOuter)
joinstring += QString("\"type\": \"leftouter\",");
joinstring += QString("\"jointable\": \"%1\",").arg(totable);
joinstring += QString("\"jointablealias\": \"%1\",").arg(toalias);
if(jcond.topRelation() == And)
joinstring += QString("\"condtoprel\": \"and\",");
if(jcond.topRelation() == Or)
joinstring += QString("\"condtoprel\": \"or\",");
joinstring += QString("\"cond\": %1").arg(jcond.json_string_top());
joinstring += "}";
return joinstring;
}
// HSqlBuilderSort ///////////////////////////////////////////////////////////////
HSqlBuilderSort::HSqlBuilderSort()
{
empty = true;
field = "";
table = "";
options = "";
}
HSqlBuilderSort::~HSqlBuilderSort()
{
}
void HSqlBuilderSort::set(const QString fieldName,const QString optionsString)
{
empty = false;
field = fieldName;
options = optionsString;
}
void HSqlBuilderSort::set(const QString fieldSpec[2],const QString optionsString)
{
empty = false;
table = fieldSpec[0];
field = fieldSpec[1];
options = optionsString;
}
void HSqlBuilderSort::set(const QString tableName,const QString fieldName,const QString optionsString)
{
empty = false;
table = tableName;
field = fieldName;
options = optionsString;
}
QString HSqlBuilderSort::local_cmd(QString dialect)
{
Q_UNUSED(dialect)
if(empty)
return "";
QString str = "";
QMap<QString,QString> opts = HSqlBuilder::genOptions(options);
QString f = (table.isEmpty() ? "" : table + ".") + field;
if(opts.value("function","").isEmpty())
str = f;
else
str = QString("%1(%2%3%4%5%6%7%8)")
.arg(opts.value("function"))
.arg(opts.value("more_args_before",""))
.arg(opts.value("more_args_before","").isEmpty() ? "":",")
.arg(f)
.arg(opts.value("more_args","").isEmpty() ? "":",")
.arg(opts.value("more_args",""))
.arg(opts.value("more_args_after","").isEmpty() ? "":",")
.arg(opts.value("more_args_after",""));
if(opts.value("direction","") == "REVERSE")
str.append(" DESC");
return str;
}
QString HSqlBuilderSort::json_string(void)
{
QString sstr;
if(empty)
return "";
sstr += "{";
sstr += QString("\"field\": \"%1\",").arg(field);
sstr += QString("\"table\": \"%1\",").arg(table);
sstr += QString("\"options\": \"%1\"").arg(options);
sstr += "}";
return sstr;
}
bool HSqlBuilderSort::isEmpty()
{
return empty;
}
// HSqlBuilder //////////////////////////////////////////////////////////////////
QString HSqlBuilder::translateDialect(QString subjectString,QString dialect)
{
int d_idx = 0,p_idx = 0;
while(true)
{
if(strlen(dialectTableDialects[d_idx]) == 0)
return subjectString;
if(dialect == dialectTableDialects[d_idx])
break;
++d_idx;
}
while(true)
{
if(strlen(dialectTablePuzzles[p_idx]) == 0)
return subjectString;
if(subjectString == dialectTablePuzzles[p_idx])
break;
++p_idx;
}
return dialectTableResolve[p_idx][d_idx];
}
HSqlBuilder::HSqlBuilder(HSqlBuilder_QueryType qtype,QString tablename,QString alias)
{
type = qtype;
base_table = tablename;
base_alias = alias;
count_alias = "";
count_field = "";
count_table = "";
limitquery = 0;
condition = HSqlBuilderCondition(And);
forBind.clear();
jsonExModeReq = UnspecifiedAuto;
}
HSqlBuilder::~HSqlBuilder()
{
}
void HSqlBuilder::add_special_cond_template(QString id,QString templ)
{
spec_conds[id] = templ;
}
QString HSqlBuilder::special_cond_template(QString id)
{
return spec_conds.value(id,"");
}
QMap<QString,QString> HSqlBuilder::genOptions(QString options)
{
int i;
QMap<QString,QString> m;
QStringList opts = options.split(";",Qt::SkipEmptyParts);
for(i = 0 ; i < opts.count() ; ++i)
{
QStringList o2 = opts.at(i).split("=");
if(o2.count() == 2)
m[o2[0]] = o2[1];
}
return m;
}
QString HSqlBuilder::addValueForBind(QVariant v)
{
QString pholder;
int index = forBind.count();
++index;
pholder = QString(":ph_%1_pos").arg(index);
forBind[pholder] = v;
return pholder;
}
QMap<QString,QVariant> HSqlBuilder::bind_array(void)
{
return forBind;
}
QStringList HSqlBuilder::query_field_list(void)
{
QStringList lst;
lst.clear();
QList<HSqlBuilderField>::iterator fi = field_list.begin();
while(fi != field_list.end())
{
if(!fi->isGetType())
{
++fi;
continue;
}
lst.push_back(fi->getVisibleName());
++fi;
}
return lst;
}
HSqlBuilder& HSqlBuilder::countingField(QString alias,QString field,QString table)