diff --git a/builq.go b/builq.go index 6298cfb..631e6e6 100644 --- a/builq.go +++ b/builq.go @@ -120,7 +120,13 @@ func (b *Builder) build() (_ string, _ []any) { err := b.write(&query, &resArgs, format, args...) b.setErr(err) } - return query.String(), resArgs + + // drop last separator for clarity. + q := query.String() + if q[len(q)-1] == b.sep { + q = q[:len(q)-1] + } + return q, resArgs } func (b *Builder) asSlice(v any) []any { diff --git a/example_test.go b/example_test.go index 3d29863..2a32be6 100644 --- a/example_test.go +++ b/example_test.go @@ -21,8 +21,10 @@ func ExampleNew() { panic(err) } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // @@ -51,10 +53,14 @@ func ExampleBuilder() { sb.Addf(orClause2, 42) query, args, err := sb.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -73,7 +79,9 @@ func ExampleOnelineBuilder() { b.Addf("WHERE id = %$", 123) query, _, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } fmt.Print(query) @@ -113,10 +121,14 @@ func ExampleColumns() { b.Addf("VALUES (%?, %?, %?);", params...) query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -135,10 +147,14 @@ func Example_query1() { Addf("AND user_id = %$ OR user = %$", 42, "root") query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -159,10 +175,14 @@ func Example_query2() { b.Addf("LIMIT 100;") query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -184,10 +204,14 @@ func Example_query3() { Addf("LIMIT 100;") query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -229,10 +253,14 @@ func Example_queryWhere() { } query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -255,10 +283,14 @@ func Example_slicePostgres() { b.Addf("VALUES (%+$);", params) query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -276,10 +308,14 @@ func Example_sliceMySQL() { b.Addf("VALUES (%+?);", params) query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -299,10 +335,14 @@ func Example_insertReturn() { b.Addf("RETURNING %s;", cols) query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -324,10 +364,14 @@ func Example_batchPostgres() { b.Addf("VALUES %#$;", params) query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -348,10 +392,14 @@ func Example_batchMySQL() { b.Addf("VALUES %#?;", params) query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -372,10 +420,14 @@ func Example_sliceInBatch() { b.Addf("VALUES %#?;", params) query, args, err := b.Build() - panicIf(err) + if err != nil { + panic(err) + } - fmt.Printf("query:\n%v", query) - fmt.Printf("args:\n%v", args) + fmt.Println("query:") + fmt.Println(query) + fmt.Println("args:") + fmt.Println(args) // Output: // query: @@ -384,9 +436,3 @@ func Example_sliceInBatch() { // args: // [42 [1 2 3] 69 [4 5 6]] } - -func panicIf(err error) { - if err != nil { - panic(err) - } -}