001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.ESqlClause;
004import gudusoft.gsqlparser.TBaseType;
005import gudusoft.gsqlparser.TCustomSqlStatement;
006
007/**
008* SQL Server declare variable
009*/
010public class TDeclareVariable extends TParseTreeNode {
011
012    private TObjectNameList variableNameList = null;
013
014    public TObjectNameList getVariableNameList() {
015        return variableNameList;
016    }
017
018    private TObjectName variableName = null;
019    private int variableType = TBaseType.declare_varaible_normal;
020    private TTypeName datatype = null;
021    private TExpression defaultValue = null;
022    private TTableElementList tableTypeDefinitions = null;
023    private boolean nullConstraintSpecified = false;
024    private boolean nullable = true;
025
026    public void setTableTypeDefinitions(TTableElementList tableTypeDefinitions) {
027        this.tableTypeDefinitions = tableTypeDefinitions;
028    }
029
030    /**
031     * Record a NULL / NOT NULL constraint specified on a scalar variable
032     * declaration, e.g. {@code DECLARE @v datetime2 NOT NULL = GETUTCDATE()}.
033     *
034     * @param nullable {@code true} when NULL was specified, {@code false} when
035     *                 NOT NULL was specified
036     */
037    public void setNullConstraint(boolean nullable) {
038        this.nullConstraintSpecified = true;
039        this.nullable = nullable;
040    }
041
042    /** @return whether an explicit NULL / NOT NULL constraint was specified. */
043    public boolean isNullConstraintSpecified() {
044        return nullConstraintSpecified;
045    }
046
047    /** @return {@code true} unless an explicit NOT NULL constraint was specified. */
048    public boolean isNullable() {
049        return nullable;
050    }
051
052    public void setVariableType(int variableType) {
053        this.variableType = variableType;
054    }
055
056    public TTableElementList getTableTypeDefinitions() {
057
058        return tableTypeDefinitions;
059    }
060
061    public void setDefaultValue(TExpression defaultValue) {
062        this.defaultValue = defaultValue;
063    }
064
065    public TTypeName getDatatype() {
066
067        return datatype;
068    }
069
070    public TExpression getDefaultValue() {
071        return defaultValue;
072    }
073
074    public TObjectName getVariableName() {
075        return variableName;
076    }
077
078    public int getVariableType() {
079        return variableType;
080    }
081
082    public void init(Object arg1){
083        if (arg1 instanceof  TObjectName){
084            this.variableName = (TObjectName)arg1;
085        }else if (arg1 instanceof TObjectNameList){
086            this.variableNameList = (TObjectNameList)arg1;
087            this.variableName = variableNameList.getObjectName(0);
088        }
089
090    }
091
092    public void setDatatype(TTypeName datatype) {
093        this.datatype = datatype;
094    }
095
096    public void init(Object arg1,Object arg2){
097         init(arg1);
098         if (arg2 != null){
099
100             TTypeName o = (TTypeName)arg2;
101             if (o.toString().equalsIgnoreCase("cursor")){
102                 this.variableType = TBaseType.declare_varaible_cursor;
103             }else{
104                 this.variableType = TBaseType.declare_varaible_normal;
105                 this.datatype = (TTypeName)arg2;
106             }
107         }else{
108             this.variableType = TBaseType.declare_varaible_table;
109         }
110
111     }
112
113    public void doParse(TCustomSqlStatement psql, ESqlClause plocation){
114        if (getDefaultValue() != null){
115            getDefaultValue().doParse(psql,plocation);
116        }
117    }
118
119
120    public void accept(TParseTreeVisitor v){
121        v.preVisit(this);
122        v.postVisit(this);
123    }
124
125    public void acceptChildren(TParseTreeVisitor v){
126        v.preVisit(this);
127        if (this.getVariableNameList() != null){
128            this.getVariableNameList().acceptChildren(v);
129        }
130        else if (this.getVariableName() != null){
131            this.getVariableName().acceptChildren(v);
132        }
133
134        if (this.getDatatype() != null){
135            this.getDatatype().acceptChildren(v);
136        }
137        if (this.getDefaultValue() != null){
138            this.getDefaultValue().acceptChildren(v);
139        }
140        v.postVisit(this);
141    }
142
143    public void setVariableName(TObjectName variableName) {
144        this.variableName = variableName;
145    }
146}