Last active
March 8, 2020 16:06
-
-
Save tym-xqo/8548c2bb35151ea6a334f3da99344b2d to your computer and use it in GitHub Desktop.
All the SQLAlchemy you need
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""Use SQLAlchemy engine to fetch a dataset from a query | |
""" | |
import os | |
from jinja2.sandbox import SandboxedEnvironment | |
from sqlalchemy import create_engine | |
def connection(): | |
db_url = os.getenv("DATABASE_URL", "sqlite:///") | |
db = create_engine(db_url) | |
conn = db.connect() | |
return conn | |
def process_template(obj, **kwargs): | |
"""Render query body using jinja2 sandbox | |
TODO: Prevent variable expansion | |
""" | |
env = SandboxedEnvironment() | |
template = env.from_string(obj) | |
return template.render(kwargs) | |
def result(sql, **kwargs): | |
try: | |
db = connection() | |
sql = process_template(sql, **kwargs) | |
cur = db.execute(sql, **kwargs) | |
cols = cur.keys() | |
result = cur.fetchall() | |
rows = [dict(zip(cols, row)) for row in result] | |
except Exception as e: | |
rows = [{"error": repr(e)}] | |
return rows | |
if __name__ == "__main__": | |
sql = "select 'foo' as bar" | |
result = result(sql) | |
assert result == [{"bar": "foo"}] | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment