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
)
18 psycopg2
.extensions
.register_type(psycopg2
.extensions
.UNICODEARRAY
)
20 class connect(object):
21 def __init__(self
, dsn
):
25 self
.driver
= psycopg2
.connect(**dsn
)
27 except psycopg2
.OperationalError
, e
:
33 self
.driver
.set_client_encoding('UNICODE')
34 self
.driver
.set_isolation_level(psycopg2
.extensions
.ISOLATION_LEVEL_AUTOCOMMIT
)
44 def __exit__(self
, type, value
, traceback
):
49 cursor
= self
.driver
.cursor(cursor_factory
=psycopg2
.extras
.DictCursor
)
56 def execute(self
, statement
, depth
=0, context
=None):
57 # two frames, accounting for execute() and @contextmanager
58 frame
= inspect
.currentframe(depth
+ 2)
60 with self
.cursor() as cursor
:
62 f_locals
= frame
.f_locals
65 context
= dict(**f_locals
)
69 percent
= statement
.find('%', start
)
73 next
= statement
[percent
+ 1]
75 start
= statement
.index(')', percent
+ 2) + 2
76 assert statement
[start
- 1] == 's'
78 start
= statement
.index('}', percent
+ 2)
79 assert statement
[start
+ 1] == 's'
80 code
= statement
[percent
+ 2:start
]
83 f_globals
= frame
.f_globals
85 key
= '__cyql__%i' % (percent
,)
86 # XXX: compile() in the frame's context
87 context
[key
] = eval(code
, f_globals
, f_locals
)
89 statement
= '%s%%(%s)%s' % (statement
[0:percent
], key
, statement
[start
+ 1:])
90 start
= percent
+ len(key
) + 4
91 elif next
in ('%', 's'):
96 cursor
.execute(statement
, context
)
105 def transact(self
, synchronous_commit
=True):
106 self
.driver
.set_isolation_level(psycopg2
.extensions
.ISOLATION_LEVEL_READ_COMMITTED
)
108 with self
.cursor() as cursor
:
109 if not synchronous_commit
:
110 cursor
.execute('set local synchronous_commit = off')
115 self
.driver
.rollback()
118 self
.driver
.set_isolation_level(psycopg2
.extensions
.ISOLATION_LEVEL_AUTOCOMMIT
)
120 def one_(self
, statement
, context
=None):
121 with self
.execute(statement
, 2, context
) as cursor
:
122 one
= cursor
.fetchone()
126 assert cursor
.fetchone() == None
129 def __call__(self
, procedure
, *parameters
):
130 with self
.execute(statement
, 1) as cursor
:
131 return cursor
.callproc(procedure
, *parameters
)
133 def run(self
, statement
, context
=None):
134 with self
.execute(statement
, 1, context
) as cursor
:
135 return cursor
.rowcount
138 def set(self
, statement
):
139 with self
.execute(statement
, 1) as cursor
:
142 def all(self
, statement
):
143 with self
.execute(statement
, 1) as cursor
:
144 return cursor
.fetchall()
146 def one(self
, statement
, context
=None):
147 return self
.one_(statement
, context
)
149 def has(self
, statement
):
150 exists
, = self
.one_('select exists(%s)' % (statement
,))
155 def replaced(*args
, **kw
):
156 with connect(dsn
) as sql
:
157 return method(*args
, sql
=sql
, **kw
)
162 def transact(dsn
, *args
, **kw
):
163 with connect(dsn
) as connection
:
164 with connection
.transact(*args
, **kw
):
168 def slap_(sql, table, keys, values, path):
171 csr.execute('savepoint iou')
173 both = dict(keys, **values)
177 insert into %s (%s) values (%s)
181 ', '.join(['%s' for key in fields])
183 except psycopg2.IntegrityError, e:
184 csr.execute('rollback to savepoint iou')
187 update %s set %s where %s
192 for key in values.keys()]),
195 for key in keys.keys()])
196 ), values.values() + keys.values())
198 return path_(csr, path)