@@ -38,6 +38,31 @@ type OnelineBuilder struct {
38
38
Builder
39
39
}
40
40
41
+ // BuildFn represents [Builder.Addf]. Just for the easier BuilderFunc declaration.
42
+ type BuildFn func (format constString , args ... any ) * Builder
43
+
44
+ // Build the query and arguments.
45
+ func (q BuildFn ) Build () (query string , args []any , err error ) {
46
+ return q ("" ).Build ()
47
+ }
48
+
49
+ // DebugBuild the query, good for debugging but not for REAL usage.
50
+ func (q BuildFn ) DebugBuild () string {
51
+ return q ("" ).DebugBuild ()
52
+ }
53
+
54
+ // New returns a new query builder, same as [Builder].
55
+ func New () BuildFn {
56
+ var b Builder
57
+ return b .Addf
58
+ }
59
+
60
+ // New returns a new query builder, same as [OnelineBuilder].
61
+ func NewOneline () BuildFn {
62
+ var b OnelineBuilder
63
+ return b .Addf
64
+ }
65
+
41
66
// Addf formats according to a format specifier, writes to query and appends args.
42
67
// Format param must be a constant string.
43
68
func (b * OnelineBuilder ) Addf (format constString , args ... any ) * Builder {
@@ -56,30 +81,32 @@ func (b *Builder) Addf(format constString, args ...any) *Builder {
56
81
return b .addf (format , args ... )
57
82
}
58
83
59
- func (b * Builder ) addf (format constString , args ... any ) * Builder {
60
- if len (b .parts ) == 0 {
61
- // TODO: better defaults
62
- b .parts = make ([]string , 0 , 10 )
63
- b .args = make ([][]any , 0 , 10 )
64
- }
65
- b .parts = append (b .parts , string (format ))
66
- b .args = append (b .args , args )
67
- return b
68
- }
69
-
84
+ // Build the query and arguments.
70
85
func (b * Builder ) Build () (query string , args []any , err error ) {
71
86
query , args = b .build ()
72
87
return query , args , b .err
73
88
}
74
89
90
+ // DebugBuild the query, good for debugging but not for REAL usage.
75
91
func (b * Builder ) DebugBuild () (query string ) {
76
92
b .debug = true
77
93
query , _ = b .build ()
78
94
b .debug = false
79
95
return query
80
96
}
81
97
82
- func (b * Builder ) build () (string , []any ) {
98
+ func (b * Builder ) addf (format constString , args ... any ) * Builder {
99
+ if len (b .parts ) == 0 {
100
+ // TODO: better defaults
101
+ b .parts = make ([]string , 0 , 10 )
102
+ b .args = make ([][]any , 0 , 10 )
103
+ }
104
+ b .parts = append (b .parts , string (format ))
105
+ b .args = append (b .args , args )
106
+ return b
107
+ }
108
+
109
+ func (b * Builder ) build () (_ string , _ []any ) {
83
110
var query strings.Builder
84
111
// TODO: better default (sum of parts + est len of indexes)
85
112
query .Grow (100 )
@@ -121,7 +148,7 @@ var (
121
148
// errUnsupportedVerb when %X is found and X isn't supported.
122
149
errUnsupportedVerb = errors .New ("unsupported verb" )
123
150
124
- // errIncorrectVerb is passed like `%+`.`
151
+ // errIncorrectVerb is passed like `%+`.
125
152
errIncorrectVerb = errors .New ("incorrect verb" )
126
153
127
154
// errMixedPlaceholders when $ AND ? are mixed in 1 query.
0 commit comments