2011-12-22 19:03:54 +01:00
|
|
|
import logging
|
2012-01-04 00:15:10 +01:00
|
|
|
from itertools import count
|
2011-12-22 19:03:54 +01:00
|
|
|
|
2012-01-04 00:15:10 +01:00
|
|
|
from sqlalchemy.sql import expression, and_
|
2013-03-30 22:28:32 +01:00
|
|
|
from sqlaload.schema import _ensure_columns, get_table
|
2011-12-22 19:03:54 +01:00
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
def resultiter(rp):
|
|
|
|
|
""" SQLAlchemy ResultProxies are not iterable to get a
|
|
|
|
|
list of dictionaries. This is to wrap them. """
|
|
|
|
|
keys = rp.keys()
|
|
|
|
|
while True:
|
|
|
|
|
row = rp.fetchone()
|
|
|
|
|
if row is None:
|
|
|
|
|
break
|
|
|
|
|
yield dict(zip(keys, row))
|
|
|
|
|
|
2012-01-04 00:15:10 +01:00
|
|
|
def find_one(engine, table, **kw):
|
2013-03-30 22:28:32 +01:00
|
|
|
table = get_table(engine, table)
|
2012-01-04 00:15:10 +01:00
|
|
|
res = list(find(engine, table, _limit=1, **kw))
|
|
|
|
|
if not len(res):
|
|
|
|
|
return None
|
|
|
|
|
return res[0]
|
|
|
|
|
|
2012-12-24 19:54:47 +01:00
|
|
|
def find(engine, table, _limit=None, _step=5000, _offset=0,
|
2013-03-30 22:28:32 +01:00
|
|
|
order_by='id', **kw):
|
|
|
|
|
table = get_table(engine, table)
|
2012-01-04 00:55:39 +01:00
|
|
|
_ensure_columns(engine, table, kw)
|
2013-03-30 22:28:32 +01:00
|
|
|
order_by = [table.c[order_by].asc()]
|
2012-01-04 00:15:10 +01:00
|
|
|
|
|
|
|
|
qargs = []
|
|
|
|
|
try:
|
|
|
|
|
for col, val in kw.items():
|
|
|
|
|
qargs.append(table.c[col]==val)
|
|
|
|
|
except KeyError:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for i in count():
|
|
|
|
|
qoffset = _offset + (_step * i)
|
|
|
|
|
qlimit = _step
|
|
|
|
|
if _limit is not None:
|
|
|
|
|
qlimit = min(_limit-(_step*i), _step)
|
|
|
|
|
if qlimit <= 0:
|
|
|
|
|
break
|
|
|
|
|
q = table.select(whereclause=and_(*qargs), limit=qlimit,
|
|
|
|
|
offset=qoffset, order_by=order_by)
|
2012-01-08 23:41:18 +01:00
|
|
|
#print q
|
2012-01-04 00:15:10 +01:00
|
|
|
rows = list(resultiter(engine.execute(q)))
|
|
|
|
|
if not len(rows):
|
|
|
|
|
return
|
|
|
|
|
for row in rows:
|
|
|
|
|
yield row
|
|
|
|
|
|
2012-08-12 11:54:32 +02:00
|
|
|
def query(engine, query):
|
|
|
|
|
for res in resultiter(engine.execute(query)):
|
|
|
|
|
yield res
|
|
|
|
|
|
2012-12-24 19:54:47 +01:00
|
|
|
def distinct(engine, table, *columns, **kw):
|
2013-03-30 22:28:32 +01:00
|
|
|
table = get_table(engine, table)
|
2012-12-24 19:54:47 +01:00
|
|
|
qargs = []
|
|
|
|
|
try:
|
2012-12-24 22:47:45 +01:00
|
|
|
columns = [table.c[c] for c in columns]
|
2012-12-24 19:54:47 +01:00
|
|
|
for col, val in kw.items():
|
|
|
|
|
qargs.append(table.c[col]==val)
|
|
|
|
|
except KeyError:
|
2012-12-24 22:47:45 +01:00
|
|
|
return []
|
2012-12-24 19:54:47 +01:00
|
|
|
|
|
|
|
|
q = expression.select(columns, distinct=True,
|
|
|
|
|
whereclause=and_(*qargs),
|
2012-01-08 23:41:18 +01:00
|
|
|
order_by=[c.asc() for c in columns])
|
2012-01-04 00:15:10 +01:00
|
|
|
return list(resultiter(engine.execute(q)))
|
2011-12-22 19:03:54 +01:00
|
|
|
|
|
|
|
|
def all(engine, table):
|
2012-01-04 00:15:10 +01:00
|
|
|
return find(engine, table)
|
2011-12-22 19:03:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|