1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.prometheus.database;
18
19 import io.github.tonywasher.joceanus.gordianknot.util.GordianUtilities;
20 import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
21 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
22 import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
23 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
24 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusPrice;
25 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRate;
26 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRatio;
27 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusUnits;
28 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
29 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusBinaryColumn;
30 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusBooleanColumn;
31 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusDateColumn;
32 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusIdColumn;
33 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusIntegerColumn;
34 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusLongColumn;
35 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusMoneyColumn;
36 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusPriceColumn;
37 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusRateColumn;
38 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusRatioColumn;
39 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusReferenceColumn;
40 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusStringColumn;
41 import io.github.tonywasher.joceanus.prometheus.database.PrometheusColumnDefinition.PrometheusUnitsColumn;
42 import io.github.tonywasher.joceanus.prometheus.exc.PrometheusLogicException;
43
44 import java.sql.PreparedStatement;
45 import java.sql.ResultSet;
46 import java.sql.SQLException;
47 import java.util.ArrayList;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.List;
51 import java.util.Map;
52
53
54
55
56 public class PrometheusTableDefinition {
57
58
59
60 private static final String PREFIX_INDEX = "idx_";
61
62
63
64
65 protected static final String QUOTE_STRING = "\"";
66
67
68
69
70 protected static final String STR_DESC = " DESC";
71
72
73
74
75 private static final int BUFFER_LEN = 1000;
76
77
78
79
80 private final String theTableName;
81
82
83
84
85 private final PrometheusJDBCDriver theDriver;
86
87
88
89
90 private final List<PrometheusColumnDefinition> theList;
91
92
93
94
95 private final List<PrometheusColumnDefinition> theSortList;
96
97
98
99
100 private boolean sortOnReference;
101
102
103
104
105 private final Map<MetisDataFieldId, PrometheusColumnDefinition> theMap;
106
107
108
109
110 private PreparedStatement theStatement;
111
112
113
114
115
116
117
118 protected PrometheusTableDefinition(final PrometheusJDBCDriver pDriver,
119 final String pName) {
120
121 theTableName = pName;
122 theDriver = pDriver;
123
124
125 theList = new ArrayList<>();
126
127
128 theSortList = new ArrayList<>();
129
130
131 theMap = new HashMap<>();
132
133
134 theList.add(new PrometheusIdColumn(this));
135 }
136
137
138
139
140
141
142 protected String getTableName() {
143 return theTableName;
144 }
145
146
147
148
149
150
151 protected Map<MetisDataFieldId, PrometheusColumnDefinition> getMap() {
152 return theMap;
153 }
154
155
156
157
158
159
160 protected boolean isIndexed() {
161 return !theSortList.isEmpty();
162 }
163
164
165
166
167
168
169 protected List<PrometheusColumnDefinition> getColumns() {
170 return theList;
171 }
172
173
174
175
176
177
178 protected List<PrometheusColumnDefinition> getSortList() {
179 return theSortList;
180 }
181
182
183
184
185
186
187 protected PrometheusJDBCDriver getDriver() {
188 return theDriver;
189 }
190
191
192
193
194 protected void setSortOnReference() {
195 sortOnReference = true;
196 }
197
198
199
200
201
202
203
204
205 public PrometheusReferenceColumn addReferenceColumn(final MetisDataFieldId pId,
206 final String pRef) {
207
208 final PrometheusReferenceColumn myColumn = new PrometheusReferenceColumn(this, pId, pRef);
209
210
211 theList.add(myColumn);
212 return myColumn;
213 }
214
215
216
217
218
219
220
221
222 public PrometheusReferenceColumn addNullReferenceColumn(final MetisDataFieldId pId,
223 final String pRef) {
224 final PrometheusReferenceColumn myColumn = addReferenceColumn(pId, pRef);
225 myColumn.setNullable();
226 return myColumn;
227 }
228
229
230
231
232
233
234
235 public PrometheusIntegerColumn addIntegerColumn(final MetisDataFieldId pId) {
236
237 final PrometheusIntegerColumn myColumn = new PrometheusIntegerColumn(this, pId);
238
239
240 theList.add(myColumn);
241 return myColumn;
242 }
243
244
245
246
247
248
249
250 public PrometheusIntegerColumn addNullIntegerColumn(final MetisDataFieldId pId) {
251 final PrometheusIntegerColumn myColumn = addIntegerColumn(pId);
252 myColumn.setNullable();
253 return myColumn;
254 }
255
256
257
258
259
260
261
262 public PrometheusLongColumn addLongColumn(final MetisDataFieldId pId) {
263
264 final PrometheusLongColumn myColumn = new PrometheusLongColumn(this, pId);
265
266
267 theList.add(myColumn);
268 return myColumn;
269 }
270
271
272
273
274
275
276
277 public PrometheusLongColumn addNullLongColumn(final MetisDataFieldId pId) {
278 final PrometheusLongColumn myColumn = addLongColumn(pId);
279 myColumn.setNullable();
280 return myColumn;
281 }
282
283
284
285
286
287
288
289 public PrometheusBooleanColumn addBooleanColumn(final MetisDataFieldId pId) {
290
291 final PrometheusBooleanColumn myColumn = new PrometheusBooleanColumn(this, pId);
292
293
294 theList.add(myColumn);
295 return myColumn;
296 }
297
298
299
300
301
302
303
304 public PrometheusBooleanColumn addNullBooleanColumn(final MetisDataFieldId pId) {
305 final PrometheusBooleanColumn myColumn = addBooleanColumn(pId);
306 myColumn.setNullable();
307 return myColumn;
308 }
309
310
311
312
313
314
315
316 public PrometheusDateColumn addDateColumn(final MetisDataFieldId pId) {
317
318 final PrometheusDateColumn myColumn = new PrometheusDateColumn(this, pId);
319
320
321 theList.add(myColumn);
322 return myColumn;
323 }
324
325
326
327
328
329
330
331 public PrometheusDateColumn addNullDateColumn(final MetisDataFieldId pId) {
332 final PrometheusDateColumn myColumn = addDateColumn(pId);
333 myColumn.setNullable();
334 return myColumn;
335 }
336
337
338
339
340
341
342
343 public PrometheusMoneyColumn addMoneyColumn(final MetisDataFieldId pId) {
344
345 final PrometheusMoneyColumn myColumn = new PrometheusMoneyColumn(this, pId);
346
347
348 theList.add(myColumn);
349 return myColumn;
350 }
351
352
353
354
355
356
357
358 public PrometheusMoneyColumn addNullMoneyColumn(final MetisDataFieldId pId) {
359 final PrometheusMoneyColumn myColumn = addMoneyColumn(pId);
360 myColumn.setNullable();
361 return myColumn;
362 }
363
364
365
366
367
368
369
370 public PrometheusRateColumn addRateColumn(final MetisDataFieldId pId) {
371
372 final PrometheusRateColumn myColumn = new PrometheusRateColumn(this, pId);
373
374
375 theList.add(myColumn);
376 return myColumn;
377 }
378
379
380
381
382
383
384
385 public PrometheusRateColumn addNullRateColumn(final MetisDataFieldId pId) {
386 final PrometheusRateColumn myColumn = addRateColumn(pId);
387 myColumn.setNullable();
388 return myColumn;
389 }
390
391
392
393
394
395
396
397 public PrometheusRatioColumn addRatioColumn(final MetisDataFieldId pId) {
398
399 final PrometheusRatioColumn myColumn = new PrometheusRatioColumn(this, pId);
400
401
402 theList.add(myColumn);
403 return myColumn;
404 }
405
406
407
408
409
410
411
412 public PrometheusRatioColumn addNullRatioColumn(final MetisDataFieldId pId) {
413 final PrometheusRatioColumn myColumn = addRatioColumn(pId);
414 myColumn.setNullable();
415 return myColumn;
416 }
417
418
419
420
421
422
423
424
425 public PrometheusBinaryColumn addBinaryColumn(final MetisDataFieldId pId,
426 final int pLength) {
427
428 final PrometheusBinaryColumn myColumn = new PrometheusBinaryColumn(this, pId, pLength);
429
430
431 theList.add(myColumn);
432 return myColumn;
433 }
434
435
436
437
438
439
440
441
442 public PrometheusBinaryColumn addNullBinaryColumn(final MetisDataFieldId pId,
443 final int pLength) {
444 final PrometheusBinaryColumn myColumn = addBinaryColumn(pId, pLength);
445 myColumn.setNullable();
446 return myColumn;
447 }
448
449
450
451
452
453
454
455
456 public PrometheusBinaryColumn addEncryptedColumn(final MetisDataFieldId pId,
457 final int pLength) {
458
459 final PrometheusBinaryColumn myColumn = new PrometheusBinaryColumn(this, pId, GordianUtilities.getKeySetEncryptionLength(pLength));
460
461
462 theList.add(myColumn);
463 return myColumn;
464 }
465
466
467
468
469
470
471
472
473 public PrometheusBinaryColumn addNullEncryptedColumn(final MetisDataFieldId pId,
474 final int pLength) {
475 final PrometheusBinaryColumn myColumn = addEncryptedColumn(pId, pLength);
476 myColumn.setNullable();
477 return myColumn;
478 }
479
480
481
482
483
484
485
486
487 public PrometheusStringColumn addStringColumn(final MetisDataFieldId pId,
488 final int pLength) {
489
490 final PrometheusStringColumn myColumn = new PrometheusStringColumn(this, pId, pLength);
491
492
493 theList.add(myColumn);
494 return myColumn;
495 }
496
497
498
499
500
501
502
503
504 public PrometheusStringColumn addNullStringColumn(final MetisDataFieldId pId,
505 final int pLength) {
506 final PrometheusStringColumn myColumn = addStringColumn(pId, pLength);
507 myColumn.setNullable();
508 return myColumn;
509 }
510
511
512
513
514
515
516 protected void resolveReferences(final List<PrometheusTableDataItem<?>> pTables) {
517
518 final Iterator<PrometheusColumnDefinition> myIterator = theList.iterator();
519
520
521 while (myIterator.hasNext()) {
522 final PrometheusColumnDefinition myDef = myIterator.next();
523 myDef.locateReference(pTables);
524 }
525 }
526
527
528
529
530
531
532
533 protected void loadResults(final ResultSet pResults) throws SQLException {
534
535 clearValues();
536
537
538 final Iterator<PrometheusColumnDefinition> myIterator = theList.iterator();
539 int myIndex = 1;
540
541
542 while (myIterator.hasNext()) {
543 final PrometheusColumnDefinition myDef = myIterator.next();
544 myDef.loadValue(pResults, myIndex++);
545 }
546 }
547
548
549
550
551
552
553
554 private String getColumnError(final PrometheusColumnDefinition pCol) {
555 return "Column " + pCol.getColumnName() + " in table " + theTableName;
556 }
557
558
559
560
561
562
563
564
565 protected void insertValues(final PreparedStatement pStmt) throws SQLException, OceanusException {
566
567 theStatement = pStmt;
568
569
570 final Iterator<PrometheusColumnDefinition> myIterator = theList.iterator();
571 int myIndex = 1;
572
573
574 while (myIterator.hasNext()) {
575 final PrometheusColumnDefinition myDef = myIterator.next();
576
577
578 if (!myDef.isValueSet()) {
579 throw new PrometheusLogicException(getColumnError(myDef) + " has no value for insert");
580 }
581
582 myDef.storeValue(theStatement, myIndex++);
583 }
584 }
585
586
587
588
589
590
591
592 protected void updateValues(final PreparedStatement pStmt) throws SQLException {
593 PrometheusColumnDefinition myId = null;
594
595
596 theStatement = pStmt;
597
598
599 final Iterator<PrometheusColumnDefinition> myIterator = theList.iterator();
600 int myIndex = 1;
601
602
603 while (myIterator.hasNext()) {
604 final PrometheusColumnDefinition myDef = myIterator.next();
605
606
607 if (myDef instanceof PrometheusIdColumn) {
608
609 myId = myDef;
610
611
612 } else if (myDef.isValueSet()) {
613 myDef.storeValue(theStatement, myIndex++);
614 }
615 }
616
617
618 if (myId != null) {
619 myId.storeValue(theStatement, myIndex);
620 }
621 }
622
623
624
625
626 protected void clearValues() {
627
628 for (PrometheusColumnDefinition myDef : theList) {
629
630 myDef.clearValue();
631 }
632 }
633
634
635
636
637
638
639
640
641 public Integer getIntegerValue(final MetisDataFieldId pId) throws OceanusException {
642
643 final PrometheusColumnDefinition myCol = getColumnForId(pId);
644
645
646 if (!(myCol instanceof PrometheusIntegerColumn)) {
647 throw new PrometheusLogicException(getColumnError(myCol) + " is not Integer type");
648 }
649
650
651 final PrometheusIntegerColumn myIntCol = (PrometheusIntegerColumn) myCol;
652 return myIntCol.getValue();
653 }
654
655
656
657
658
659
660
661
662 public Long getLongValue(final MetisDataFieldId pId) throws OceanusException {
663
664 final PrometheusColumnDefinition myCol = getColumnForId(pId);
665
666
667 if (!(myCol instanceof PrometheusLongColumn)) {
668 throw new PrometheusLogicException(getColumnError(myCol) + " is not Long type");
669 }
670
671
672 final PrometheusLongColumn myLongCol = (PrometheusLongColumn) myCol;
673 return myLongCol.getValue();
674 }
675
676
677
678
679
680
681
682
683 public OceanusDate getDateValue(final MetisDataFieldId pId) throws OceanusException {
684
685 final PrometheusColumnDefinition myCol = getColumnForId(pId);
686
687
688 if (!(myCol instanceof PrometheusDateColumn)) {
689 throw new PrometheusLogicException(getColumnError(myCol) + " is not Date type");
690 }
691
692
693 final PrometheusDateColumn myDateCol = (PrometheusDateColumn) myCol;
694 return myDateCol.getValue();
695 }
696
697
698
699
700
701
702
703
704 public Boolean getBooleanValue(final MetisDataFieldId pId) throws OceanusException {
705
706 final PrometheusColumnDefinition myCol = getColumnForId(pId);
707
708
709 if (!(myCol instanceof PrometheusBooleanColumn)) {
710 throw new PrometheusLogicException("Column " + getColumnError(myCol) + " is not Boolean type");
711 }
712
713
714 final PrometheusBooleanColumn myBoolCol = (PrometheusBooleanColumn) myCol;
715 return myBoolCol.getValue();
716 }
717
718
719
720
721
722
723
724
725 public String getStringValue(final MetisDataFieldId pId) throws OceanusException {
726
727 final PrometheusColumnDefinition myCol = getColumnForId(pId);
728
729
730 if (!(myCol instanceof PrometheusStringColumn)) {
731 throw new PrometheusLogicException(getColumnError(myCol) + " is not String type");
732 }
733
734
735 final PrometheusStringColumn myStringCol = (PrometheusStringColumn) myCol;
736 return myStringCol.getValue();
737 }
738
739
740
741
742
743
744
745
746
747 public OceanusMoney getMoneyValue(final MetisDataFieldId pId,
748 final OceanusDataFormatter pFormatter) throws OceanusException {
749
750 final PrometheusColumnDefinition myCol = getColumnForId(pId);
751
752
753 if (!(myCol instanceof PrometheusMoneyColumn)) {
754 throw new PrometheusLogicException(getColumnError(myCol) + " is not money type");
755 }
756
757
758 final PrometheusMoneyColumn myMoneyCol = (PrometheusMoneyColumn) myCol;
759 return myMoneyCol.getValue(pFormatter);
760 }
761
762
763
764
765
766
767
768
769
770 public OceanusPrice getPriceValue(final MetisDataFieldId pId,
771 final OceanusDataFormatter pFormatter) throws OceanusException {
772
773 final PrometheusColumnDefinition myCol = getColumnForId(pId);
774
775
776 if (!(myCol instanceof PrometheusPriceColumn)) {
777 throw new PrometheusLogicException(getColumnError(myCol) + " is not Price type");
778 }
779
780
781 final PrometheusPriceColumn myPriceCol = (PrometheusPriceColumn) myCol;
782 return myPriceCol.getValue(pFormatter);
783 }
784
785
786
787
788
789
790
791
792
793 public OceanusRate getRateValue(final MetisDataFieldId pId,
794 final OceanusDataFormatter pFormatter) throws OceanusException {
795
796 final PrometheusColumnDefinition myCol = getColumnForId(pId);
797
798
799 if (!(myCol instanceof PrometheusRateColumn)) {
800 throw new PrometheusLogicException(getColumnError(myCol) + " is not Rate type");
801 }
802
803
804 final PrometheusRateColumn myRateCol = (PrometheusRateColumn) myCol;
805 return myRateCol.getValue(pFormatter);
806 }
807
808
809
810
811
812
813
814
815
816 public OceanusUnits getUnitsValue(final MetisDataFieldId pId,
817 final OceanusDataFormatter pFormatter) throws OceanusException {
818
819 final PrometheusColumnDefinition myCol = getColumnForId(pId);
820
821
822 if (!(myCol instanceof PrometheusUnitsColumn)) {
823 throw new PrometheusLogicException(getColumnError(myCol) + " is not Units type");
824 }
825
826
827 final PrometheusUnitsColumn myUnitsCol = (PrometheusUnitsColumn) myCol;
828 return myUnitsCol.getValue(pFormatter);
829 }
830
831
832
833
834
835
836
837
838
839 public OceanusRatio getRatioValue(final MetisDataFieldId pId,
840 final OceanusDataFormatter pFormatter) throws OceanusException {
841
842 final PrometheusColumnDefinition myCol = getColumnForId(pId);
843
844
845 if (!(myCol instanceof PrometheusRatioColumn)) {
846 throw new PrometheusLogicException(getColumnError(myCol) + " is not Ratio type");
847 }
848
849
850 final PrometheusRatioColumn myRatioCol = (PrometheusRatioColumn) myCol;
851 return myRatioCol.getValue(pFormatter);
852 }
853
854
855
856
857
858
859
860
861 public byte[] getBinaryValue(final MetisDataFieldId pId) throws OceanusException {
862
863 final PrometheusColumnDefinition myCol = getColumnForId(pId);
864
865
866 if (!(myCol instanceof PrometheusBinaryColumn)) {
867 throw new PrometheusLogicException(getColumnError(myCol) + " is not Binary type");
868 }
869
870
871 final PrometheusBinaryColumn myBinaryCol = (PrometheusBinaryColumn) myCol;
872 return myBinaryCol.getValue();
873 }
874
875
876
877
878
879
880
881
882 public void setIntegerValue(final MetisDataFieldId pId,
883 final Integer pValue) throws OceanusException {
884
885 final PrometheusColumnDefinition myCol = getColumnForId(pId);
886
887
888 if (!(myCol instanceof PrometheusIntegerColumn)) {
889 throw new PrometheusLogicException(getColumnError(myCol) + " is not Integer type");
890 }
891
892
893 final PrometheusIntegerColumn myIntCol = (PrometheusIntegerColumn) myCol;
894 myIntCol.setValue(pValue);
895 }
896
897
898
899
900
901
902
903
904 public void setLongValue(final MetisDataFieldId pId,
905 final Long pValue) throws OceanusException {
906
907 final PrometheusColumnDefinition myCol = getColumnForId(pId);
908
909
910 if (!(myCol instanceof PrometheusLongColumn)) {
911 throw new PrometheusLogicException(getColumnError(myCol) + " is not Long type");
912 }
913
914
915 final PrometheusLongColumn myLongCol = (PrometheusLongColumn) myCol;
916 myLongCol.setValue(pValue);
917 }
918
919
920
921
922
923
924
925
926 public void setBooleanValue(final MetisDataFieldId pId,
927 final Boolean pValue) throws OceanusException {
928
929 final PrometheusColumnDefinition myCol = getColumnForId(pId);
930
931
932 if (!(myCol instanceof PrometheusBooleanColumn)) {
933 throw new PrometheusLogicException(getColumnError(myCol) + " is not Boolean type");
934 }
935
936
937 final PrometheusBooleanColumn myBoolCol = (PrometheusBooleanColumn) myCol;
938 myBoolCol.setValue(pValue);
939 }
940
941
942
943
944
945
946
947
948 public void setDateValue(final MetisDataFieldId pId,
949 final OceanusDate pValue) throws OceanusException {
950
951 final PrometheusColumnDefinition myCol = getColumnForId(pId);
952
953
954 if (!(myCol instanceof PrometheusDateColumn)) {
955 throw new PrometheusLogicException(getColumnError(myCol) + " is not Date type");
956 }
957
958
959 final PrometheusDateColumn myDateCol = (PrometheusDateColumn) myCol;
960 myDateCol.setValue(pValue);
961 }
962
963
964
965
966
967
968
969
970 public void setStringValue(final MetisDataFieldId pId,
971 final String pValue) throws OceanusException {
972
973 final PrometheusColumnDefinition myCol = getColumnForId(pId);
974
975
976 if (!(myCol instanceof PrometheusStringColumn)) {
977 throw new PrometheusLogicException(getColumnError(myCol) + " is not String type");
978 }
979
980
981 final PrometheusStringColumn myStringCol = (PrometheusStringColumn) myCol;
982 myStringCol.setValue(pValue);
983 }
984
985
986
987
988
989
990
991
992 public void setBinaryValue(final MetisDataFieldId pId,
993 final byte[] pValue) throws OceanusException {
994
995 final PrometheusColumnDefinition myCol = getColumnForId(pId);
996
997
998 if (!(myCol instanceof PrometheusBinaryColumn)) {
999 throw new PrometheusLogicException(getColumnError(myCol) + " is not Binary type");
1000 }
1001
1002
1003 final PrometheusBinaryColumn myBinaryCol = (PrometheusBinaryColumn) myCol;
1004 myBinaryCol.setValue(pValue);
1005 }
1006
1007
1008
1009
1010
1011
1012
1013
1014 public void setMoneyValue(final MetisDataFieldId pId,
1015 final OceanusMoney pValue) throws OceanusException {
1016
1017 final PrometheusColumnDefinition myCol = getColumnForId(pId);
1018
1019
1020 if (!(myCol instanceof PrometheusMoneyColumn)) {
1021 throw new PrometheusLogicException(getColumnError(myCol) + " is not Money type");
1022 }
1023
1024
1025 final PrometheusMoneyColumn myMoneyCol = (PrometheusMoneyColumn) myCol;
1026 myMoneyCol.setValue(pValue);
1027 }
1028
1029
1030
1031
1032
1033
1034
1035
1036 public void setRateValue(final MetisDataFieldId pId,
1037 final OceanusRate pValue) throws OceanusException {
1038
1039 final PrometheusColumnDefinition myCol = getColumnForId(pId);
1040
1041
1042 if (!(myCol instanceof PrometheusRateColumn)) {
1043 throw new PrometheusLogicException(getColumnError(myCol) + " is not Rate type");
1044 }
1045
1046
1047 final PrometheusRateColumn myRateCol = (PrometheusRateColumn) myCol;
1048 myRateCol.setValue(pValue);
1049 }
1050
1051
1052
1053
1054
1055
1056
1057
1058 public void setRatioValue(final MetisDataFieldId pId,
1059 final OceanusRatio pValue) throws OceanusException {
1060
1061 final PrometheusColumnDefinition myCol = getColumnForId(pId);
1062
1063
1064 if (!(myCol instanceof PrometheusRatioColumn)) {
1065 throw new PrometheusLogicException(getColumnError(myCol) + " is not Ratio type");
1066 }
1067
1068
1069 final PrometheusRatioColumn myRatioCol = (PrometheusRatioColumn) myCol;
1070 myRatioCol.setValue(pValue);
1071 }
1072
1073
1074
1075
1076
1077
1078
1079
1080 private PrometheusColumnDefinition getColumnForId(final MetisDataFieldId pId) throws OceanusException {
1081
1082 final PrometheusColumnDefinition myDef = theMap.get(pId);
1083
1084
1085 if (myDef == null) {
1086 throw new PrometheusLogicException("Invalid Column Id: " + pId + " for " + theTableName);
1087 }
1088
1089
1090 return myDef;
1091 }
1092
1093
1094
1095
1096
1097
1098 protected String getCreateTableString() {
1099 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1100
1101
1102 myBuilder.append("create table ");
1103 addQuoteIfAllowed(myBuilder);
1104 myBuilder.append(theTableName);
1105 addQuoteIfAllowed(myBuilder);
1106 myBuilder.append(" (");
1107
1108
1109 final Iterator<PrometheusColumnDefinition> myIterator = theList.iterator();
1110 boolean myFirst = true;
1111
1112
1113 while (myIterator.hasNext()) {
1114 final PrometheusColumnDefinition myDef = myIterator.next();
1115 if (!myFirst) {
1116 myBuilder.append(", ");
1117 }
1118 myDef.buildCreateString(myBuilder);
1119 myFirst = false;
1120 }
1121
1122
1123 myBuilder.append(')');
1124 return myBuilder.toString();
1125 }
1126
1127
1128
1129
1130
1131
1132 protected String getCreateIndexString() {
1133 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1134
1135
1136 if (!isIndexed()) {
1137 return null;
1138 }
1139
1140
1141 myBuilder.append("create index ");
1142 addQuoteIfAllowed(myBuilder);
1143 myBuilder.append(PREFIX_INDEX);
1144 myBuilder.append(theTableName);
1145 addQuoteIfAllowed(myBuilder);
1146 myBuilder.append(" on ");
1147 addQuoteIfAllowed(myBuilder);
1148 myBuilder.append(theTableName);
1149 addQuoteIfAllowed(myBuilder);
1150 myBuilder.append(" (");
1151
1152
1153 final Iterator<PrometheusColumnDefinition> myIterator = theSortList.iterator();
1154 boolean myFirst = true;
1155
1156
1157 while (myIterator.hasNext()) {
1158 final PrometheusColumnDefinition myDef = myIterator.next();
1159 if (!myFirst) {
1160 myBuilder.append(", ");
1161 }
1162 addQuoteIfAllowed(myBuilder);
1163 myBuilder.append(myDef.getColumnName());
1164 addQuoteIfAllowed(myBuilder);
1165 if (myDef.getSortOrder() == PrometheusSortOrder.DESCENDING) {
1166 myBuilder.append(STR_DESC);
1167 }
1168 myFirst = false;
1169 }
1170
1171
1172 myBuilder.append(')');
1173 return myBuilder.toString();
1174 }
1175
1176
1177
1178
1179
1180
1181 protected String getDropTableString() {
1182 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1183 myBuilder.append("drop table if exists ");
1184 addQuoteIfAllowed(myBuilder);
1185 myBuilder.append(theTableName);
1186 addQuoteIfAllowed(myBuilder);
1187
1188
1189 return myBuilder.toString();
1190 }
1191
1192
1193
1194
1195
1196
1197 protected String getDropIndexString() {
1198
1199 if (!isIndexed() || !theDriver.explicitDropIndex()) {
1200 return null;
1201 }
1202
1203
1204 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1205 myBuilder.append("drop index if exists ");
1206 addQuoteIfAllowed(myBuilder);
1207 myBuilder.append(PREFIX_INDEX);
1208 myBuilder.append(theTableName);
1209 addQuoteIfAllowed(myBuilder);
1210 if (!PrometheusJDBCDriver.POSTGRESQL.equals(theDriver)) {
1211 myBuilder.append(" on ");
1212 addQuoteIfAllowed(myBuilder);
1213 myBuilder.append(theTableName);
1214 addQuoteIfAllowed(myBuilder);
1215 }
1216
1217
1218 return myBuilder.toString();
1219 }
1220
1221
1222
1223
1224
1225
1226 protected String getLoadString() {
1227 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1228
1229
1230 myBuilder.append("select ");
1231
1232
1233 final Iterator<PrometheusColumnDefinition> myIterator = theList.iterator();
1234 boolean myFirst = true;
1235
1236
1237 while (myIterator.hasNext()) {
1238 final PrometheusColumnDefinition myDef = myIterator.next();
1239 if (!myFirst) {
1240 myBuilder.append(", ");
1241 }
1242 if (sortOnReference) {
1243 myBuilder.append("a.");
1244 }
1245 addQuoteIfAllowed(myBuilder);
1246 myBuilder.append(myDef.getColumnName());
1247 addQuoteIfAllowed(myBuilder);
1248 myFirst = false;
1249 }
1250
1251
1252 myBuilder.append(" from ");
1253 addQuoteIfAllowed(myBuilder);
1254 myBuilder.append(theTableName);
1255 addQuoteIfAllowed(myBuilder);
1256 if (sortOnReference) {
1257 myBuilder.append(" a");
1258 }
1259
1260
1261 if (isIndexed()) {
1262
1263 if (sortOnReference) {
1264 myBuilder.append(getJoinString('a', 1));
1265 }
1266
1267
1268 myBuilder.append(" order by ");
1269
1270
1271 myBuilder.append(getOrderString('a', 0));
1272 }
1273
1274 return myBuilder.toString();
1275 }
1276
1277
1278
1279
1280
1281
1282
1283
1284 protected String getJoinString(final char pChar,
1285 final Integer pOffset) {
1286
1287 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1288 for (PrometheusColumnDefinition myDef : theSortList) {
1289
1290 if (!(myDef instanceof PrometheusReferenceColumn)) {
1291 continue;
1292 }
1293
1294
1295 final PrometheusReferenceColumn myCol = (PrometheusReferenceColumn) myDef;
1296 myCol.buildJoinString(myBuilder, pChar, pOffset);
1297 }
1298
1299 return myBuilder.toString();
1300 }
1301
1302
1303
1304
1305
1306
1307
1308
1309 protected String getOrderString(final char pChar,
1310 final Integer pOffset) {
1311 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1312
1313
1314 final Iterator<PrometheusColumnDefinition> myIterator = theSortList.iterator();
1315 boolean myFirst = true;
1316
1317
1318 while (myIterator.hasNext()) {
1319 final PrometheusColumnDefinition myDef = myIterator.next();
1320
1321 if (!myFirst) {
1322 myBuilder.append(", ");
1323 }
1324
1325
1326 if (sortOnReference || pChar > 'a') {
1327
1328 if (myDef instanceof PrometheusReferenceColumn myCol) {
1329
1330 myCol.buildOrderString(myBuilder, pOffset + 1);
1331 } else {
1332
1333 myBuilder.append(pChar);
1334 myBuilder.append(".");
1335 addQuoteIfAllowed(myBuilder);
1336 myBuilder.append(myDef.getColumnName());
1337 addQuoteIfAllowed(myBuilder);
1338 if (myDef.getSortOrder() == PrometheusSortOrder.DESCENDING) {
1339 myBuilder.append(STR_DESC);
1340 }
1341 }
1342 } else {
1343
1344 addQuoteIfAllowed(myBuilder);
1345 myBuilder.append(myDef.getColumnName());
1346 addQuoteIfAllowed(myBuilder);
1347 if (myDef.getSortOrder() == PrometheusSortOrder.DESCENDING) {
1348 myBuilder.append(STR_DESC);
1349 }
1350 }
1351
1352
1353 myFirst = false;
1354 }
1355
1356 return myBuilder.toString();
1357 }
1358
1359
1360
1361
1362
1363
1364 protected String getInsertString() {
1365 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1366 final StringBuilder myValues = new StringBuilder(BUFFER_LEN);
1367
1368
1369 myBuilder.append("insert into ");
1370 addQuoteIfAllowed(myBuilder);
1371 myBuilder.append(theTableName);
1372 addQuoteIfAllowed(myBuilder);
1373 myBuilder.append(" (");
1374
1375
1376 final Iterator<PrometheusColumnDefinition> myIterator = theList.iterator();
1377 boolean myFirst = true;
1378
1379
1380 while (myIterator.hasNext()) {
1381 final PrometheusColumnDefinition myDef = myIterator.next();
1382 if (!myFirst) {
1383 myBuilder.append(", ");
1384 myValues.append(", ");
1385 }
1386 addQuoteIfAllowed(myBuilder);
1387 myBuilder.append(myDef.getColumnName());
1388 addQuoteIfAllowed(myBuilder);
1389 myValues.append('?');
1390 myFirst = false;
1391 }
1392
1393
1394 myBuilder.append(") values(");
1395 myBuilder.append(myValues);
1396 myBuilder.append(')');
1397 return myBuilder.toString();
1398 }
1399
1400
1401
1402
1403
1404
1405
1406 protected String getUpdateString() throws OceanusException {
1407 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1408
1409
1410 myBuilder.append("update ");
1411 addQuoteIfAllowed(myBuilder);
1412 myBuilder.append(theTableName);
1413 addQuoteIfAllowed(myBuilder);
1414 myBuilder.append(" set ");
1415
1416
1417 final Iterator<PrometheusColumnDefinition> myIterator = theList.iterator();
1418 PrometheusColumnDefinition myId = null;
1419 boolean myFirst = true;
1420
1421
1422 while (myIterator.hasNext()) {
1423 final PrometheusColumnDefinition myDef = myIterator.next();
1424
1425
1426 if (myDef instanceof PrometheusIdColumn) {
1427
1428 if (!myDef.isValueSet()) {
1429 throw new PrometheusLogicException(getColumnError(myDef) + " has no value for update");
1430 }
1431
1432
1433 myId = myDef;
1434
1435
1436 } else if (myDef.isValueSet()) {
1437
1438 if (!myFirst) {
1439 myBuilder.append(", ");
1440 }
1441 addQuoteIfAllowed(myBuilder);
1442 myBuilder.append(myDef.getColumnName());
1443 addQuoteIfAllowed(myBuilder);
1444 myBuilder.append("=?");
1445 myFirst = false;
1446 }
1447 }
1448
1449
1450 if (myFirst || myId == null) {
1451 return null;
1452 }
1453
1454
1455 myBuilder.append(" where ");
1456 addQuoteIfAllowed(myBuilder);
1457 myBuilder.append(myId.getColumnName());
1458 addQuoteIfAllowed(myBuilder);
1459 myBuilder.append("=?");
1460 return myBuilder.toString();
1461 }
1462
1463
1464
1465
1466
1467
1468 protected String getDeleteString() {
1469 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1470
1471
1472 myBuilder.append("delete from ");
1473 addQuoteIfAllowed(myBuilder);
1474 myBuilder.append(theTableName);
1475 addQuoteIfAllowed(myBuilder);
1476 myBuilder.append(" where ");
1477
1478
1479 final PrometheusColumnDefinition myId = theList.get(0);
1480
1481
1482 addQuoteIfAllowed(myBuilder);
1483 myBuilder.append(myId.getColumnName());
1484 addQuoteIfAllowed(myBuilder);
1485 myBuilder.append("=?");
1486 return myBuilder.toString();
1487 }
1488
1489
1490
1491
1492
1493
1494 protected String getPurgeString() {
1495 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1496
1497
1498 myBuilder.append("delete from ");
1499 addQuoteIfAllowed(myBuilder);
1500 myBuilder.append(theTableName);
1501 addQuoteIfAllowed(myBuilder);
1502 return myBuilder.toString();
1503 }
1504
1505
1506
1507
1508
1509
1510 protected String getCountString() {
1511 final StringBuilder myBuilder = new StringBuilder(BUFFER_LEN);
1512
1513
1514 myBuilder.append("select count(*) from ");
1515 addQuoteIfAllowed(myBuilder);
1516 myBuilder.append(theTableName);
1517 addQuoteIfAllowed(myBuilder);
1518 return myBuilder.toString();
1519 }
1520
1521
1522
1523
1524
1525
1526 void addQuoteIfAllowed(final StringBuilder pBuilder) {
1527 if (theDriver.useQuotes()) {
1528 pBuilder.append(QUOTE_STRING);
1529 }
1530 }
1531
1532
1533
1534
1535 public enum PrometheusSortOrder {
1536
1537
1538
1539 ASCENDING,
1540
1541
1542
1543
1544 DESCENDING;
1545 }
1546 }