-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdbapi20.py
More file actions
338 lines (302 loc) · 7.74 KB
/
Copy pathdbapi20.py
File metadata and controls
338 lines (302 loc) · 7.74 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
##
# copyright 2009, James William Pye
# http://python.projects.postgresql.org
##
"""
DB-API 2.0 conforming interface using postgresql.driver.
"""
threadsafety = 1
paramstyle = 'pyformat'
apilevel = '2.0'
from operator import itemgetter
from functools import partial
import datetime
import time
import re
from .. import driver as pg_driver
from .. import types as pg_type
from .. import string as pg_str
##
# Basically, is it a mapping, or is it a sequence?
# If findall()'s first index is 's', it's a sequence.
# If it starts with '(', it's mapping.
# The pain here is due to a need to recognize any %% escapes.
parameters_re = re.compile(
r'(?:%%)+|%(s|[(][^)]*[)]s)'
)
def percent_parameters(sql):
# filter any %% matches(empty strings).
return [
x for x in parameters_re.findall(sql) if x
]
def convert_keywords(keys, mapping):
return [
mapping[k] for k in keys
]
from postgresql.exceptions import \
Error, DataError, InternalError, \
ICVError as IntegrityError, \
SEARVError as ProgrammingError, \
IRError as OperationalError, \
DriverError as InterfaceError, \
Warning
DatabaseError = Error
class NotSupportedError(DatabaseError):
pass
STRING = str
BINARY = bytes
NUMBER = int
DATETIME = datetime.datetime
ROWID = int
Binary = BINARY
Date = datetime.date
Time = datetime.time
Timestamp = datetime.datetime
DateFromTicks = lambda x: Date(*time.localtime(x)[:3])
TimeFromTicks = lambda x: Time(*time.localtime(x)[3:6])
TimestampFromTicks = lambda x: Timestamp(*time.localtime(x)[:7])
def dbapi_type(typid):
if typid in (
pg_type.TEXTOID,
pg_type.CHAROID,
pg_type.VARCHAROID,
pg_type.NAMEOID,
pg_type.CSTRINGOID,
):
return STRING
elif typid == pg_type.BYTEAOID:
return BINARY
elif typid in (pg_type.INT8OID, pg_type.INT2OID, pg_type.INT4OID):
return NUMBER
elif typid in (pg_type.TIMESTAMPOID, pg_type.TIMESTAMPTZOID):
return DATETIME
elif typid == pg_type.OIDOID:
return ROWID
class Cursor(object):
rowcount = -1
arraysize = 1
description = None
def __init__(self, C):
self.connection = C
self.database = C.database
self.description = ()
self.__portals = []
def setinputsizes(self, sizes):
pass
def setoutputsize(self, sizes, columns = None):
pass
def callproc(self, proname, args):
p = self.database.prepare("SELECT %s(%s)" %(
proname, ','.join([
'$%d' %(x,) for x in range(1, len(args) + 1)
])
))
self.__portals.insert(0, p._cursor(*args))
return args
def fetchone(self):
try:
return next(self._portal)
except StopIteration:
return None
def __next__(self):
return next(self._portal)
next = __next__
def __iter__(self):
return self
def fetchmany(self, arraysize = None):
return self._portal.read(arraysize or self.arraysize or 1)
def fetchall(self):
return self._portal.read()
def nextset(self):
del self._portal
return len(self.__portals) or None
def _convert_query(self, string):
parts = list(pg_str.split(string))
style = None
count = 0
keys = []
kmap = {}
transformer = tuple
rparts = []
for part in parts:
if type(part) is type(()):
# skip quoted portions
rparts.append(part)
else:
r = percent_parameters(part)
pcount = 0
for x in r:
if x == 's':
pcount += 1
else:
x = x[1:-2]
if x not in keys:
kmap[x] = '$' + str(len(keys) + 1)
keys.append(x)
if r:
if pcount:
# format
params = tuple([
'$' + str(i+1) for i in range(count, count + pcount)
])
count += pcount
rparts.append(part % params)
else:
# pyformat
rparts.append(part % kmap)
else:
# no parameters identified in string
rparts.append(part)
if keys:
if count:
raise TypeError(
"keyword parameters and positional parameters used in query"
)
transformer = partial(convert_keywords, keys)
count = len(keys)
return (pg_str.unsplit(rparts) if rparts else string, transformer, count)
def execute(self, statement, parameters = ()):
sql, pxf, nparams = self._convert_query(statement)
if nparams != -1 and len(parameters) != nparams:
raise TypeError(
"statement require %d parameters, given %d" %(
nparams, len(parameters)
)
)
ps = self.database.prepare(sql)
c = ps._cursor(*pxf(parameters))
if ps._output is not None and len(ps._output) > 0:
# name, relationId, columnNumber, typeId, typlen, typmod, format
self.rowcount = -1
self.description = tuple([
(self.database.typio.decode(x[0]), dbapi_type(x[3]),
None, None, None, None, None)
for x in ps._output
])
self.__portals.insert(0, c)
else:
self.rowcount = c.count()
if self.rowcount is None:
self.rowcount = -1
self.description = None
if self.__portals:
del self._portal
return self
def executemany(self, statement, parameters):
sql, pxf, nparams = self._convert_query(statement)
ps = self.database.prepare(sql)
if ps._input is not None:
ps.load(map(pxf, parameters))
else:
ps.load(parameters)
self.rowcount = -1
return self
def close(self):
self.description = None
ps = self.__portals
if self.__portals is not None:
self.__portals = None
for p in ps: p.close()
# Describe the "real" cursor as a "portal".
# This should keep ambiguous terminology out of adaptor.
def _portal():
def fget(self):
if self.__portals is None:
raise Error("access on closed cursor")
try:
p = self.__portals[0]
except IndexError:
raise InterfaceError("no portal on stack")
return p
def fdel(self):
try:
del self.__portals[0]
except IndexError:
raise InterfaceError("no portal on stack")
return locals()
_portal = property(**_portal())
class Connection(object):
"""
DB-API 2.0 connection implementation for PG-API connection objects.
"""
from postgresql.exceptions import \
Error, DataError, InternalError, \
ICVError as IntegrityError, \
SEARVError as ProgrammingError, \
IRError as OperationalError, \
DriverError as InterfaceError, \
Warning
DatabaseError = DatabaseError
NotSupportedError = NotSupportedError
def autocommit_set(self, val):
if val:
# already in autocommit mode.
if self._xact is None:
return
self._xact.rollback()
self._xact = None
else:
if self._xact is not None:
return
self._xact = self.database.xact()
self._xact.start()
def autocommit_get(self):
return self._xact is None
def autocommit_del(self):
self.autocommit = False
autocommit = property(
fget = autocommit_get,
fset = autocommit_set,
fdel = autocommit_del,
)
del autocommit_set, autocommit_get, autocommit_del
def __init__(self, connection):
self.database = connection
self._xact = self.database.xact()
self._xact.start()
def close(self):
if self.database.closed:
err = Error(
"connection already closed",
source = 'DRIVER',
)
self.database.ife_descend(err)
err.raise_exception()
self.database.close()
def cursor(self):
return Cursor(self)
def commit(self):
if self._xact is None:
err = InterfaceError(
"commit on connection in autocommit mode",
source = 'DRIVER',
details = {
'hint': 'The "autocommit" property on the connection was set to True.'
}
)
self.database.ife_descend(err)
err.raise_exception()
self._xact.commit()
self._xact = self.database.xact()
self._xact.start()
def rollback(self):
if self._xact is None:
err = InterfaceError(
"rollback on connection in autocommit mode",
source = 'DRIVER',
details = {
'hint': 'The "autocommit" property on the connection was set to True.'
}
)
self.database.ife_descend(err)
err.raise_exception()
self._xact.rollback()
self._xact = self.database.xact()
self._xact.start()
def connect(**kw):
"""
Create a DB-API connection using the given parameters.
"""
db = pg_driver.connect(**kw)
dbapi = Connection(db)
return dbapi