sqltmplx examples
sqltmplx Examples
This page collects the runnable examples/sqltmplx programs and maps them to the API surface they demonstrate.
Run Locally
Run from the examples/sqltmplx module:
cd examples/sqltmplx
go run ./basic
go run ./postgres
go run ./sqlite_update
go run ./precompileExample Matrix
| Example | Focus | Directory |
|---|---|---|
basic | MySQL dialect, registry-based validator selection, map parameters | examples/sqltmplx/basic |
postgres | PostgreSQL bind variables, struct tag binding, parser-backed validation | examples/sqltmplx/postgres |
sqlite_update | set block cleanup for dynamic update statements | examples/sqltmplx/sqlite_update |
precompile | Compile() once and render many times | examples/sqltmplx/precompile |
Example: Dynamic WHERE
engine := sqltmplx.New(
postgres.New(),
sqltmplx.WithValidator(validate.NewSQLParser(postgres.New())),
)
bound, err := engine.Render(tpl, Query{
Tenant: "acme",
Name: "alice",
IDs: []int{1, 2, 3},
})
if err != nil {
panic(err)
}Example: Dynamic SET
engine := sqltmplx.New(sqlite.New())
bound, err := engine.Render(tpl, UpdateCommand{
ID: 42,
Name: "alice",
Status: "active",
})
if err != nil {
panic(err)
}Example: Template Reuse
engine := sqltmplx.New(mysql.New())
tpl, err := engine.Compile(queryText)
if err != nil {
panic(err)
}
bound1, _ := tpl.Render(map[string]any{"Tenant": "acme", "Status": "PAID"})
bound2, _ := tpl.Render(map[string]any{"Tenant": "acme", "Status": "SHIPPED"})