Skip to content

Commit

Permalink
Better debug (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Apr 20, 2023
1 parent 764bdc5 commit 12a3b0b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
15 changes: 11 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package builq_test
import (
"fmt"
"regexp"
"time"

"github.com/cristalhq/builq"
)
Expand Down Expand Up @@ -56,18 +57,24 @@ func ExampleOnelineBuilder() {
func ExampleBuilder_DebugBuild() {
cols := builq.Columns{"foo", "bar"}

ts := time.Date(2009, time.November, 10, 12, 13, 15, 16, time.UTC)

var sb builq.Builder
sb.Addf("SELECT %s FROM table", cols)
sb.Addf("WHERE id = %$", 123)
sb.Addf("OR id = %$ + %d", "42", 690)
sb.Addf("OR id = %$ + %d", "42", 69.069)
sb.Addf("XOR created_at = %$", ts)
sb.Addf("MAYBE IN arr = %$", []int{1, 2, 3})

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

// Output:
// debug:
// SELECT foo, bar FROM table
// WHERE id = 123
// OR id = '42' + 690
// OR id = '42' + 69.069
// XOR created_at = '2009-11-10 12:13:15:999999'
// MAYBE IN arr = '[1 2 3]'
}

func ExampleColumns() {
Expand Down Expand Up @@ -171,7 +178,7 @@ func Example_queryWhere() {
"category": []int{1, 2, 3},
"pat": regexp.MustCompile("pat+"),
"prob": 0.42,
"limit": 100,
"limit": 100.1,
}

var b builq.Builder
Expand Down Expand Up @@ -208,7 +215,7 @@ func Example_queryWhere() {
// AND category IN ($2, $3, $4)
// AND page LIKE 'pat+'
// AND prob < 0.42
// LIMIT 100;
// LIMIT 100.1;
// args:
// [the best 1 2 3]
}
Expand Down
40 changes: 31 additions & 9 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"strings"
"time"
)

func (b *Builder) write(sb *strings.Builder, resArgs *[]any, s string, args ...any) error {
Expand Down Expand Up @@ -81,14 +82,7 @@ func (b *Builder) writeSlice(sb *strings.Builder, resArgs *[]any, verb byte, arg

func (b *Builder) writeArg(sb *strings.Builder, resArgs *[]any, verb byte, arg any) {
if b.debug {
switch arg := arg.(type) {
case string:
sb.WriteByte('\'')
sb.WriteString(arg)
sb.WriteByte('\'')
default:
fmt.Fprint(sb, arg)
}
b.writeDebug(sb, arg)
return
}

Expand Down Expand Up @@ -134,10 +128,38 @@ func (b *Builder) writeArg(sb *strings.Builder, resArgs *[]any, verb byte, arg a
}
}

func (b *Builder) writeDebug(sb *strings.Builder, arg any) {
switch arg := arg.(type) {
case Columns:
sb.WriteString(arg.String())
case time.Time:
sb.WriteByte('\'')
sb.WriteString(arg.UTC().Format("2006-01-02 15:04:05:999999"))
sb.WriteByte('\'')
case fmt.Stringer:
sb.WriteByte('\'')
sb.WriteString(arg.String())
sb.WriteByte('\'')
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64:
fmt.Fprint(sb, arg)
case string:
sb.WriteByte('\'')
sb.WriteString(arg)
sb.WriteByte('\'')
default:
sb.WriteByte('\'')
fmt.Fprint(sb, arg)
sb.WriteByte('\'')
}
}

func (b *Builder) assertNumber(v any) {
switch v.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64:
uint, uint8, uint16, uint32, uint64,
float32, float64:
default:
b.setErr(errNonNumericArg)
}
Expand Down

0 comments on commit 12a3b0b

Please sign in to comment.