]>
git.saurik.com Git - cyql.git/blob - __init__.py
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
.autocommit
= True
39 psycopg2
.extras
.register_hstore(self
.driver
, globally
=False, unicode=True)
40 except psycopg2
.ProgrammingError
, e
:
49 def __exit__(self
, type, value
, traceback
):
53 self
.driver
.autocommit
= False
59 self
.driver
.rollback()
63 cursor
= self
.driver
.cursor(cursor_factory
=psycopg2
.extras
.DictCursor
)
70 def execute(self
, statement
, depth
=0, context
=None):
71 # two frames, accounting for execute() and @contextmanager
72 frame
= inspect
.currentframe(depth
+ 2)
74 with self
.cursor() as cursor
:
76 f_locals
= frame
.f_locals
79 context
= dict(**f_locals
)
83 percent
= statement
.find('%', start
)
87 next
= statement
[percent
+ 1]
89 start
= statement
.index(')', percent
+ 2) + 2
90 assert statement
[start
- 1] == 's'
92 start
= statement
.index('}', percent
+ 2)
93 assert statement
[start
+ 1] == 's'
94 code
= statement
[percent
+ 2:start
]
97 f_globals
= frame
.f_globals
99 key
= '__cyql__%i' % (percent
,)
100 # XXX: compile() in the frame's context
101 context
[key
] = eval(code
, f_globals
, f_locals
)
103 statement
= '%s%%(%s)%s' % (statement
[0:percent
], key
, statement
[start
+ 1:])
104 start
= percent
+ len(key
) + 4
105 elif next
in ('%', 's'):
110 cursor
.execute(statement
, context
)
119 def transact(self
, synchronous_commit
=True):
120 self
.driver
.autocommit
= False
122 with self
.cursor() as cursor
:
123 if not synchronous_commit
:
124 cursor
.execute('set local synchronous_commit = off')
129 self
.driver
.rollback()
132 self
.driver
.autocommit
= True
134 def one_(self
, statement
, context
=None):
135 with self
.execute(statement
, 2, context
) as cursor
:
136 one
= cursor
.fetchone()
140 assert cursor
.fetchone() == None
143 def __call__(self
, procedure
, *parameters
):
144 with self
.execute(statement
, 1) as cursor
:
145 return cursor
.callproc(procedure
, *parameters
)
147 def run(self
, statement
, context
=None):
148 with self
.execute(statement
, 1, context
) as cursor
:
149 return cursor
.rowcount
152 def set(self
, statement
):
153 with self
.execute(statement
, 1) as cursor
:
156 def all(self
, statement
, context
=None):
157 with self
.execute(statement
, 1, context
) as cursor
:
158 return cursor
.fetchall()
160 def one(self
, statement
, context
=None):
161 return self
.one_(statement
, context
)
163 def has(self
, statement
):
164 exists
, = self
.one_('select exists(%s)' % (statement
,))
169 def replaced(*args
, **kw
):
170 with connect(dsn
) as sql
:
171 return method(*args
, sql
=sql
, **kw
)
176 def transact(dsn
, *args
, **kw
):
177 with connect(dsn
) as connection
:
178 with connection
.transact(*args
, **kw
):
182 def slap_(sql, table, keys, values, path):
185 csr.execute('savepoint iou')
187 both = dict(keys, **values)
191 insert into %s (%s) values (%s)
195 ', '.join(['%s' for key in fields])
197 except psycopg2.IntegrityError, e:
198 csr.execute('rollback to savepoint iou')
201 update %s set %s where %s
206 for key in values.keys()]),
209 for key in keys.keys()])
210 ), values.values() + keys.values())
212 return path_(csr, path)