Skip to main content

Write queries in SQL.

Project description

# SQL-template

SQL is a fantastic language — one of the most successful programming languages in the world. We should use it, not try to replace it with a bespoke DSL.

Yet there are a couple of things that are nice to have help with in constructing SQL queries:

  • dialect-aware safe value substitution: Every database interface has its own syntax for substituting values safely (not to allow SQL injection) — for example, $1 or ? or :varname. They also have different requirements for the format of the sql + values argument lists. I want to able to write my queries with the same value substituion syntax, regardless of which database interface I am using, and know that the SQL will be output correctly for my interface, and that the values will be passed to the database engine safely.
  • dynamic attributes: In many applications, I don’t know in advance which attributes I am going to select, insert, update, or filter by. I want to SELECT a given list of attributes, or filter WHERE a given key/value mapping, or UPDATE or INSERT particular attributes, without having to rewrite the SQL query.
  • block composition: Some SQL queries are very complex. I want to able to compose blocks of SQL into larger queries, so that I can manage this complexity effectively. (Most database query DSLs are unable to deal with complex queries, or they invent a hard-to-learn language for writing those queries. Learning SQL is a better use of our time, but having some assistance managing/manipulating the different blocks in a query would be very helpful.)

Behold SQL-template:

  • One class, SQL, with one field, query, and one method, .render, which takes one optional argument, dialect.
  • Colon-key syntax (:varname) for value replacement, which can be rendered to the other value-replacement dialects.
  • Dynamic attribute/value lists in SELECT, WHERE, INSERT, and UPDATE syntax via ??? [TBD]
  • Block composition via ??? [TBD]
  • Supported value substitution dialects: postgresql ($1), sqlalchemy (:varname), embedded (:varname), mysql (%(varname)s), sqlite (?). Default style is :varname.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sql-query, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size sql_query-0.1.0-py2.py3-none-any.whl (7.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size sql-query-0.1.0.tar.gz (2.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page