SQL templating for Go: authored .sql files made dynamic and composable, with a PostgreSQL
dialect and a conventions linter.
github.com/standards-lab/sqlate is the base module, and it imports only the standard library.
The postgres sub-module is the PostgreSQL dialect, the postgres/pgvector sub-module names
the pgvector extension, and the sqlint sub-module is the linter. Each sub-module is released on
its own tags.
Web services usually avoid real SQL. Queries are embedded as string literals in the host
language or generated by an ORM, and either way the SQL is written in a language that is not
SQL. sqlate keeps queries in .sql files, so SQL is expressed in its own language. It layers a
small syntax onto the files, so a file can express reusable fragments and compose dynamic
queries. The name combines SQL and template, for the composition the library adds to .sql
files.
- Concepts: patterns and statements by example, and the grammar the files are written in.
- Quick start: a working program from
go getto a linted, unit-tested store, each feature shown once. - Features: every feature, package by package, in the detail needed to use it without reading the source.
- Glossary: the vocabulary the library introduces, by layer.
The package documentation, go doc github.com/standards-lab/sqlate/..., is the reference for
each exported name.
A statement is a .sql file with a header of declarations and a body. The library reads the
header; the body is the SQL the engine receives. The engine is the database server the driver
sends SQL to, PostgreSQL through the postgres sub-module. This statement edits a row under an
optimistic-concurrency guard:
--| tier: standard
UPDATE team
SET name = {{name}}, {{> sql.guard_set}}
WHERE {{> sql.guard_where}}{{name}} is a parameter, bound by name. {{> sql.guard_set}} and {{> sql.guard_where}}
are includes of two patterns the library publishes under its own namespace, sql:
--| tier: standard
-- The protocol columns a guarded command advances; the command's own SET
-- list precedes it.
updated_at = CURRENT_TIMESTAMP, version = version + 1--| tier: standard
-- The optimistic-concurrency guard's predicate, over a guarded table's id
-- and version columns; a command includes it as {{> sql.guard_where}}.
id = {{id}} AND version = {{version}}At compile time the library splices the patterns' bodies into the statement, and the pattern's parameters become the statement's. Compiled for PostgreSQL, the engine receives:
UPDATE team
SET name = $1, updated_at = CURRENT_TIMESTAMP, version = version + 1
WHERE id = $2 AND version = $3In Go, the statement directory is compiled once against the catalog of patterns, the statement
is fetched by its file name and bound to the value that runs it, and the value runs with its
arguments by name. db is the session over the program's own *sql.DB.
//go:embed statements/*.sql
var files embed.FS
catalog := query.MustCatalog(query.Patterns())
stmts := catalog.MustCompile(files, "statements", db.Dialect())
edit := stmts.Statement("edit").Guarded(stmts.Statement("version"), "version")
// id is the row's key and current the version the caller read.
version, err := edit.Run(ctx, db, current, query.Args{"id": id, "name": "Core Platform"})Run returns the new version, sql.ErrNoRows when no row has that id, and
query.ErrVersionMismatch when the row exists at another version than current.
Three conventions the library keeps are stricter than a reader might expect:
- The base module depends on the standard library alone. A sourced dependency enters only
through a sub-module's
go.mod: pgx throughpostgres, the TOML parser throughsqlint. sqltestis a public package: the scripted driver the library's own tests run over is the one a consumer's tests run over too, so a unit test needs no database.- Every statement and pattern declares its tier, standard or native, and a native file names
the engine feature it uses and the port to other engines. The port list of a code base is one
search for
--| tier: native.
sqlate(the root) is the session layer:DBover a plain*sql.DB, transactions, theDialectinterface, and the error taxonomy every engine returns.headerreads the--|declaration header of a.sqlfile.querycompiles authored statements against the pattern catalog and binds them to typed values: rows, projections with request directives, and guards.migrateversions a schema over authored SQL, and reconciles the extensions an application declares, under the engine's lock, and replaces a third-party migration dependency.sqltestis the scripteddatabase/sqldriver for hermetic tests.postgres(sub-module) is the PostgreSQL dialect: placeholders, error classification, the advisory lock, the extension statements, and the engine's native forms. Itspgtestpackage gives an integration test its own throwaway database, migrated or empty, dropped when the test ends.postgres/pgvector(sub-module) names the pgvector extension: its catalog name,vector, and the metric table of thevectortype, each metric's distance operator and index operator class. Itssqlint.tomlexports pgvector's native forms. It imports only the standard library; its tests takepostgresforpgtest.sqlint(sub-module) is the conventions linter as a package, with the command atsqlint/cmd/sqlint.
The repository is a Go workspace of four modules. Tasks run through mise:
mise run check # build, vet, format, fix, tidy, unit tier, and lint every module (vet, fix, and lint take the integration suites too), then sqlint, lint-context, lint-references; writes nothing
mise run currency # report tools, Go, requirements, actions, images, and Dockerfile source ARGs behind their latest, except a held one; writes nothing
mise run upgrade # move every pin currency reports to its latest, except a held one: tools, then Go and requirements, actions, images, and Dockerfile source ARGs
mise run db:up # build and start the compose PostgreSQL and wait until it is healthy
mise run db:down # stop PostgreSQL, keeping its data
mise run db:reset # stop PostgreSQL and drop its data
mise run integration # run the integration tier of postgres and postgres/pgvector against the running PostgreSQL
mise run acceptance # run db:up and integration, then db:reset whatever the result
mise run build # build the sqlint command into the git-ignored bin/
mise run fmt # format all Go source
mise run tidy # run go mod tidy in every modulecompose.yml runs PostgreSQL on port 5433, beside a service's own on 5432; POSTGRES_PORT
moves it. The service builds from compose/postgres/Dockerfile, whose FROM line is its one
image pin and which carries its configuration and health check. The Dockerfile builds pgvector
from source at ARG PGVECTOR_VERSION, the extension's one version pin
(the image). mise run currency reports either pin
when it trails its latest: it compares the FROM tag with the image's tags, and the ARG with
the highest semver tag of the GitHub repository that the ADD line fetches; mise run upgrade
moves it. A pin named in mise.toml's HOLDS list, one <item> <pin>: <reason> line each, is
reported as held and never moved. mise.toml sets SQLATE_DSN, whose port follows
POSTGRES_PORT.
The integration tier covers two modules, postgres and postgres/pgvector, each behind the
integration build tag. mise run integration runs both against the running PostgreSQL, and
mise run acceptance starts the compose PostgreSQL, runs both, and drops its data. The tier runs
on a developer's machine, not in CI.
The compose PostgreSQL reports healthy once it accepts TCP connections to the app database. The probe goes over TCP, not the socket, because the first start's initialization runs a temporary server on the socket alone, which would report ready before the real server starts.
GO_MODULES in mise.toml lists the module directories, kept in sync with the go.work
use-list.