-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathBaseDB.java
More file actions
102 lines (82 loc) · 2.68 KB
/
BaseDB.java
File metadata and controls
102 lines (82 loc) · 2.68 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
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql.pipe.db;
import com.ziclix.python.sql.DataHandler;
import com.ziclix.python.sql.PyConnection;
import com.ziclix.python.sql.PyCursor;
import com.ziclix.python.sql.zxJDBC;
import org.python.core.Py;
import java.lang.reflect.Constructor;
/**
* Abstract class to assist in generating cursors.
*
* @author brian zimmer
*/
public abstract class BaseDB {
/**
* Field cursor
*/
protected PyCursor cursor;
/**
* Field dataHandler
*/
protected Class dataHandler;
/**
* Field tableName
*/
protected String tableName;
/**
* Field connection
*/
protected PyConnection connection;
/**
* Construct the helper.
*/
public BaseDB(PyConnection connection, Class dataHandler, String tableName) {
this.tableName = tableName;
this.dataHandler = dataHandler;
this.connection = connection;
this.cursor = this.cursor();
}
/**
* Create a new constructor and optionally bind a new DataHandler. The new DataHandler must act as
* a Decorator, having a single argument constructor of another DataHandler. The new DataHandler is
* then expected to delegate all calls to the original while enhancing the functionality in any matter
* desired. This allows additional functionality without losing any previous work or requiring any
* complicated inheritance dependencies.
*/
protected PyCursor cursor() {
PyCursor cursor = this.connection.cursor(true);
DataHandler origDataHandler = cursor.getDataHandler(), newDataHandler = null;
if ((origDataHandler != null) && (this.dataHandler != null)) {
Constructor cons = null;
try {
Class[] args = new Class[1];
args[0] = DataHandler.class;
cons = this.dataHandler.getConstructor(args);
} catch (Exception e) {
return cursor;
}
if (cons == null) {
String msg = zxJDBC.getString("invalidCons", new Object[]{this.dataHandler.getName()});
throw zxJDBC.makeException(msg);
}
try {
Object[] args = new Object[1];
args[0] = origDataHandler;
newDataHandler = (DataHandler) cons.newInstance(args);
} catch (Exception e) {
return cursor;
}
if (newDataHandler != null) {
cursor.__setattr__("datahandler", Py.java2py(newDataHandler));
}
}
return cursor;
}
}