Skip to content

Commit 0169e52

Browse files
committed
Use io.StringWriter instead of io.Writer for efficiency
1 parent 96da65f commit 0169e52

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+349
-362
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ linters:
3131
errcheck:
3232
exclude-functions:
3333
- (io.Writer).Write
34+
- (io.StringWriter).WriteString
3435
- (*text/template.Template).Parse
3536
- (github.com/jackc/pgx/v5.Tx).Rollback
3637
revive:

cached.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func CacheN(ctx context.Context, exec Executor, q Query, start int) (BaseQuery[*
2828
cached := BaseQuery[*cached]{
2929
QueryType: q.Type(),
3030
Expression: &cached{
31-
query: []byte(query),
31+
query: query,
3232
args: args,
3333
start: start,
3434
},
@@ -55,19 +55,19 @@ func (e WrongStartError) Error() string {
5555
}
5656

5757
type cached struct {
58-
query []byte
58+
query string
5959
args []any
6060
start int
6161
Load
6262
}
6363

6464
// WriteSQL implements Expression.
65-
func (c *cached) WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error) {
65+
func (c *cached) WriteSQL(ctx context.Context, w io.StringWriter, d Dialect, start int) ([]any, error) {
6666
if start != c.start {
6767
return nil, WrongStartError{Expected: c.start, Got: start}
6868
}
6969

70-
if _, err := w.Write(c.query); err != nil {
70+
if _, err := w.WriteString(c.query); err != nil {
7171
return nil, err
7272
}
7373

clause/combine.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,27 @@ func (s *Combine) SetCombine(c Combine) {
3434
*s = c
3535
}
3636

37-
func (s Combine) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
37+
func (s Combine) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
3838
if s.Strategy == "" {
3939
return nil, ErrNoCombinationStrategy
4040
}
4141

42-
w.Write([]byte(s.Strategy))
42+
w.WriteString(s.Strategy)
4343

4444
if s.All {
45-
w.Write([]byte(" ALL "))
45+
w.WriteString(" ALL ")
4646
} else {
47-
w.Write([]byte(" "))
47+
w.WriteString(" ")
4848
}
4949

50-
w.Write([]byte("("))
50+
w.WriteString("(")
5151

5252
args, err := s.Query.WriteQuery(ctx, w, start)
5353
if err != nil {
5454
return nil, err
5555
}
5656

57-
w.Write([]byte(")"))
57+
w.WriteString(")")
5858

5959
return args, nil
6060
}

clause/conflict.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ type ConflictClause struct {
2222
Where
2323
}
2424

25-
func (c ConflictClause) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
26-
w.Write([]byte("ON CONFLICT"))
25+
func (c ConflictClause) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
26+
w.WriteString("ON CONFLICT")
2727

2828
args, err := bob.ExpressIf(ctx, w, d, start, c.Target, true, "", "")
2929
if err != nil {
3030
return nil, err
3131
}
3232

33-
w.Write([]byte(" DO "))
34-
w.Write([]byte(c.Do))
33+
w.WriteString(" DO ")
34+
w.WriteString(c.Do)
3535

3636
setArgs, err := bob.ExpressIf(ctx, w, d, start+len(args), c.Set, len(c.Set.Set) > 0, " SET\n", "")
3737
if err != nil {
@@ -55,7 +55,7 @@ type ConflictTarget struct {
5555
Where []any
5656
}
5757

58-
func (c ConflictTarget) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
58+
func (c ConflictTarget) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
5959
if c.Constraint != "" {
6060
return bob.ExpressIf(ctx, w, d, start, c.Constraint, true, " ON CONSTRAINT ", "")
6161
}

clause/cte.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@ type CTE struct {
1717
Cycle CTECycle
1818
}
1919

20-
func (c CTE) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
21-
w.Write([]byte(c.Name))
20+
func (c CTE) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
21+
w.WriteString(c.Name)
2222
_, err := bob.ExpressSlice(ctx, w, d, start, c.Columns, "(", ", ", ")")
2323
if err != nil {
2424
return nil, err
2525
}
2626

27-
w.Write([]byte(" AS "))
27+
w.WriteString(" AS ")
2828

2929
switch {
3030
case c.Materialized == nil:
3131
// do nothing
3232
break
3333
case *c.Materialized:
34-
w.Write([]byte("MATERIALIZED "))
34+
w.WriteString("MATERIALIZED ")
3535
case !*c.Materialized:
36-
w.Write([]byte("NOT MATERIALIZED "))
36+
w.WriteString("NOT MATERIALIZED ")
3737
}
3838

39-
w.Write([]byte("("))
39+
w.WriteString("(")
4040
args, err := c.Query.WriteQuery(ctx, w, start)
4141
if err != nil {
4242
return nil, err
4343
}
44-
w.Write([]byte(")"))
44+
w.WriteString(")")
4545

4646
searchArgs, err := bob.ExpressIf(ctx, w, d, start+len(args), c.Search,
4747
len(c.Search.Columns) > 0, "\n", "")
@@ -71,16 +71,16 @@ type CTESearch struct {
7171
Set string
7272
}
7373

74-
func (c CTESearch) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
74+
func (c CTESearch) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
7575
// [ SEARCH { BREADTH | DEPTH } FIRST BY column_name [, ...] SET search_seq_col_name ]
76-
fmt.Fprintf(w, "SEARCH %s FIRST BY ", c.Order)
76+
w.WriteString(fmt.Sprintf("SEARCH %s FIRST BY ", c.Order))
7777

7878
args, err := bob.ExpressSlice(ctx, w, d, start, c.Columns, "", ", ", "")
7979
if err != nil {
8080
return nil, err
8181
}
8282

83-
fmt.Fprintf(w, " SET %s", c.Set)
83+
w.WriteString(fmt.Sprintf(" SET %s", c.Set))
8484

8585
return args, nil
8686
}
@@ -93,16 +93,16 @@ type CTECycle struct {
9393
DefaultVal any
9494
}
9595

96-
func (c CTECycle) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
96+
func (c CTECycle) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
9797
//[ CYCLE column_name [, ...] SET cycle_mark_col_name [ TO cycle_mark_value DEFAULT cycle_mark_default ] USING cycle_path_col_name ]
98-
w.Write([]byte("CYCLE "))
98+
w.WriteString("CYCLE ")
9999

100100
args, err := bob.ExpressSlice(ctx, w, d, start, c.Columns, "", ", ", "")
101101
if err != nil {
102102
return nil, err
103103
}
104104

105-
fmt.Fprintf(w, " SET %s", c.Set)
105+
w.WriteString(fmt.Sprintf(" SET %s", c.Set))
106106

107107
markArgs, err := bob.ExpressIf(ctx, w, d, start+len(args), c.SetVal,
108108
c.SetVal != nil, " TO ", "")
@@ -118,7 +118,7 @@ func (c CTECycle) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, star
118118
}
119119
args = append(args, defaultArgs...)
120120

121-
fmt.Fprintf(w, " USING %s", c.Using)
121+
w.WriteString(fmt.Sprintf(" USING %s", c.Using))
122122

123123
return args, nil
124124
}

clause/fetch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (f *Fetch) SetFetch(fetch Fetch) {
1616
*f = fetch
1717
}
1818

19-
func (f Fetch) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
19+
func (f Fetch) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
2020
if f.Count == nil {
2121
return nil, nil
2222
}

clause/frame.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (f *Frame) SetExclusion(excl string) {
3535
f.Exclusion = excl
3636
}
3737

38-
func (f Frame) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
38+
func (f Frame) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
3939
if f.Mode == "" {
4040
f.Mode = "RANGE"
4141
}
@@ -46,11 +46,11 @@ func (f Frame) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start i
4646

4747
var args []any
4848

49-
w.Write([]byte(f.Mode))
50-
w.Write([]byte(" "))
49+
w.WriteString(f.Mode)
50+
w.WriteString(" ")
5151

5252
if f.End != nil {
53-
w.Write([]byte("BETWEEN "))
53+
w.WriteString("BETWEEN ")
5454
}
5555

5656
startArgs, err := bob.Express(ctx, w, d, start, f.Start)

clause/from.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ func (f TableRef) As(alias string, columns ...string) TableRef {
9494
return f
9595
}
9696

97-
func (f TableRef) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
97+
func (f TableRef) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
9898
if f.Only {
99-
w.Write([]byte("ONLY "))
99+
w.WriteString("ONLY ")
100100
}
101101

102102
if f.Lateral {
103-
w.Write([]byte("LATERAL "))
103+
w.WriteString("LATERAL ")
104104
}
105105

106106
args, err := bob.Express(ctx, w, d, start, f.Expression)
@@ -109,7 +109,7 @@ func (f TableRef) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, star
109109
}
110110

111111
if f.WithOrdinality {
112-
w.Write([]byte(" WITH ORDINALITY"))
112+
w.WriteString(" WITH ORDINALITY")
113113
}
114114

115115
_, err = bob.ExpressSlice(ctx, w, d, start, f.Partitions, " PARTITION (", ", ", ")")
@@ -118,20 +118,20 @@ func (f TableRef) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, star
118118
}
119119

