A tiny library for submitting raw SQL via SQLAlchemy
Project description
sqla-raw
An opinionated, minimalist library for fetching data from a SQLAlchemy connection, when you don't need or want an ORM. You know how to write SQL; sqla-raw
wants to make it as simple as possible to send that SQL to your database and get results.
Really not much more than a single method (raw.db.result
) making it E-Z to submit raw SQL via a sqla Engine. Returns results as a list of dictionaries, with each dict keyed by column names.
As a further convenience, result_from_file
is almost the same except it will read the query from a given path, rather than taking a SQL string argument directly.
Installation
pip install sqla-raw[pg]
Usage
Configure your database connection string by setting $DATABASE_URL
in your environment.
>>> from raw import db
>>> x = db.result('select version()');
>>> x
[{'version': 'PostgreSQL 10.10 on x86_64-apple-darwin14.5.0, compiled by Apple LLVM version 7.0.0 (clang-700.1.76), 64-bit'}]
Because it's SQLAlchemy, you can safely use named parameters in your SQL string with colon-prepended :key
format, and assign values in kwargs
.
>>> db.result('select :foo as bar', foo='baz')
[{'bar': 'baz'}]
You can also use Jinja2 templating syntax to interpolate the query, if desired. db.result()
simply inspects the query for template tags ("{%.*%}"
) and renders the template to SQL before submitting. (It uses a SandboxedEnvironment
for some measure of injection safety, but avoid this option with untrusted inputs, for obvious reasons.)
Tests
pytest
tests are located in tests/. Install test prerequisites with pip install -r tests/requirements.txt
, then they can be run with: python setup.py test
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
Built Distribution
Hashes for sqla_raw-0.4.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 013693eaa646dd170b10187154eb5ae9def80142528fc9ac1e57e7acb4d79ecc |
|
MD5 | 4b69dcfeee4c795101e32d056ca89c7e |
|
BLAKE2b-256 | 07d83313a7393dfdd4bb5e4c63960aea1cd8e43b0c29787ba0962c6f098b7fd4 |