~sgdg/stado/stado25

« back to all changes in this revision

Viewing changes to src/org/postgresql/stado/parser/SqlDeclareCursor.java

  • Committer: Jim Mlodgenski
  • Date: 2011-08-30 22:39:37 UTC
  • mfrom: (1.1.3 stado)
  • Revision ID: jim@cirrusql.com-20110830223937-25q231a31x0e08b4
Merge from Spatial branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * Copyright (C) 2008 EnterpriseDB Corporation.
 
3
 * Copyright (C) 2011 Stado Global Development Group.
 
4
 *
 
5
 * This file is part of Stado.
 
6
 *
 
7
 * Stado is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * Stado is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with Stado.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 * You can find Stado at http://www.stado.us
 
21
 *
 
22
 ****************************************************************************/
 
23
package org.postgresql.stado.parser;
 
24
 
 
25
import java.util.ArrayList;
 
26
import java.util.Collection;
 
27
import java.util.Collections;
 
28
 
 
29
import org.postgresql.stado.engine.Engine;
 
30
import org.postgresql.stado.engine.ExecutableRequest;
 
31
import org.postgresql.stado.engine.ExecutionResult;
 
32
import org.postgresql.stado.engine.IExecutable;
 
33
import org.postgresql.stado.engine.XDBSessionContext;
 
34
import org.postgresql.stado.exception.ErrorMessageRepository;
 
35
import org.postgresql.stado.exception.XDBServerException;
 
36
import org.postgresql.stado.metadata.DBNode;
 
37
import org.postgresql.stado.metadata.SysDatabase;
 
38
import org.postgresql.stado.metadata.SysTable;
 
39
import org.postgresql.stado.metadata.scheduler.ILockCost;
 
40
import org.postgresql.stado.metadata.scheduler.LockSpecification;
 
41
import org.postgresql.stado.optimizer.QueryTree;
 
42
import org.postgresql.stado.parser.core.syntaxtree.Deallocate;
 
43
import org.postgresql.stado.parser.core.syntaxtree.DeclareCursor;
 
44
import org.postgresql.stado.parser.core.visitor.ObjectDepthFirst;
 
45
import org.postgresql.stado.parser.handler.IdentifierHandler;
 
46
import org.postgresql.stado.parser.handler.QueryTreeHandler;
 
47
import org.postgresql.stado.parser.handler.QueryTreeTracker;
 
48
 
 
49
public class SqlDeclareCursor extends ObjectDepthFirst implements IXDBSql,
 
50
        IExecutable {
 
51
 
 
52
    private XDBSessionContext client;
 
53
 
 
54
    private String cursorName = null;
 
55
 
 
56
    public QueryTree aQueryTree = null;
 
57
 
 
58
    private Command commandToExecute;
 
59
 
 
60
    public SqlDeclareCursor(XDBSessionContext client) {
 
61
        this.client = client;
 
62
 
 
63
        commandToExecute = new Command(Command.SELECT, this,
 
64
                new QueryTreeTracker(), client);
 
65
 
 
66
        aQueryTree = new QueryTree();
 
67
 
 
68
    }
 
69
 
 
70
    // ******************************
 
71
    // BEGIN GRAMMAR
 
72
    // ******************************
 
73
 
 
74
    /**
 
75
     * Grammar production:
 
76
     * f0 -> <DECLARE_>
 
77
     * f1 -> Identifier(prn)
 
78
     * f2 -> <CURSOR_>
 
79
     * f3 -> <FOR_>
 
80
     * f4 -> Select(prn)
 
81
     */
 
82
    @Override
 
83
    public Object visit(DeclareCursor n, Object argu) {
 
84
        Object _ret = null;
 
85
 
 
86
        cursorName = (String) n.f1.accept(new IdentifierHandler(), argu);
 
87
        
 
88
        QueryTreeHandler aQueryTreeHandler = new QueryTreeHandler(
 
89
                commandToExecute);
 
90
        n.f4.accept(aQueryTreeHandler, aQueryTree);
 
91
 
 
92
        return _ret;
 
93
    }
 
94
 
 
95
    /**
 
96
     * This will return the cost of executing this statement in time , milli
 
97
     * seconds
 
98
     */
 
99
 
 
100
    public long getCost() {
 
101
        return ILockCost.LOW_COST;
 
102
    }
 
103
 
 
104
    /**
 
105
     * This return the Lock Specification for the system
 
106
     *
 
107
     * @param theMData
 
108
     * @return
 
109
     */
 
110
    public LockSpecification<SysTable> getLockSpecs() {
 
111
        Collection<SysTable> empty = Collections.emptyList();
 
112
        LockSpecification<SysTable> aLspec = new LockSpecification<SysTable>(
 
113
                empty, empty);
 
114
        return aLspec;
 
115
    }
 
116
 
 
117
    /*
 
118
     * (non-Javadoc)
 
119
     *
 
120
     * @see org.postgresql.stado.Parser.IXDBSql#getNodeList()
 
121
     */
 
122
    public Collection<DBNode> getNodeList() {
 
123
        Collection<DBNode> empty = Collections.emptyList();
 
124
        return new ArrayList<DBNode>(empty);
 
125
    }
 
126
 
 
127
    public ExecutionResult execute(Engine engine) throws Exception {
 
128
        if (!client.isInTransaction()) {
 
129
            throw new XDBServerException(
 
130
                    ErrorMessageRepository.DECLARE_CURSOR_TRANS,
 
131
                    0,
 
132
                    ErrorMessageRepository.DECLARE_CURSOR_TRANS_CODE);          
 
133
        }
 
134
        
 
135
        client.createCursor(cursorName, aQueryTree, false, false);
 
136
        
 
137
        return ExecutionResult.createSuccessResult(ExecutionResult.COMMAND_DECLARE_CURSOR);
 
138
    }
 
139
 
 
140
    /*
 
141
     * (non-Javadoc)
 
142
     *
 
143
     * @see org.postgresql.stado.MetaData.Scheduler.ILockCost#needCoordinatorConnection()
 
144
     */
 
145
    public boolean needCoordinatorConnection() {
 
146
        return false;
 
147
    }
 
148
}