Skip to content

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 ./precompile

Example Matrix

ExampleFocusDirectory
basicMySQL dialect, registry-based validator selection, map parametersexamples/sqltmplx/basic
postgresPostgreSQL bind variables, struct tag binding, parser-backed validationexamples/sqltmplx/postgres
sqlite_updateset block cleanup for dynamic update statementsexamples/sqltmplx/sqlite_update
precompileCompile() once and render many timesexamples/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"})