-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathzxJDBC.java
More file actions
496 lines (450 loc) · 17.2 KB
/
zxJDBC.java
File metadata and controls
496 lines (450 loc) · 17.2 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
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql;
import org.python.core.ClassDictInit;
import org.python.core.Options;
import org.python.core.Py;
import org.python.core.PyArray;
import org.python.core.PyBuiltinFunctionSet;
import org.python.core.PyDictionary;
import org.python.core.PyException;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyStringMap;
import org.python.core.Untraversable;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
/**
* Creates database connections.
* <p>
* <pre>
* from com.ziclix.python.sql import zxJDBC
* db = zxJDBC.connect("jdbc:mysql://localhost:3306/MySql", None, None, "org.gjt.mm.mysql.Driver")
* </pre>
*
* @author brian zimmer
*/
@Untraversable
public class zxJDBC extends PyObject implements ClassDictInit {
/**
* Field Error
*/
public static PyObject Error = Py.None;
/**
* Field Warning
*/
public static PyObject Warning = Py.None;
/**
* Field InterfaceError
*/
public static PyObject InterfaceError = Py.None;
/**
* Field DatabaseError
*/
public static PyObject DatabaseError = Py.None;
/**
* Field InternalError
*/
public static PyObject InternalError = Py.None;
/**
* Field OperationalError
*/
public static PyObject OperationalError = Py.None;
/**
* Field ProgrammingError
*/
public static PyObject ProgrammingError = Py.None;
/**
* Field IntegrityError
*/
public static PyObject IntegrityError = Py.None;
/**
* Field DataError
*/
public static PyObject DataError = Py.None;
/**
* Field NotSupportedError
*/
public static PyObject NotSupportedError = Py.None;
/**
* The ResourceBundle with error messages and doc strings
*/
private static ResourceBundle resourceBundle = null;
/**
* Instance used to create date-like objects as per the API
*/
public static DateFactory datefactory = new JavaDateFactory();
static {
try {
resourceBundle =
ResourceBundle.getBundle("com.ziclix.python.sql.resource.zxJDBCMessages");
} catch (MissingResourceException e) {
throw new RuntimeException("missing zxjdbc resource bundle");
}
}
/**
* Initializes the module.
*
* @param dict
*/
public static void classDictInit(PyObject dict) {
dict.__setitem__("apilevel", new PyString("2.0"));
dict.__setitem__("threadsafety", new PyInteger(1));
dict.__setitem__("paramstyle", new PyString("qmark"));
dict.__setitem__("Date", new zxJDBCFunc("Date", 1, 3, 3,
"construct a Date from year, month, day"));
dict.__setitem__("Time", new zxJDBCFunc("Time", 2, 3, 3,
"construct a Date from hour, minute, second"));
dict.__setitem__("Timestamp",
new zxJDBCFunc("Timestamp", 3, 6, 6,
"construct a Timestamp from year, month, day, hour, "
+ "minute, second"));
dict.__setitem__("DateFromTicks",
new zxJDBCFunc("DateFromTicks", 4, 1, 1,
"construct a Date from seconds since the epoch"));
dict.__setitem__("TimeFromTicks",
new zxJDBCFunc("TimeFromTicks", 5, 1, 1,
"construct a Time from seconds since the epoch"));
dict.__setitem__("TimestampFromTicks",
new zxJDBCFunc("TimestampFromTicks", 6, 1, 1,
"construct a Timestamp from seconds since the epoch"));
dict.__setitem__("Binary",
new zxJDBCFunc("Binary", 7, 1, 1,
"construct an object capable of holding binary data"));
zxJDBC._addSqlTypes(dict);
zxJDBC._addConnectors(dict);
zxJDBC._buildExceptions(dict);
// hide from python
dict.__setitem__("initModule", null);
dict.__setitem__("toString", null);
dict.__setitem__("getPyClass", null);
dict.__setitem__("classDictInit", null);
dict.__setitem__("_addSqlTypes", null);
dict.__setitem__("_addConnectors", null);
dict.__setitem__("_buildExceptions", null);
dict.__setitem__("buildClass", null);
dict.__setitem__("createExceptionMessage", null);
dict.__setitem__("resourceBundle", null);
dict.__setitem__("getString", null);
dict.__setitem__("makeException", null);
}
/**
* Add the types from java.sql.Types
*
* @param dict
* @throws PyException
*/
protected static void _addSqlTypes(PyObject dict) throws PyException {
PyDictionary sqltype = new PyDictionary();
dict.__setitem__("sqltype", sqltype);
try {
Class<?> c = Class.forName("java.sql.Types");
Field[] fields = c.getFields();
for (Field f : fields) {
PyString name = Py.newString(f.getName());
PyObject value = new DBApiType(f.getInt(c));
dict.__setitem__(name, value);
sqltype.__setitem__(value, name);
}
c = Class.forName("java.sql.ResultSet");
fields = c.getFields();
for (Field f : fields) {
PyString name = Py.newString(f.getName());
PyObject value = Py.newInteger(f.getInt(c));
dict.__setitem__(name, value);
}
} catch (Throwable t) {
throw makeException(t);
}
dict.__setitem__("ROWID", dict.__getitem__(Py.newString("OTHER")));
dict.__setitem__("NUMBER", dict.__getitem__(Py.newString("NUMERIC")));
dict.__setitem__("STRING", dict.__getitem__(Py.newString("VARCHAR")));
dict.__setitem__("DATETIME", dict.__getitem__(Py.newString("TIMESTAMP")));
}
/**
* Add all the possible connectors
*
* @param dict
* @throws PyException
*/
protected static void _addConnectors(PyObject dict) throws PyException {
PyObject connector = Py.None;
Properties props = new Properties();
props.put("connect", "com.ziclix.python.sql.connect.Connect");
props.put("lookup", "com.ziclix.python.sql.connect.Lookup");
props.put("connectx", "com.ziclix.python.sql.connect.Connectx");
Enumeration<?> names = props.propertyNames();
while (names.hasMoreElements()) {
String name = ((String) names.nextElement()).trim();
String className = props.getProperty(name).trim();
try {
connector =
(PyObject) Class.forName(className).getDeclaredConstructor().newInstance();
dict.__setitem__(name, connector);
Py.writeComment("zxJDBC", "loaded connector [" + className + "] as [" + name
+ "]");
} catch (Throwable t) {
Py.writeComment("zxJDBC", "failed to load connector [" + name
+ "] using class [" + className + "]");
}
}
}
/**
* Create the exception classes and get their descriptions from the resource bundle.
*
* @param dict
*/
protected static void _buildExceptions(PyObject dict) {
Error = buildClass("Error", Py.StandardError);
Warning = buildClass("Warning", Py.StandardError);
InterfaceError = buildClass("InterfaceError", Error);
DatabaseError = buildClass("DatabaseError", Error);
InternalError = buildClass("InternalError", DatabaseError);
OperationalError = buildClass("OperationalError", DatabaseError);
ProgrammingError = buildClass("ProgrammingError", DatabaseError);
IntegrityError = buildClass("IntegrityError", DatabaseError);
DataError = buildClass("DataError", DatabaseError);
NotSupportedError = buildClass("NotSupportedError", DatabaseError);
}
/**
* Return the string associated with the key for the default resource bundle. It
* first checks for 'key.N' where N starts at 0 and increments by one. If any indexed
* key is found, the results of all the indexed values are concatenated with the line
* separator. If no indexed key is found, it defaults to checking the bundle by the
* key value alone.
*
* @param key
* @return String
*/
public static String getString(String key) {
int i = 0;
List<String> lines = null;
String resource = null;
while (true) {
try {
resource = resourceBundle.getString(key + "." + (i++));
if (lines == null) {
lines = new ArrayList<String>();
}
lines.add(resource);
} catch (MissingResourceException e) {
break;
}
}
if (lines == null || lines.size() == 0) {
try {
resource = resourceBundle.getString(key);
} catch (MissingResourceException e) {
return key;
}
} else {
String sep = System.getProperty("line.separator");
StringBuffer sb = new StringBuffer();
for (i = 0; i < lines.size() - 1; i++) {
sb.append(lines.get(i)).append(sep);
}
sb.append(lines.get(lines.size() - 1));
resource = sb.toString();
}
return resource;
}
/**
* Return a formatted string. The key is used to get the format and the values
* are passed, along with the format, to a MessageFormat who formats it appropriately.
*
* @param key
* @param values
* @return String
*/
public static String getString(String key, Object[] values) {
String format = getString(key);
return MessageFormat.format(format, values);
}
/**
* Return a newly instantiated PyException of the type Error.
*
* @param msg
* @return PyException
*/
public static PyException makeException(String msg) {
return makeException(Error, msg);
}
/**
* Return a newly instantiated PyException of the given type.
*
* @param type
* @param msg
* @return PyException
*/
public static PyException makeException(PyObject type, String msg) {
return Py.makeException(type, msg == null ? Py.EmptyString : Py.newStringOrUnicode(msg));
}
/**
* Return a newly instantiated PyException of the type Error.
*
* @param throwable
* @return PyException
*/
public static PyException makeException(Throwable throwable) {
PyObject type = Error;
if (throwable instanceof SQLException) {
String state = ((SQLException)throwable).getSQLState();
// The SQL standard is not freely available, but
// http://www.postgresql.org/docs/current/static/errcodes-appendix.html
// contains most of the SQLSTATES codes.
// Otherwise, the state is not following the standard.
if (state != null && state.length() == 5) {
if (state.startsWith("23")) {
// Class 23 => Integrity Constraint Violation
type = IntegrityError;
} else if (state.equals("40002")) {
// 40002 => TRANSACTION INTEGRITY CONSTRAINT VIOLATION
type = IntegrityError;
}
}
}
return makeException(type, throwable);
}
/**
* Return a newly instantiated PyException of the given type.
*
* @param type
* @param t
* @return PyException
*/
public static PyException makeException(PyObject type, Throwable t) {
return makeException(type, t, -1);
}
/**
* Return a newly instantiated PyException of the given type.
*
* @param type
* @param t
* @param rowIndex Row index where the error has happened. Useful for diagnosing.
* @return PyException
*/
public static PyException makeException(PyObject type, Throwable t, int rowIndex) {
if (Options.showJavaExceptions) {
CharArrayWriter buf = new CharArrayWriter();
PrintWriter writer = new PrintWriter(buf);
writer.println("Java Traceback:");
if (t instanceof PyException) {
((PyException) t).super__printStackTrace(writer);
} else {
t.printStackTrace(writer);
}
Py.stderr.print(buf.toString());
}
if (t instanceof PyException) {
return (PyException) t;
} else if (t instanceof SQLException) {
SQLException sqlException = (SQLException) t;
StringBuffer buffer = new StringBuffer();
do {
buffer.append(sqlException.getMessage());
buffer.append(" [SQLCode: " + sqlException.getErrorCode() + "]");
if (sqlException.getSQLState() != null) {
buffer.append(", [SQLState: " + sqlException.getSQLState() + "]");
}
if (rowIndex >= 0) {
buffer.append(", [Row number: " + rowIndex + "]");
}
sqlException = sqlException.getNextException();
if (sqlException != null) {
buffer.append(System.getProperty("line.separator"));
}
} while (sqlException != null);
return makeException(type, buffer.toString());
} else {
return makeException(type, t.getMessage());
}
}
/**
* Method buildClass
*
* @param classname
* @param superclass
* @return PyObject
*/
protected static PyObject buildClass(String classname, PyObject superclass) {
PyObject dict = new PyStringMap();
dict.__setitem__("__doc__", Py.newString(getString(classname)));
dict.__setitem__("__module__", Py.newString("zxJDBC"));
return Py.makeClass(classname, superclass, dict);
}
}
@Untraversable
class zxJDBCFunc extends PyBuiltinFunctionSet {
zxJDBCFunc(String name, int index, int minargs, int maxargs, String doc) {
super(name, index, minargs, maxargs, doc);
}
@Override
public PyObject __call__(PyObject arg) {
long ticks;
switch (index) {
case 4:
ticks = ((Number) arg.__tojava__(Number.class)).longValue();
return zxJDBC.datefactory.DateFromTicks(ticks);
case 5:
ticks = ((Number) arg.__tojava__(Number.class)).longValue();
return zxJDBC.datefactory.TimeFromTicks(ticks);
case 6:
ticks = ((Number) arg.__tojava__(Number.class)).longValue();
return zxJDBC.datefactory.TimestampFromTicks(ticks);
case 7:
if (arg instanceof PyString) {
arg = PyArray.TYPE.__call__(Py.newString("b"), arg);
}
return arg;
default :
throw info.unexpectedCall(1, false);
}
}
@Override
public PyObject __call__(PyObject arga, PyObject argb, PyObject argc) {
switch (index) {
case 1:
int year = ((Number) arga.__tojava__(Number.class)).intValue();
int month = ((Number) argb.__tojava__(Number.class)).intValue();
int day = ((Number) argc.__tojava__(Number.class)).intValue();
return zxJDBC.datefactory.Date(year, month, day);
case 2:
int hour = ((Number) arga.__tojava__(Number.class)).intValue();
int minute = ((Number) argb.__tojava__(Number.class)).intValue();
int second = ((Number) argc.__tojava__(Number.class)).intValue();
return zxJDBC.datefactory.Time(hour, minute, second);
default :
throw info.unexpectedCall(3, false);
}
}
@Override
public PyObject fancyCall(PyObject[] args) {
switch (index) {
case 3:
int year = ((Number) args[0].__tojava__(Number.class)).intValue();
int month = ((Number) args[1].__tojava__(Number.class)).intValue();
int day = ((Number) args[2].__tojava__(Number.class)).intValue();
int hour = ((Number) args[3].__tojava__(Number.class)).intValue();
int minute = ((Number) args[4].__tojava__(Number.class)).intValue();
int second = ((Number) args[5].__tojava__(Number.class)).intValue();
return zxJDBC.datefactory.Timestamp(year, month, day, hour, minute, second);
default :
throw info.unexpectedCall(args.length, false);
}
}
}