001package gudusoft.gsqlparser.stmt.oracle;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.compiler.TVariable;
005import gudusoft.gsqlparser.nodes.*;
006import gudusoft.gsqlparser.nodes.oracle.TInvokerRightsClause;
007import gudusoft.gsqlparser.nodes.oracle.TParallelEnableClause;
008import gudusoft.gsqlparser.nodes.oracle.TResultCacheClause;
009import gudusoft.gsqlparser.stmt.TCommonStoredProcedureSqlStatement;
010
011
012/**
013 * This class represents 3 types of SQL statement:
014 * <ul>
015 * <li> Create function statement, creates or replaces a standalone function or a call specification.
016 * </li>
017 * <li> Declare a function inside a PL/SQL block or package.
018 * </li>
019 * <li> Define a function inside a PL/SQL block or package.
020 * </li>
021 * </ul>
022 *
023 * @see gudusoft.gsqlparser.stmt.TCommonStoredProcedureSqlStatement#declareStatements
024 * @see gudusoft.gsqlparser.stmt.TCommonStoredProcedureSqlStatement#bodyStatements
025*/
026
027/**
028 * Oracle create function.
029 *
030 */
031public class TPlsqlCreateFunction extends TCommonStoredProcedureSqlStatement {
032
033    private boolean isDeterministic;
034    private TInvokerRightsClause invokerRightsClause;
035    private TParallelEnableClause parallelEnableClause;
036    private TResultCacheClause resultCacheClause;
037    private TObjectName implementionType;
038
039    public TObjectName getImplementionType() {
040        return implementionType;
041    }
042
043    public boolean isDeterministic() {
044        return isDeterministic;
045    }
046
047    public TParallelEnableClause getParallelEnableClause() {
048        return parallelEnableClause;
049    }
050
051    public TResultCacheClause getResultCacheClause() {
052        return resultCacheClause;
053    }
054
055    public TInvokerRightsClause getInvokerRightsClause() {
056
057        return invokerRightsClause;
058    }
059    @Override
060    public TObjectName getStoredProcedureName(){
061        return functionName;
062    }
063
064
065    /**
066     * The name that you give to the function that you are declaring or defining.
067     * @return
068     */
069    public TObjectName getFunctionName() {
070        return functionName;
071    }
072    
073    /**
074     * Indicates what's kind of SQL statement this class represents for, create function, declare a function or define a function.
075     * @return
076     */
077    public int getKind() {
078        return kind;
079    }
080
081    private TObjectName functionName = null;
082
083    public void setKind(int kind) {
084        this.kind = kind;
085    }
086
087    private int kind = TBaseType.kind_create;// what's kind of this function
088
089    public TPlsqlCreateFunction(EDbVendor dbvendor){
090        super(dbvendor);
091        sqlstatementtype = ESqlStatementType.sstplsql_createfunction ;
092        }
093
094    private TCallSpec callSpec = null;
095
096    /**
097     * call specification
098     * @return
099     */
100    public TCallSpec getCallSpec() {
101        return callSpec;
102    }
103
104    void buildsql() {
105    }
106
107
108
109    void clear() {
110    }
111
112    String getasprettytext() {
113        return "";
114    }
115
116    void iterate(TVisitorAbs pvisitor) {
117    }
118
119 //   private TStatementListSqlNode stmts = null;
120
121     public int doParseStatement(TCustomSqlStatement psql) {
122         if (rootNode == null) return -1;
123         if (isWrapped()) return  0;
124         TCreateFunctionSqlNode createFunctionNode = (TCreateFunctionSqlNode)rootNode;
125
126         //super.doParseStatement(psql);
127         if (super.doParseStatement(psql) != 0) return -1;
128         functionName = createFunctionNode.getFunctionName();
129         this.setParameterDeclarations(createFunctionNode.getParameters());
130         this.returnDataType = createFunctionNode.getReturnDataType(); 
131
132         invokerRightsClause = createFunctionNode.getInvokerRightsClause();
133         isDeterministic = createFunctionNode.isDeterministic();
134         parallelEnableClause = createFunctionNode.getParallelEnableClause();
135         resultCacheClause = createFunctionNode.getResultCacheClause();
136
137         this.implementionType = createFunctionNode.getImplementionType();
138         this.callSpec = createFunctionNode.getCallSpec();
139         setLabelName(createFunctionNode.getLabelName());
140         setEndlabelName(createFunctionNode.getLabelName());
141
142         // push parameterDeclarations into symbolTable
143         if (this.getParameterDeclarations() != null){
144             for(int i=0;i< this.getParameterDeclarations().size();i++){
145                this.getTopStatement().getSymbolTable().push( new TSymbolTableItem(TObjectName.ttobjParameter,this, this.getParameterDeclarations().getParameterDeclarationItem(i)));
146
147                 TParameterDeclaration parameterDeclaration = this.getParameterDeclarations().getParameterDeclarationItem(i);
148                 if (parameterDeclaration.getParameterName() != null){
149                     this.stmtScope.addSymbol(new TVariable(parameterDeclaration.getParameterName(),parameterDeclaration,functionName));
150                 }
151             }
152         }
153
154         if (createFunctionNode.getDeclareStmts() != null){
155            createFunctionNode.getDeclareStmts().doParse(this, ESqlClause.unknown);
156
157             // push variable declare into symbolTable    , and add to declareStatements
158             for(int i=0;i<createFunctionNode.getDeclareStmts().size();i++){
159                this.getTopStatement().getSymbolTable().push( new TSymbolTableItem(TObjectName.ttobjVariable,this,createFunctionNode.getDeclareStmts().getStatementSqlNode(i).getStmt() ));
160                TCustomSqlStatement declareStmt = createFunctionNode.getDeclareStmts().getStatementSqlNode(i).getStmt();
161                this.getDeclareStatements().add(declareStmt);
162             }
163         }
164
165
166         if (createFunctionNode.getStmts() != null){
167            createFunctionNode.getStmts().doParse(this,ESqlClause.unknown);
168             for(int i= 0; i<createFunctionNode.getStmts().size();i++){
169               this.getBodyStatements().add(createFunctionNode.getStmts().getStatementSqlNode(i).getStmt());
170             }
171         }
172
173         if (createFunctionNode.getExceptionClause() != null){
174             createFunctionNode.getExceptionClause().doParse(this,ESqlClause.unknown);
175            // this.setExceptionClause(createFunctionNode.getExceptionClause());
176         }
177         
178//        stmts.doParse(this, ESqlClause.unknown);
179
180         if (createFunctionNode.getDeclareStmts() != null){
181           
182             // pop variable declare from symbolTable
183             for(int i=0;i<createFunctionNode.getDeclareStmts().size();i++){
184                this.getTopStatement().getSymbolTable().pop();
185             }
186
187         }
188
189         // pop parameterDeclarations from symbolTable
190         if (this.getParameterDeclarations() != null){
191             for(int i=0;i< this.getParameterDeclarations().size();i++){
192                this.getTopStatement().getSymbolTable().pop();
193             }
194         }
195
196        return 0;
197    }
198
199    public void init(Object arg1)
200    {
201   //     stmts = (TStatementListSqlNode)arg1;
202    }
203
204    private TTypeName returnDataType = null;
205
206    public void setReturnDataType(TTypeName returnDataType) {
207        this.returnDataType = returnDataType;
208    }
209
210    /**
211     * Datatype of value returned by this function.
212     * @return
213     */
214    public TTypeName getReturnDataType() {
215
216        return returnDataType;
217    }
218
219
220    public void accept(TParseTreeVisitor v){
221        v.preVisit(this);
222        v.postVisit(this);
223    }
224
225    public void acceptChildren(TParseTreeVisitor v){
226        v.preVisit(this);
227
228        if (this.getParameterDeclarations() != null) getParameterDeclarations().acceptChildren(v);
229        if (this.getDeclareStatements().size() > 0) this.getDeclareStatements().acceptChildren(v);
230        if ( this.getBodyStatements().size() > 0) getBodyStatements().acceptChildren(v);
231        if (getExceptionClause() != null) getExceptionClause().acceptChildren(v);
232
233        v.postVisit(this);
234    }
235
236    public void setDeterministic(boolean isDeterministic) {
237        this.isDeterministic = isDeterministic;
238    }
239
240    public void setInvokerRightsClause(TInvokerRightsClause invokerRightsClause) {
241        this.invokerRightsClause = invokerRightsClause;
242    }
243
244    public void setParallelEnableClause(TParallelEnableClause parallelEnableClause) {
245        this.parallelEnableClause = parallelEnableClause;
246    }
247
248    public void setResultCacheClause(TResultCacheClause resultCacheClause) {
249        this.resultCacheClause = resultCacheClause;
250    }
251
252    public void setImplementionType(TObjectName implementionType) {
253        this.implementionType = implementionType;
254    }
255
256    public void setFunctionName(TObjectName functionName) {
257        this.functionName = functionName;
258    }
259
260    public void setCallSpec(TCallSpec callSpec) {
261        this.callSpec = callSpec;
262    }
263}