summaryrefslogtreecommitdiffstats
path: root/tags/java-stable-20060304/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/Statement.java
blob: 6bc18dc229d00ab7f2ef5880c0054adc2e1b2c0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.das.rdb.impl;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

import org.apache.tuscany.das.rdb.Parameter;
import org.apache.tuscany.das.rdb.util.DebugUtil;

//TODO - Can use some refactoring.  Much code is duplicated in "execute" methods
public class Statement {

    protected final QueryString queryString;

    protected ConnectionImpl jdbcConnection;

    private static final boolean debug = false;

    private PreparedStatement preparedStatement;

    private boolean isPaging = false;

    public Statement(String sqlString) {
        this.queryString = new QueryString(sqlString);
    }

    public ResultSet executeQuery(Parameters parameters) throws SQLException {

        PreparedStatement ps = getPreparedStatement();
        ps = setParameters(ps, parameters);
        ResultSet rs = ps.executeQuery();

        return rs;
    }

    public ResultSet executeCall(Parameters parameters) throws SQLException {
        try {
            CallableStatement cs = jdbcConnection.prepareCall(queryString);

            Iterator inParams = parameters.inParams().iterator();
            while (inParams.hasNext()) {
                Parameter param = (Parameter) inParams.next();
                if (param.getIndex() == 0)
                    param.setIndex(queryString.getParameterIndex(param.getName()));
                cs.setObject(param.getIndex(), param.getValue());
            }

            // register out parameters
            Iterator outParams = parameters.outParams().iterator();
            while (outParams.hasNext()) {
                Parameter param = (Parameter) outParams.next();
                if (param.getIndex() == 0)
                    param.setIndex(queryString.getParameterIndex(param.getName()));
                DebugUtil.debugln(getClass(), debug, "Registering parameter " + param.getName());
                cs.registerOutParameter(param.getIndex(), SDODataTypeHelper.sqlTypeFor(param.getType()));
            }

            // Using execute because Derby does not currenlty support
            // executeQuery
            // for SP
            cs.execute();
            ResultSet results = cs.getResultSet();

            Iterator i = parameters.outParams().iterator();
            while (i.hasNext()) {
                Parameter param = (Parameter) i.next();
                param.setValue(cs.getObject(param.getIndex()));
            }

            return results;
        } catch (SQLException ex) {
            ex.printStackTrace();
            throw ex;
        }

    }

    public void executeUpdateCall(Parameters parameters) throws SQLException {
        CallableStatement cs = jdbcConnection.prepareCall(queryString);

        Iterator inParams = parameters.inParams().iterator();
        while (inParams.hasNext()) {
            Parameter param = (Parameter) inParams.next();
            if (param.getIndex() == 0)
                param.setIndex(queryString.getParameterIndex(param.getName()));
            cs.setObject(param.getIndex(), param.getValue());
        }

        // register out parameters
        Iterator outParams = parameters.outParams().iterator();
        while (outParams.hasNext()) {
            Parameter param = (Parameter) outParams.next();
            if (param.getIndex() == 0)
                param.setIndex(queryString.getParameterIndex(param.getName()));
            DebugUtil.debugln(getClass(), debug, "Registering parameter " + param.getName());
            cs.registerOutParameter(param.getIndex(), SDODataTypeHelper.sqlTypeFor(param.getType()));
        }

        cs.execute();

        Iterator out = parameters.outParams().iterator();
        while (out.hasNext()) {
            Parameter param = (Parameter) out.next();
            param.setValue(cs.getObject(param.getIndex()));
        }

    }

    /**
     * TODO - We need to look at using specific ps.setXXX methods when a type
     * has been specified and try setObject otherwise.
     */
    public int executeUpdate(Parameters parameters) throws SQLException {
        DebugUtil.debugln(getClass(), debug, "Executing statement " + queryString.getPreparedString());
        PreparedStatement ps = getPreparedStatement();
        Iterator i = parameters.inParams().iterator();
        while (i.hasNext()) {
            Parameter param = (Parameter) i.next();
            if (param.getIndex() == 0)
                param.setIndex(queryString.getParameterIndex(param.getName()));
            Object value = param.getValue();
            DebugUtil.debugln(getClass(), debug, "Setting parameter " + param.getIndex() + " to " + value);
            if (value == null) {            
                ps.setNull(param.getIndex(), SDODataTypeHelper.sqlTypeFor(param.getType()));
            } else {           
                ps.setObject(param.getIndex(), value);
            }
        }
        return ps.executeUpdate();
    }

    protected PreparedStatement setParameters(PreparedStatement ps, Parameters parameters)
            throws SQLException {
        Iterator i = parameters.inParams().iterator();
        while (i.hasNext()) {
            Parameter param = (Parameter) i.next();
            if (param.getIndex() == 0)
                param.setIndex(queryString.getParameterIndex(param.getName()));
            ps.setObject(param.getIndex(), param.getValue());
        }
        return ps;
    }

    public void setConnection(ConnectionImpl jdbcConnection) {
        this.jdbcConnection = jdbcConnection;
    }

    public ConnectionImpl getConnection() {
        return this.jdbcConnection;
    }

    private PreparedStatement getPreparedStatement() throws SQLException {
        DebugUtil.debugln(getClass(), debug, "Getting prepared statement");
        if (preparedStatement == null)
            if (isPaging)
                preparedStatement = jdbcConnection.preparePagedStatement(queryString);
            else
                preparedStatement = jdbcConnection.prepareStatement(queryString);

        return preparedStatement;
    }

    public Integer getGeneratedKey() throws SQLException {

        ResultSet rs = getPreparedStatement().getGeneratedKeys();
        if (rs.next())
            return new Integer(rs.getInt(1));

        return null;
    }

    protected void enablePaging() {
        isPaging = true;
    }

    public void close() {

        if (this.preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }

}