Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

What is this library ?

This library allows to make a SQL script (or possibly any script) addressable. Once preprocessed or parsed, you can get any chunk of it (which could be a single query for instance) with its line number, key, or index of the chunk.

Example & How to use:

With a target SQL script : (toggle)

raw_script.sql :

CREATE TEMPORARY VIEW fibonacci (a, index_) AS
WITH RECURSIVE inner_fibo(b, c, index_) AS (
    SELECT 0, 1, 0
    UNION
    SELECT c, c + b, index_ + 1
    FROM inner_fibo
)
SELECT b, index_ FROM inner_fibo;

SELECT * FROM fibonacci LIMIT 1 OFFSET 3;
SELECT * FROM fibonacci LIMIT 1 OFFSET 4;
SELECT * FROM fibonacci LIMIT 1 OFFSET 5;
SELECT * FROM fibonacci LIMIT 1 OFFSET 10;


CREATE TEMPORARY TABLE test_table (a integer) STRICT;
We modify the above script by adding the following delimiter :
 -- % key
where key is the string that will reference a particular chunk of the script that we want to separate and make addressable. Note the prepended space. The delimiter is modifiable. For instance :

raw_script.sql :

 -- % fibonacci_view_creation
CREATE TEMPORARY VIEW fibonacci (a, index_) AS
WITH RECURSIVE inner_fibo(b, c, index_) AS (
    SELECT 0, 1, 0
    UNION
    SELECT c, c + b, index_ + 1
    FROM inner_fibo
)
SELECT b, index_ FROM inner_fibo;

 -- % contiguous_view_tests
SELECT * FROM fibonacci LIMIT 1 OFFSET 3;
SELECT * FROM fibonacci LIMIT 1 OFFSET 4;
SELECT * FROM fibonacci LIMIT 1 OFFSET 5;
 -- % testing_10nth_value_of_view
SELECT * FROM fibonacci LIMIT 1 OFFSET 10;


CREATE TEMPORARY TABLE test_table (a integer) STRICT;

Then we preprocess the script :

import addressable_sql_queries
with (open("raw_script.sql") as input_file,
    open("preprocessed_script.py", "w") as output_file):
    output_file.write(
        addressable_sql_queries.preprocess_to_str(input_file.read()))

This step can also be done with :

python -m addressable_sql_queries path/to/raw_script.sql -o path/to/preprocessed_script.py

As all keys in the example above are not only valid but usable python ids, you can then access chunks of this script like this :

from preprocessed_script import *
print(contiguous_view_tests)

Which will outputs :

 -- % contiguous_view_tests
SELECT * FROM fibonacci LIMIT 1 OFFSET 3;
SELECT * FROM fibonacci LIMIT 1 OFFSET 4;
SELECT * FROM fibonacci LIMIT 1 OFFSET 5;

Even when keys are invalid ids, you can access the chunks of the script by these means :

from preprocessed_script import queries as sql_queries
print(sql_queries[0]) # This will print the chunk before the first delimiter, which is nothing in this case : "\n".
print(sql_queries[None]) # Another way to do the same thing. None is always the first key.
print(sql_queries["fibonacci_view_creation"])
"""This will print :
 -- % fibonacci_view_creation
CREATE TEMPORARY VIEW fibonacci (a, index_) AS
WITH RECURSIVE inner_fibo(b, c, index_) AS (
    SELECT 0, 1, 0
    UNION
    SELECT c, c + b, index_ + 1
    FROM inner_fibo
)
SELECT b, index_ FROM inner_fibo;


"""
print(sql_queries[1]) # This will print the same thing as above (the second chunk).
print(sql_queries.having_line(4)) # This will print the chunk containing the line 4 (counting from 1), and thus does the same thing as above.

Parser mode example :

Alternatively, parser mode can be used :

from addressable_sql_queries import Query
q = Query.fromFile("raw_script.sql")
print(q[3])
"""This will print :
 -- % testing_10nth_value_of_view
SELECT * FROM fibonacci LIMIT 1 OFFSET 10;


CREATE TEMPORARY TABLE test_table (a integer) STRICT;
"""

Query object can also be built directly from a string.

Note : in the case where the first line is a delimiter, thus making the first and second chunk shares the first line (starting at 1), the having_line function will return the second, non empty chunk.

Metadata

Release files for addressable-sql-queries 0.0.1.dev1

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

Source distribution (sdist)

Source distribution for addressable-sql-queries 0.0.1.dev1
File Size Uploaded Provenance
addressable_sql_queries-0.0.1.dev1.tar.gz 12.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for addressable-sql-queries 0.0.1.dev1
File Interpreter ABI Platform
addressable_sql_queries-0.0.1.dev1-py3-none-any.whl Python 3 none any Details

Total release size: 22.1 kB

Release files / addressable_sql_queries-0.0.1.dev1.tar.gz

Download URL addressable_sql_queries-0.0.1.dev1.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3d6784c6c06f1261f63925328048d990ed19858333dc905cc89782360de7da86
BLAKE2b-256 checksum
How to use checksums
cc5f9b25d8976f10bc8ea0f82521df691bb3c23b05f8f729c8713cf5372d2300
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release files / addressable_sql_queries-0.0.1.dev1-py3-none-any.whl

Download URL addressable_sql_queries-0.0.1.dev1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
501202d2a7cbcdc3f3e888b63695ec129794c55de24ab26ae33bafd56790ce8f
BLAKE2b-256 checksum
How to use checksums
e2fb5b9fa74b4fa65b49967cb334cf699c849168e9d6e3684da709b4023c9855
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Security for addressable-sql-queries 0.0.1.dev1

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.

Release history Release notifications | RSS feed

This release
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