001package gudusoft.gsqlparser.nodes;
002
003
004import gudusoft.gsqlparser.*;
005import gudusoft.gsqlparser.nodes.flink.TFlinkWithClause;
006import gudusoft.gsqlparser.nodes.hive.*;
007import gudusoft.gsqlparser.nodes.oracle.TPhysicalProperties;
008import gudusoft.gsqlparser.nodes.postgresql.TPartitionBoundSpecSqlNode;
009import gudusoft.gsqlparser.nodes.teradata.TIndexDefinition;
010import gudusoft.gsqlparser.stmt.TCreateTableSqlStatement;
011import gudusoft.gsqlparser.nodes.postgresql.TInheritsClause;
012
013import java.util.ArrayList;
014import java.util.EnumSet;
015
016public class TCreateTableSqlNode extends TParseTreeNode {
017
018    private TDistributeBy distributeBy;
019
020    public void setDistributeBy(TDistributeBy distributeBy) {
021        this.distributeBy = distributeBy;
022    }
023
024    public TDistributeBy getDistributeBy() {
025        return distributeBy;
026    }
027
028    private TBaseTablePartition tablePartition;
029
030    public void setTablePartition(TBaseTablePartition tablePartition) {
031        this.tablePartition = tablePartition;
032    }
033
034    public TBaseTablePartition getTablePartition() {
035        return tablePartition;
036    }
037
038    public void setPartitionBoundSpec(TPartitionBoundSpecSqlNode partitionBoundSpec) {
039        this.partitionBoundSpec = partitionBoundSpec;
040    }
041
042    public TPartitionBoundSpecSqlNode getPartitionBoundSpec() {
043        return partitionBoundSpec;
044    }
045
046    /**
047     * postgresql PartitionBoundSpec
048     * https://www.postgresql.org/docs/current/sql-createtable.html
049     */
050    private TPartitionBoundSpecSqlNode partitionBoundSpec;
051
052    public TTableProperties getTableProperties() {
053        return tableProperties;
054    }
055
056    public void setTableProperties(TTableProperties tableProperties) {
057        this.tableProperties = tableProperties;
058    }
059
060    private TTableProperties tableProperties;
061
062    private ArrayList<TConstant> locationFiles;
063
064    public void setLocationFiles(ArrayList<TConstant> locationFiles) {
065        this.locationFiles = locationFiles;
066    }
067
068    public ArrayList<TConstant> getLocationFiles() {
069        return locationFiles;
070    }
071
072    public void setOptionStartParenthesis(TSourceToken optionStartParenthesis) {
073        this.optionStartParenthesis = optionStartParenthesis;
074    }
075
076    public void setOptionEndParenthesis(TSourceToken optionEndParenthesis) {
077        this.optionEndParenthesis = optionEndParenthesis;
078    }
079
080    public TSourceToken getOptionStartParenthesis() {
081        return optionStartParenthesis;
082    }
083
084    public TSourceToken getOptionEndParenthesis() {
085        return optionEndParenthesis;
086    }
087
088    private TSourceToken optionStartParenthesis;
089    private TSourceToken optionEndParenthesis;
090
091    private TCreateTableSqlStatement.TableSourceType tableSourceType = TCreateTableSqlStatement.TableSourceType.normal;
092
093    private TObjectName cloneSourceTable = null;
094
095    private TDummy netezzaExternalTableOption = null;
096
097    public void setCloneSourceTable(TObjectName cloneSourceTable) {
098        this.cloneSourceTable = cloneSourceTable;
099        this.tableSourceType = TCreateTableSqlStatement.TableSourceType.clone;
100    }
101
102    public TObjectName getCloneSourceTable() {
103        return cloneSourceTable;
104    }
105
106    public void setNetezzaExternalTableOption(TDummy netezzaExternalTableOption) {
107        this.netezzaExternalTableOption = netezzaExternalTableOption;
108    }
109
110    public TDummy getNetezzaExternalTableOption() {
111        return netezzaExternalTableOption;
112    }
113
114    public TObjectName getComment(){
115        return this.tableComment;
116    }
117    public void setTableOptions(ArrayList<TCreateTableOption> tableOptions, boolean enableAppend) {
118        if (tableOptions == null) return;
119        for(TCreateTableOption option:tableOptions){
120            if (option.getCreateTableOptionType() == ECreateTableOption.etoComment){
121                this.tableComment = option.getComment();
122            }
123        }
124
125        if ((enableAppend) && (this.tableOptions != null)){
126            this.tableOptions.addAll(tableOptions);
127        }else{
128            this.tableOptions = tableOptions;
129        }
130    }
131
132    public void setTableOptions(ArrayList<TCreateTableOption> tableOptions) {
133
134        this.setTableOptions(tableOptions,false);
135//        if (tableOptions == null) return;
136//        for(TCreateTableOption option:tableOptions){
137//            if (option.getCreateTableOptionType() == ECreateTableOption.etoComment){
138//                this.tableComment = option.getComment();
139//            }
140//        }
141//
142//        this.tableOptions = tableOptions;
143
144    }
145
146    public void appendTableOption(TCreateTableOption tableOption) {
147        if (tableOption == null) return;
148        if (tableOptions == null){
149            tableOptions = new ArrayList<>();
150        }
151        tableOptions.add(tableOption);
152    }
153
154    public void addTableOptionByToken(TSourceToken st, ECreateTableOption createTableOption){
155        if (st == null) return;
156
157        TCreateTableOption newOption = new TCreateTableOption();
158        newOption.init(createTableOption,st);
159        switch (createTableOption){
160            case etoFileFormat:
161                newOption.setFileFormatType(st);
162                break;
163            case etoEncoding:
164                newOption.setEncoding(st);
165                break;
166        }
167
168        if (tableOptions == null){
169            tableOptions = new ArrayList<>();
170        }
171
172        tableOptions.add(newOption);
173    }
174    public ArrayList<TCreateTableOption> getTableOptions() {
175        return tableOptions;
176    }
177
178    private ArrayList<TCreateTableOption> tableOptions;
179
180    public void setOnFilegroup(TDummy onFilegroup) {
181        this.onFilegroup = onFilegroup;
182    }
183
184    public TDummy getOnFilegroup() {
185        return onFilegroup;
186    }
187
188    private TDummy onFilegroup = null;
189
190    private TPhysicalProperties physicalProperties;
191
192    public void setPhysicalProperties(TPhysicalProperties physicalProperties) {
193        this.physicalProperties = physicalProperties;
194    }
195
196    public void setTableOption(TCreateTableOption option){
197        if (option == null) return;
198        if (this.tableOptions == null){
199            this.tableOptions = new ArrayList<>();
200        }
201        this.tableOptions.add(option);
202    }
203
204    /**
205     * Oracle physical properties
206     *
207     * @return Oracle physical properties
208     */
209    public TPhysicalProperties getPhysicalProperties() {
210        return physicalProperties;
211    }
212
213    public void setTableKindByToken(TSourceToken st){
214        if (st == null) return;
215        if (st.toString().equalsIgnoreCase("set")){
216            tableKinds.add(ETableKind.etkSet);
217        }else if (st.toString().equalsIgnoreCase("multiset")){
218            tableKinds.add(ETableKind.etkMultiset);
219        }else if (st.toString().equalsIgnoreCase("volatile")){
220            tableKinds.add(ETableKind.etkVolatile);
221        }else if (st.toString().equalsIgnoreCase("global")){
222            tableKinds.add(ETableKind.etkGlobalTemporary);
223        }else if (st.toString().equalsIgnoreCase("temporary")){
224            tableKinds.add(ETableKind.etkTemporary);
225        }else if (st.toString().equalsIgnoreCase("temp")){
226            tableKinds.add(ETableKind.etkTemp);
227        }else if (st.toString().equalsIgnoreCase("external")){
228            tableKinds.add(ETableKind.etkExternal);
229            this.external = true;
230        }
231    }
232    public void setTableKindByNode(TDummy node) {
233        if ((TDummy) node.node1 == null) return;
234        TDummy node2 = (TDummy) node.node1;
235        setTableKindByToken(node2.st1);
236        setTableKindByToken(node2.st2);
237    }
238
239    private EnumSet<ETableKind> tableKinds = EnumSet.noneOf(ETableKind.class);
240
241    public void setTableKinds(EnumSet<ETableKind> tableKinds) {
242        this.tableKinds = tableKinds;
243    }
244
245    public EnumSet<ETableKind> getTableKinds() {
246
247        return tableKinds;
248    }
249
250    //private TObjectName asTableName;
251    private TTable asTable = null;
252
253    public TTable getAsTable() {
254        return asTable;
255    }
256
257    public void setAsTableName(TObjectName asTableName) {
258        asTable = new TTable();
259        asTableName.setObjectType(TObjectName.ttobjTable);
260        asTable.setTableName(asTableName);
261        asTable.setTableType(ETableSource.objectname);
262        asTable.setEffectType(ETableEffectType.tetCreateAs);
263    }
264
265
266
267    public void setExternalTable(boolean externalTable) {
268        this.externalTable = externalTable;
269    }
270
271    /**
272     * @deprecated As of v1.9.7.2 , replace by {@link #isExternal}
273     *
274     * @return true if it's an external table
275     */
276    public boolean isExternalTable() {
277
278        return externalTable;
279    }
280
281    private boolean externalTable;
282
283    private ArrayList <TIndexDefinition> indexDefinitions;
284
285    public void setIndexDefinitions(ArrayList<TIndexDefinition> indexDefinitions) {
286        this.indexDefinitions = indexDefinitions;
287    }
288
289    public ArrayList<TIndexDefinition> getIndexDefinitions() {
290
291        return indexDefinitions;
292    }
293
294    private boolean readable = false;
295    private boolean writable = false;
296    private boolean webTable = false;
297
298    public void setWebTable(boolean webTable) {
299        this.webTable = webTable;
300    }
301
302    public boolean isWebTable() {
303        return webTable;
304    }
305
306    private TConstant executeCmd;
307
308    public void setExecuteCmd(TConstant executeCmd) {
309        this.executeCmd = executeCmd;
310    }
311
312    public TConstant getExecuteCmd() {
313        return executeCmd;
314    }
315
316    public void setReadWritableByToken(TSourceToken st){
317        if (st == null) return;
318        if (st.toString().equalsIgnoreCase("readable")){
319            readable = true;
320        }else if (st.toString().equalsIgnoreCase("writable")){
321            writable = true;
322        }
323    }
324
325    public boolean isReadable() {
326        return readable;
327    }
328
329    public boolean isWritable() {
330        return writable;
331    }
332
333    private boolean external;
334    private boolean ifNotExists;
335    private TObjectName tableLocation;
336    private TObjectName tableComment;
337    private THiveTableProperties hiveTableProperties;
338    private THiveTablePartition hiveTablePartition;
339    private THiveTableBuckets hiveTableBuckets;
340    private THiveTableSkewed hiveTableSkewed;
341    private THiveRowFormat hiveRowFormat;
342    private THiveTableFileFormat hiveTableFileFormat;
343    private TObjectName likeTableName;
344
345    public void setDummyOfTemp(TDummy dummy){
346        if (dummy == null) return;
347        if (dummy.st1.toString().equalsIgnoreCase("external")){
348            this.external = true;
349        }
350    }
351
352    public void setExternal(boolean external) {
353        this.external = external;
354    }
355
356    public void setHiveRowFormat(THiveRowFormat hiveRowFormat) {
357        this.hiveRowFormat = hiveRowFormat;
358    }
359
360    public void setHiveTableBuckets(THiveTableBuckets hiveTableBuckets) {
361        this.hiveTableBuckets = hiveTableBuckets;
362    }
363
364    public void setHiveTableFileFormat(THiveTableFileFormat hiveTableFileFormat) {
365        this.hiveTableFileFormat = hiveTableFileFormat;
366    }
367
368    public void setHiveTablePartition(THiveTablePartition hiveTablePartition) {
369        this.hiveTablePartition = hiveTablePartition;
370    }
371
372    public void setHiveTableProperties(THiveTableProperties hiveTableProperties) {
373        this.hiveTableProperties = hiveTableProperties;
374    }
375
376    public void setHiveTableSkewed(THiveTableSkewed hiveTableSkewed) {
377        this.hiveTableSkewed = hiveTableSkewed;
378    }
379
380    public void setIfNotExists(boolean ifNotExists) {
381        this.ifNotExists = ifNotExists;
382    }
383
384    public void setLikeTableName(TObjectName likeTableName) {
385        if ((likeTableName != null)&&(tableSourceType == TCreateTableSqlStatement.TableSourceType.normal)){
386           tableSourceType = TCreateTableSqlStatement.TableSourceType.like;
387        }
388        this.likeTableName = likeTableName;
389    }
390
391    public void setTableComment(TObjectName tableComment) {
392        this.tableComment = tableComment;
393    }
394
395    public void setTableLocation(TObjectName tableLocation) {
396        this.tableLocation = tableLocation;
397    }
398
399    public boolean isExternal() {
400
401        return external;
402    }
403
404    public THiveRowFormat getHiveRowFormat() {
405        return hiveRowFormat;
406    }
407
408    public THiveTableBuckets getHiveTableBuckets() {
409        return hiveTableBuckets;
410    }
411
412    public THiveTableFileFormat getHiveTableFileFormat() {
413        return hiveTableFileFormat;
414    }
415
416    public THiveTablePartition getHiveTablePartition() {
417        return hiveTablePartition;
418    }
419
420    public THiveTableProperties getHiveTableProperties() {
421        return hiveTableProperties;
422    }
423
424    public THiveTableSkewed getHiveTableSkewed() {
425        return hiveTableSkewed;
426    }
427
428    public boolean isIfNotExists() {
429        return ifNotExists;
430    }
431
432    public TObjectName getLikeTableName() {
433        return likeTableName;
434    }
435
436    public TObjectName getTableComment() {
437        return tableComment;
438    }
439
440    public TObjectName getTableLocation() {
441        return tableLocation;
442    }
443
444    private TObjectName rowTypeName;
445    private TObjectName superTableName;
446
447    public void setRowTypeName(TObjectName rowTypeName) {
448        this.rowTypeName = rowTypeName;
449    }
450
451    public void setSuperTableName(TObjectName superTableName) {
452        this.superTableName = superTableName;
453    }
454
455    public TObjectName getRowTypeName() {
456
457        return rowTypeName;
458    }
459
460    public TObjectName getSuperTableName() {
461        return superTableName;
462    }
463
464
465    private TPTNodeList <TMySQLCreateTableOption> mySQLTableOptionList;
466
467    public void setMySQLTableOptionList(TPTNodeList<TMySQLCreateTableOption> mySQLTableOptionList) {
468        this.mySQLTableOptionList = mySQLTableOptionList;
469    }
470
471    public TPTNodeList<TMySQLCreateTableOption> getMySQLTableOptionList() {
472
473        return mySQLTableOptionList;
474    }
475
476    private TTable table = null;
477
478    public void setTableElementList(TTableElementList tableElementList) {
479        this.tableElementList = tableElementList;
480    }
481
482    private TTableElementList tableElementList = null;
483
484    public TObjectNameList getColumnList() {
485        return columnList;
486    }
487
488    public TTableElementList getTableElementList() {
489        return tableElementList;
490    }
491
492    public TTable getTable() {
493
494        return table;
495    }
496
497    public void setColumnList(TObjectNameList columnList) {
498        this.columnList = columnList;
499    }
500
501    public TSelectSqlNode getSubQueryNode() {
502        return subQueryNode;
503    }
504
505    private TObjectNameList columnList = null;
506    private TSelectSqlNode subQueryNode = null;
507    private TStatementSqlNode refreshUsingStatementNode = null;
508    private boolean usingTemplate = false;
509
510    /**
511     * Snowflake custom incremental dynamic-table refresh DML.
512     *
513     * @return the MERGE or INSERT statement from REFRESH USING, or null
514     */
515    public TStatementSqlNode getRefreshUsingStatementNode() {
516        return refreshUsingStatementNode;
517    }
518
519    public void setRefreshUsingStatementNode(
520            TStatementSqlNode refreshUsingStatementNode) {
521        this.refreshUsingStatementNode = refreshUsingStatementNode;
522        if (refreshUsingStatementNode != null) {
523            tableSourceType = TCreateTableSqlStatement.TableSourceType.refreshUsing;
524        }
525    }
526
527    /**
528     * Snowflake: CREATE TABLE ... USING TEMPLATE &lt;query&gt;. When true, the
529     * subquery (see {@link #getSubQueryNode()}) is a schema-inference template
530     * whose result derives the table's column definitions, rather than a
531     * CREATE TABLE AS SELECT that also populates the table with data.
532     *
533     * @return true if the table is created via USING TEMPLATE
534     */
535    public boolean isUsingTemplate() {
536        return usingTemplate;
537    }
538
539    public void setUsingTemplate(boolean usingTemplate) {
540        this.usingTemplate = usingTemplate;
541        if (usingTemplate) {
542            // Distinguish a schema-inference template from a CTAS subquery so that
543            // downstream consumers (lineage, metadata) do not treat the template
544            // query as data that populates the target table. The query object is
545            // still exposed via getSubQueryNode() for resolving its own sources.
546            this.tableSourceType = TCreateTableSqlStatement.TableSourceType.usingTemplate;
547        }
548    }
549
550    public void setExecuteSqlNode(TExecuteSqlNode executeSqlNode) {
551        this.executeSqlNode = executeSqlNode;
552    }
553
554    public TExecuteSqlNode getExecuteSqlNode() {
555
556        return executeSqlNode;
557    }
558
559    private TExecuteSqlNode executeSqlNode = null;//greenplum
560
561    public void setSubQueryNode(TSelectSqlNode subQueryNode) {
562        if (subQueryNode != null){
563            this.subQueryNode = subQueryNode;
564            tableSourceType = TCreateTableSqlStatement.TableSourceType.subquery;
565        }
566
567    }
568
569    public void init(Object arg1)
570    {
571        table = new TTable();
572        ((TObjectName)arg1).setObjectType(TObjectName.ttobjTable);
573        table.setTableName((TObjectName)arg1);
574        table.setTableType(ETableSource.objectname);
575        table.setEffectType(ETableEffectType.tetCreate);
576        table.setGsqlparser(((TObjectName)arg1).getGsqlparser());
577        if (table.getTableName().toString().startsWith("##")){
578            // SQL Server global temporary table (##tablename)
579            this.getTableKinds().add(ETableKind.etkGlobalTemporary);
580            table.getTableName().setTableKind(ETableKind.etkGlobalTemporary);
581        } else if (table.getTableName().toString().startsWith("#")){
582            // SQL Server local temporary table (#tablename)
583            this.getTableKinds().add(ETableKind.etkTemporary);
584            table.getTableName().setTableKind(ETableKind.etkTemporary);
585        }
586    }
587
588    public TCreateTableSqlStatement.TableSourceType getTableSourceType() {
589        return tableSourceType;
590    }
591
592    public void init(Object arg1, Object arg2){
593        tableSourceType = (TCreateTableSqlStatement.TableSourceType)arg1;
594        init(arg2);
595    }
596
597    public void setInheritsClause(TInheritsClause inheritsClause) {
598        this.inheritsClause = inheritsClause;
599    }
600
601    public TInheritsClause getInheritsClause() {
602        return inheritsClause;
603    }
604
605    private TInheritsClause inheritsClause;
606
607    /**
608     * Flink WITH clause for connector properties
609     */
610    private TFlinkWithClause flinkWithClause;
611
612    /**
613     * Get the Flink WITH clause containing connector properties.
614     * @return the Flink WITH clause, or null if not present
615     */
616    public TFlinkWithClause getFlinkWithClause() {
617        return flinkWithClause;
618    }
619
620    /**
621     * Set the Flink WITH clause.
622     * @param flinkWithClause the Flink WITH clause
623     */
624    public void setFlinkWithClause(TFlinkWithClause flinkWithClause) {
625        this.flinkWithClause = flinkWithClause;
626    }
627}