Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify examples #42

Merged
merged 2 commits into from
Mar 22, 2024
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
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,27 @@ if err != nil {
panic(err)
}

fmt.Printf("query:\n%v", query)
fmt.Printf("args:\n%v", args)
debug := q.DebugBuild()

fmt.Println("query:")
fmt.Println(query)
fmt.Println("\nargs:")
fmt.Println(args)
fmt.Println("\ndebug:")
fmt.Println(debug)

// Output:
//
// query:
// SELECT foo, bar FROM users
// WHERE active IS TRUE
// AND user_id = $1 OR user = $2
//
// args:
// [42 root]
//
// debug:
// SELECT foo, bar FROM 'users'
// WHERE active IS TRUE
// AND user_id = 42 OR user = 'root'
```

See examples: [example_test.go](example_test.go).
Expand Down
27 changes: 15 additions & 12 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func ExampleNew() {

fmt.Println("query:")
fmt.Println(query)
fmt.Println("args:")
fmt.Println("\nargs:")
fmt.Println(args)
fmt.Println("debug:")
fmt.Println("\ndebug:")
fmt.Println(debug)

// Output:
Expand All @@ -56,8 +56,10 @@ func ExampleNew() {
// SELECT foo, bar FROM users
// WHERE active IS TRUE
// AND user_id = $1 OR user = $2
//
// args:
// [42 root]
//
// debug:
// SELECT foo, bar FROM 'users'
// WHERE active IS TRUE
Expand Down Expand Up @@ -130,7 +132,8 @@ func ExampleBuilder_DebugBuild() {
b.Addf("MORE offset = %$", d)
b.Addf("MAYBE IN arr = %$", []int{1, 2, 3})

fmt.Printf("debug:\n%v", b.DebugBuild())
fmt.Println("debug:")
fmt.Println(b.DebugBuild())

// Output:
// debug:
Expand Down Expand Up @@ -283,27 +286,27 @@ func Example_queryWhere() {
"limit": 100.1,
}

var b builq.Builder
b.Addf("SELECT * FROM foo")
b.Addf("WHERE active IS TRUE")
q := builq.New()
q("SELECT * FROM foo")
q("WHERE active IS TRUE")

if name, ok := filter["name"]; ok {
b.Addf("AND name = %$", name)
q("AND name = %$", name)
}
if cat, ok := filter["category"]; ok {
b.Addf("AND category IN (%+$)", cat)
q("AND category IN (%+$)", cat)
}
if pat, ok := filter["pat"]; ok {
b.Addf("AND page LIKE '%s'", pat)
q("AND page LIKE '%s'", pat)
}
if prob, ok := filter["prob"]; ok {
b.Addf("AND prob < %s", prob)
q("AND prob < %s", prob)
}
if limit, ok := filter["limit"]; ok {
b.Addf("LIMIT %d;", limit)
q("LIMIT %d;", limit)
}

query, args, err := b.Build()
query, args, err := q.Build()
if err != nil {
panic(err)
}
Expand Down
Loading