120120
if f.Alias != "" {
121-
w.Write([]byte(" AS "))
121+
w.WriteString(" AS ")
122122
d.WriteQuoted(w, f.Alias)
123123
}
124124

125125
if len(f.Columns) > 0 {
126-
w.Write([]byte("("))
126+
w.WriteString("(")
127127
for k, cAlias := range f.Columns {
128128
if k != 0 {
129-
w.Write([]byte(", "))
129+
w.WriteString(", ")
130130
}
131131

132132
d.WriteQuoted(w, cAlias)
133133
}
134-
w.Write([]byte(")"))
134+
w.WriteString(")")
135135
}
136136

137137
// No args for index hints
@@ -144,9 +144,9 @@ func (f TableRef) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, star
144144
case f.IndexedBy == nil:
145145
break
146146
case *f.IndexedBy == "":
147-
w.Write([]byte(" NOT INDEXED"))
147+
w.WriteString(" NOT INDEXED")
148148
default:
149-
fmt.Fprintf(w, " INDEXED BY %q", *f.IndexedBy)
149+
w.WriteString(fmt.Sprintf(" INDEXED BY %q", *f.IndexedBy))
150150
}
151151

152152
joinArgs, err := bob.ExpressSlice(ctx, w, d, start+len(args), f.Joins, "\n", "\n", "")
@@ -164,24 +164,24 @@ type IndexHint struct {
164164
For string // JOIN, ORDER BY or GROUP BY
165165
}
166166

