-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJDBCConnectionWrapper.py
More file actions
559 lines (481 loc) · 20.5 KB
/
Copy pathJDBCConnectionWrapper.py
File metadata and controls
559 lines (481 loc) · 20.5 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
"""This module holds a ConnectionWrapper that is used with a
JDBC Connection. The module should only be used when running Jython.
"""
# Copyright (c) 2009-2011, Christian Thomsen (chr@cs.aau.dk)
# All rights reserved.
# Redistribution and use in source anqd binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import java.sql as jdbc
from copy import copy as pcopy
from datetime import datetime
from sys import modules
from threading import Thread
from Queue import Queue
import pygrametl
from pygrametl.FIFODict import FIFODict
# NOTE: This module is made for Jython.
__author__ = "Christian Thomsen"
__maintainer__ = "Christian Thomsen"
__version__ = '0.2.0'
__all__ = ['JDBCConnectionWrapper', 'BackgroundJDBCConnectionWrapper']
class JDBCConnectionWrapper(object):
"""Wrap a JDBC Connection.
All Dimension and FactTable communicate with the data warehouse using
a ConnectionWrapper. In this way, the code for loading the DW does not
have to care about which parameter format is used.
This ConnectionWrapper is a special one for JDBC in Jython.
"""
def __init__(self, jdbcconn, stmtcachesize=20):
"""Create a ConnectionWrapper around the given JDBC connection.
If no default ConnectionWrapper already exists, the new
ConnectionWrapper is set to be the default ConnectionWrapper.
Arguments:
- jdbcconn: An open JDBC Connection (not a PEP249 Connection)
- stmtcachesize: The maximum number of PreparedStatements kept
open. Default: 20.
"""
if not isinstance(jdbcconn, jdbc.Connection):
raise TypeError, '1st argument must implement java.sql.Connection'
if jdbcconn.isClosed():
raise ValueError, '1st argument must be an open Connection'
self.__jdbcconn = jdbcconn
# Add a finalizer to __prepstmts to close PreparedStatements when
# they are pushed out
self.__prepstmts = FIFODict(stmtcachesize, lambda k, v: v[0].close())
self.__resultmeta = FIFODict(stmtcachesize)
self.__resultset = None
self.__resultnames = None
self.__resulttypes = None
self.nametranslator = lambda s: s
self.__jdbcconn.setAutoCommit(False)
if pygrametl._defaulttargetconnection is None:
pygrametl._defaulttargetconnection = self
def __preparejdbcstmt(self, sql):
# Find pyformat arguments and change them to question marks while
# appending the attribute names to a list
names = []
newsql = sql
while True:
start = newsql.find('%(')
if start == -1:
break
end = newsql.find(')s', start)
if end == -1:
break
name = newsql[start+2 : end]
names.append(name)
newsql = newsql.replace(newsql[start:end+2], '?', 1)
ps = self.__jdbcconn.prepareStatement(newsql)
# Find parameter types
types = []
parmeta = ps.getParameterMetaData()
for i in range(len(names)):
types.append(parmeta.getParameterType(i+1))
self.__prepstmts[sql] = (ps, names, types)
def __executejdbcstmt(self, sql, args):
if self.__resultset:
self.__resultset.close()
if sql not in self.__prepstmts:
self.__preparejdbcstmt(sql)
(ps, names, types) = self.__prepstmts[sql]
for pos in range(len(names)): # Not very Pythonic, but we're doing Java
if args[names[pos]] is None:
ps.setNull(pos + 1, types[pos])
else:
ps.setObject(pos + 1, args[names[pos]], types[pos])
if ps.execute():
self.__resultset = ps.getResultSet()
if sql not in self.__resultmeta:
self.__resultmeta[sql] = \
self.__extractresultmetadata(self.__resultset)
(self.__resultnames, self.__resulttypes) = self.__resultmeta[sql]
else:
self.__resultset = None
(self.__resultnames, self.__resulttypes) = (None, None)
def __extractresultmetadata(self, resultset):
# Get jdbc resultset metadata. extract names and types
# and add it to self.__resultmeta
meta = resultset.getMetaData()
names = []
types = []
for col in range(meta.getColumnCount()):
names.append(meta.getColumnName(col+1))
types.append(meta.getColumnType(col+1))
return (names, types)
def __readresultrow(self):
if self.__resultset is None:
return None
result = []
for i in range(len(self.__resulttypes)):
e = self.__resulttypes[i] # Not Pythonic, but we need i for JDBC
if e in (jdbc.Types.CHAR, jdbc.Types.VARCHAR,
jdbc.Types.LONGVARCHAR):
result.append(self.__resultset.getString(i+1))
elif e in (jdbc.Types.BIT, jdbc.Types.BOOLEAN):
result.append(self.__resultset.getBool(i+1))
elif e in (jdbc.Types.TINYINT, jdbc.Types.SMALLINT,
jdbc.Types.INTEGER):
result.append(self.__resultset.getInt(i+1))
elif e in (jdbc.Types.BIGINT, ):
result.append(self.__resultset.getLong(i+1))
elif e in (jdbc.Types.DATE, ):
result.append(self.__resultset.getDate(i+1))
elif e in (jdbc.Types.TIMESTAMP, ):
result.append(self.__resultset.getTimestamp(i+1))
elif e in (jdbc.Types.TIME, ):
result.append(self.__resultset.getTime(i+1))
else:
# Try this and hope for the best...
result.append(self.__resultset.getString(i+1))
return tuple(result)
def execute(self, stmt, arguments=None, namemapping=None, ignored=None):
"""Execute a statement.
Arguments:
- stmt: the statement to execute
- arguments: a mapping with the arguments (default: None)
- namemapping: a mapping of names such that if stmt uses %(arg)s
and namemapping[arg]=arg2, the value arguments[arg2] is used
instead of arguments[arg]
- ignored: An ignored argument only present to accept the same
number of arguments as ConnectionWrapper.execute
"""
if namemapping and arguments:
arguments = pygrametl.copy(arguments, **namemapping)
self.__executejdbcstmt(stmt, arguments)
def executemany(self, stmt, params, ignored=None):
"""Execute a sequence of statements.
Arguments:
- stmt: the statement to execute
- params: a sequence of arguments
- ignored: An ignored argument only present to accept the same
number of arguments as ConnectionWrapper.executemany
"""
for paramset in params:
self.__executejdbcstmt(stmt, paramset)
def rowfactory(self, names=None):
"""Return a generator object returning result rows (i.e. dicts)."""
if names is None:
if self.__resultnames is None:
return
else:
names = [self.nametranslator(t[0]) for t in self.__resultnames]
empty = (None, ) * len(self.__resultnames)
while True:
tuple = self.fetchonetuple()
if tuple == empty:
return
yield dict(zip(names, tuple))
def fetchone(self, names=None):
"""Return one result row (i.e. dict)."""
if self.__resultset is None:
return {}
if names is None:
names = [self.nametranslator(t[0]) for t in self.__resultnames]
values = self.fetchonetuple()
return dict(zip(names, values))
def fetchonetuple(self):
"""Return one result tuple."""
if self.__resultset is None:
return ()
if not self.__resultset.next():
return (None, ) * len(self.__resultnames)
else:
return self.__readresultrow()
def fetchmanytuples(self, cnt):
"""Return cnt result tuples."""
if self.__resultset is None:
return []
empty = (None, ) * len(self.__resultnames)
result = []
for i in range(cnt):
tmp = self.fetchonetuple()
if tmp == empty:
break
result.append(tmp)
return result
def fetchalltuples(self):
"""Return all result tuples"""
if self.__resultset is None:
return []
result = []
empty = (None, ) * len(self.__resultnames)
while True:
tmp = self.fetchonetuple()
if tmp == empty:
return result
result.append(tmp)
def rowcount(self):
"""Not implemented. Return 0. Should return the size of the result."""
return 0
def getunderlyingmodule(self):
"""Return a reference to the underlying connection's module."""
return modules[self.__class__.__module__]
def commit(self):
"""Commit the transaction."""
pygrametl.endload()
self.__jdbcconn.commit()
def close(self):
"""Close the connection to the database,"""
self.__jdbcconn.close()
def rollback(self):
"""Rollback the transaction."""
self.__jdbcconn.rollback()
def setasdefault(self):
"""Set this ConnectionWrapper as the default connection."""
pygrametl._defaulttargetconnection = self
def cursor(self):
"""Not implemented for this JDBC connection wrapper!"""
raise NotImplementedError, ".cursor() not supported"
def resultnames(self):
if self.__resultnames is None:
return None
else:
return tuple(self.__resultnames)
# BackgroundJDBCConnectionWrapper is added for experiments. It is quite similar
# to JDBCConnectionWrapper and one of them may be removed.
class BackgroundJDBCConnectionWrapper(object):
"""Wrap a JDBC Connection and do all DB communication in the background.
All Dimension and FactTable communicate with the data warehouse using
a ConnectionWrapper. In this way, the code for loading the DW does not
have to care about which parameter format is used.
This ConnectionWrapper is a special one for JDBC in Jython and does DB
communication from a Thread.
"""
def __init__(self, jdbcconn, stmtcachesize=20):
"""Create a ConnectionWrapper around the given JDBC connection """
self.__jdbcconn = jdbcconn
# Add a finalizer to __prepstmts to close PreparedStatements when
# they are pushed out
self.__prepstmts = FIFODict(stmtcachesize, lambda k, v: v[0].close())
self.__resultmeta = FIFODict(stmtcachesize)
self.__resultset = None
self.__resultnames = None
self.__resulttypes = None
self.nametranslator = lambda s: s
self.__jdbcconn.setAutoCommit(False)
self.__queue = Queue(5000)
t = Thread(target=self.__worker)
t.setDaemon(True) # NB: "t.daemon = True" does NOT work...
t.setName('BackgroundJDBCConnectionWrapper')
t.start()
def __worker(self):
while True:
(sql, args) = self.__queue.get()
self.__executejdbcstmt(sql, args)
self.__queue.task_done()
def __preparejdbcstmt(self, sql):
# Find pyformat arguments and change them to question marks while
# appending the attribute names to a list
names = []
newsql = sql
while True:
start = newsql.find('%(')
if start == -1:
break
end = newsql.find(')s', start)
if end == -1:
break
name = newsql[start+2 : end]
names.append(name)
newsql = newsql.replace(newsql[start:end+2], '?', 1)
ps = self.__jdbcconn.prepareStatement(newsql)
# Find parameter types
types = []
parmeta = ps.getParameterMetaData()
for i in range(len(names)):
types.append(parmeta.getParameterType(i+1))
self.__prepstmts[sql] = (ps, names, types)
def __executejdbcstmt(self, sql, args):
if self.__resultset:
self.__resultset.close()
if sql not in self.__prepstmts:
self.__preparejdbcstmt(sql)
(ps, names, types) = self.__prepstmts[sql]
for pos in range(len(names)): # Not very Pythonic, but we're doing Java
if args[names[pos]] is None:
ps.setNull(pos + 1, types[pos])
else:
ps.setObject(pos + 1, args[names[pos]], types[pos])
if ps.execute():
self.__resultset = ps.getResultSet()
if sql not in self.__resultmeta:
self.__resultmeta[sql] = \
self.__extractresultmetadata(self.__resultset)
(self.__resultnames, self.__resulttypes) = self.__resultmeta[sql]
else:
self.__resultset = None
(self.__resultnames, self.__resulttypes) = (None, None)
def __extractresultmetadata(self, resultset):
# Get jdbc resultset metadata. extract names and types
# and add it to self.__resultmeta
meta = resultset.getMetaData()
names = []
types = []
for col in range(meta.getColumnCount()):
names.append(meta.getColumnName(col+1))
types.append(meta.getColumnType(col+1))
return (names, types)
def __readresultrow(self):
if self.__resultset is None:
return None
result = []
for i in range(len(self.__resulttypes)):
e = self.__resulttypes[i] # Not Pythonic, but we need i for JDBC
if e in (jdbc.Types.CHAR, jdbc.Types.VARCHAR,
jdbc.Types.LONGVARCHAR):
result.append(self.__resultset.getString(i+1))
elif e in (jdbc.Types.BIT, jdbc.Types.BOOLEAN):
result.append(self.__resultset.getBool(i+1))
elif e in (jdbc.Types.TINYINT, jdbc.Types.SMALLINT,
jdbc.Types.INTEGER):
result.append(self.__resultset.getInt(i+1))
elif e in (jdbc.Types.BIGINT, ):
result.append(self.__resultset.getLong(i+1))
elif e in (jdbc.Types.DATE, ):
result.append(self.__resultset.getDate(i+1))
elif e in (jdbc.Types.TIMESTAMP, ):
result.append(self.__resultset.getTimestamp(i+1))
elif e in (jdbc.Types.TIME, ):
result.append(self.__resultset.getTime(i+1))
else:
# Try this and hope for the best...
result.append(self.__resultset.getString(i+1))
return tuple(result)
def execute(self, stmt, arguments=None, namemapping=None, ignored=None):
"""Execute a statement.
Arguments:
- stmt: the statement to execute
- arguments: a mapping with the arguments (default: None)
- namemapping: a mapping of names such that if stmt uses %(arg)s
and namemapping[arg]=arg2, the value arguments[arg2] is used
instead of arguments[arg]
- ignored: An ignored argument only present to accept the same
number of arguments as ConnectionWrapper.execute
"""
if namemapping and arguments:
arguments = pygrametl.copy(arguments, **namemapping)
else:
arguments = pcopy(arguments)
self.__queue.put((stmt, arguments))
def executemany(self, stmt, params, ignored=None):
"""Execute a sequence of statements.
Arguments:
- stmt: the statement to execute
- params: a sequence of arguments
- ignored: An ignored argument only present to accept the same
number of arguments as ConnectionWrapper.executemany
"""
for paramset in params:
self.__queue.put((stmt, paramset))
def rowfactory(self, names=None):
"""Return a generator object returning result rows (i.e. dicts)."""
self.__queue.join()
if names is None:
if self.__resultnames is None:
return
else:
names = [self.nametranslator(t[0]) for t in self.__resultnames]
empty = (None, ) * len(self.__resultnames)
while True:
tuple = self.fetchonetuple()
if tuple == empty:
return
yield dict(zip(names, tuple))
def fetchone(self, names=None):
"""Return one result row (i.e. dict)."""
self.__queue.join()
if self.__resultset is None:
return {}
if names is None:
names = [self.nametranslator(t[0]) for t in self.__resultnames]
values = self.fetchonetuple()
return dict(zip(names, values))
def fetchonetuple(self):
"""Return one result tuple."""
self.__queue.join()
if self.__resultset is None:
return ()
if not self.__resultset.next():
return (None, ) * len(self.__resultnames)
else:
return self.__readresultrow()
def fetchmanytuples(self, cnt):
"""Return cnt result tuples."""
self.__queue.join()
if self.__resultset is None:
return []
empty = (None, ) * len(self.__resultnames)
result = []
for i in range(cnt):
tmp = self.fetchonetuple()
if tmp == empty:
break
result.append(tmp)
return result
def fetchalltuples(self):
"""Return all result tuples"""
self.__queue.join()
if self.__resultset is None:
return []
result = []
empty = (None, ) * len(self.__resultnames)
while True:
tmp = self.fetchonetuple()
if tmp == empty:
return result
result.append(tmp)
def rowcount(self):
"""Not implemented. Return 0. Should return the size of the result."""
return 0
def getunderlyingmodule(self):
"""Return a reference to the underlying connection's module."""
return modules[self.__class__.__module__]
def commit(self):
"""Commit the transaction."""
pygrametl.endload()
self.__queue.join()
self.__jdbcconn.commit()
def close(self):
"""Close the connection to the database,"""
self.__queue.join()
self.__jdbcconn.close()
def rollback(self):
"""Rollback the transaction."""
self.__queue.join()
self.__jdbcconn.rollback()
def setasdefault(self):
"""Set this ConnectionWrapper as the default connection."""
pygrametl._defaulttargetconnection = self
def cursor(self):
"""Not implemented for this JDBC connection wrapper!"""
raise NotImplementedError, ".cursor() not supported"
def resultnames(self):
self.__queue.join()
if self.__resultnames is None:
return None
else:
return tuple(self.__resultnames)
def Date(year, month, day):
date = '%s-%s-%s' % \
(str(year).zfill(4), str(month).zfill(2), str(day).zfill(2))
return jdbc.Date.valueOf(date)
def Timestamp(year, month, day, hour, minute, second):
date = '%s-%s-%s %s:%s:%s' % \
(str(year).zfill(4), str(month).zfill(2), str(day).zfill(2),
str(hour).zfill(2), str(minute).zfill(2), str(second).zfill(2))
return jdbc.Timestamp.valueOf(date)