001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.teradata.TDataDefinition;
005
006import java.util.ArrayList;
007
008
009/**
010 * Define the characteristics of the column.
011 *
012 */
013public class TColumnDefinition extends TParseTreeNode implements Cloneable {
014
015    private TColumnGeneratedClause columnGeneratedClause;
016
017    public void setColumnGeneratedClause(TColumnGeneratedClause columnGeneratedClause) {
018        this.columnGeneratedClause = columnGeneratedClause;
019    }
020
021    public TColumnGeneratedClause getColumnGeneratedClause() {
022        return columnGeneratedClause;
023    }
024
025    public TColumnDefinition clone(){
026        TColumnDefinition columnDefinitionClone = new TColumnDefinition();
027        if (this.columnName != null){
028            columnDefinitionClone.columnName = this.columnName.clone();
029        }
030        if (this.getDatatype() != null){
031            columnDefinitionClone.datatype = this.datatype.clone();
032        }
033
034        return columnDefinitionClone;
035    }
036    private ArrayList<TDataDefinition> dataDefinitions;
037
038    public void setDataDefinitions(ArrayList<TDataDefinition> dataDefinitions) {
039        this.dataDefinitions = dataDefinitions;
040    }
041
042    public ArrayList<TDataDefinition> getDataDefinitions() {
043        return dataDefinitions;
044    }
045
046    public void setAsJson(boolean asJson) {
047        this.asJson = asJson;
048    }
049
050    public boolean isAsJson() {
051        return asJson;
052    }
053
054    private boolean asJson = false;
055    private String columnPath; // sql server openjson column_path
056
057    public void setColumnPath(TSourceToken columnPath) {
058        this.columnPath = columnPath.toString();
059    }
060
061    public String getColumnPath() {
062        return columnPath;
063    }
064
065    private TIdentityClause identityClause;
066
067    public void setIdentityClause(TIdentityClause identityClause) {
068        this.identityClause = identityClause;
069    }
070
071    public TIdentityClause getIdentityClause() {
072        return identityClause;
073    }
074
075    private TSourceToken xmlTableColumnPath;
076
077    public void setXmlTableColumnPath(TSourceToken xmlTableColumnPath) {
078        this.xmlTableColumnPath = xmlTableColumnPath;
079    }
080
081    /**
082     * Oracle, path of xml table column
083     * @return Oracle, path of xml table column
084     */
085    public TSourceToken getXmlTableColumnPath() {
086
087        return xmlTableColumnPath;
088    }
089
090    private  TColumnAttributes columnAttributes;
091
092    public TColumnAttributes getColumnAttributes() {
093        return columnAttributes;
094    }
095
096    public void setColumnAttributes(TColumnAttributes pcolumnAttributes) {
097
098        columnAttributes = pcolumnAttributes;
099//        if (columnAttributes != null){
100//            if (columnAttributes.getDefaultValue() != null){
101//                defaultExpression = columnAttributes.getDefaultValue();
102//            }
103//            if (columnAttributes.getIdentitySeed() != null){
104//                this.seed = columnAttributes.getIdentitySeed();
105//            }
106//            if (columnAttributes.getIdentityStep() != null){
107//                this.increment = columnAttributes.getIdentityStep();
108//            }
109//            if (columnAttributes.getEncoding() != null){
110//                this.encoding = columnAttributes.getEncoding();
111//            }
112//        }
113    }
114
115    private TObjectName comment;
116
117    public TObjectName getComment() {
118        return comment;
119    }
120
121    public void setComment(TObjectName comment) {
122
123        this.comment = comment;
124    }
125
126    public void setCommentByCreateTableOption(TCreateTableOption comment) {
127       if (comment == null) return;
128       this.comment = comment.getComment();
129    }
130
131
132    private TObjectName columnName;
133
134    private TObjectNameList columnReferences = null;
135
136    private TObjectNameList getColumnReferences() {
137        if (columnReferences == null){
138            columnReferences = new TObjectNameList();
139        }
140
141        return columnReferences;
142    }
143
144    public TColumnDefinition(){
145        
146    }
147
148    public TColumnDefinition(TObjectName columnName){
149        this.columnName = columnName;
150    }
151
152    public void init(Object arg1)
153    {
154        this.columnName = (TObjectName)arg1;
155        this.columnName.setObjectType(TObjectName.ttobjColumn);
156    }
157
158    public void init(Object arg1,Object arg2)
159    {
160        this.columnName = (TObjectName)arg1;
161        this.columnName.setObjectType(TObjectName.ttobjColumn);
162        if (arg2 != null){
163            datatype = (TTypeName )arg2;
164        }
165    }
166
167    public void init(Object arg1,Object arg2,Object arg3)
168    {
169        this.columnName = (TObjectName)arg1;
170        this.columnName.setObjectType(TObjectName.ttobjColumn);
171        if (arg2 != null){
172            datatype = (TTypeName )arg2;
173        }
174        this.constraints = (TConstraintList)arg3;
175    }
176
177    /**
178     *
179     * @return the name of a column of the table.
180     */
181    public TObjectName getColumnName() {
182        return columnName;
183    }
184
185    public void setColumnName(TObjectName columnName) {
186        this.columnName = columnName;
187    }
188
189    public void setColumnReferences(TObjectNameList columnReferences) {
190        this.columnReferences = columnReferences;
191    }
192
193    public void setDatatype(TTypeName datatype) {
194        this.datatype = datatype;
195    }
196
197    public void setConstraints(TConstraintList constraints) {
198        this.constraints = constraints;
199    }
200
201    public void setRowGuidCol(boolean isRowGuidCol) {
202        this.isRowGuidCol = isRowGuidCol;
203    }
204
205    public void setSeed(TExpression seed) {
206        this.seed = seed;
207    }
208
209    public void setIncrement(TExpression increment) {
210        this.increment = increment;
211    }
212
213    public void setIdentity(boolean isIdentity) {
214        this.isIdentity = isIdentity;
215    }
216
217    /**
218     * When create table by using a subquery, then you can omit column and datatype unless you are creating an index-organized table.
219     * <p>If you specify AS subquery when creating an index-organized table, then you must specify column, and you must omit datatype.
220     * <p>So datetype can be null.
221
222     * @return the datatype of a column.
223     */
224
225    public TTypeName getDatatype() {
226        return datatype;
227    }
228
229    private TTypeName datatype = null;
230
231    private TConstraintList constraints = null;
232
233    public void addNewConstraint(TConstraint pConstraint){
234        if (constraints == null){
235            constraints = new TConstraintList();
236        }
237        constraints.addConstraint(pConstraint);
238    }
239    /**
240     *
241     * @return constraint list of this column if any.
242     */
243    public TConstraintList getConstraints() {
244        return constraints;
245    }
246
247    private TExpression defaultExpression = null;
248
249    public void setDefaultExpression(TExpression defaultExpression) {
250        this.defaultExpression = defaultExpression;
251    }
252
253    /**
254     * The DEFAULT clause lets you specify a value to be assigned to the column if a
255     * subsequent INSERT statement omits a value for the column.
256     * 
257     * @return   The DEFAULT expression can include any SQL function as long as the function does
258     * not return a literal argument, a column reference, or a nested function invocation.
259     */
260    
261    public TExpression getDefaultExpression() {
262        return defaultExpression;
263    }
264
265    private boolean isNull = false;
266
267    public void setNull(boolean aNull) {
268        isNull = aNull;
269    }
270
271    public boolean isNull() {
272
273        return isNull;
274    }
275
276    /**
277     *
278     * @return SQL Server ROWGUIDCOL
279     */
280    public boolean isRowGuidCol() {
281        return isRowGuidCol;
282    }
283
284    private boolean isRowGuidCol = false;
285
286    public void accept(TParseTreeVisitor v){
287        v.preVisit(this);
288        v.postVisit(this);
289    }
290
291    public void acceptChildren(TParseTreeVisitor v){
292        v.preVisit(this);
293
294        if (this.getColumnName() != null){
295            this.getColumnName().acceptChildren(v);
296        }
297        if (this.getDatatype() != null){
298            this.getDatatype().acceptChildren(v);
299        }
300        if (this.getIdentityClause() != null){
301            this.getIdentityClause().acceptChildren(v);
302        }
303        if (constraints != null){
304            for (int i=0;i<constraints.size();i++){
305                constraints.getConstraint(i).acceptChildren(v);
306            }
307        }
308        v.postVisit(this);
309    }
310
311    private  String encoding = null;
312
313    public void setEncoding(String encoding) {
314        this.encoding = encoding;
315    }
316
317    public String getEncoding() {
318
319        return encoding;
320    }
321
322    private String collationName = null;
323
324    public void setCollationName(String collationName) {
325        this.collationName = collationName;
326    }
327
328    public String getCollationName() {
329
330        if (collationName == null){
331            if ((this.getDatatype() != null) && (this.getDatatype().getCollationName() != null)){
332                collationName = this.getDatatype().getCollationName();
333            }
334        }
335        return collationName;
336    }
337
338    private TExpression seed;
339
340    public TExpression getIncrement() {
341        return increment;
342    }
343
344    public TExpression getSeed() {
345        return seed;
346    }
347
348    private TExpression increment;
349
350    private boolean isIdentity = false;
351
352    /**
353     *
354     * @return is identity column of sql server.
355     */
356    public boolean isIdentity() {
357        return isIdentity;
358    }
359
360    private TExpression computedColumnExpression = null;
361
362    public void setComputedColumnExpression(TExpression computedColumnExpression) {
363        this.computedColumnExpression = computedColumnExpression;
364    }
365
366    /**
367     *
368     * @return computed_column_expression of sql server.
369     */
370    public TExpression getComputedColumnExpression() {
371
372        return computedColumnExpression;
373    }
374
375    public void doParse(TCustomSqlStatement psql, ESqlClause plocation){
376
377        // teradata data definition
378        if (dataDefinitions != null){
379            for(int i=0;i<dataDefinitions.size();i++){
380                TDataDefinition dataDefinition = dataDefinitions.get(i);
381                switch (dataDefinition.getDataDefinitionType()){
382                    case columnConstraint:
383                        this.addNewConstraint(dataDefinition.getColumnConstraint());
384                        break;
385                    case dataAttribute:
386                        break;
387                    case columnStorage:
388                        break;
389                }
390            }
391        }
392
393        // get fake constraint from constraints, and then remove that from constraints.
394        if (this.constraints == null) {return;}
395        for(int i = this.constraints.size() - 1; i>=0; i--){
396            this.constraints.getConstraint(i).setConstraintLevel(TBaseType.constraint_level_column);
397            switch (this.constraints.getConstraint(i).getConstraint_type()){
398                case fake_collate:
399                    this.collationName = this.constraints.getConstraint(i).getEndToken().toString();
400                    this.constraints.removeElementWithoutSyncTokens(i);
401                    break;
402                case fake_null:
403                    this.setNull(true);
404                    this.constraints.removeElementWithoutSyncTokens(i);
405                    break;
406                case fake_rowguidcol:
407                    this.isRowGuidCol = true;
408                    this.constraints.removeElementWithoutSyncTokens(i);
409                    break;
410                case fake_identity:
411                    this.isIdentity = true;
412                    this.seed = this.constraints.getConstraint(i).getSeed();
413                    this.increment = this.constraints.getConstraint(i).getIncrement();
414                    this.constraints.removeElementWithoutSyncTokens(i);
415                    break;
416                case fake_comment:
417                    this.comment = this.constraints.getConstraint(i).getCommentObject();
418                    this.constraints.removeElementWithoutSyncTokens(i);
419                    break;
420                case fake_maked_with:
421                    this.constraints.removeElementWithoutSyncTokens(i);
422                    break;
423                case fake_fuzzy_search_index:
424                    this.constraints.removeElementWithoutSyncTokens(i);
425                    break;
426                case fake_load_unit:
427                    this.constraints.removeElementWithoutSyncTokens(i);
428                    break;
429                case fake_column_generated_as_expr:
430                    this.columnGeneratedClause = this.constraints.getConstraint(i).getColumnGeneratedClause();
431                    this.constraints.removeElementWithoutSyncTokens(i);
432                    break;
433                case fake_column_generated_as_identity:
434                    this.columnGeneratedClause = this.constraints.getConstraint(i).getColumnGeneratedClause();
435                    this.constraints.removeElementWithoutSyncTokens(i);
436                    break;
437                case fake_db2:
438                    this.defaultExpression = this.constraints.getConstraint(i).getDefaultExpression();
439                    this.constraints.removeElementWithoutSyncTokens(i);
440                    break;
441                case fake_default_expr:
442                    this.defaultExpression = this.constraints.getConstraint(i).getDefaultExpression();
443                    this.constraints.removeElementWithoutSyncTokens(i);
444                    break;
445                case fake_compress_off:
446                    this.constraints.removeElementWithoutSyncTokens(i);
447                    break;
448                default:
449                    break;
450            }
451        }
452
453
454
455        for (int i=0;i<constraints.size();i++){
456            constraints.getConstraint(i).doParse(psql,plocation);
457        }
458
459    }
460
461    private boolean isNestedTableColumn = false; // Oracle json_table, nested table column
462    private String nestedTableColumnPath = null; // Oracle json_table, nested table column path
463    private TColumnDefinitionList nestedTableColumns = null; // Oracle json_table, nested table columns
464
465    public void setNestedTableColumn(boolean isNestedTableColumn) {
466        this.isNestedTableColumn = isNestedTableColumn;
467    }
468
469    public void setColumnPath(String columnPath) {
470        this.columnPath = columnPath;
471    }
472
473    public void setNestedTableColumnPath(TConstant nestedTableColumnPath) {
474        this.nestedTableColumnPath = nestedTableColumnPath.toString();
475    }
476
477    public void setNestedTableColumns(TColumnDefinitionList nestedTableColumns) {
478        this.nestedTableColumns = nestedTableColumns;
479    }
480
481    public boolean isNestedTableColumn() {
482        return isNestedTableColumn;
483    }
484
485    public String getNestedTableColumnPath() {
486        return nestedTableColumnPath;
487    }
488
489    public TColumnDefinitionList getNestedTableColumns() {
490        return nestedTableColumns;
491    }
492}