-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyExtendedCursor.java
More file actions
568 lines (484 loc) · 16.9 KB
/
PyExtendedCursor.java
File metadata and controls
568 lines (484 loc) · 16.9 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import org.python.core.Py;
import org.python.core.PyBuiltinMethodSet;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyString;
/**
* A cursor with extensions to the DB API 2.0.
*
* @author brian zimmer
*/
public class PyExtendedCursor extends PyCursor {
/**
* Field __members__
*/
protected static PyList __members__;
/**
* Field __methods__
*/
protected static PyList __methods__;
static {
PyObject[] m = new PyObject[9];
m[0] = new PyString("tables");
m[1] = new PyString("columns");
m[2] = new PyString("primarykeys");
m[3] = new PyString("foreignkeys");
m[4] = new PyString("procedures");
m[5] = new PyString("procedurecolumns");
m[6] = new PyString("statistics");
m[7] = new PyString("bestrow");
m[8] = new PyString("versioncolumns");
__methods__ = new PyList(m);
__methods__.extend(PyCursor.__methods__);
m = new PyObject[0];
__members__ = new PyList(m);
__members__.extend(PyCursor.__members__);
}
/**
* Constructor PyExtendedCursor
*
* @param connection
*/
PyExtendedCursor(PyConnection connection) {
super(connection);
}
/**
* Constructor PyExtendedCursor
*
* @param connection
* @param dynamicFetch
*/
PyExtendedCursor(PyConnection connection, boolean dynamicFetch) {
super(connection, dynamicFetch);
}
/**
* Constructor PyExtendedCursor
*
* @param connection
* @param dynamicFetch
* @param rsType
* @param rsConcur
*/
PyExtendedCursor(PyConnection connection, boolean dynamicFetch, PyObject rsType,
PyObject rsConcur) {
super(connection, dynamicFetch, rsType, rsConcur);
}
/**
* String representation of the object.
*
* @return a string representation of the object.
*/
@Override
public String toString() {
return "<PyExtendedCursor object instance at " + Py.id(this) + ">";
}
/**
* Initializes the module.
*
* @param dict
*/
static public void classDictInit(PyObject dict) {
PyCursor.classDictInit(dict);
dict.__setitem__("tables", new ExtendedCursorFunc("tables", 100, 4, 4, "query for table information"));
dict.__setitem__("columns", new ExtendedCursorFunc("columns", 101, 4, 4, "query for column information"));
dict.__setitem__("primarykeys", new ExtendedCursorFunc("primarykeys", 102, 3, 3, "query for primary keys"));
dict.__setitem__("foreignkeys", new ExtendedCursorFunc("foreignkeys", 103, 6, 6, "query for foreign keys"));
dict.__setitem__("procedures", new ExtendedCursorFunc("procedures", 104, 3, 3, "query for procedures"));
dict.__setitem__("procedurecolumns", new ExtendedCursorFunc("procedurecolumns", 105, 4, 4, "query for procedures columns"));
dict.__setitem__("statistics", new ExtendedCursorFunc("statistics", 106, 5, 5, "description of a table's indices and statistics"));
dict.__setitem__("gettypeinfo", new ExtendedCursorFunc("gettypeinfo", 107, 0, 1, "query for sql type info"));
dict.__setitem__("gettabletypeinfo", new ExtendedCursorFunc("gettabletypeinfo", 108, 0, 1, "query for table types"));
dict.__setitem__("bestrow", new ExtendedCursorFunc("bestrow", 109, 3, 3, "optimal set of columns that uniquely identifies a row"));
dict.__setitem__("versioncolumns", new ExtendedCursorFunc("versioncolumns", 110, 3, 3, "columns that are automatically updated when any value in a row is updated"));
// hide from python
dict.__setitem__("classDictInit", null);
dict.__setitem__("toString", null);
}
/**
* Finds the attribute.
*
* @param name the name of the attribute of interest
* @return the value for the attribute of the specified name
*/
@Override
public PyObject __findattr_ex__(String name) {
if ("__methods__".equals(name)) {
return __methods__;
} else if ("__members__".equals(name)) {
return __members__;
}
return super.__findattr_ex__(name);
}
/**
* Only table descriptions matching the catalog, schema, table name and type
* criteria are returned. They are ordered by TABLE_TYPE, TABLE_SCHEM and
* TABLE_NAME.
*
* @param qualifier
* @param owner
* @param table
* @param type
*/
protected void tables(PyObject qualifier, PyObject owner, PyObject table, PyObject type) {
clear();
String q = getMetaDataName(qualifier);
String o = getMetaDataName(owner);
String t = getMetaDataName(table);
String[] y = null;
// postgresql interprets the types to be uppercase exclusively
// so we'll force this on everyone else as well
if (type != Py.None) {
String typeName = null;
if (isSeq(type)) {
int len = type.__len__();
y = new String[len];
for (int i = 0; i < len; i++) {
typeName = getMetaDataName(type.__getitem__(i));
y[i] = (typeName == null) ? null : typeName.toUpperCase();
}
} else {
typeName = getMetaDataName(type.__getitem__(type));
y = new String[]{(typeName == null) ? null : typeName.toUpperCase()};
}
}
try {
this.fetch.add(getMetaData().getTables(q, o, t, y));
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Returns the columns for a table.
*
* @param qualifier
* @param owner
* @param table
* @param column
*/
protected void columns(PyObject qualifier, PyObject owner, PyObject table, PyObject column) {
clear();
String q = getMetaDataName(qualifier);
String o = getMetaDataName(owner);
String t = getMetaDataName(table);
String c = getMetaDataName(column);
try {
this.fetch.add(getMetaData().getColumns(q, o, t, c));
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Gets a description of the stored procedures available for the qualifier and owner.
*
* @param qualifier
* @param owner
* @param procedure
*/
protected void procedures(PyObject qualifier, PyObject owner, PyObject procedure) {
clear();
String q = getMetaDataName(qualifier);
String o = getMetaDataName(owner);
String p = getMetaDataName(procedure);
try {
this.fetch.add(getMetaData().getProcedures(q, o, p));
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Gets the columns for the procedure.
*
* @param qualifier
* @param owner
* @param procedure
* @param column
*/
protected void procedurecolumns(PyObject qualifier, PyObject owner, PyObject procedure,
PyObject column) {
clear();
String q = getMetaDataName(qualifier);
String o = getMetaDataName(owner);
String p = getMetaDataName(procedure);
String c = getMetaDataName(column);
try {
this.fetch.add(getMetaData().getProcedureColumns(q, o, p, c));
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Gets a description of a table's primary key columns. They are ordered by
* COLUMN_NAME.
*
* @param qualifier a schema name
* @param owner an owner name
* @param table a table name
*/
protected void primarykeys(PyObject qualifier, PyObject owner, PyObject table) {
clear();
String q = getMetaDataName(qualifier);
String o = getMetaDataName(owner);
String t = getMetaDataName(table);
try {
this.fetch.add(getMetaData().getPrimaryKeys(q, o, t));
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Gets a description of the foreign key columns in the foreign key table
* that reference the primary key columns of the primary key table (describe
* how one table imports another's key.) This should normally return a single
* foreign key/primary key pair (most tables only import a foreign key from a
* table once.) They are ordered by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME,
* and KEY_SEQ.
*
* @param primaryQualifier
* @param primaryOwner
* @param primaryTable
* @param foreignQualifier
* @param foreignOwner
* @param foreignTable
*/
protected void foreignkeys(PyObject primaryQualifier, PyObject primaryOwner,
PyObject primaryTable, PyObject foreignQualifier,
PyObject foreignOwner, PyObject foreignTable) {
clear();
String pq = getMetaDataName(primaryQualifier);
String po = getMetaDataName(primaryOwner);
String pt = getMetaDataName(primaryTable);
String fq = getMetaDataName(foreignQualifier);
String fo = getMetaDataName(foreignOwner);
String ft = getMetaDataName(foreignTable);
try {
this.fetch.add(getMetaData().getCrossReference(pq, po, pt, fq, fo, ft));
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Gets a description of a table's indices and statistics. They are ordered by
* NON_UNIQUE, TYPE, INDEX_NAME, and ORDINAL_POSITION.
*
* @param qualifier
* @param owner
* @param table
* @param unique
* @param accuracy
*/
protected void statistics(PyObject qualifier, PyObject owner, PyObject table, PyObject unique,
PyObject accuracy) {
clear();
Set<Integer> skipCols = new HashSet<Integer>();
skipCols.add(12);
String q = getMetaDataName(qualifier);
String o = getMetaDataName(owner);
String t = getMetaDataName(table);
boolean u = unique.__nonzero__();
boolean a = accuracy.__nonzero__();
try {
this.fetch.add(getMetaData().getIndexInfo(q, o, t, u, a), skipCols);
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Gets a description of the type information for a given type.
*
* @param type data type for which to provide information
*/
protected void typeinfo(PyObject type) {
clear();
Set<Integer> skipCols = new HashSet<Integer>();
skipCols.add(16);
skipCols.add(17);
try {
this.fetch.add(getMetaData().getTypeInfo(), skipCols);
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
// if the type is non-null, then trim the result list down to that type only
// if(type != null &&!Py.None.equals(type)) {
// for(int i = 0; i < ((PyObject) this.results.get(0)).__len__(); i++) {
// PyObject row = ((PyObject) this.results.get(0)).__getitem__(new PyInteger(i));
// PyObject sqlType = row.__getitem__(new PyInteger(1));
// if(type.equals(sqlType)) {
// this.results.remove(0);
// this.results.add(0, new PyList(new PyObject[] {
// row
// }));
// }
// }
// }
}
/**
* Gets a description of possible table types.
*/
protected void tabletypeinfo() {
clear();
try {
this.fetch.add(getMetaData().getTableTypes());
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Gets a description of a table's optimal set of columns that uniquely
* identifies a row. They are ordered by SCOPE.
*
* @param qualifier
* @param owner
* @param table
*/
protected void bestrow(PyObject qualifier, PyObject owner, PyObject table) {
clear();
String c = getMetaDataName(qualifier);
String s = getMetaDataName(owner);
String t = getMetaDataName(table);
int p = DatabaseMetaData.bestRowSession; // scope
boolean n = true; // nullable
try {
this.fetch.add(getMetaData().getBestRowIdentifier(c, s, t, p, n));
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Gets a description of a table's columns that are automatically
* updated when any value in a row is updated. They are unordered.
*
* @param qualifier a schema name
* @param owner an owner name
* @param table a table name
*/
protected void versioncolumns(PyObject qualifier, PyObject owner, PyObject table) {
clear();
String q = getMetaDataName(qualifier);
String o = getMetaDataName(owner);
String t = getMetaDataName(table);
try {
this.fetch.add(getMetaData().getVersionColumns(q, o, t));
} catch (SQLException e) {
throw zxJDBC.makeException(e);
}
}
/**
* Method getMetaDataName
*
* @param name
* @return String
*/
protected String getMetaDataName(PyObject name) {
if (name == Py.None) {
return null;
}
String string = name.__str__().toString();
// see if the driver can help us
try {
if (getMetaData().storesLowerCaseIdentifiers()) {
return string.toLowerCase();
} else if (getMetaData().storesUpperCaseIdentifiers()) {
return string.toUpperCase();
}
} catch (SQLException e) {
}
// well we don't know yet so give it to the datahandler
return datahandler.getMetaDataName(name);
}
}
class ExtendedCursorFunc extends PyBuiltinMethodSet {
ExtendedCursorFunc(String name, int index, int argcount, String doc) {
this(name, index, argcount, argcount, doc);
}
ExtendedCursorFunc(String name, int index, int minargs, int maxargs, String doc) {
super(name, index, minargs, maxargs, doc, PyExtendedCursor.class);
}
@Override
public PyObject __call__() {
PyExtendedCursor cursor = (PyExtendedCursor) __self__;
switch (index) {
case 107:
cursor.typeinfo(Py.None);
return Py.None;
case 108:
cursor.tabletypeinfo();
return Py.None;
default :
throw info.unexpectedCall(0, false);
}
}
@Override
public PyObject __call__(PyObject arga) {
PyExtendedCursor cursor = (PyExtendedCursor) __self__;
switch (index) {
case 107:
cursor.typeinfo(arga);
return Py.None;
default :
throw info.unexpectedCall(1, false);
}
}
@Override
public PyObject __call__(PyObject arga, PyObject argb, PyObject argc) {
PyExtendedCursor cursor = (PyExtendedCursor) __self__;
switch (index) {
case 102:
cursor.primarykeys(arga, argb, argc);
return Py.None;
case 104:
cursor.procedures(arga, argb, argc);
return Py.None;
case 109:
cursor.bestrow(arga, argb, argc);
return Py.None;
case 110:
cursor.versioncolumns(arga, argb, argc);
return Py.None;
default:
throw info.unexpectedCall(3, false);
}
}
@Override
public PyObject fancyCall(PyObject[] args) {
PyExtendedCursor cursor = (PyExtendedCursor) __self__;
switch (index) {
case 103:
cursor.foreignkeys(args[0], args[1], args[2], args[3], args[4], args[5]);
return Py.None;
case 106:
cursor.statistics(args[0], args[1], args[2], args[3], args[4]);
return Py.None;
default:
throw info.unexpectedCall(args.length, true);
}
}
@Override
public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3, PyObject arg4) {
PyExtendedCursor cursor = (PyExtendedCursor) __self__;
switch (index) {
case 100:
cursor.tables(arg1, arg2, arg3, arg4);
return Py.None;
case 101:
cursor.columns(arg1, arg2, arg3, arg4);
return Py.None;
case 105:
cursor.procedurecolumns(arg1, arg2, arg3, arg4);
return Py.None;
default:
throw info.unexpectedCall(4, false);
}
}
}