Skip to content

Commit 12a3b0b

Browse files
authored
Better debug (#27)
1 parent 764bdc5 commit 12a3b0b

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

example_test.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package builq_test
33
import (
44
"fmt"
55
"regexp"
6+
"time"
67

78
"github.com/cristalhq/builq"
89
)
@@ -56,18 +57,24 @@ func ExampleOnelineBuilder() {
5657
func ExampleBuilder_DebugBuild() {
5758
cols := builq.Columns{"foo", "bar"}
5859

60+
ts := time.Date(2009, time.November, 10, 12, 13, 15, 16, time.UTC)
61+
5962
var sb builq.Builder
6063
sb.Addf("SELECT %s FROM table", cols)
6164
sb.Addf("WHERE id = %$", 123)
62-
sb.Addf("OR id = %$ + %d", "42", 690)
65+
sb.Addf("OR id = %$ + %d", "42", 69.069)
66+
sb.Addf("XOR created_at = %$", ts)
67+
sb.Addf("MAYBE IN arr = %$", []int{1, 2, 3})
6368

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

6671
// Output:
6772
// debug:
6873
// SELECT foo, bar FROM table
6974
// WHERE id = 123
70-
// OR id = '42' + 690
75+
// OR id = '42' + 69.069
76+
// XOR created_at = '2009-11-10 12:13:15:999999'
77+
// MAYBE IN arr = '[1 2 3]'
7178
}
7279

7380
func ExampleColumns() {
@@ -171,7 +178,7 @@ func Example_queryWhere() {
171178
"category": []int{1, 2, 3},
172179
"pat": regexp.MustCompile("pat+"),
173180
"prob": 0.42,
174-
"limit": 100,
181+
"limit": 100.1,
175182
}
176183

177184
var b builq.Builder
@@ -208,7 +215,7 @@ func Example_queryWhere() {
208215
// AND category IN ($2, $3, $4)
209216
// AND page LIKE 'pat+'
210217
// AND prob < 0.42
211-
// LIMIT 100;
218+
// LIMIT 100.1;
212219
// args:
213220
// [the best 1 2 3]
214221
}

write.go

+31-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strconv"
66
"strings"
7+
"time"
78
)
89

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

8283
func (b *Builder) writeArg(sb *strings.Builder, resArgs *[]any, verb byte, arg any) {
8384
if b.debug {
84-
switch arg := arg.(type) {
85-
case string:
86-
sb.WriteByte('\'')
87-
sb.WriteString(arg)
88-
sb.WriteByte('\'')
89-
default:
90-
fmt.Fprint(sb, arg)
91-
}
85+
b.writeDebug(sb, arg)
9286
return
9387
}
9488

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

131+
func (b *Builder) writeDebug(sb *strings.Builder, arg any) {
132+
switch arg := arg.(type) {
133+
case Columns:
134+
sb.WriteString(arg.String())
135+
case time.Time:
136+
sb.WriteByte('\'')
137+
sb.WriteString(arg.UTC().Format("2006-01-02 15:04:05:999999"))
138+
sb.WriteByte('\'')
139+
case fmt.Stringer:
140+
sb.WriteByte('\'')
141+
sb.WriteString(arg.String())
142+
sb.WriteByte('\'')
143+
case int, int8, int16, int32, int64,
144+
uint, uint8, uint16, uint32, uint64,
145+
float32, float64:
146+
fmt.Fprint(sb, arg)
147+
case string:
148+
sb.WriteByte('\'')
149+
sb.WriteString(arg)
150+
sb.WriteByte('\'')
151+
default:
152+
sb.WriteByte('\'')
153+
fmt.Fprint(sb, arg)
154+
sb.WriteByte('\'')
155+
}
156+
}
157+
137158
func (b *Builder) assertNumber(v any) {
138159
switch v.(type) {
139160
case int, int8, int16, int32, int64,
140-
uint, uint8, uint16, uint32, uint64:
161+
uint, uint8, uint16, uint32, uint64,
162+
float32, float64:
141163
default:
142164
b.setErr(errNonNumericArg)
143165
}

0 commit comments

Comments
 (0)