Skip to content

Commit

Permalink
Drop last separator (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Mar 13, 2024
1 parent ce019f6 commit e611632
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 46 deletions.
8 changes: 7 additions & 1 deletion builq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
136 changes: 91 additions & 45 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//
Expand Down Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)
}
}

0 comments on commit e611632

Please sign in to comment.