Skip to main content

Composition of linked SQL files

Project description

sqlcompose: Composition of linked SQL files

sqlcompose allows you to compose sql files from multiple files by introducing INCLUDE keywords. The SQL output is composed as CTE's or Common Table Expressions.

Examples

Execute the script directly:

sqlcompose query.sql
sqlcompose 'select * from $INCLUDE(included-query1.sql)' # on linux
sqlcompose "select * from $INCLUDE(included-query1.sql)" # on windows

Import it in another script:

from sqlcompose import load, loads
# method 1 : loading from a file
sql1 = load("query.sql")

# method 2 : loading from an SQL string
sql2 = loads("""
    select *
      from dataset.table main
inner join $INCLUDE(other.sql) other
        on other.field = main.field
  """)

Preparing SQL scripts

Insert a $INCLUDE(filename) where the reference to the file should be in the resulting SQL, keeping in mind that references are loaded relative to the file loaded or the current working dir in case of an SQL string.

--main-query.sql
select * from $INCLUDE(includes\included-query2.sql)
--included-query1.sql
select 1 as test
--included-query2.sql
select * from $INCLUDE(included-query1.sql)
union all
select * from $INCLUDE(nested\included-query3.sql)
--nested\included-query3.sql
select 1 as test

Which outputs:

WITH Q_1_1 AS (
  WITH Q_2_1 AS (
    --includes\included-query1.sql
    select 1 as test
  ), Q_2_2 AS (
    --includes\nested\included-query3.sql
    select 1 as test
  ), Q_2 AS (
    --includes\included-query2.sql
    select * from Q_2_1
    union all
    select * from Q_2_2
  )
  SELECT * FROM Q_2
), Q_1 AS (
  --test\main-query.sql
  select * from Q_1_1
)
SELECT * FROM Q_1

Project details


Download files

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

Source Distribution

sqlcompose-0.0.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlcompose-0.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file sqlcompose-0.0.1.tar.gz.

File metadata

  • Download URL: sqlcompose-0.0.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sqlcompose-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b68fe4c33a9464d1d1f7c773176a57daf5db1d0387fe5b717e32ac8cf2e1e48c
MD5 da62bcd90b5ebd9894f74de4df15c96f
BLAKE2b-256 7a8525f6c9a93da9666390021eaac2895fa20860b315e1e62131b8e7eca385b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcompose-0.0.1.tar.gz:

Publisher: python-publish.yml on apmadsen/sqlcompose

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlcompose-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sqlcompose-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sqlcompose-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a77068c722194252ed2fcc68feb7b0639b5c4dfdc6d7bda92bb4e7e7ca90965
MD5 32543cd1e30d84eebf94ed9d8f7a7db6
BLAKE2b-256 dfce899e0c88599618d91081c0a78ef88bfaa7429af05bfba49f43a4469351c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcompose-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on apmadsen/sqlcompose

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page