]>
git.saurik.com Git - cyql.git/blob - __init__.py
bc205292e79719b98b91b5460204f423cb30602a
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
:
32 self
.driver
.autocommit
= True
34 # XXX: all of my databases default to this...
36 # self.driver.set_client_encoding('UNICODE')
42 psycopg2
.extras
.register_hstore(self
.driver
, globally
=False, unicode=True)
43 except psycopg2
.ProgrammingError
, e
:
52 def __exit__(self
, type, value
, traceback
):
56 self
.driver
.autocommit
= False
62 self
.driver
.rollback()
66 cursor
= self
.driver
.cursor(cursor_factory
=psycopg2
.extras
.DictCursor
)
73 def execute(self
, statement
, depth
=0, context
=None):
74 # two frames, accounting for execute() and @contextmanager
75 frame
= inspect
.currentframe(depth
+ 2)
77 with self
.cursor() as cursor
:
79 f_locals
= frame
.f_locals
82 context
= dict(**f_locals
)
86 percent
= statement
.find('%', start
)
90 next
= statement
[percent
+ 1]
92 start
= statement
.index(')', percent
+ 2) + 2
93 assert statement
[start
- 1] == 's'
95 start
= statement
.index('}', percent
+ 2)
96 assert statement
[start
+ 1] == 's'
97 code
= statement
[percent
+ 2:start
]
100 f_globals
= frame
.f_globals
102 key
= '__cyql__%i' % (percent
,)
103 # XXX: compile() in the frame's context
104 context
[key
] = eval(code
, f_globals
, f_locals
)
106 statement
= '%s%%(%s)%s' % (statement
[0:percent
], key
, statement
[start
+ 1:])
107 start
= percent
+ len(key
) + 4
108 elif next
in ('%', 's'):
113 cursor
.execute(statement
, context
)
122 def transact(self
, synchronous_commit
=True):
123 self
.driver
.autocommit
= False
125 with self
.cursor() as cursor
:
126 if not synchronous_commit
:
127 cursor
.execute('set local synchronous_commit = off')
132 self
.driver
.rollback()
135 self
.driver
.autocommit
= True
137 def one_(self
, statement
, context
=None):
138 with self
.execute(statement
, 2, context
) as cursor
:
139 one
= cursor
.fetchone()
143 assert cursor
.fetchone() == None
146 def __call__(self
, procedure
, *parameters
):
147 with self
.execute(statement
, 1) as cursor
:
148 return cursor
.callproc(procedure
, *parameters
)
150 def run(self
, statement
, context
=None):
151 with self
.execute(statement
, 1, context
) as cursor
:
152 return cursor
.rowcount
155 def set(self
, statement
):
156 with self
.execute(statement
, 1) as cursor
:
159 def all(self
, statement
, context
=None):
160 with self
.execute(statement
, 1, context
) as cursor
:
161 return cursor
.fetchall()
163 def one(self
, statement
, context
=None):
164 return self
.one_(statement
, context
)
166 def has(self
, statement
):
167 exists
, = self
.one_('select exists(%s)' % (statement
,))
172 def replaced(*args
, **kw
):
173 with connect(dsn
) as sql
:
174 return method(*args
, sql
=sql
, **kw
)
179 def transact(dsn
, *args
, **kw
):
180 with connect(dsn
) as connection
:
181 with connection
.transact(*args
, **kw
):
185 def slap_(sql, table, keys, values, path):
188 csr.execute('savepoint iou')
190 both = dict(keys, **values)
194 insert into %s (%s) values (%s)
198 ', '.join(['%s' for key in fields])
200 except psycopg2.IntegrityError, e:
201 csr.execute('rollback to savepoint iou')
204 update %s set %s where %s
209 for key in values.keys()]),
212 for key in keys.keys()])
213 ), values.values() + keys.values())
215 return path_(csr, path)