-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathProcedure.java
More file actions
387 lines (312 loc) · 10.7 KB
/
Procedure.java
File metadata and controls
387 lines (312 loc) · 10.7 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql;
import org.python.core.Py;
import org.python.core.PyInteger;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyString;
import java.sql.CallableStatement;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.BitSet;
/**
* This class provides the necessary functionality to call stored
* procedures. It handles managing the database metadata and binding
* the appropriate parameters.
*
* @author brian zimmer
*/
public class Procedure extends Object {
/**
* Field NAME
*/
protected static final int NAME = 3;
/**
* Field COLUMN_TYPE
*/
protected static final int COLUMN_TYPE = 4;
/**
* Field DATA_TYPE
*/
protected static final int DATA_TYPE = 5;
/**
* Field DATA_TYPE_NAME
*/
protected static final int DATA_TYPE_NAME = 6;
/**
* Field PRECISION
*/
protected static final int PRECISION = 7;
/**
* Field LENGTH
*/
protected static final int LENGTH = 8;
/**
* Field SCALE
*/
protected static final int SCALE = 9;
/**
* Field NULLABLE
*/
protected static final int NULLABLE = 11;
/**
* Field cursor
*/
protected PyCursor cursor;
/**
* Field columns
*/
protected PyObject columns;
/**
* Field procedureCatalog
*/
protected PyObject procedureCatalog;
/**
* Field procedureSchema
*/
protected PyObject procedureSchema;
/**
* Field procedureName
*/
protected PyObject procedureName;
/**
* Field inputSet
*/
protected BitSet inputSet;
/**
* Constructor Procedure
*
* @param cursor cursor an open cursor
* @param name name a string or tuple representing the name
* @throws SQLException
*/
public Procedure(PyCursor cursor, PyObject name) throws SQLException {
this.cursor = cursor;
this.inputSet = new BitSet();
if (name instanceof PyString) {
this.procedureCatalog = getDefault();
this.procedureSchema = getDefault();
this.procedureName = name;
} else if (PyCursor.isSeq(name)) {
if (name.__len__() == 3) {
this.procedureCatalog = name.__getitem__(0);
this.procedureSchema = name.__getitem__(1);
this.procedureName = name.__getitem__(2);
} else {
// throw an exception
}
} else {
// throw an exception
}
fetchColumns();
}
/**
* Prepares the statement and registers the OUT/INOUT parameters (if any).
*
* @return CallableStatement
* @throws SQLException
*/
public CallableStatement prepareCall() throws SQLException {
return prepareCall(Py.None, Py.None);
}
/**
* Prepares the statement and registers the OUT/INOUT parameters (if any).
*
* @param rsType the value of to be created ResultSet type
* @param rsConcur the value of the to be created ResultSet concurrency
* @return CallableStatement
* @throws SQLException
*/
public CallableStatement prepareCall(PyObject rsType, PyObject rsConcur) throws SQLException {
// prepare the statement
CallableStatement statement = null;
boolean normal = ((rsType == Py.None) && (rsConcur == Py.None));
try {
// build the full call syntax
String sqlString = toSql();
if (normal) {
statement = cursor.connection.connection.prepareCall(sqlString);
} else {
int t = rsType.asInt();
int c = rsConcur.asInt();
statement = cursor.connection.connection.prepareCall(sqlString, t, c);
}
// prepare the OUT parameters
registerOutParameters(statement);
} catch (SQLException e) {
if (statement != null) {
try {
statement.close();
} catch (Exception ex) {
}
}
throw e;
}
return statement;
}
/**
* Prepare the binding dictionary with the correct datatypes.
*
* @param params a non-None list of params
* @param bindings a dictionary of bindings
*/
public void normalizeInput(PyObject params, PyObject bindings) throws SQLException {
if (this.columns == Py.None) {
return;
}
// do nothing with params at the moment
for (int i = 0, len = this.columns.__len__(), binding = 0; i < len; i++) {
PyObject column = this.columns.__getitem__(i);
int colType = column.__getitem__(COLUMN_TYPE).asInt();
switch (colType) {
case DatabaseMetaData.procedureColumnIn:
case DatabaseMetaData.procedureColumnInOut:
// bindings are Python-indexed
PyInteger key = Py.newInteger(binding++);
if (bindings.__finditem__(key) == null) {
int dataType = column.__getitem__(DATA_TYPE).asInt();
bindings.__setitem__(key, Py.newInteger(dataType));
}
// inputs are JDBC-indexed
this.inputSet.set(i + 1);
break;
}
}
}
/**
* This method determines whether the param at the specified index is an
* IN or INOUT param for a stored procedure. This is only configured properly
* AFTER a call to normalizeInput().
*
* @param index JDBC indexed column index (1, 2, ...)
* @return true if the column is an input, false otherwise
* @throws SQLException
*/
public boolean isInput(int index) throws SQLException {
return this.inputSet.get(index);
}
/**
* Returns the call in the syntax: <pre>{@literal
* {? = call <procedure-name>(?, ?, ...)}
* {call <procedure-name>(?, ?, ...)}
* }</pre>
*
* As of now, all parameters variables are created and no support for named variable calling is
* supported.
*
* @return String
*/
public String toSql() throws SQLException {
int colParam = 0;
int colReturn = 0;
if (this.columns != Py.None) {
for (int i = 0, len = this.columns.__len__(); i < len; i++) {
PyObject column = this.columns.__getitem__(i);
int colType = column.__getitem__(COLUMN_TYPE).asInt();
switch (colType) {
case DatabaseMetaData.procedureColumnUnknown:
throw zxJDBC.makeException(zxJDBC.NotSupportedError, "procedureColumnUnknown");
case DatabaseMetaData.procedureColumnResult:
throw zxJDBC.makeException(zxJDBC.NotSupportedError, "procedureColumnResult");
// these go on the right hand side
case DatabaseMetaData.procedureColumnIn:
case DatabaseMetaData.procedureColumnInOut:
case DatabaseMetaData.procedureColumnOut:
colParam++;
break;
// these go on the left hand side
case DatabaseMetaData.procedureColumnReturn:
colReturn++;
break;
default :
throw zxJDBC.makeException(zxJDBC.DataError, "unknown column type [" + colType + "]");
}
}
}
StringBuffer sql = new StringBuffer("{");
if (colReturn > 0) {
PyList list = new PyList();
for (; colReturn > 0; colReturn--) {
list.append(Py.newString("?"));
}
sql.append(Py.newString(",").join(list)).append(" = ");
}
String name = this.getProcedureName();
sql.append("call ").append(name).append("(");
if (colParam > 0) {
PyList list = new PyList();
for (; colParam > 0; colParam--) {
list.append(Py.newString("?"));
}
sql.append(Py.newString(",").join(list));
}
return sql.append(")}").toString();
}
/**
* Registers the OUT/INOUT parameters of the statement.
*
* @param statement statement
* @throws SQLException
*/
protected void registerOutParameters(CallableStatement statement) throws SQLException {
if (this.columns == Py.None) {
return;
}
for (int i = 0, len = this.columns.__len__(); i < len; i++) {
PyObject column = this.columns.__getitem__(i);
int colType = column.__getitem__(COLUMN_TYPE).asInt();
int dataType = column.__getitem__(DATA_TYPE).asInt();
String dataTypeName = column.__getitem__(DATA_TYPE_NAME).toString();
switch (colType) {
case DatabaseMetaData.procedureColumnInOut:
case DatabaseMetaData.procedureColumnOut:
case DatabaseMetaData.procedureColumnReturn:
cursor.datahandler.registerOut(statement, i + 1, colType, dataType, dataTypeName);
break;
}
}
}
/**
* Get the columns for the stored procedure.
*
* @throws SQLException
*/
protected void fetchColumns() throws SQLException {
PyExtendedCursor pec = (PyExtendedCursor) cursor.connection.cursor();
try {
pec.datahandler = this.cursor.datahandler;
pec.procedurecolumns(procedureCatalog, procedureSchema, procedureName, Py.None);
this.columns = pec.fetchall();
} finally {
pec.close();
}
}
/**
* The value for a missing schema or catalog. This value is used to find
* the column names for the procedure. Not all DBMS use the same default
* value; for instance Oracle uses an empty string and SQLServer a null.
* This implementation returns the empty string.
*
* @return the default value (the empty string)
* @see java.sql.DatabaseMetaData#getProcedureColumns
*/
protected PyObject getDefault() {
return Py.EmptyString;
}
/**
* Construct a procedure name for the relevant schema and catalog information.
*/
protected String getProcedureName() {
StringBuffer proc = new StringBuffer();
if (this.procedureCatalog.__nonzero__()) {
proc.append(this.procedureCatalog.toString()).append(".");
}
return proc.append(this.procedureName.toString()).toString();
}
}