Python tool for slicing and dicing SQL
Project description
sql-athame
Python tool for slicing and dicing SQL
Example
from sql_athame import sql
def get_orders(query):
where = []
if "id" in query:
where.append(sql("id = {}", query["id"]))
if "eventId" in query:
where.append(sql("event_id = {}", query["eventId"]))
if "startTime" in query:
where.append(sql("start_time = {}", query["startTime"]))
if "from" in query:
where.append(sql("start_time >= {}", query["from"]))
if "until" in query:
where.append(sql("start_time < {}", query["until"]))
return sql("SELECT * FROM orders WHERE {}", sql.all(where))
print(get_orders({}).query())
# ('SELECT * FROM orders WHERE TRUE', [])
print(list(get_orders({})))
# ['SELECT * FROM orders WHERE TRUE']
print(get_orders({"id": "xyzzy"}).query())
# ('SELECT * FROM orders WHERE TRUE AND id = $1', ['xyzzy'])
print(list(get_orders({"id": "xyzzy"})))
# ['SELECT * FROM orders WHERE TRUE AND id = $1', 'xyzzy']
print(
*get_orders(
{"eventId": "plugh", "from": "2019-05-01", "until": "2019-08-26"}
)
)
# SELECT * FROM orders WHERE TRUE AND event_id = $1 AND start_time >= $2 AND start_time < $3 ['plugh', '2019-05-01', '2019-08-26']
superquery = sql(
"""
SELECT *
FROM ({subquery}) sq
JOIN other_table ot ON (ot.id = sq.id)
WHERE ot.foo = {foo}
LIMIT {limit}
""",
subquery=get_orders({"id": "xyzzy"}),
foo="bork",
limit=50,
)
print(superquery.query())
# ("""
# SELECT *
# FROM (SELECT * FROM orders WHERE TRUE AND id = $1) sq
# JOIN other_table ot ON (ot.id = sq.id)
# WHERE ot.foo = $2
# LIMIT $3
# """, ['xyzzy', 'bork', 50])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sql-athame-0.3.8.tar.gz
(8.6 kB
view hashes)
Built Distribution
Close
Hashes for sql_athame-0.3.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b27343c6e05b18fdc0dbfe32f9cc0de0500eec2fa5c1f5b38b9b003ba190eb0f |
|
MD5 | f0fc42ec82c0c39cb8540e798349e141 |
|
BLAKE2b-256 | 63e90a60bcbcf7cd73bd90bdb85658a7625bd11b44b45d3b36bd600823c8be77 |