-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyStatement.java
More file actions
316 lines (272 loc) · 9.05 KB
/
PyStatement.java
File metadata and controls
316 lines (272 loc) · 9.05 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql;
import org.python.core.Visitproc;
import org.python.core.codecs;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyUnicode;
import org.python.core.Traverseproc;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Class PyStatement
*
* @author brian zimmer
*/
public class PyStatement extends PyObject implements Traverseproc {
/** Denotes a simple Statement with no parameters. */
public static final int STATEMENT_STATIC = 2;
/** Denotes a PreparedStatement either explicitly created by the user, or from a
* cursor (due to the presence of bind parameters). */
public static final int STATEMENT_PREPARED = 4;
/** Denotes a stored procedure call. */
public static final int STATEMENT_CALLABLE = 8;
/** One of the above styles. */
private int style;
/** The underlying sql, a String or a Procedure. */
private Object sql;
/** Whether this statement is closed. */
private boolean closed;
/** The underlying java.sql.Statement. */
Statement statement;
/** Field __methods__ */
protected static PyList __methods__;
/** Field __members__ */
protected static PyList __members__;
static {
PyObject[] m = new PyObject[1];
m[0] = new PyString("close");
__methods__ = new PyList(m);
m = new PyObject[3];
m[0] = new PyString("style");
m[1] = new PyString("closed");
m[2] = new PyString("__statement__");
__members__ = new PyList(m);
}
/**
* Constructor PyStatement
*
* @param statement
* @param sql
* @param style
*/
public PyStatement(Statement statement, Object sql, int style) {
this.statement = statement;
this.sql = sql;
this.style = style;
closed = false;
}
/**
* Constructor PyStatement
*
* @param statement
* @param procedure
*/
public PyStatement(Statement statement, Procedure procedure) {
this(statement, procedure, STATEMENT_CALLABLE);
}
@Override
public PyUnicode __unicode__() {
if (sql instanceof String) {
return Py.newUnicode((String) sql);
} else if (sql instanceof Procedure) {
try {
return Py.newUnicode(((Procedure) sql).toSql());
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
return super.__unicode__();
}
@Override
public PyString __str__() {
return Py.newString(__unicode__().encode(codecs.getDefaultEncoding(), "replace"));
}
@Override
public String toString() {
return String.format("<PyStatement object at %s for [%s]", Py.idstr(this), __unicode__());
}
/**
* Gets the value of the attribute name.
*
* @param name
* @return the attribute for the given name
*/
@Override
public PyObject __findattr_ex__(String name) {
if ("style".equals(name)) {
return Py.newInteger(style);
} else if ("closed".equals(name)) {
return Py.newBoolean(closed);
} else if ("__statement__".equals(name)) {
return Py.java2py(statement);
} else if ("__methods__".equals(name)) {
return __methods__;
} else if ("__members__".equals(name)) {
return __members__;
}
return super.__findattr_ex__(name);
}
/**
* Initializes the object's namespace.
*
* @param dict
*/
static public void classDictInit(PyObject dict) {
// hide from python
dict.__setitem__("classDictInit", null);
dict.__setitem__("statement", null);
dict.__setitem__("execute", null);
dict.__setitem__("prepare", null);
dict.__setitem__("STATEMENT_STATIC", null);
dict.__setitem__("STATEMENT_PREPARED", null);
dict.__setitem__("STATEMENT_CALLABLE", null);
}
/**
* Delete the statement.
*/
public void __del__() {
close();
}
/**
* Method execute
*
* @param cursor
* @param params
* @param bindings
* @throws SQLException
*/
public void execute(PyCursor cursor, PyObject params, PyObject bindings) throws SQLException {
if (closed) {
throw zxJDBC.makeException(zxJDBC.ProgrammingError, "statement is closed");
}
prepare(cursor, params, bindings);
Fetch fetch = cursor.fetch;
switch (style) {
case STATEMENT_STATIC:
if (statement.execute((String) sql)) {
fetch.add(statement.getResultSet());
}
break;
case STATEMENT_PREPARED:
final PreparedStatement preparedStatement = (PreparedStatement) statement;
if (preparedStatement.execute()) {
fetch.add(preparedStatement.getResultSet());
}
break;
case STATEMENT_CALLABLE:
final CallableStatement callableStatement = (CallableStatement) statement;
if (callableStatement.execute()) {
fetch.add(callableStatement.getResultSet());
}
fetch.add(callableStatement, (Procedure) sql, params);
break;
default:
throw zxJDBC.makeException(zxJDBC.ProgrammingError,
zxJDBC.getString("invalidStyle"));
}
}
/**
* Method prepare
*
* @param cursor
* @param params
* @param bindings
* @throws SQLException
*/
private void prepare(PyCursor cursor, PyObject params, PyObject bindings) throws SQLException {
if (params == Py.None || style == STATEMENT_STATIC) {
return;
}
// [3, 4] or (3, 4)
final DataHandler datahandler = cursor.datahandler;
int columns = 0, column = 0, index = params.__len__();
final PreparedStatement preparedStatement = (PreparedStatement) statement;
final Procedure procedure = style == STATEMENT_CALLABLE ? (Procedure) sql : null;
if (style != STATEMENT_CALLABLE) {
columns = params.__len__();
// clear the statement so all new bindings take affect only if not a callproc
// this is because Procedure already registered the OUT parameters and we
// don't want to lose those
preparedStatement.clearParameters();
} else {
columns = procedure.columns == Py.None ? 0 : procedure.columns.__len__();
}
// count backwards through all the columns
while (columns-- > 0) {
column = columns + 1;
if (procedure != null && !procedure.isInput(column)) {
continue;
}
// working from right to left
PyObject param = params.__getitem__(--index);
if (bindings != Py.None) {
PyObject binding = bindings.__finditem__(Py.newInteger(index));
if (binding != null) {
try {
int bindingValue = binding.asInt();
datahandler.setJDBCObject(preparedStatement, column, param, bindingValue);
} catch (PyException e) {
throw zxJDBC.makeException(zxJDBC.ProgrammingError,
zxJDBC.getString("bindingValue"));
}
continue;
}
}
datahandler.setJDBCObject(preparedStatement, column, param);
}
}
/**
* Method close
*/
public void close() {
try {
statement.close();
} catch (SQLException e) {
throw zxJDBC.makeException(e);
} finally {
closed = true;
}
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
if (sql != null) {
if (sql instanceof PyObject) {
int retVal = visit.visit((PyObject) sql, arg);
if (retVal != 0) {
return retVal;
}
} else if (sql instanceof Traverseproc) {
int retVal = ((Traverseproc) sql).traverse(visit, arg);
if (retVal != 0) {
return retVal;
}
}
}
return 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
if (sql == null || ob == null) {
return false;
} else if (sql instanceof PyObject) {
return sql == ob;
} else if (sql instanceof Traverseproc) {
return ((Traverseproc) sql).refersDirectlyTo(ob);
} else{
return false;
}
}
}