-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
71 lines (49 loc) · 1.37 KB
/
db.py
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
import untwisted
class connect:
def __init__(ctx, *args, **kwds):
try:
module = kwds['module']
del kwds['module']
except KeyError:
import MySQLdb as module
ctx.connect = lambda: (ctx, setattr(ctx, 'conn', module.connect(*args, **kwds)))[0]
ctx.connect()
ctx.module = module
class cursor:
class __metaclass__(type):
__get__ = untwisted.ctxual
def __init__(ctx):
ctx.cursor = ctx.ctx.conn.cursor()
def close(ctx):
ctx.cursor.close()
return ctx
def execute(ctx, operation, *args):
try:
args, = args
except ValueError:
pass
try:
ctx.cursor.execute(operation, args)
except ctx.ctx.module.OperationalError:
ctx.cursor = ctx.ctx.connect().conn.cursor()
ctx.cursor.execute(operation, args)
return ctx
def executemany(ctx, operation, *args):
try:
ctx.cursor.executemany(operation, args)
except ctx.ctx.module.OperationalError:
ctx.cursor = ctx.ctx.connect().conn.cursor()
ctx.cursor.executemany(operation, args)
return ctx
__iter__ = untwisted.identity
def next(ctx):
result = ctx.cursor.fetchone()
if result:
return result
raise StopIteration
def __iter__(ctx):
cursor = ctx.cursor()
try:
yield cursor
finally:
cursor.close()