SQLpp
SQLpp
- SQL Preprocessor
SQLpp
(available in ./lib/pre-process/mod.ts
) is a cross-database utility
designed for pre-processing SQL files. Its functionality mirrors a subset of the
meta-commands provided by PostgreSQL’s psql
utility.
psql
is a terminal-based front-end to PostgreSQL. It enables users to type in
SQL queries interactively, issue them to PostgreSQL, and see the query results.
psql
also provides a number of meta-commands and various shell-like features
to facilitate writing scripts and automating a wide variety of tasks.
Key among these meta-commands are \set
and \include
, which allow for setting
variables and including other SQL scripts respectively.
SQLpp adopts the similar meta-command functionalities offered by psql
for
pre-processing SQL files. It enables you to define and override \set
variables
within your SQL source code and support include
statements, akin to psql
.
However, unlike psql
, which directly executes SQL queries on PostgreSQL
databases, SQLpp focuses on preparing SQL files for execution.
The primary advantage of SQLpp over psql
is its database-agnostic nature.
While psql
is specifically designed for PostgreSQL databases, SQLpp’s output
can be utilized across almost all databases. This includes SQLite, DuckDB,
Clickhouse local, Dolt, MySQL, SQL*Server, among others.
SQLpp provides a unique and efficient solution for SQL file pre-processing.
While it does not execute SQL like psql
, its focus on SQL preparation and its
wide compatibility with almost all databases make it an incredibly useful
utility in various SQL-related operations.
This does not mean that SQLpp is a substitute for SQL generators in all scenarios, but rather that it serves a distinct purpose and offers unique advantages in certain situations.
SQLpp, with its simple syntax mirroring a subset of PostgreSQL’s psql
utility
meta-commands, offers a more intuitive and easier to learn approach to SQL
pre-processing. This makes it an ideal tool for those new to SQL or those that
know existing tools like psql
well.
SQLpp is an excellent choice for situations where complex SQL generation is not necessary. This might include scenarios where you’re dealing with simpler queries, or when you need to quickly prototype SQL scripts without type-safety in mind.
Usage Summary
Use the help
command for usage instructions:
Usage: mod.ts psql <src>
Options:
Examples:
psql
-style Usage
Preprocess text in a way that psql
would. Specifically, allow \set
variables
to be defined in the source and overridden via CLI.
\include
Supports these types of include
statements (must be at the start of a line):
Similar to psql
, \i
and \include
are synonyms; relative files are resolved
from the current working directory but other rules can be provided as well.
\ir
Supports these types of include relative statements:
Similar to psql
, \ir
includes files relative to the location of the source
file. Because source file might not be clear in the context of SQLpp you may
need to provide a callback function to the preprocessor to define what “relative”
means.
\set
Supports these types of set
statements (must be at the start of a line):
Similar to psql
, supports this type of replacement:
Style | Replacement |
---|---|
:name | John |
:'name' | ’John’ |
:"name" | ”John” |
Examples
Assume this file:
Assume these declarations in a *.psql
file:
Running this command:
Produces this output: