/
TeiidDdlImporter.java
1469 lines (1303 loc) · 57 KB
/
TeiidDdlImporter.java
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
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.designer.ddl.importer.node;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.modeshape.sequencer.ddl.DdlConstants;
import org.modeshape.sequencer.ddl.StandardDdlLexicon;
import org.modeshape.sequencer.ddl.dialect.teiid.TeiidDdlConstants;
import org.modeshape.sequencer.ddl.dialect.teiid.TeiidDdlLexicon;
import org.modeshape.sequencer.ddl.node.AstNode;
import org.teiid.core.designer.ModelerCoreException;
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.core.designer.util.StringUtilities;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.types.DatatypeConstants;
import org.teiid.designer.core.util.NewModelObjectHelperManager;
import org.teiid.designer.ddl.DdlImporterManager;
import org.teiid.designer.ddl.importer.DdlImporterI18n;
import org.teiid.designer.ddl.importer.DdlImporterPlugin;
import org.teiid.designer.ddl.importer.TeiidDDLConstants;
import org.teiid.designer.extension.ExtensionPlugin;
import org.teiid.designer.extension.definition.ModelExtensionDefinition;
import org.teiid.designer.extension.registry.ModelExtensionRegistry;
import org.teiid.designer.metamodels.relational.extension.RestModelExtensionConstants;
import org.teiid.designer.relational.model.RelationalAccessPattern;
import org.teiid.designer.relational.model.RelationalColumn;
import org.teiid.designer.relational.model.RelationalForeignKey;
import org.teiid.designer.relational.model.RelationalIndex;
import org.teiid.designer.relational.model.RelationalModel;
import org.teiid.designer.relational.model.RelationalParameter;
import org.teiid.designer.relational.model.RelationalPrimaryKey;
import org.teiid.designer.relational.model.RelationalProcedure;
import org.teiid.designer.relational.model.RelationalProcedureResultSet;
import org.teiid.designer.relational.model.RelationalReference;
import org.teiid.designer.relational.model.RelationalSchema;
import org.teiid.designer.relational.model.RelationalTable;
import org.teiid.designer.relational.model.RelationalUniqueConstraint;
import org.teiid.designer.relational.model.RelationalViewProcedure;
import org.teiid.designer.relational.model.RelationalViewTable;
import org.teiid.designer.type.IDataTypeManagerService;
/**
* Teiid DDL node importer
*/
public class TeiidDdlImporter extends StandardImporter {
private static final String NS_TEIID_ODATA = "teiid_odata"; //$NON-NLS-1$
private static final String NS_TEIID_WEBSERVICE= "teiid_ws"; //$NON-NLS-1$
private static final String NS_TEIID_MONGO = "teiid_mongo"; //$NON-NLS-1$
private static final String NS_TEIID_SALESFORCE = "teiid_sf"; //$NON-NLS-1$
private static final String NS_TEIID_RELATIONAL = "teiid_rel"; //$NON-NLS-1$
private static final String NS_TEIID_ACCUMULO = "teiid_accumulo"; //$NON-NLS-1$
private static final String NS_TEIID_EXCEL = "teiid_excel"; //$NON-NLS-1$
private static final String NS_TEIID_JPA = "teiid_jpa"; //$NON-NLS-1$
private static final String NS_DESIGNER_ODATA = "odata"; //$NON-NLS-1$
private static final String NS_DESIGNER_WEBSERVICE= "ws"; //$NON-NLS-1$
private static final String NS_DESIGNER_MONGO = "mongodb"; //$NON-NLS-1$
private static final String NS_DESIGNER_SALESFORCE = "salesforce"; //$NON-NLS-1$
private static final String NS_DESIGNER_RELATIONAL = "relational"; //$NON-NLS-1$
private static final String NS_DESIGNER_ACCUMULO = "accumulo"; //$NON-NLS-1$
private static final String NS_DESIGNER_EXCEL = "excel"; //$NON-NLS-1$
private static final String NS_DESIGNER_JPA = "jpa2"; //$NON-NLS-1$
private static final String REST_COLON_PREFIX= "REST:"; //$NON-NLS-1$
private static final String REST_URI = "URI"; //$NON-NLS-1$
private static final String REST_METHOD = "METHOD"; //$NON-NLS-1$
private static final String REST_CHARSET = "CHARSET"; //$NON-NLS-1$
// Added to address TEIID-3629
private static final String SF_PROPNAME_CALCULATED_BAD = "calculated"; //$NON-NLS-1$
private static final String SF_PROPNAME_CALCULATED_GOOD = "Calculated"; //$NON-NLS-1$
private static final String XMLLITERAL_TYPE_NAME = DatatypeConstants.MetaMatrixExtendedBuiltInNames.XML_LITERAL;
interface TYPES_UPPER {
String ARRAY = "ARRAY"; //$NON-NLS-1$
String BIGDECIMAL = "BIGDECIMAL"; //$NON-NLS-1$
String BINARY = "BINARY"; //$NON-NLS-1$
String BIT = "BIT"; //$NON-NLS-1$
String BLOB = "BLOB"; //$NON-NLS-1$
String BYTE = "BYTE"; //$NON-NLS-1$
String CHAR = "CHAR"; //$NON-NLS-1$
String CLOB = "CLOB"; //$NON-NLS-1$
String DATE = "DATE"; //$NON-NLS-1$
String DATETIME = "DATETIME"; //$NON-NLS-1$
String DECIMAL = "DECIMAL"; //$NON-NLS-1$
String DOUBLE = "DOUBLE"; //$NON-NLS-1$
String FLOAT = "FLOAT"; //$NON-NLS-1$
String INT = "INT"; //$NON-NLS-1$
String INTEGER = "INTEGER"; //$NON-NLS-1$
String BIGINTEGER = "BIGINTEGER"; //$NON-NLS-1$
String LONGVARBINARY = "LONGVARBINARY"; //$NON-NLS-1$
String LONGVARCHAR = "LONGVARCHAR"; //$NON-NLS-1$
String NCHAR = "NCHAR"; //$NON-NLS-1$
String NUMERIC = "NUMERIC"; //$NON-NLS-1$
String OBJECT = "OBJECT"; //$NON-NLS-1$
String REAL = "REAL"; //$NON-NLS-1$
String REF = "REF"; //$NON-NLS-1$
String SHORT = "SHORT"; //$NON-NLS-1$
String STRING = "STRING"; //$NON-NLS-1$
String SMALLINT = "SMALLINT"; //$NON-NLS-1$
String TIMES = "TIME"; //$NON-NLS-1$
String TIMESTAMP = "TIMESTAMP"; //$NON-NLS-1$
String TINYINT = "TINYINT"; //$NON-NLS-1$
String VARBINARY = "VARBINARY"; //$NON-NLS-1$
String VARCHAR = "VARCHAR"; //$NON-NLS-1$
String IMAGE = "IMAGE"; //$NON-NLS-1$
String TEXT = "TEXT"; //$NON-NLS-1$
String XML = DatatypeConstants.RuntimeTypeNames.XML;
}
static int DEFAULT_NULL_VALUE_COUNT = -1;
/*
* return !(type == Types.LONGVARBINARY || type == Types.LONGVARCHAR || type == Types.VARBINARY || type == Types.VARCHAR
|| type == Types.ARRAY || type == Types.BLOB || type == Types.CLOB);
*/
private class TeiidInfo extends Info {
/**
* @param node the AstNode
* @param model the RelationalModel
*
* @throws Exception
*/
public TeiidInfo(AstNode node, RelationalModel model) throws Exception {
super(node, model);
}
}
@Override
protected TeiidInfo createInfo(AstNode node, RelationalModel model) throws Exception {
return new TeiidInfo(node, model);
}
@Override
protected String getTeiidDataTypeName(String datatype) throws Exception {
String resultTypeName = null;
String targetTypeName = datatype;
/*
* Get the Datatype for Teiid DDL.
* First tries to match the datatype string with a teiid built-in type.
* If a built-in type is not found, then attempt to use the relational mapping to find a match.
*
* Also an issue with Teiid's XML type.. this will map to XMLLiteral name
*/
if( datatype.equalsIgnoreCase(TYPES_UPPER.XML) ) {
targetTypeName = XMLLITERAL_TYPE_NAME;
}
// Look up matching Built-In type
EObject[] builtInTypes = ModelerCore.getWorkspaceDatatypeManager().getAllDatatypes();
String dtName = null;
for (int i = 0; i < builtInTypes.length; i++) {
dtName = ModelerCore.getWorkspaceDatatypeManager().getName(builtInTypes[i]);
if (dtName != null && dtName.equalsIgnoreCase(targetTypeName)) {
resultTypeName = dtName;
break;
}
}
// Built In type not found, try mapping from native to built-in
if(resultTypeName == null) {
resultTypeName = super.getTeiidDataTypeName(datatype);
}
return resultTypeName;
}
/**
* Creates constraints for Table for Teiid DDL
* @param constraintNode the AstNode for the constraint
* @param table the RelationalTable object
* @param model the RelationalModel
* @param allRefs the collection of all RelationalReference objects in the model
*
* @throws CoreException
*/
private void createConstraint(AstNode constraintNode, RelationalTable table, RelationalModel model, Collection<RelationalReference> allRefs) throws CoreException {
String type = constraintNode.getProperty(TeiidDdlLexicon.Constraint.TYPE).toString();
RelationalReference key = null;
if (DdlConstants.PRIMARY_KEY.equals(type)) {
createPrimaryKey(constraintNode, table, allRefs);
key = table.getPrimaryKey();
} else if (DdlConstants.INDEX.equals(type)) {
// TODO need to process teiidddl:expression property
key = createIndex(constraintNode, table, allRefs);
} else if (DdlConstants.UNIQUE.equals(type)) {
key = createUniqueConstraint(constraintNode, table, allRefs);
} else if (TeiidDdlConstants.TeiidNonReservedWord.ACCESSPATTERN.toDdl().equals(type)) {
key = createAccessPattern(constraintNode, table, allRefs);
} else if (DdlConstants.FOREIGN_KEY.equals(type)) {
key = createForeignKey(constraintNode, table, allRefs);
} else {
assert false : "Unexpected constraint type of '" + type + "'"; //$NON-NLS-1$ //$NON-NLS-2$
}
// Find all the Option properties
List<AstNode> optionNodes = new ArrayList<AstNode>();
List<AstNode> children = constraintNode.getChildren();
for(AstNode child: children) {
if(is(child, StandardDdlLexicon.TYPE_STATEMENT_OPTION)) {
optionNodes.add(child);
}
}
// process the Column Options
if( ! optionNodes.isEmpty() ) {
processOptions(optionNodes,key);
}
}
private boolean columnsMatch(List<AstNode> columns_1, Collection<RelationalColumn> columns_2) {
if( columns_1 == null && columns_2 == null ) return true;
if( columns_1 == null || columns_2 == null ) return false;
// Neither can be NULL
if( columns_1.size() != columns_2.size() ) return false;
// Size is the same
for( AstNode colOuter_1 : columns_1) {
boolean foundIt = false;
String columnName = colOuter_1.getName();
for( RelationalColumn colInner_2 : columns_2) {
if( columnName.equals(colInner_2.getName()) ) {
foundIt = true;
}
}
if( !foundIt ) return false;
}
return true;
}
@Override
protected void createPrimaryKey(AstNode node, RelationalTable table, Collection<RelationalReference> allRefs) throws CoreException {
RelationalPrimaryKey key = getFactory().createPrimaryKey();
table.setPrimaryKey(key);
initialize(key, node);
// process referenced columns multi-valued property
List<AstNode> references = (List<AstNode>)node.getProperty(TeiidDdlLexicon.Constraint.REFERENCES);
for (AstNode ref : references) {
try {
RelationalColumn column = find(RelationalColumn.class, ref, table, allRefs);
key.getColumns().add(column);
} catch (EntityNotFoundException error) {
addProgressMessage(error.getMessage());
}
}
}
private RelationalReference createUniqueConstraint(AstNode constraintNode, RelationalTable table, Collection<RelationalReference> allRefs) throws CoreException {
RelationalUniqueConstraint constraint = getFactory().createUniqueConstraint();
initialize(constraint, constraintNode);
table.addUniqueConstraint(constraint);
// process referenced columns multi-valued property
List<AstNode> references = (List<AstNode>)constraintNode.getProperty(TeiidDdlLexicon.Constraint.REFERENCES);
for (AstNode ref : references) {
try {
RelationalColumn col = find(RelationalColumn.class, ref, table, allRefs);
if(col!=null) {
constraint.getColumns().add(col);
}
} catch (EntityNotFoundException error) {
addProgressMessage(error.getMessage());
}
}
return constraint;
}
private RelationalReference createAccessPattern(AstNode constraintNode, RelationalTable table, Collection<RelationalReference> allRefs) throws CoreException {
RelationalAccessPattern constraint = getFactory().createAccessPattern();
initialize(constraint, constraintNode);
table.addAccessPattern(constraint);
// process referenced columns multi-valued property
List<AstNode> references = (List<AstNode>)constraintNode.getProperty(TeiidDdlLexicon.Constraint.REFERENCES);
for (AstNode ref : references) {
try {
RelationalColumn col = find(RelationalColumn.class, ref, table, allRefs);
if(col!=null) {
constraint.getColumns().add(col);
}
} catch (EntityNotFoundException error) {
addProgressMessage(error.getMessage());
}
}
return constraint;
}
private RelationalReference createIndex(AstNode constraintNode, RelationalTable table, Collection<RelationalReference> allRefs) throws CoreException {
RelationalIndex constraint = getFactory().createIndex();
initialize(constraint, constraintNode);
table.addIndex(constraint);
// process referenced columns multi-valued property
List<AstNode> references = (List<AstNode>)constraintNode.getProperty(TeiidDdlLexicon.Constraint.REFERENCES);
for (AstNode ref : references) {
try {
RelationalColumn col = find(RelationalColumn.class, ref, table, allRefs);
if(col!=null) {
constraint.getColumns().add(col);
}
} catch (EntityNotFoundException error) {
addProgressMessage(error.getMessage());
}
}
return constraint;
}
private RelationalReference createForeignKey(AstNode constraintNode, RelationalTable table, Collection<RelationalReference> allRefs) throws CoreException {
RelationalForeignKey foreignKey = getFactory().createForeignKey();
initializeFK(table.getForeignKeys(), foreignKey, constraintNode);
table.addForeignKey(foreignKey);
// process referenced columns multi-valued property
List<AstNode> references = (List<AstNode>)constraintNode.getProperty(TeiidDdlLexicon.Constraint.REFERENCES);
for (AstNode ref : references) {
try {
RelationalColumn col = find(RelationalColumn.class, ref, table, allRefs);
if(col!=null) {
foreignKey.getColumns().add(col);
}
} catch (EntityNotFoundException error) {
addProgressMessage(error.getMessage());
}
}
AstNode tableRefNode = (AstNode)constraintNode.getProperty(TeiidDdlLexicon.Constraint.TABLE_REFERENCE);
if(tableRefNode==null) {
addProgressMessage(DdlImporterI18n.FK_TABLE_REF_NOT_FOUND_MSG + " '"+ foreignKey.getName() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
return foreignKey;
}
try {
RelationalTable tableRef = find(RelationalTable.class, tableRefNode, null, allRefs);
// Note that a REFERENCE table can have either a PK reference or a Unique Constraint reference.
// So get the RelationalReference object for each
RelationalPrimaryKey tableRefPrimaryKey = tableRef.getPrimaryKey();
RelationalUniqueConstraint tableRefUC = null;
if( ! tableRef.getUniqueConstraints().isEmpty() ) {
tableRefUC = tableRef.getUniqueConstraints().iterator().next();
}
// check to see if foreign table columns are referenced
Object tempRefColumns = constraintNode.getProperty(TeiidDdlLexicon.Constraint.TABLE_REFERENCE_REFERENCES);
// Create list of AstNode's
List<AstNode> foreignTableColumnNodes = (tempRefColumns==null) ? Collections.<AstNode>emptyList() : (List<AstNode>)tempRefColumns;
// Get number of columns that are referenced
int numFKTableReferenceColumns = foreignTableColumnNodes.size();
if( numFKTableReferenceColumns == 0 ) {
if(tableRefPrimaryKey!=null) {
foreignKey.setUniqueKeyName(tableRefPrimaryKey.getName());
foreignKey.setUniqueKeyTableName(tableRef.getName());
} else if( tableRefUC != null ) {
foreignKey.setUniqueKeyName(tableRefUC.getName());
foreignKey.setUniqueKeyTableName(tableRef.getName());
}
// That's all we can do, there are no references
return foreignKey;
}
int numPKColumns = 0;
if( tableRefPrimaryKey != null ) {
numPKColumns = tableRefPrimaryKey.getColumns().size();
}
int numUCColumns = 0;
if( tableRefUC != null ) {
numUCColumns = tableRefUC.getColumns().size();
}
boolean constraintWasPK = false;
// there are referenced columns in FK.
// Check if #PK columns matches
if( numFKTableReferenceColumns == numPKColumns ) {
// Check PK to see if the columns are found and match
// Assumes UC and PK can't reference the same keys
// Need to compare the foreignTableColumnNodes (AstNode's) against the PK column names
boolean columnsMatch = columnsMatch(foreignTableColumnNodes, tableRefPrimaryKey.getColumns());
if( columnsMatch ) {
foreignKey.setUniqueKeyName(tableRefPrimaryKey.getName());
foreignKey.setUniqueKeyTableName(tableRef.getName());
constraintWasPK = true;
}
}
if( ! constraintWasPK ) {
if( numUCColumns == numFKTableReferenceColumns ) {
// Check UC to see if the columns are found and match
// Assumes UC and PK can't reference the same keys
boolean columnsMatch = columnsMatch(foreignTableColumnNodes, tableRefUC.getColumns());
if( columnsMatch ) {
foreignKey.setUniqueKeyName(tableRefUC.getName());
foreignKey.setUniqueKeyTableName(tableRef.getName());
}
} else {
foreignKey.setUniqueKeyName(tableRefPrimaryKey.getName());
foreignKey.setUniqueKeyTableName(tableRef.getName());
}
}
} catch (EntityNotFoundException error) {
addProgressMessage(error.getMessage());
}
return foreignKey;
}
@Override
protected RelationalColumn createColumn(AstNode node, RelationalTable table) throws Exception {
RelationalColumn column = super.createColumn(node, table);
// StandardImporter sets the native type to datatype. Need to unset this because Teiid defines it in the OPTIONS() clause
// or it's null
column.setNativeType(RelationalColumn.DEFAULT_NATIVE_TYPE);
// Handle Teiid-specific properties and options
Object prop = node.getProperty(TeiidDdlLexicon.CreateTable.AUTO_INCREMENT);
if(prop != null)
column.setAutoIncremented(((Boolean)prop).booleanValue());
// Find all the Option properties
List<AstNode> optionNodes = new ArrayList<AstNode>();
List<AstNode> children = node.getChildren();
for(AstNode child: children) {
if(is(child, StandardDdlLexicon.TYPE_STATEMENT_OPTION)) {
optionNodes.add(child);
}
}
// process the Column Options
processOptions(optionNodes,column);
return column;
}
/**
* Create Column from the provided AstNode within ColumnSet
* @param node the provided AstNode
* @param column
*
* @throws Exception
*/
@Override
protected void setDataType(AstNode node, RelationalColumn column) throws Exception {
String datatype = node.getProperty(StandardDdlLexicon.DATATYPE_NAME).toString();
String teiidType = getTeiidDataTypeName(datatype);
if( teiidType.toUpperCase().equals(TYPES_UPPER.INTEGER)) {
column.setDatatype(TYPES_UPPER.INT.toLowerCase());
} else {
column.setDatatype(teiidType);
}
// Datatype length
Object prop = node.getProperty(StandardDdlLexicon.DATATYPE_LENGTH);
if (prop != null) {
column.setLength(Integer.parseInt(prop.toString()));
} else {
// Length is not provided for type 'string', use the default length specified in preferences...
//String dtName = ModelerCore.getWorkspaceDatatypeManager().getName(type);
if(teiidType != null) {
if( teiidType.equalsIgnoreCase(STRING_TYPENAME)) {
column.setLength(ModelerCore.getTransformationPreferences().getDefaultStringLength());
} else if( teiidType.equalsIgnoreCase(CHAR_TYPENAME) ) {
column.setLength(1);
}
}
}
prop = node.getProperty(StandardDdlLexicon.DATATYPE_PRECISION);
if (prop != null) {
column.setPrecision(Integer.parseInt(prop.toString()));
} else {
// IF type == FLOAT, BIG_DECIMAL, DECIMAL, then set precision to at least 1
if( teiidType.equalsIgnoreCase(IDataTypeManagerService.DataTypeName.BIGDECIMAL.name()) ||
teiidType.equalsIgnoreCase(IDataTypeManagerService.DataTypeName.DECIMAL.name()) ||
teiidType.equalsIgnoreCase(IDataTypeManagerService.DataTypeName.FLOAT.name()) ||
teiidType.equalsIgnoreCase(IDataTypeManagerService.DataTypeName.DOUBLE.name())) {
column.setPrecision(DEFAULT_PRECISION);
}
}
prop = node.getProperty(StandardDdlLexicon.DATATYPE_SCALE);
if (prop != null)
column.setScale(Integer.parseInt(prop.toString()));
prop = node.getProperty(StandardDdlLexicon.NULLABLE);
if (prop != null)
column.setNullable(getRelRefNullable(prop.toString()));
prop = node.getProperty(StandardDdlLexicon.DEFAULT_VALUE);
if (prop != null)
column.setDefaultValue(prop.toString());
}
@Override
protected RelationalProcedure createProcedure(AstNode procedureNode, RelationalModel model) throws Exception {
RelationalProcedure procedure = super.createProcedure(procedureNode, model);
List<AstNode> procOptionNodes = new ArrayList<AstNode>();
for (AstNode child : procedureNode) {
if (is(child, TeiidDdlLexicon.CreateProcedure.PARAMETER)) {
createProcedureParameter(child, procedure);
} else if(is(child, TeiidDdlLexicon.CreateProcedure.RESULT_COLUMNS)) {
RelationalProcedureResultSet result = getFactory().createProcedureResultSet();
procedure.setResultSet(result);
initialize(result, procedureNode);
for(AstNode resultCol: child) {
if(resultCol.hasMixin(TeiidDdlLexicon.CreateProcedure.RESULT_COLUMN)) {
createColumn(resultCol,result);
}
}
} else if(is(child, TeiidDdlLexicon.CreateProcedure.RESULT_DATA_TYPE)) {
RelationalProcedureResultSet result = getFactory().createProcedureResultSet();
procedure.setResultSet(result);
initialize(result, procedureNode);
createColumn(child,result);
} else if(is(child, StandardDdlLexicon.TYPE_STATEMENT_OPTION)) {
procOptionNodes.add(child);
}
}
// process the Procedure Options
processOptions(procOptionNodes,procedure);
return procedure;
}
protected RelationalProcedure createVirtualProcedure(AstNode procedureNode, RelationalModel model) throws Exception {
RelationalViewProcedure procedure = getFactory().createViewProcedure();
Info info = createInfo(procedureNode, model);
if (info.getSchema() == null)
model.addChild(procedure);
else {
info.getSchema().getProcedures().add(procedure);
procedure.setParent(info.getSchema());
}
initialize(procedure, procedureNode, info.getName());
// TODO: determine how to handle Procedure StatementOption
// TODO: determine how to handle Procedure Statement
if (procedureNode.getProperty(StandardDdlLexicon.DATATYPE_NAME) != null) {
RelationalProcedureResultSet result = getFactory().createProcedureResultSet();
procedure.setResultSet(result);
initialize(result, procedureNode);
}
List<AstNode> procOptionNodes = new ArrayList<AstNode>();
for (AstNode child : procedureNode) {
if (is(child, TeiidDdlLexicon.CreateProcedure.PARAMETER)) {
createProcedureParameter(child, procedure);
} else if(is(child, TeiidDdlLexicon.CreateProcedure.RESULT_COLUMNS)) {
RelationalProcedureResultSet result = getFactory().createProcedureResultSet();
procedure.setResultSet(result);
initialize(result, procedureNode);
for(AstNode resultCol: child) {
if(resultCol.hasMixin(TeiidDdlLexicon.CreateProcedure.RESULT_COLUMN)) {
createColumn(resultCol,result);
}
}
} else if(is(child, TeiidDdlLexicon.CreateProcedure.RESULT_DATA_TYPE)) {
RelationalProcedureResultSet result = getFactory().createProcedureResultSet();
procedure.setResultSet(result);
initialize(result, procedureNode);
createColumn(child,result);
} else if(is(child, StandardDdlLexicon.TYPE_STATEMENT_OPTION)) {
procOptionNodes.add(child);
}
}
try {
NewModelObjectHelperManager.helpCreate(procedure, new Properties());
} catch (ModelerCoreException err) {
DdlImporterPlugin.UTIL.log(IStatus.ERROR, err, err.getMessage());
}
String queryExpression = (String)procedureNode.getProperty(TeiidDdlLexicon.CreateProcedure.STATEMENT);
if( ! StringUtilities.isEmpty(queryExpression) ) {
procedure.setTransformationSQL(queryExpression);
}
// process the Procedure Options
processOptions(procOptionNodes,procedure);
return procedure;
}
@Override
protected RelationalParameter createProcedureParameter(AstNode node, RelationalProcedure procedure) throws Exception {
RelationalParameter prm = super.createProcedureParameter(node, procedure);
// Handle Teiid-specific properties and options
Object prop = node.getProperty(TeiidDdlLexicon.CreateProcedure.PARAMETER_TYPE);
if(prop != null) {
String direction = prop.toString();
prm.setDirection(direction);
}
// Find all the Option properties
List<AstNode> optionNodes = new ArrayList<AstNode>();
List<AstNode> children = node.getChildren();
for(AstNode child: children) {
if(is(child, StandardDdlLexicon.TYPE_STATEMENT_OPTION)) {
optionNodes.add(child);
}
}
processOptions(optionNodes,prm);
return prm;
}
/**
* Perform the import
* @param rootNode the rootNode of the DDL
* @param importManager the import manager which maintains import options
* @return the RelationalModel created
* @throws Exception
*/
@Override
public RelationalModel importNode(AstNode rootNode, DdlImporterManager importManager, Properties props) throws Exception {
setImporterManager(importManager);
getImporterManager().optToGenerateDefaultSQL(false);
getImporterManager().optToHelpCreateTransform(false);
// Get table updatable override property value from props.
// (1) null = dont change anything. (2) true = set all updatable to true (3) false = set all updatable to false
String updatableOverride = (String)props.get(TeiidDDLConstants.DDL_IMPORT_TABLE_UPDATABLE_OVERRIDE);
getImporterManager().setTableUpdatableOverride(updatableOverride);
// Create a RelationalModel for the imported DDL
RelationalModel model = getFactory().createModel("ddlImportedModel"); //$NON-NLS-1$
// Map for holding deferred nodes, which much be created later
Map<AstNode,RelationalReference> deferredCreateMap = new HashMap<AstNode,RelationalReference>();
// Create objects from the DDL. (populated map of deferred nodes)
for (AstNode node : rootNode) {
if (is(node, StandardDdlLexicon.TYPE_CREATE_SCHEMA_STATEMENT)) {
RelationalSchema schema = getFactory().createSchema();
model.addChild(schema);
initialize(schema, node);
for (AstNode node1 : node) {
Map<AstNode,RelationalReference> deferredMap = createObject(node1, model, schema);
if(!deferredMap.isEmpty()) {
deferredCreateMap.putAll(deferredMap);
}
}
} else if (is(node, TeiidDdlLexicon.OptionNamespace.STATEMENT)) {
// No Objects created for Teiid Option namespace
} else {
Map<AstNode,RelationalReference> deferredMap = createObject(node, model, null);
if(!deferredMap.isEmpty()) {
deferredCreateMap.putAll(deferredMap);
}
}
}
// Now process all the 'deferred' nodes. These are nodes which reference other nodes (which are required to exist first)
createDeferredObjects(deferredCreateMap,model);
String doFilterStr = (String)props.get(TeiidDDLConstants.DDL_IMPORT_FILTER_CONSTRAINTS);
if( doFilterStr != null ) {
boolean doIt = Boolean.parseBoolean(doFilterStr);
if( doIt ) removeRedundantConstraints(model);
}
return model;
}
/**
* Create RelationalReference objects
* @param node the provided AstNode
* @param model the RelationalModel being created
* @param schema the schema
* @return the map of AstNodes which need to be deferred
* @throws Exception
*/
@Override
protected Map<AstNode,RelationalReference> createObject(AstNode node, RelationalModel model, RelationalSchema schema) throws Exception {
Map<AstNode,RelationalReference> deferredMap = new HashMap<AstNode,RelationalReference>();
// -----------------------------------------------------------------------
// Handle Creation of Teiid Entities
// -----------------------------------------------------------------------
if (is(node, TeiidDdlLexicon.CreateTable.TABLE_STATEMENT) ) {
RelationalTable baseTable = getFactory().createBaseTable();
initializeTable(baseTable, node, model);
List<AstNode> optionNodes = new ArrayList<AstNode>();
for (AstNode child : node) {
// Table Elements
if (is(child, TeiidDdlLexicon.CreateTable.TABLE_ELEMENT)) {
createColumn(child, baseTable);
// Statement Options
} else if (is(child, StandardDdlLexicon.TYPE_STATEMENT_OPTION)) {
optionNodes.add(child);
// Contraints
} else if (is(child, TeiidDdlLexicon.Constraint.TABLE_ELEMENT)
|| is(child, TeiidDdlLexicon.Constraint.FOREIGN_KEY_CONSTRAINT)
|| is(child, TeiidDdlLexicon.Constraint.INDEX_CONSTRAINT)) {
deferredMap.put(child, baseTable);
}
}
// processes all options for this table
if(!optionNodes.isEmpty()) {
processOptions(optionNodes,baseTable);
}
} else if (is(node, TeiidDdlLexicon.CreateTable.VIEW_STATEMENT)) {
RelationalViewTable viewTable = getFactory().createViewTable();
initializeTable(viewTable, node, model);
List<AstNode> optionNodes = new ArrayList<AstNode>();
for (AstNode child : node) {
// Table Elements
if (is(child, TeiidDdlLexicon.CreateTable.TABLE_ELEMENT)) {
createColumn(child, viewTable);
// Statement Options
} else if (is(child, StandardDdlLexicon.TYPE_STATEMENT_OPTION)) {
optionNodes.add(child);
// Contraints
} else if (is(child, TeiidDdlLexicon.Constraint.TABLE_ELEMENT)
|| is(child, TeiidDdlLexicon.Constraint.FOREIGN_KEY_CONSTRAINT)
|| is(child, TeiidDdlLexicon.Constraint.INDEX_CONSTRAINT)) {
deferredMap.put(child, viewTable);
}
}
// processes all options for this table
if(!optionNodes.isEmpty()) {
processOptions(optionNodes,viewTable);
}
String queryExpression = (String)node.getProperty(TeiidDdlLexicon.CreateTable.QUERY_EXPRESSION);
if( ! StringUtilities.isEmpty(queryExpression) ) {
viewTable.setTransformationSQL(queryExpression);
}
} else if (is(node, TeiidDdlLexicon.CreateProcedure.PROCEDURE_STATEMENT)
|| is(node, TeiidDdlLexicon.CreateProcedure.FUNCTION_STATEMENT)) {
String modelType = (String)node.getProperty(TeiidDdlLexicon.SchemaElement.TYPE);
if( modelType != null ) {
if( modelType.equalsIgnoreCase("VIRTUAL")) {
createVirtualProcedure(node, model);
} else {
createProcedure(node, model);
}
}
// Handle Alter Table
} else if (is(node, TeiidDdlLexicon.AlterOptions.TABLE_STATEMENT)) {
deferredMap.put(node, null);
} else if (is(node, TeiidDdlLexicon.AlterOptions.VIEW_STATEMENT)
|| is(node, TeiidDdlLexicon.AlterOptions.PROCEDURE_STATEMENT)) {
} else {
// -----------------------------------------------------------------------
// All other Non-Teiid DDL
// -----------------------------------------------------------------------
return super.createObject(node, model, schema);
}
return deferredMap;
}
/**
* Create deferred objects using the supplied map
* @param deferredNodes the map of deferred AstNodes
* @param model the RelationalModel being created
* @throws Exception
*/
@Override
protected void createDeferredObjects(Map<AstNode,RelationalReference> deferredNodes, RelationalModel model) throws Exception {
Collection<RelationalReference> allRefs = model.getAllReferences();
// Make first pass to create the PKs
Set<AstNode> astNodes = deferredNodes.keySet();
for(AstNode node:astNodes) {
if (is(node, TeiidDdlLexicon.Constraint.TABLE_ELEMENT)) {
RelationalTable table = (RelationalTable)deferredNodes.get(node);
createConstraint(node, table, model, allRefs);
}
}
// Second pass create FKs, options, others
for(AstNode node:astNodes) {
if (is(node, TeiidDdlLexicon.Constraint.FOREIGN_KEY_CONSTRAINT)
|| is(node, TeiidDdlLexicon.Constraint.INDEX_CONSTRAINT)) {
RelationalTable table = (RelationalTable)deferredNodes.get(node);
createConstraint(node, table, model, allRefs);
} else if (is(node, TeiidDdlLexicon.AlterOptions.TABLE_STATEMENT)) {
//FIXME: find different way
RelationalTable table = find(RelationalTable.class, node, null, allRefs);
List<AstNode> optionNodes = new ArrayList<AstNode>();
if (table != null) {
for (AstNode child : node) {
if (is(child, TeiidDdlLexicon.AlterOptions.OPTIONS_LIST)) {
List<AstNode> nodeList = child.getChildren();
for (AstNode listItem : nodeList) {
if (listItem.hasMixin(StandardDdlLexicon.TYPE_STATEMENT_OPTION)) {
optionNodes.add(listItem);
}
}
}
}
}
// processes all options for this table
if(!optionNodes.isEmpty()) {
processOptions(optionNodes,table);
}
// Handle Alter View and Procedure
// TODO: could potentially be combined with alter table block above
}
}
}
/**
* Process the Statement Option AstNodes for the supplied relational entity.
* @param optionNodes the list of AstNodes
* @param relationalReference the RelationalReference
*/
private void processOptions(List<AstNode> optionNodes, RelationalReference relationalReference) {
// process the standard teiid options. Recognized Options are removed from the list as they are processed.
processTeiidStandardOptions(optionNodes,relationalReference);
// Add the remaining Options as extension properties.
processTeiidExtensionOptions(optionNodes,relationalReference);
}
/**
* Process the options that are specific to the provided entity type
* @param optionNodes the list of AstNode
* @param relationalReference the RelationalReference
*/
private void processTeiidStandardOptions(List<AstNode> optionNodes, RelationalReference relationalReference) {
// process Options common to all Entities
processTeiidCommonOptions(optionNodes,relationalReference);
// process Options specific to entity type
if(relationalReference instanceof RelationalTable) {
processTeiidTableOptions(optionNodes,(RelationalTable)relationalReference);
} else if(relationalReference instanceof RelationalColumn) {
processTeiidColumnOptions(optionNodes,(RelationalColumn)relationalReference);
} else if(relationalReference instanceof RelationalProcedure) {
processTeiidProcedureOptions(optionNodes,(RelationalProcedure)relationalReference);
}
}
/**
* Handles statementOption common to all relational entities for Teiid DDL
* @param optionNodes the list of statementOption AstNodes
* @param entity the RelationalEntity
*/
private void processTeiidCommonOptions(List<AstNode> optionNodes, RelationalReference entity) {
Iterator<AstNode> nodeIter = optionNodes.iterator();
while(nodeIter.hasNext()) {
AstNode optionNode = nodeIter.next();
String optionName = optionNode.getName();
Object optionValue = optionNode.getProperty(StandardDdlLexicon.VALUE);
if(!CoreStringUtil.isEmpty(optionName)) {
String optionValueStr = (String)optionValue;
if(!CoreStringUtil.isEmpty(optionValueStr)) {
if(optionName.equalsIgnoreCase(TeiidDDLConstants.ANNOTATION)) {
entity.setDescription(optionValueStr);
nodeIter.remove();
} else if(optionName.equalsIgnoreCase(TeiidDDLConstants.UUID)) {
// entity.setUUID();
nodeIter.remove();
} else if(optionName.equalsIgnoreCase(TeiidDDLConstants.NAMEINSOURCE)) {
entity.setNameInSource(optionValueStr);
nodeIter.remove();
}
}
}
}
return;
}
/**
* Handle the OPTION keys that may be set on Tables for Teiid DDL
* @param optionNodes
* @param table
*/
private void processTeiidTableOptions(List<AstNode> optionNodes, RelationalTable table) {
Iterator<AstNode> nodeIter = optionNodes.iterator();
while(nodeIter.hasNext()) {
AstNode optionNode = nodeIter.next();
String optionName = optionNode.getName();
Object optionValue = optionNode.getProperty(StandardDdlLexicon.VALUE);
if(!CoreStringUtil.isEmpty(optionName)) {
String optionValueStr = (String)optionValue;
if(!CoreStringUtil.isEmpty(optionValueStr)) {
if(optionName.equalsIgnoreCase(TeiidDDLConstants.CARDINALITY)) {
table.setCardinality(Integer.parseInt(optionValueStr));
nodeIter.remove();
} else if(optionName.equalsIgnoreCase(TeiidDDLConstants.MATERIALIZED)) {
table.setMaterialized(isTrue(optionValueStr));
nodeIter.remove();
} else if(optionName.equalsIgnoreCase(TeiidDDLConstants.MATERIALIZED_TABLE)) {
//Table mattable = new Table();
//mattable.setName(value);
//table.setMaterializedTable(mattable);
nodeIter.remove();
} else if(optionName.equalsIgnoreCase(TeiidDDLConstants.UPDATABLE)) {
table.setSupportsUpdate(isTrue(optionValueStr));
nodeIter.remove();
}
}
}
}
// Updatable Override sets supportsUpdate to specified value, regardless of whether OPTION is present
if(hasTableUpdatableOverride()) {
table.setSupportsUpdate(getTableUpdatableOverride());
}
}
/*
* Determine if override of the table updatable property has been requested
*/
private boolean hasTableUpdatableOverride() {
return (getImporterManager().getTableUpdatableOverride()!=null);
}
/*
* Get the table updatable value (true or false) - if it has been specified.
*/
private boolean getTableUpdatableOverride() {
return Boolean.parseBoolean(getImporterManager().getTableUpdatableOverride());
}
/**
* Handle the OPTION keys that may be set on Procedures for Teiid DDL
* @param optionNodes the list of optionNodes for a Procedure
* @param procedure the procedure
*/
private void processTeiidProcedureOptions(List<AstNode> optionNodes, RelationalProcedure procedure) {
Iterator<AstNode> nodeIter = optionNodes.iterator();
while(nodeIter.hasNext()) {
AstNode optionNode = nodeIter.next();
String optionName = optionNode.getName();
Object optionValue = optionNode.getProperty(StandardDdlLexicon.VALUE);
if(!CoreStringUtil.isEmpty(optionName)) {
String optionValueStr = (String)optionValue;
// If any function properties are present, the setFuntion boolean is also set
if(!CoreStringUtil.isEmpty(optionValueStr)) {
if(optionName.equalsIgnoreCase(TeiidDDLConstants.UPDATECOUNT)) {
procedure.setUpdateCount(optionValueStr);
nodeIter.remove();