]>
git.saurik.com Git - cyql.git/blob - __init__.py
ca9ae63228ae4e0ee95d1d0e851c8b465e2b346f
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
):
23 if 'cache' in options
:
38 self
.driver
= psycopg2
.connect(**options
)
40 except psycopg2
.OperationalError
, e
:
45 self
.driver
.autocommit
= True
47 # XXX: all of my databases default to this...
49 # self.driver.set_client_encoding('UNICODE')
54 hstore
= cache
['hstore']
56 hstore
= psycopg2
.extras
.HstoreAdapter
.get_oids(self
.driver
)
62 psycopg2
.extras
.register_hstore(self
.driver
, globally
=False, unicode=True, oid
=hstore
)
63 except psycopg2
.ProgrammingError
, e
:
75 def __exit__(self
, type, value
, traceback
):
79 self
.driver
.autocommit
= False
85 self
.driver
.rollback()
89 cursor
= self
.driver
.cursor(cursor_factory
=psycopg2
.extras
.DictCursor
)
96 def execute(self
, statement
, depth
=0, context
=None):
97 # two frames, accounting for execute() and @contextmanager
98 frame
= inspect
.currentframe(depth
+ 2)
100 with self
.cursor() as cursor
:
102 f_locals
= frame
.f_locals
105 context
= dict(**f_locals
)
109 percent
= statement
.find('%', start
)
113 next
= statement
[percent
+ 1]
115 start
= statement
.index(')', percent
+ 2) + 2
116 assert statement
[start
- 1] == 's'
118 start
= statement
.index('}', percent
+ 2)
119 assert statement
[start
+ 1] == 's'
120 code
= statement
[percent
+ 2:start
]
122 if f_globals
== None:
123 f_globals
= frame
.f_globals
125 key
= '__cyql__%i' % (percent
,)
126 # XXX: compile() in the frame's context
127 context
[key
] = eval(code
, f_globals
, f_locals
)
129 statement
= '%s%%(%s)%s' % (statement
[0:percent
], key
, statement
[start
+ 1:])
130 start
= percent
+ len(key
) + 4
131 elif next
in ('%', 's'):
136 cursor
.execute(statement
, context
)
145 def transact(self
, synchronous_commit
=True):
146 self
.driver
.autocommit
= False
148 with self
.cursor() as cursor
:
149 if not synchronous_commit
:
150 cursor
.execute('set local synchronous_commit = off')
155 self
.driver
.rollback()
158 self
.driver
.autocommit
= True
160 def one_(self
, statement
, context
=None):
161 with self
.execute(statement
, 2, context
) as cursor
:
162 one
= cursor
.fetchone()
166 assert cursor
.fetchone() == None
169 def __call__(self
, procedure
, *parameters
):
170 with self
.execute(statement
, 1) as cursor
:
171 return cursor
.callproc(procedure
, *parameters
)
173 def run(self
, statement
, context
=None):
174 with self
.execute(statement
, 1, context
) as cursor
:
175 return cursor
.rowcount
178 def set(self
, statement
):
179 with self
.execute(statement
, 2) as cursor
:
182 def all(self
, statement
, context
=None):
183 with self
.execute(statement
, 1, context
) as cursor
:
184 return cursor
.fetchall()
186 def one(self
, statement
, context
=None):
187 return self
.one_(statement
, context
)
189 def has(self
, statement
):
190 exists
, = self
.one_('select exists(%s)' % (statement
,))
195 def replaced(*args
, **kw
):
196 with connect(dsn
) as sql
:
197 return method(*args
, sql
=sql
, **kw
)
202 def transact(dsn
, *args
, **kw
):
203 with connect(dsn
) as connection
:
204 with connection
.transact(*args
, **kw
):
208 def slap_(sql, table, keys, values, path):
211 csr.execute('savepoint iou')
213 both = dict(keys, **values)
217 insert into %s (%s) values (%s)
221 ', '.join(['%s' for key in fields])
223 except psycopg2.IntegrityError, e:
224 csr.execute('rollback to savepoint iou')
227 update %s set %s where %s
232 for key in values.keys()]),
235 for key in keys.keys()])
236 ), values.values() + keys.values())
238 return path_(csr, path)