• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stephenafamo / bob / 19668632728

25 Nov 2025 11:54AM UTC coverage: 42.301%. Remained the same
19668632728

push

github

web-flow
Merge pull request #589 from stephenafamo/string-writer

Use io.StringWriter instead of io.Writer for efficiency

196 of 295 new or added lines in 67 files covered. (66.44%)

9 existing lines in 5 files now uncovered.

9750 of 23049 relevant lines covered (42.3%)

569.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.59
/dialect/psql/dialect/function.go
1
package dialect
2

3
import (
4
        "context"
5
        "io"
6

7
        "github.com/stephenafamo/bob"
8
        "github.com/stephenafamo/bob/clause"
9
        "github.com/stephenafamo/bob/expr"
10
)
11

12
func NewFunction(name string, args ...any) *Function {
16✔
13
        f := &Function{name: name, args: args}
16✔
14
        f.Chain = expr.Chain[Expression, Expression]{Base: f}
16✔
15

16✔
16
        return f
16✔
17
}
16✔
18

19
type Function struct {
20
        name string
21
        args []any
22

23
        Distinct    bool
24
        WithinGroup bool
25
        clause.OrderBy
26
        Filter []any
27
        w      *clause.Window
28

29
        Alias   string // used when there should be an alias before the columns
30
        Columns []columnDef
31

32
        expr.Chain[Expression, Expression]
33
}
34

35
func (f *Function) SetWindow(w clause.Window) {
6✔
36
        f.w = &w
6✔
37
}
6✔
38

39
func (f *Function) AppendColumn(name, datatype string) {
4✔
40
        f.Columns = append(f.Columns, columnDef{
4✔
41
                name:     name,
4✔
42
                dataType: datatype,
4✔
43
        })
4✔
44
}
4✔
45

46
func (f *Function) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
16✔
47
        if f.name == "" {
16✔
48
                return nil, nil
×
49
        }
×
50

51
        w.WriteString(f.name)
16✔
52
        w.WriteString("(")
16✔
53

16✔
54
        if f.Distinct {
16✔
NEW
55
                w.WriteString("DISTINCT ")
×
56
        }
×
57

58
        args, err := bob.ExpressSlice(ctx, w, d, start, f.args, "", ", ", "")
16✔
59
        if err != nil {
16✔
60
                return nil, err
×
61
        }
×
62

63
        if !f.WithinGroup {
32✔
64
                orderArgs, err := bob.ExpressIf(ctx, w, d, start+len(args), f.OrderBy,
16✔
65
                        len(f.OrderBy.Expressions) > 0, " ", "")
16✔
66
                if err != nil {
16✔
67
                        return nil, err
×
68
                }
×
69
                args = append(args, orderArgs...)
16✔
70
        }
71
        w.WriteString(")")
16✔
72

16✔
73
        if f.WithinGroup {
16✔
74
                orderArgs, err := bob.ExpressIf(ctx, w, d, start+len(args), f.OrderBy,
×
75
                        len(f.OrderBy.Expressions) > 0, " WITHIN GROUP (", ")")
×
76
                if err != nil {
×
77
                        return nil, err
×
78
                }
×
79
                args = append(args, orderArgs...)
×
80
        }
81

82
        filterArgs, err := bob.ExpressSlice(ctx, w, d, start, f.Filter, " FILTER (WHERE ", " AND ", ")")
16✔
83
        if err != nil {
16✔
84
                return nil, err
×
85
        }
×
86
        args = append(args, filterArgs...)
16✔
87

16✔
88
        if len(f.Columns) > 0 || len(f.Alias) > 0 {
18✔
89
                w.WriteString(" AS ")
2✔
90
        }
2✔
91

92
        if len(f.Alias) > 0 {
16✔
NEW
93
                w.WriteString(f.Alias)
×
NEW
94
                w.WriteString(" ")
×
UNCOV
95
        }
×
96

97
        colArgs, err := bob.ExpressSlice(ctx, w, d, start+len(args), f.Columns, "(", ", ", ")")
16✔
98
        if err != nil {
16✔
99
                return nil, err
×
100
        }
×
101
        args = append(args, colArgs...)
16✔
102

16✔
103
        winargs, err := bob.ExpressIf(ctx, w, d, start+len(args), f.w, f.w != nil, "OVER (", ")")
16✔
104
        if err != nil {
16✔
105
                return nil, err
×
106
        }
×
107
        args = append(args, winargs...)
16✔
108

16✔
109
        return args, nil
16✔
110
}
111

112
type columnDef struct {
113
        name     string
114
        dataType string
115
}
116

117
func (c columnDef) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
4✔
118
        w.WriteString(c.name + " " + c.dataType)
4✔
119

4✔
120
        return nil, nil
4✔
121
}
4✔
122

123
type Functions []*Function
124

125
func (f Functions) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
2✔
126
        if len(f) > 1 {
4✔
127
                w.WriteString("ROWS FROM (")
2✔
128
        }
2✔
129

130
        args, err := bob.ExpressSlice(ctx, w, d, start, f, "", ", ", "")
2✔
131
        if err != nil {
2✔
132
                return nil, err
×
133
        }
×
134

135
        if len(f) > 1 {
4✔
136
                w.WriteString(")")
2✔
137
        }
2✔
138

139
        return args, nil
2✔
140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc