001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.ESqlClause;
004import gudusoft.gsqlparser.TCustomSqlStatement;
005import gudusoft.gsqlparser.TStatementList;
006
007/**
008* TBlockSqlNode represents a block in a stored procedure.
009 * It's used in {@link gudusoft.gsqlparser.stmt.TCreateFunctionStmt} and {@link gudusoft.gsqlparser.stmt.TCreateProcedureStmt}
010 * to retrieve the declarations and statements as shown below.
011 *<br><br>
012 * The mainly used methods are {@link #getDeclareStmts()} to retrieve the declarations, {@link #getBodyStatements()} to
013 * retrieve the statements.
014 * <br><br>
015 * {@link #getExceptionClause()} represents the exception section in the block if any.
016 *
017* <pre>{@code
018* [ <<label>> ]
019* [ DECLARE
020*     declarations ]
021* BEGIN
022*     statements
023* END [ label ];
024* }</pre>
025 *
026* @see gudusoft.gsqlparser.stmt.TCreateProcedureStmt
027 * @see gudusoft.gsqlparser.stmt.TCreateFunctionStmt
028*/
029public class TBlockSqlNode extends TNodeWithLabel {
030
031    protected TObjectName outerLabelName = null;
032    protected TStatementList declareStatements = null;
033    protected TStatementList bodyStatements = null;
034    protected TExceptionClause exceptionClause = null;
035
036    private TStatementListSqlNode stmts = null;
037    private TStatementListSqlNode declareStmts = null;
038
039    private boolean isParsed = false;
040
041    public void setParsed(boolean parsed) {
042        isParsed = parsed;
043    }
044
045    public boolean isParsed() {
046        return isParsed;
047    }
048
049    public void init(Object arg1)
050    {
051        stmts = (TStatementListSqlNode)arg1;
052    }
053
054
055
056    public TExceptionClause getExceptionClause() {
057        return exceptionClause;
058    }
059
060    public void setExceptionClause(TExceptionClause exceptionClause) {
061
062        this.exceptionClause = exceptionClause;
063    }
064
065
066    public TStatementListSqlNode getDeclareStmts() {
067        return declareStmts;
068    }
069
070    public TStatementListSqlNode getStmts() {
071        return stmts;
072    }
073
074
075    public void setStmts(TStatementListSqlNode stmts) {
076        this.stmts = stmts;
077    }
078
079    public void setDeclareStmts(TStatementListSqlNode declareStmts) {
080        this.declareStmts = declareStmts;
081    }
082
083
084    public void setOuterLabelName(TObjectName outerLabelName) {
085        this.outerLabelName = outerLabelName;
086    }
087    public TObjectName getOuterLabelName() {
088        return outerLabelName;
089    }
090
091
092
093    public void setDeclareStatements(TStatementList declareStatements) {
094        this.declareStatements = declareStatements;
095    }
096    public TStatementList getDeclareStatements() {
097        if (declareStatements == null){
098            declareStatements = new TStatementList();
099        }
100        return declareStatements;
101    }
102
103
104    public void setBodyStatements(TStatementList bodyStatements) {
105        this.bodyStatements = bodyStatements;
106    }
107    public TStatementList getBodyStatements() {
108        if (bodyStatements == null){
109            bodyStatements = new TStatementList();
110        }
111        return bodyStatements;
112    }
113
114
115    @Override
116    public void doParse(TCustomSqlStatement psql, ESqlClause plocation) {
117        if (isParsed) return;
118        super.doParse(psql, plocation);
119        if (declareStmts != null){
120            declareStmts.doParse(psql,plocation);
121            for(int i=0;i<declareStmts.size();i++){
122                declareStmts.getStatementSqlNode(i).getStmt().setParent(this);
123                this.getDeclareStatements().add(declareStmts.getStatementSqlNode(i).getStmt());
124            }
125
126            for(int i=0;i<declareStmts.size();i++) {
127                psql.getTopStatement().getSymbolTable().push(new TSymbolTableItem(TObjectName.ttobjVariable
128                        , psql
129                        ,declareStmts.getStatementSqlNode(i).getStmt())
130                );
131            }
132        }
133
134        if (stmts != null){
135            stmts.doParse(psql,plocation);
136
137            for(int i=0;i<stmts.size();i++){
138                stmts.getStatementSqlNode(i).getStmt().setParent(this);
139//                if (stmts.getStatementSqlNode(i).getStmt() == null) {
140//                    System.out.println("null statement:"+i);
141//                }
142                this.getBodyStatements().add(stmts.getStatementSqlNode(i).getStmt());
143            }
144        }
145
146        if (exceptionClause != null){
147            exceptionClause.doParse(psql,plocation);
148            exceptionClause.setParent(this);
149            this.setExceptionClause(exceptionClause);
150        }
151
152        if (declareStmts != null){
153
154            // pop variable declare from symbolTable
155            for(int i=0;i<declareStmts.size();i++){
156                psql.getTopStatement().getSymbolTable().pop();
157            }
158
159        }
160    }
161
162    public void accept(TParseTreeVisitor v){
163        v.preVisit(this);
164        v.postVisit(this);
165    }
166
167    public void acceptChildren(TParseTreeVisitor v){
168        v.preVisit(this);
169        if ((this.getDeclareStatements() != null)&&(this.getDeclareStatements().size()>0)) this.getDeclareStatements().acceptChildren(v);
170        if ((this.getBodyStatements() != null)&&(this.getBodyStatements().size()>0)) this.getBodyStatements().acceptChildren(v);
171        if (exceptionClause != null) exceptionClause.acceptChildren(v);
172        v.postVisit(this);
173    }
174
175}