sqlinclude
Tiny SQL source preprocessor. Expands @include directives and @define
variables recursively. No SQL parsing, no opinion about any database. Intended
to be piped into a query tool:
sqlinclude analysis.sql | bq query
bq query "$(sqlinclude analysis.sql)"
Written for Unix and Windows alike: it is a single dependency-free Python
package and installs a sqlinclude console command (a sqlinclude.exe on
Windows).
Install
pipx install sqlinclude
or, into the current environment:
pip install sqlinclude
Requires Python 3.12+.
Directives
Line-oriented, leading whitespace allowed:
@include file.sql
@include "file.sql"
@define name = value (the `= ` is optional; value is the rest of the
line, kept verbatim including quotes)
Include paths are resolved relative to the including file. Includes may nest; cycles are detected and reported as errors.
Variables form a single environment filled in expansion order: the first
@define name ... seen anywhere in the tree wins, and later definitions of the
same name are ignored (so a parent file's definition always overrides an
included fragment's default). Definitions from an included file leak back to
the including file. A @name with no definition passes through unchanged --
BigQuery's native @param syntax is never touched.
Each include block is bracketed with -- #line N "file" markers (with forward
slashes on every platform) so error messages from downstream tools point at the
originating source file and line. Use -n/--no-markers to suppress them.
Encoding
Sources are read as UTF-8 (a leading byte-order mark is ignored), and stdin,
stdout, and stderr are forced to UTF-8. Queries containing non-ASCII characters
still work when the output is piped or redirected on Windows, where the console
code page (e.g. cp1250) cannot represent them. Use --ascii if your terminal
cannot render the Unicode box characters in --tree.
Example
analysis.sql:
@define start = '2024-01-01'
@define end = '2024-12-31'
WITH users AS (
@include "users.sql"
),
orders AS (
@include orders.sql
)
SELECT ... WHERE created BETWEEN @start AND @end
Run:
sqlinclude analysis.sql | bq query
sqlinclude --vars start='2024-06-01' analysis.sql | bq query
sqlinclude --tree analysis.sql # show the include tree, not SQL
sqlinclude --tree --ascii analysis.sql # ASCII box characters instead of Unicode
Options
| Option | Description |
|---|---|
file |
SQL file to preprocess (default: read stdin) |
-n, --no-markers |
do not emit -- #line markers around includes |
--vars name=value |
set a variable (repeatable); wins over @define |
-e, --edit |
open the editor even when no variable is undefined |
--no-edit |
never open the editor; leave undefined @names as-is |
-t, --tree |
print the @include dependency tree instead of SQL |
--ascii |
use ASCII box characters in the include tree |
--version |
show the version |
Editing undefined variables
When a variable is undefined, sqlinclude opens the editor named by
$VISUAL/$EDITOR on the controlling terminal, even when its output is piped.
Edit values, or delete a line to leave that variable undefined. Blank lines and
# comments are ignored. On Windows the same variables are honored, falling
back to notepad. With -e/--edit the buffer also shows the @include
tree. Undefined variables are always reported on stderr; pass --no-edit to
silence everything and pass undefined names through untouched.
Development
python -m venv .venv
. .venv/bin/activate
pip install -e ".[dev]"
pytest
License
MIT -- see LICENSE.
Metadata
Release files for sqlinclude 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sqlinclude-0.1.1.tar.gz | 14.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqlinclude-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.8 kB
Release files / sqlinclude-0.1.1.tar.gz
| Download URL | sqlinclude-0.1.1.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
26fc7409b2c6d5ab450bc5568bc7c8113f9d73990b02f12b860006013f99ee58
|
|
BLAKE2b-256 checksum How to use checksums |
dc2a9b10b45723d696ffd6be27128a62050d824516ce355876390ca4905c9d3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Release files / sqlinclude-0.1.1-py3-none-any.whl
| Download URL | sqlinclude-0.1.1-py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
67bbaf4b90e689ec22dd19e6a27565fde6e9e14be228dc1f61e09483e50b48c1
|
|
BLAKE2b-256 checksum How to use checksums |
1f2a5cbcb6b988ffeb4055ae2a7d2b9c7c8729892a1bc31c71100bc15ee471f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|