001package gudusoft.gsqlparser.stmt;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.compiler.TFrame;
005import gudusoft.gsqlparser.compiler.TVariable;
006import gudusoft.gsqlparser.nodes.*;
007
008import static gudusoft.gsqlparser.EDeclareType.variable;
009
010
011/**
012 * Represents declare statement that used to decalre variable, constant, exception and subtype. 
013 * Or Signifies that the statement is a pragma (compiler directive), including exception_init_pragma,
014 *  autonomous_transaction_pragma, restrict_references_pragma, serially_resuable_pragma and pragma_timestamp
015 */
016
017public class TVarDeclStmt extends TCustomSqlStatement {
018
019    public TVarDeclStmt(){
020        this(EDbVendor.dbvoracle);
021    }
022
023    public TVarDeclStmt(EDbVendor dbvendor){
024        super(dbvendor);
025        sqlstatementtype = ESqlStatementType.sstplsql_vardecl ;
026        }
027
028    void buildsql() {
029    }
030
031    void clear() {
032    }
033
034    String getasprettytext() {
035        return "";
036    }
037
038    void iterate(TVisitorAbs pvisitor) {
039    }
040
041    private TObjectName elementName = null;
042    private TTypeName dataType = null;
043    private boolean NotNull = false;
044    private TExpression value = null;
045
046
047    private int howtoSetValue = TBaseType.howtoSetValue_none;
048
049    /**
050     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#variable}
051     */
052    public final static int whatDeclared_variable = 1;
053    /**
054     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#constant}
055     */
056    public final static int whatDeclared_constant = 2;
057    /**
058     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#exception}
059     */
060    public final static int whatDeclared_exception = 3;
061    /**
062     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#subtype}
063     */
064    public final static int whatDeclared_subtype = 4;
065    /**
066     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_exception_init}
067     */
068    public final static int whatDeclared_pragma_exception_init = 5;
069    /**
070     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_autonomous_transaction}
071     */
072    public final static int whatDeclared_pragma_autonomous_transaction = 6;
073    /**
074     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_serially_reusable}
075     */
076    public final static int whatDeclared_pragma_serially_reusable = 7;
077    /**
078     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_timestamp}
079     */
080    public final static int whatDeclared_pragma_timestamp = 8;
081    /**
082     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_restrict_references}
083     */
084    public final static int whatDeclared_pragma_restrict_references = 9;
085    /**
086     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_interface}
087     */
088    public final static int whatDeclared_pragma_interface = 10;
089    /**
090     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_coverage}
091     */
092    public final static int whatDeclared_pragma_coverage = 11;
093    /**
094     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_deprecate}
095     */
096    public final static int whatDeclared_pragma_deprecate = 12;
097    /**
098     * @deprecated As of v1.4.8.8, replaced by {@link EDeclareType#pragma_suppresses_warning_6009}
099     */
100    public final static int whatDeclared_pragma_suppresses_warning_6009 = 13;
101
102    public void setWhatDeclared(int whatDeclared) {
103        this.whatDeclared = whatDeclared;
104    }
105
106    /**
107     * Indicates what's kind of element this delcare statement declared such as variable, constant, exception, subtype and various pragma clause.
108     * @return
109     */
110    public int getWhatDeclared() {
111        return whatDeclared;
112    }
113
114    /**
115     * How deafult value of a variable or constant was set.
116     * <p> := | DEFAULT
117     * <p> := {@link  TBaseType#howtoSetValue_assign}
118     * <p> or DEFAULT keyword  {@link  TBaseType#howtoSetValue_default};
119     * @return
120     */
121
122    public int getHowtoSetValue() {
123        return howtoSetValue;
124    }
125
126    /**
127     * @deprecated As of v1.4.8.8, replaced by {@link #getDefaultValue()}
128     */
129    public TExpression getValue() {
130        return value;
131    }
132
133    /**
134     * Specifies that no element can have the value NULL.
135     * @return
136     */
137    public boolean getNotNull() {
138        return NotNull;
139    }
140
141    private int whatDeclared = whatDeclared_variable;
142    private EDeclareType declareType = variable;
143    private TObjectName collateName;
144    private TExpression defaultValue;
145
146    public void setDefaultValue(TExpression defaultValue) {
147        this.defaultValue = defaultValue;
148    }
149
150    /**
151     * Default value of a variable or constant that declared.
152     */
153    public TExpression getDefaultValue() {
154
155        return defaultValue;
156    }
157
158    public void setCollateName(TObjectName collateName) {
159        this.collateName = collateName;
160    }
161
162    public TObjectName getCollateName() {
163
164        return collateName;
165    }
166
167    public void setDeclareType(EDeclareType declareType) {
168        this.declareType = declareType;
169    }
170
171    public EDeclareType getDeclareType() {
172
173        return declareType;
174    }
175
176    private TObjectName exception_name = null;
177    private TExpression error_number = null;
178
179    public void setError_number(TExpression error_number) {
180        this.error_number = error_number;
181    }
182
183    public void setException_name(TObjectName exception_name) {
184        this.exception_name = exception_name;
185    }
186
187    /**
188     * Any valid Oracle Database error number. Used when this class represents exception_init_pragma.
189     * @return
190     */
191    public TExpression getError_number() {
192        return error_number;
193    }
194
195    /**
196     * Name of a user-defined exception declared within the current scope. Used when this class represents exception_init_pragma.
197     * @return
198     */
199
200    public TObjectName getException_name() {
201        return exception_name;
202    }
203
204    private TObjectName aliasItem;
205
206    public void setAliasItem(TObjectName aliasItem) {
207        this.aliasItem = aliasItem;
208    }
209
210    public TObjectName getAliasItem() {
211
212        return aliasItem;
213    }
214
215    // SUBTYPE RANGE constraint fields (Oracle 26c)
216    private TExpression rangeLowBound;
217    private TExpression rangeHighBound;
218
219    /**
220     * Sets the low bound of the RANGE constraint for a SUBTYPE declaration.
221     * @param rangeLowBound the low bound expression
222     */
223    public void setRangeLowBound(TExpression rangeLowBound) {
224        this.rangeLowBound = rangeLowBound;
225    }
226
227    /**
228     * Gets the low bound of the RANGE constraint for a SUBTYPE declaration.
229     * For example, in "SUBTYPE Digit IS PLS_INTEGER RANGE 0..9", returns 0.
230     * @return the low bound expression, or null if no RANGE constraint
231     */
232    public TExpression getRangeLowBound() {
233        return rangeLowBound;
234    }
235
236    /**
237     * Sets the high bound of the RANGE constraint for a SUBTYPE declaration.
238     * @param rangeHighBound the high bound expression
239     */
240    public void setRangeHighBound(TExpression rangeHighBound) {
241        this.rangeHighBound = rangeHighBound;
242    }
243
244    /**
245     * Gets the high bound of the RANGE constraint for a SUBTYPE declaration.
246     * For example, in "SUBTYPE Digit IS PLS_INTEGER RANGE 0..9", returns 9.
247     * @return the high bound expression, or null if no RANGE constraint
248     */
249    public TExpression getRangeHighBound() {
250        return rangeHighBound;
251    }
252
253    /**
254     * Checks if this SUBTYPE declaration has a RANGE constraint.
255     * @return true if RANGE constraint is present
256     */
257    public boolean hasRangeConstraint() {
258        return rangeLowBound != null && rangeHighBound != null;
259    }
260
261    public void init(Object arg1){
262        if (arg1 instanceof EDeclareType){
263            this.declareType = (EDeclareType)arg1;
264        }else{
265            elementName = (TObjectName)arg1;
266            elementName.setObjectType(TObjectName.ttobjVariable);
267        }
268    }
269
270    public void init(Object arg1,Object arg2){
271        init(arg1);
272        switch (declareType){
273            case exception:
274            case procedure:
275                elementName = (TObjectName)arg2;
276                break;
277            default:
278                if (arg2 != null){
279                    dataType = (TTypeName)arg2;
280                }
281                break;
282        }
283    }
284
285    public void init(Object arg1,Object arg2,Object arg3,Object arg4){
286
287        init(arg1,arg2);
288        NotNull = (arg3 != null);
289
290        if (arg4 != null){ //tdummy
291            if (arg4 instanceof TDummy){
292                value = (TExpression)((TDummy)arg4).node1;
293                defaultValue = value;
294                howtoSetValue = ((TDummy)arg4).int1;
295            }else if (arg4 instanceof TExpression){
296                defaultValue = (TExpression)arg4;
297            }
298        }
299    }
300
301    /**
302     * Datatype of declared element.
303     * @return
304     */
305    public TTypeName getDataType() {
306        return dataType;
307    }
308
309
310    public int doParseStatement(TCustomSqlStatement psql) {
311        super.doParseStatement(psql);
312        if (elementName != null){
313            TFrame stackFrame = getFrameStack().peek();
314            stackFrame.getScope().addSymbol(new TVariable(elementName,this));
315        }
316        if (value != null){
317            value.doParse(this,ESqlClause.unknown);
318        }
319
320        if (defaultValue != null){
321            defaultValue.doParse(this,ESqlClause.unknown);
322        }
323        return 0;
324    }
325
326    /**
327     * Name of element that declared.
328     * @return
329     */
330    public TObjectName getElementName() {
331        return elementName;
332    }
333
334    public void accept(TParseTreeVisitor v){
335        v.preVisit(this);
336        v.postVisit(this);
337    }
338
339    public void acceptChildren(TParseTreeVisitor v){
340        v.preVisit(this);
341        v.postVisit(this);
342    }
343
344    public void setElementName(TObjectName elementName) {
345        this.elementName = elementName;
346    }
347
348    public void setDataType(TTypeName dataType) {
349        this.dataType = dataType;
350    }
351
352    public void setNotNull(boolean notNull) {
353        NotNull = notNull;
354    }
355
356    public void setValue(TExpression value) {
357        this.value = value;
358    }
359
360    public void setHowtoSetValue(int howtoSetValue) {
361        this.howtoSetValue = howtoSetValue;
362    }
363}