167-
func (f IndexHint) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
167+
func (f IndexHint) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
168168
if f.Type == "" {
169169
return nil, nil
170170
}
171-
fmt.Fprintf(w, "%s INDEX ", f.Type)
171+
w.WriteString(fmt.Sprintf("%s INDEX ", f.Type))
172172

173173
_, err := bob.ExpressIf(ctx, w, d, start, f.For, f.For != "", " FOR ", "")
174174
if err != nil {
175175
return nil, err
176176
}
177177

178178
// Always include the brackets
179-
fmt.Fprint(w, " (")
179+
w.WriteString(" (")
180180
_, err = bob.ExpressSlice(ctx, w, d, start, f.Indexes, "", ", ", "")
181181
if err != nil {
182182
return nil, err
183183
}
184-
fmt.Fprint(w, ")")
184+
w.WriteString(")")
185185

186186
return nil, nil
187187
}

clause/group_by.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ func (g *GroupBy) SetGroupByDistinct(distinct bool) {
2929
g.Distinct = distinct
3030
}
3131

32-
func (g GroupBy) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
32+
func (g GroupBy) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
3333
var args []any
3434

3535
// don't write anything if there are no groups
3636
if len(g.Groups) == 0 {
3737
return args, nil
3838
}
3939

40-
w.Write([]byte("GROUP BY "))
40+
w.WriteString("GROUP BY ")
4141
if g.Distinct {
42-
w.Write([]byte("DISTINCT "))
42+
w.WriteString("DISTINCT ")
4343
}
4444

4545
args, err := bob.ExpressSlice(ctx, w, d, start, g.Groups, "", ", ", "")
@@ -48,8 +48,8 @@ func (g GroupBy) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start
4848
}
4949

5050
if g.With != "" {
51-
w.Write([]byte(" WITH "))
52-
w.Write([]byte(g.With))
51+
w.WriteString(" WITH ")
52+
w.WriteString(g.With)
5353
}
5454

5555
return args, nil
@@ -60,8 +60,8 @@ type GroupingSet struct {
6060
Type string // GROUPING SET | CUBE | ROLLUP
6161
}
6262

63-
func (g GroupingSet) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
64-
w.Write([]byte(g.Type))
63+
func (g GroupingSet) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
64+
w.WriteString(g.Type)
6565
args, err := bob.ExpressSlice(ctx, w, d, start, g.Groups, " (", ", ", ")")
6666
if err != nil {
6767
return nil, err

clause/having.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (h *Having) AppendHaving(e ...any) {
1515
h.Conditions = append(h.Conditions, e...)
1616
}
1717

18-
func (h Having) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
18+
func (h Having) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
1919
args, err := bob.ExpressSlice(ctx, w, d, start, h.Conditions, "HAVING ", " AND ", "")
2020
if err != nil {
2121
return nil, err

0 commit comments

Comments
 (0)