sqltmplx 示例
sqltmplx 示例
这一页汇总了 examples/sqltmplx 下的可运行程序,并说明它们分别覆盖哪些 API 场景。
本地运行
在 examples/sqltmplx 模块目录下执行:
cd examples/sqltmplx
go run ./basic
go run ./postgres
go run ./sqlite_update
go run ./precompile示例矩阵
| 示例 | 重点 | 目录 |
|---|---|---|
basic | MySQL 方言、注册式 validator 选择、map 参数 | examples/sqltmplx/basic |
postgres | PostgreSQL bind 变量、结构体 tag 绑定、parser 校验 | examples/sqltmplx/postgres |
sqlite_update | 动态 set 语句清理 | examples/sqltmplx/sqlite_update |
precompile | Compile() 一次,多次渲染 | examples/sqltmplx/precompile |
示例:动态 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)
}示例:动态 SET
engine := sqltmplx.New(sqlite.New())
bound, err := engine.Render(tpl, UpdateCommand{
ID: 42,
Name: "alice",
Status: "active",
})
if err != nil {
panic(err)
}示例:模板复用
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"})