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        if (this.columnName != null) {
162            this.columnName.setObjectType(TObjectName.ttobjColumn);
163        }
164        if (arg2 != null){
165            datatype = (TTypeName )arg2;
166        }
167    }
168
169    public void init(Object arg1,Object arg2,Object arg3)
170    {
171        this.columnName = (TObjectName)arg1;
172        if (this.columnName != null) {
173            this.columnName.setObjectType(TObjectName.ttobjColumn);
174        }
175        if (arg2 != null){
176            datatype = (TTypeName )arg2;
177        }
178        this.constraints = (TConstraintList)arg3;
179    }
180
181    /**
182     *
183     * @return the name of a column of the table.
184     */
185    public TObjectName getColumnName() {
186        return columnName;
187    }
188
189    public void setColumnName(TObjectName columnName) {
190        this.columnName = columnName;
191    }
192
193    public void setColumnReferences(TObjectNameList columnReferences) {
194        this.columnReferences = columnReferences;
195    }
196
197    public void setDatatype(TTypeName datatype) {
198        this.datatype = datatype;
199    }
200
201    public void setConstraints(TConstraintList constraints) {
202        this.constraints = constraints;
203    }
204
205    public void setRowGuidCol(boolean isRowGuidCol) {
206        this.isRowGuidCol = isRowGuidCol;
207    }
208
209    public void setSeed(TExpression seed) {
210        this.seed = seed;
211    }
212
213    public void setIncrement(TExpression increment) {
214        this.increment = increment;
215    }
216
217    public void setIdentity(boolean isIdentity) {
218        this.isIdentity = isIdentity;
219    }
220
221    /**
222     * When create table by using a subquery, then you can omit column and datatype unless you are creating an index-organized table.
223     * <p>If you specify AS subquery when creating an index-organized table, then you must specify column, and you must omit datatype.
224     * <p>So datetype can be null.
225
226     * @return the datatype of a column.
227     */
228
229    public TTypeName getDatatype() {
230        return datatype;
231    }
232
233    private TTypeName datatype = null;
234
235    private TConstraintList constraints = null;
236
237    public void addNewConstraint(TConstraint pConstraint){
238        if (constraints == null){
239            constraints = new TConstraintList();
240        }
241        constraints.addConstraint(pConstraint);
242    }
243    /**
244     *
245     * @return constraint list of this column if any.
246     */
247    public TConstraintList getConstraints() {
248        return constraints;
249    }
250
251    private TExpression defaultExpression = null;
252
253    public void setDefaultExpression(TExpression defaultExpression) {
254        this.defaultExpression = defaultExpression;
255    }
256
257    /**
258     * The DEFAULT clause lets you specify a value to be assigned to the column if a
259     * subsequent INSERT statement omits a value for the column.
260     * 
261     * @return   The DEFAULT expression can include any SQL function as long as the function does
262     * not return a literal argument, a column reference, or a nested function invocation.
263     */
264    
265    public TExpression getDefaultExpression() {
266        return defaultExpression;
267    }
268
269    private boolean isNull = false;
270
271    public void setNull(boolean aNull) {
272        isNull = aNull;
273    }
274
275    public boolean isNull() {
276
277        return isNull;
278    }
279
280    /**
281     *
282     * @return SQL Server ROWGUIDCOL
283     */
284    public boolean isRowGuidCol() {
285        return isRowGuidCol;
286    }
287
288    private boolean isRowGuidCol = false;
289
290    public void accept(TParseTreeVisitor v){
291        v.preVisit(this);
292        v.postVisit(this);
293    }
294
295    public void acceptChildren(TParseTreeVisitor v){
296        v.preVisit(this);
297
298        if (this.getColumnName() != null){
299            this.getColumnName().acceptChildren(v);
300        }
301        if (this.getDatatype() != null){
302            this.getDatatype().acceptChildren(v);
303        }
304        if (this.getIdentityClause() != null){
305            this.getIdentityClause().acceptChildren(v);
306        }
307        if (constraints != null){
308            for (int i=0;i<constraints.size();i++){
309                constraints.getConstraint(i).acceptChildren(v);
310            }
311        }
312        v.postVisit(this);
313    }
314
315    private  String encoding = null;
316
317    public void setEncoding(String encoding) {
318        this.encoding = encoding;
319    }
320
321    public String getEncoding() {
322
323        return encoding;
324    }
325
326    private String collationName = null;
327
328    public void setCollationName(String collationName) {
329        this.collationName = collationName;
330    }
331
332    public String getCollationName() {
333
334        if (collationName == null){
335            if ((this.getDatatype() != null) && (this.getDatatype().getCollationName() != null)){
336                collationName = this.getDatatype().getCollationName();
337            }
338        }
339        return collationName;
340    }
341
342    private TExpression seed;
343
344    public TExpression getIncrement() {
345        return increment;
346    }
347
348    public TExpression getSeed() {
349        return seed;
350    }
351
352    private TExpression increment;
353
354    private boolean isIdentity = false;
355
356    /**
357     *
358     * @return is identity column of sql server.
359     */
360    public boolean isIdentity() {
361        return isIdentity;
362    }
363
364    private TExpression computedColumnExpression = null;
365
366    public void setComputedColumnExpression(TExpression computedColumnExpression) {
367        this.computedColumnExpression = computedColumnExpression;
368    }
369
370    /**
371     *
372     * @return computed_column_expression of sql server.
373     */
374    public TExpression getComputedColumnExpression() {
375
376        return computedColumnExpression;
377    }
378
379    public void doParse(TCustomSqlStatement psql, ESqlClause plocation){
380
381        // teradata data definition
382        if (dataDefinitions != null){
383            for(int i=0;i<dataDefinitions.size();i++){
384                TDataDefinition dataDefinition = dataDefinitions.get(i);
385                switch (dataDefinition.getDataDefinitionType()){
386                    case columnConstraint:
387                        this.addNewConstraint(dataDefinition.getColumnConstraint());
388                        break;
389                    case dataAttribute:
390                        break;
391                    case columnStorage:
392                        break;
393                }
394            }
395        }
396
397        // get fake constraint from constraints, and then remove that from constraints.
398        if (this.constraints == null) {return;}
399        for(int i = this.constraints.size() - 1; i>=0; i--){
400            this.constraints.getConstraint(i).setConstraintLevel(TBaseType.constraint_level_column);
401            switch (this.constraints.getConstraint(i).getConstraint_type()){
402                case fake_collate:
403                    this.collationName = this.constraints.getConstraint(i).getEndToken().toString();
404                    this.constraints.removeElementWithoutSyncTokens(i);
405                    break;
406                case fake_null:
407                    this.setNull(true);
408                    this.constraints.removeElementWithoutSyncTokens(i);
409                    break;
410                case fake_rowguidcol:
411                    this.isRowGuidCol = true;
412                    this.constraints.removeElementWithoutSyncTokens(i);
413                    break;
414                case fake_identity:
415                    this.isIdentity = true;
416                    this.seed = this.constraints.getConstraint(i).getSeed();
417                    this.increment = this.constraints.getConstraint(i).getIncrement();
418                    this.constraints.removeElementWithoutSyncTokens(i);
419                    break;
420                case fake_comment:
421                    this.comment = this.constraints.getConstraint(i).getCommentObject();
422                    this.constraints.removeElementWithoutSyncTokens(i);
423                    break;
424                case fake_maked_with:
425                    this.constraints.removeElementWithoutSyncTokens(i);
426                    break;
427                case fake_fuzzy_search_index:
428                    this.constraints.removeElementWithoutSyncTokens(i);
429                    break;
430                case fake_load_unit:
431                    this.constraints.removeElementWithoutSyncTokens(i);
432                    break;
433                case fake_column_generated_as_expr:
434                    this.columnGeneratedClause = this.constraints.getConstraint(i).getColumnGeneratedClause();
435                    this.constraints.removeElementWithoutSyncTokens(i);
436                    break;
437                case fake_column_generated_as_identity:
438                    this.columnGeneratedClause = this.constraints.getConstraint(i).getColumnGeneratedClause();
439                    this.constraints.removeElementWithoutSyncTokens(i);
440                    break;
441                case fake_db2:
442                    this.defaultExpression = this.constraints.getConstraint(i).getDefaultExpression();
443                    this.constraints.removeElementWithoutSyncTokens(i);
444                    break;
445                case fake_default_expr:
446                    this.defaultExpression = this.constraints.getConstraint(i).getDefaultExpression();
447                    this.constraints.removeElementWithoutSyncTokens(i);
448                    break;
449                case fake_compress_off:
450                    this.constraints.removeElementWithoutSyncTokens(i);
451                    break;
452                default:
453                    break;
454            }
455        }
456
457
458
459        for (int i=0;i<constraints.size();i++){
460            constraints.getConstraint(i).doParse(psql,plocation);
461        }
462
463    }
464
465    private boolean isNestedTableColumn = false; // Oracle json_table, nested table column
466    private String nestedTableColumnPath = null; // Oracle json_table, nested table column path
467    private TColumnDefinitionList nestedTableColumns = null; // Oracle json_table, nested table columns
468
469    public void setNestedTableColumn(boolean isNestedTableColumn) {
470        this.isNestedTableColumn = isNestedTableColumn;
471    }
472
473    public void setColumnPath(String columnPath) {
474        this.columnPath = columnPath;
475    }
476
477    public void setNestedTableColumnPath(TConstant nestedTableColumnPath) {
478        this.nestedTableColumnPath = nestedTableColumnPath.toString();
479    }
480
481    public void setNestedTableColumns(TColumnDefinitionList nestedTableColumns) {
482        this.nestedTableColumns = nestedTableColumns;
483    }
484
485    public boolean isNestedTableColumn() {
486        return isNestedTableColumn;
487    }
488
489    public String getNestedTableColumnPath() {
490        return nestedTableColumnPath;
491    }
492
493    public TColumnDefinitionList getNestedTableColumns() {
494        return nestedTableColumns;
495    }
496}