-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathDBSource.java
More file actions
144 lines (112 loc) · 3.89 KB
/
DBSource.java
File metadata and controls
144 lines (112 loc) · 3.89 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
/*
* 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.PyConnection;
import com.ziclix.python.sql.pipe.Source;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyTuple;
/**
* A database source. Given a PyConnection and information about the query, produce the data.
*
* @author brian zimmer
*/
public class DBSource extends BaseDB implements Source {
/**
* Field sql
*/
protected String sql;
/**
* Field sentHeader
*/
protected boolean sentHeader;
/**
* Field params, include
*/
protected PyObject params, include;
/**
* Constructor for handling the generation of data.
*
* @param connection the database connection
* @param dataHandler a custom DataHandler for the cursor, can be None
* @param tableName the table in question on the source database
* @param where an optional where clause, defaults to '(1=1)' if null
* @param include the columns to be queried from the source, '*' if None
* @param params optional params to substituted in the where clause
*/
public DBSource(PyConnection connection, Class dataHandler, String tableName, String where, PyObject include, PyObject params) {
super(connection, dataHandler, tableName);
this.params = params;
this.include = include;
this.sentHeader = false;
this.sql = this.createSql(where);
}
/**
* Create the sql string given the where clause.
*/
protected String createSql(String where) {
// create the sql statement, using the columns if available
StringBuffer sb = new StringBuffer("select ");
if ((this.include == Py.None) || (this.include.__len__() == 0)) {
sb.append("*");
} else {
for (int i = 1; i < this.include.__len__(); i++) {
sb.append(this.include.__getitem__(i)).append(",");
}
sb.append(this.include.__getitem__(this.include.__len__() - 1));
}
sb.append(" from ").append(this.tableName);
sb.append(" where ").append((where == null) ? "(1=1)" : where);
String sql = sb.toString();
return sql;
}
/**
* Return the next row in the result set. The first row returned will be column information.
*/
public PyObject next() {
if (this.sentHeader) {
// Py.None will be sent when all done, so this will close down the queue
return this.cursor.fetchone();
} else {
this.cursor.execute(Py.newString(this.sql), this.params, Py.None, Py.None);
PyObject description = this.cursor.__findattr__("description");
// we can't insert if we don't know column names
if ((description == Py.None) || (description.__len__() == 0)) {
// let the destination worry about handling the empty set
return Py.None;
}
int len = description.__len__();
PyObject[] columns = new PyObject[len];
for (int i = 0; i < len; i++) {
PyObject[] colInfo = new PyObject[2];
// col name
colInfo[0] = description.__getitem__(i).__getitem__(0);
// col type
colInfo[1] = description.__getitem__(i).__getitem__(1);
columns[i] = new PyTuple(colInfo);
}
PyObject row = new PyTuple(columns);
Py.writeDebug("db-source", row.toString());
this.sentHeader = true;
return row;
}
}
/**
* Method start
*/
public void start() {
}
/**
* Close the cursor.
*/
public void end() {
if (this.cursor != null) {
this.cursor.close();
}
}
}