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

Add handy Q helper #36

Merged
merged 1 commit into from
Mar 13, 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
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
Loading