001package gudusoft.gsqlparser.stmt.db2;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.*;
005import gudusoft.gsqlparser.stmt.TStoredProcedureSqlStatement;
006
007/**
008 * DB2 create function
009 *
010 * CREATE FUNCTION AVG(EURO)
011 *   RETURNS EURO
012 *   SOURCE SYSIBM.AVG(DECIMAL);
013 *
014 * @deprecated since v2.6.4.1, use {@link gudusoft.gsqlparser.stmt.TCreateFunctionStmt} instead.
015 *
016 */
017public class TDb2CreateFunction extends TStoredProcedureSqlStatement{
018    public TDb2CreateFunction (EDbVendor dbvendor){
019        super(dbvendor);
020        sqlstatementtype = ESqlStatementType.sstdb2createfunction ;
021        }
022
023
024    void buildsql() {
025    }
026
027    void clear() {
028    }
029
030    String getasprettytext() {
031        return "";
032    }
033
034    void iterate(TVisitorAbs pvisitor) {
035    }
036
037    @Override
038    public TObjectName getStoredProcedureName(){
039        return this.functionName;
040    }
041
042
043    public int doParseStatement(TCustomSqlStatement psql) {
044        if (rootNode == null) return -1;
045        TCreateFunctionSqlNode createFunctionNode = (TCreateFunctionSqlNode)rootNode;
046        TCompoundSqlNode compoundSqlNode = createFunctionNode.getCompoundSql();
047        TReturnSqlNode returnSqlNode = createFunctionNode.getReturnSqlNode();
048
049        super.doParseStatement(psql);
050
051        functionName = createFunctionNode.getFunctionName();
052        this.setParameterDeclarations(createFunctionNode.getParameters());
053
054
055        // push parameterDeclarations into symbolTable
056        if (this.getParameterDeclarations() != null){
057            for(int i=0;i< this.getParameterDeclarations().size();i++){
058               this.getTopStatement().getSymbolTable().push( new TSymbolTableItem(TObjectName.ttobjParameter,this, this.getParameterDeclarations().getParameterDeclarationItem(i)));
059            }
060        }
061
062        //T_ReturnSqlNode
063        
064        if (compoundSqlNode != null){
065            if (compoundSqlNode.getDeclareStmts() != null){
066               compoundSqlNode.getDeclareStmts().doParse(this,ESqlClause.unknown);
067
068                // push variable declare into symbolTable, and add to declareStatements
069                for(int i=0;i<compoundSqlNode.getDeclareStmts().size();i++){
070                   this.getTopStatement().getSymbolTable().push( new TSymbolTableItem(TObjectName.ttobjVariable,this,compoundSqlNode.getDeclareStmts().getStatementSqlNode(i).getStmt() ));
071                   this.getDeclareStatements().add(compoundSqlNode.getDeclareStmts().getStatementSqlNode(i).getStmt());
072                }
073            }
074
075            if (compoundSqlNode.getStmts() != null){
076               compoundSqlNode.getStmts().doParse(this,ESqlClause.unknown);
077
078                for(int i= 0; i<compoundSqlNode.getStmts().size();i++){
079                  this.getBodyStatements().add(compoundSqlNode.getStmts().getStatementSqlNode(i).getStmt());
080                }
081            }
082
083            if (compoundSqlNode.getDeclareStmts() != null){
084                // pop variable declare from symbolTable
085                for(int i=0;i<compoundSqlNode.getDeclareStmts().size();i++){
086                   this.getTopStatement().getSymbolTable().pop();
087                }
088            }
089        }else if (returnSqlNode != null){
090           this.returnStmt = new TDb2ReturnStmt(EDbVendor.dbvdb2);
091           this.returnStmt.rootNode  = returnSqlNode;
092           this.returnStmt.doParseStatement(this);
093        }
094
095        // pop parameterDeclarations from symbolTable
096        if (this.getParameterDeclarations() != null){
097            for(int i=0;i< this.getParameterDeclarations().size();i++){
098               this.getTopStatement().getSymbolTable().pop();
099            }
100        }
101
102       return 0;
103   }
104
105    private TObjectName functionName = null;
106
107    public TObjectName getFunctionName() {
108        return functionName;
109    }
110
111    private TDb2ReturnStmt  returnStmt = null;
112
113    public TDb2ReturnStmt getReturnStmt() {
114        return returnStmt;
115    }
116
117    public void accept(TParseTreeVisitor v){
118        v.preVisit(this);
119
120        if (this.getParameterDeclarations() != null) getParameterDeclarations().accept(v);
121        if (this.getDeclareStatements().size() > 0) this.getDeclareStatements().accept(v);
122        if ( this.getBodyStatements().size() > 0) getBodyStatements().accept(v);
123
124        if (returnStmt != null)  returnStmt.accept(v);
125
126
127        v.postVisit(this);
128    }
129
130    public void setFunctionName(TObjectName functionName) {
131        this.functionName = functionName;
132    }
133
134    public void setReturnStmt(TDb2ReturnStmt returnStmt) {
135        this.returnStmt = returnStmt;
136    }
137
138
139}