Skip to content

Commit

Permalink
Add handy Q helper (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Mar 13, 2024
1 parent e611632 commit 10ff76d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions builq.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type OnelineBuilder struct {
Builder
}

// Q is a handy helper. Works as [NewOnline] and [Build] in one call.
func Q(format constString, args ...any) (query string, resArgs []any, err error) {
return NewOneline()(format, args...).Build()
}

// BuildFn represents [Builder.Addf]. Just for the easier BuilderFunc declaration.
type BuildFn func(format constString, args ...any) *Builder

Expand Down
20 changes: 20 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ import (
"github.com/cristalhq/builq"
)

func ExampleQ() {
cols := builq.Columns{"foo, bar"}

query, args, err := builq.Q("SELECT %s FROM %s WHERE id = %$", cols, "users", 123)
if err != nil {
panic(err)
}

fmt.Println("query:")
fmt.Println(query)
fmt.Println("args:")
fmt.Println(args)

// Output:
// query:
// SELECT foo, bar FROM users WHERE id = $1
// args:
// [123]
}

func ExampleNew() {
cols := builq.Columns{"foo, bar"}

Expand Down

0 comments on commit 10ff76d

Please sign in to comment.