001package gudusoft.gsqlparser.stmt.mssql;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.compiler.TFrame;
005import gudusoft.gsqlparser.compiler.TVariable;
006import gudusoft.gsqlparser.nodes.*;
007import gudusoft.gsqlparser.stmt.TSelectSqlStatement;
008
009import java.util.ArrayList;
010
011
012public class TMssqlDeclare extends TCustomSqlStatement {
013
014    /**
015     * with return only clause
016     *
017     * @return
018     */
019    public boolean isWithReturnOnly() {
020        return withReturnOnly;
021    }
022
023    private boolean withReturnOnly = false;
024
025     public TMssqlDeclare (EDbVendor dbvendor){
026        super(dbvendor);
027        sqlstatementtype = ESqlStatementType.sstmssqldeclare ;
028        }
029
030    private TSelectSqlStatement subquery = null;
031
032    void buildsql() {
033    }
034
035    void clear() {
036    }
037
038    String getasprettytext() {
039        return "";
040    }
041
042    void iterate(TVisitorAbs pvisitor) {
043    }
044
045    private TObjectName cursorName = null;
046    private EDeclareType declareType = EDeclareType.variable;
047
048    public TObjectName getCursorName() {
049        return cursorName;
050    }
051
052    /**
053     *
054     * @return   EDeclareType.variable or EDeclareType.cursor
055     */
056    public EDeclareType getDeclareType() {
057        return declareType;
058    }
059
060    public TSelectSqlStatement getSubquery() {
061        return subquery;
062    }
063
064    public TDeclareVariableList getVariables() {
065        return variables;
066    }
067
068    private TDeclareVariableList variables = null;
069
070    protected TStatementList bodyStatements = null;
071
072    public TStatementList getBodyStatements() {
073        if (this.bodyStatements == null){
074            this.bodyStatements = new TStatementList();
075        }
076        return bodyStatements;
077    }
078
079    private TConstant stateValue;
080    private TConstant errorCode;
081
082    public TConstant getStateValue() {
083        return stateValue;
084    }
085
086    public TConstant getErrorCode() {
087        return errorCode;
088    }
089
090    private TObjectName conditionName = null;//mysql
091
092    public TObjectName getConditionName() {
093        return conditionName;
094    }
095
096    private TBlockSqlNode handleBlock;
097
098    public TBlockSqlNode getHandleBlock() {
099        return handleBlock;
100    }
101
102    private ArrayList<THandlerForCondition> handlerForConditions;
103
104    public ArrayList<THandlerForCondition> getHandlerForConditions() {
105        return handlerForConditions;
106    }
107
108    public int doParseStatement(TCustomSqlStatement psql) {
109        if (rootNode == null) return -1;
110        TDeclareSqlNode declareSqlNode = (TDeclareSqlNode)rootNode;
111
112        super.doParseStatement(psql);
113        this.declareType = declareSqlNode.getDeclareType();
114        this.cursorName = declareSqlNode.getCursorName();
115        this.conditionName = declareSqlNode.getConditionName();
116        this.stateValue = declareSqlNode.getStateValue();
117        this.errorCode = declareSqlNode.getErrorCode();
118
119        if (this.declareType == EDeclareType.variable){
120            this.variables = declareSqlNode.getVariables();
121            for(int i=0;i<variables.size();i++){
122                variables.getDeclareVariable(i).doParse(this,ESqlClause.unknown);
123                if (variables.getDeclareVariable(i).getVariableName() != null){
124                    TFrame stackFrame = getFrameStack().peek();
125                    TDeclareVariable declareVariable = variables.getDeclareVariable(i);
126                    TVariable v = new TVariable(declareVariable.getVariableName(),this);
127                    if (declareVariable.getDefaultValue() != null){
128                        declareVariable.getDefaultValue().evaluate(this.getFrameStack(),this);
129                        v.setVariableStr(declareVariable.getDefaultValue().getPlainText());
130                        //System.out.println(declareVariable.getDefaultValue().getPlainText());
131                    }
132                    stackFrame.getScope().addSymbol(v);
133                }
134            }
135        }
136
137        this.handlerForConditions = declareSqlNode.getHandlerForConditions();
138        
139        if(declareSqlNode.getSelectSqlNode() != null){
140            subquery = new TSelectSqlStatement(this.dbvendor);
141            subquery.rootNode = declareSqlNode.getSelectSqlNode();
142            subquery.doParseStatement(this);
143        }
144
145        if (declareSqlNode.getHandleStmt() != null){
146            declareSqlNode.getHandleStmt().doParse(this,ESqlClause.unknown);
147            this.getBodyStatements().add(declareSqlNode.getHandleStmt().getStmt());
148        }
149        else if (declareSqlNode.getHandlerBlock() != null){
150            handleBlock = declareSqlNode.getHandlerBlock();
151            handleBlock.doParse(this,ESqlClause.unknown);
152//            declareSqlNode.getHandlerBlock().getStmts().doParse(this,ESqlClause.unknown);
153//
154//            for(int i=0;i<declareSqlNode.getHandlerBlock().getStmts().size();i++){
155//                this.getBodyStatements().add(declareSqlNode.getHandlerBlock().getStmts().getStatementSqlNode(i).getStmt());
156//            }
157        }else if (declareSqlNode.getStmtSqlNode() != null){
158            //teradata declare handler for sqlblock/sqlstmt
159            declareSqlNode.getStmtSqlNode().doParse(this,ESqlClause.unknown);
160            this.getBodyStatements().add(declareSqlNode.getStmtSqlNode().getStmt());
161        }
162
163        withReturnOnly = declareSqlNode.isWithReturnOnly();
164
165        return 0;
166    }
167
168    public void accept(TParseTreeVisitor v){
169        v.preVisit(this);
170        v.postVisit(this);
171    }
172
173    public void acceptChildren(TParseTreeVisitor v){
174        v.preVisit(this);
175
176        if (this.cursorName != null){
177            this.cursorName.acceptChildren(v);
178        }
179
180        if (this.declareType == EDeclareType.variable){
181            this.variables.acceptChildren(v);
182        }
183
184
185        if (subquery != null) subquery.acceptChildren(v);
186        v.postVisit(this);
187    }
188
189    public void setSubquery(TSelectSqlStatement subquery) {
190        this.subquery = subquery;
191    }
192
193    public void setCursorName(TObjectName cursorName) {
194        this.cursorName = cursorName;
195    }
196
197    public void setDeclareType(EDeclareType declareType) {
198        this.declareType = declareType;
199    }
200
201    public void setVariables(TDeclareVariableList variables) {
202        this.variables = variables;
203    }
204}