1 from __future__
import absolute_import
2 from __future__
import division
3 from __future__
import print_function
4 from __future__
import unicode_literals
6 from future_builtins
import ascii
, filter, hex, map, oct, zip
11 from contextlib
import contextmanager
14 import psycopg2
.extras
17 psycopg2
.extensions
.register_type(psycopg2
.extensions
.UNICODE
)
19 class connect(object):
20 def __init__(self
, dsn
):
24 self
.driver
= psycopg2
.connect(**dsn
)
26 except psycopg2
.OperationalError
, e
:
32 self
.driver
.set_client_encoding('UNICODE')
33 self
.driver
.set_isolation_level(psycopg2
.extensions
.ISOLATION_LEVEL_AUTOCOMMIT
)
43 def __exit__(self
, type, value
, traceback
):
48 cursor
= self
.driver
.cursor(cursor_factory
=psycopg2
.extras
.DictCursor
)
55 def execute(self
, statement
, depth
=0, context
=None):
56 # two frames, accounting for execute() and @contextmanager
57 frame
= inspect
.currentframe(depth
+ 2)
59 with self
.cursor() as cursor
:
61 f_locals
= frame
.f_locals
64 context
= dict(**f_locals
)
68 percent
= statement
.find('%', start
)
72 next
= statement
[percent
+ 1]
74 start
= statement
.index(')', percent
+ 2) + 2
75 assert statement
[start
- 1] == 's'
77 start
= statement
.index('}', percent
+ 2)
78 assert statement
[start
+ 1] == 's'
79 code
= statement
[percent
+ 2:start
]
82 f_globals
= frame
.f_globals
84 key
= '__cyql__%i' % (percent
,)
85 # XXX: compile() in the frame's context
86 context
[key
] = eval(code
, f_globals
, f_locals
)
88 statement
= '%s%%(%s)%s' % (statement
[0:percent
], key
, statement
[start
+ 1:])
89 start
= percent
+ len(key
) + 4
90 elif next
in ('%', 's'):
95 cursor
.execute(statement
, context
)
104 def transact(self
, synchronous_commit
=True):
105 self
.driver
.set_isolation_level(psycopg2
.extensions
.ISOLATION_LEVEL_READ_COMMITTED
)
107 with self
.cursor() as cursor
:
108 if not synchronous_commit
:
109 cursor
.execute('set local synchronous_commit = off')
114 self
.driver
.rollback()
117 self
.driver
.set_isolation_level(psycopg2
.extensions
.ISOLATION_LEVEL_AUTOCOMMIT
)
119 def one_(self
, statement
, context
=None):
120 with self
.execute(statement
, 2, context
) as cursor
:
121 one
= cursor
.fetchone()
125 assert cursor
.fetchone() == None
128 def __call__(self
, procedure
, *parameters
):
129 with self
.execute(statement
, 1) as cursor
:
130 return cursor
.callproc(procedure
, *parameters
)
132 def run(self
, statement
, context
=None):
133 with self
.execute(statement
, 1, context
) as cursor
:
134 return cursor
.rowcount
137 def set(self
, statement
):
138 with self
.execute(statement
, 1) as cursor
:
141 def all(self
, statement
):
142 with self
.execute(statement
, 1) as cursor
:
143 return cursor
.fetchall()
145 def one(self
, statement
, context
=None):
146 return self
.one_(statement
, context
)
148 def has(self
, statement
):
149 exists
, = self
.one_('select exists(%s)' % (statement
,))
154 def replaced(*args
, **kw
):
155 with connect(dsn
) as sql
:
156 return method(*args
, sql
=sql
, **kw
)
161 def transact(dsn
, *args
, **kw
):
162 with connect(dsn
) as connection
:
163 with connection
.transact(*args
, **kw
):
167 def slap_(sql, table, keys, values, path):
170 csr.execute('savepoint iou')
172 both = dict(keys, **values)
176 insert into %s (%s) values (%s)
180 ', '.join(['%s' for key in fields])
182 except psycopg2.IntegrityError, e:
183 csr.execute('rollback to savepoint iou')
186 update %s set %s where %s
191 for key in values.keys()]),
194 for key in keys.keys()])
195 ), values.values() + keys.values())
197 return path_(csr, path)