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