Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ templates. A string template, rather than a file, is parsed with `p.Parse` inste
`bisql.ExpandFile` functions are shortcuts that construct a single-use parser from the given
options.

`Build` accepts the parameters as a `map[string]any`, a **struct**, or an `expr.Scope`. Passing a
struct matches exported fields to bind names (an embedded struct's fields are promoted, and also
reachable qualified, e.g. `Filter.Status`), so parameters carry Go's type checking rather than
being untyped map values.

A `Statement` exposes the following members:

| Member | Type | Description |
Expand Down
29 changes: 29 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,35 @@ func Example_dialects() {
// sqlserver: select id from users where name = @p1 and age >= @p2
}

// Build accepts a typed struct as well as a map[string]any, so parameters can be passed with
// Go's type checking rather than as untyped map values. Exported fields are matched to bind
// names; an embedded struct's fields are promoted (and also reachable qualified, e.g.
// Filter.Status).
func Example_structParams() {
type Filter struct {
Status string
MinAge int
}
type Params struct {
Filter
Name string
}
tmpl, _ := bisql.Parse(
"select id from users where 1 = 1" +
" and status = /*Status*/'active'" +
" and age >= /*MinAge*/0" +
" and name like /*Name*/'%x%'",
)
stmt, _ := tmpl.Build(Params{Filter: Filter{Status: "active", MinAge: 18}, Name: "%ali%"})
fmt.Println(stmt.SQL)
fmt.Println(stmt.Args)
fmt.Println(stmt.SQLWithArgs())
// Output:
// select id from users where 1 = 1 and status = ? and age >= ? and name like ?
// [active 18 %ali%]
// select id from users where 1 = 1 and status = 'active' and age >= 18 and name like '%ali%'
}

// --- Function examples. ---

// Parse compiles a template string into a reusable *Template.
Expand Down
Loading