Skip to main content

query-selector

https://travis-ci.org/solidsnack/query-selector.svg?branch=master

Query selector allows one treat a file full of SQL queries as a record, with one attribute for each annotated query. This makes working with long, ad-hoc SQL queries more hygienic, and has the benefit of making it easy to find the queries.

The QuerySelector constructor accepts a string, file handle or (<package>, <resource) pair and parses the SQL into groups annotated with --@ <name> <mode>. The <name> is any Python compatible name; it will become an attribute of the object. The <mode> is merely metadata, and can be omitted; it describes whether a query should have one, none or many results.

For example, a file like this:

--@ t one
SELECT now();

becomes an object with a single attribute t:

>>> q.t
Query(args=[], mode=u'one', readonly=False, text=u'SELECT * FROM now();')

A QuerySelector object is iterable, providing pairs of name and query in the order that the queries originally appeared in the file.

>>> for name, q in qs:
...     print '%s: %s' % (name, q)
t: Query(args=[], mode=u'one', readonly=True, text='SELECT now();')

The Query Convention

If you have a script task.py and a SQL file task.sql, or a module in a package package/module.py and a SQL file package/module.sql, QuerySelector has a shortcut for you:

from query_selector.magic import queries


for q in queries:
    print q

The magic module overrides the normal module loading machinery to determine which script or module is importing it and locate an adjacent SQL file. This magic is in a separate module to make it stricly opt-in!

Modes

Modes can be one of none, one, one? and many. When not specified, default is many. A mode string can also be followed with the single word ro as a clue that the query is read-only.

Realistically, SELECT now() is a read-only query. We can annotate it as such, the resulting query datastructure records this:

>>> QuerySelector("""
...   --@ t one ro
...   SELECT now();
... """).t
Query(args=[], mode=u'one', readonly=True, text=u'SELECT * FROM now();')

Parameters

query-selector recognizes the %(...)s style parameter references defined in Python DBI 2.0. Say that we’d like to pass a timezone when selecting the server time. We can do so by adding AT TIME ZONE %(tz)s to our query. The presence of this parameter is stored in the args field of the parsed result. (The parameters in .args are listed in the order of their first appearance in the query.)

>>> QuerySelector("""
...     --@ t one ro
...     SELECT now() AT TIME ZONE %(tz)s AS t;
... """).t
Query(args=[u'tz'], mode=u'one', readonly=True,
      text=u'SELECT now() AT TIME ZONE %(tz)s AS t;')

Metadata

Release files for query-selector 0.99.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for query-selector 0.99.4
File Size Uploaded Provenance Actions
query-selector-0.99.4.tar.gz 5.3 kB Without provenance Details for query-selector-0.99.4.tar.gz

Release files / query-selector-0.99.4.tar.gz

Download URL query-selector-0.99.4.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a71cb745d1452cffd1910a80c319f15fc34db6eae4b5e50ca8134460bd2b189c
BLAKE2b-256 checksum
How to use checksums
d574ea01e308c61aa1e67ecf2fbfbca5c6c063b683f5e40477bbc06033bf3087
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for query-selector 0.99.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.99.4 This release

1 release file No provenance

0.98

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page