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

huandu / go-sqlbuilder / 13257642920

11 Feb 2025 07:10AM UTC coverage: 96.743% (-0.1%) from 96.879%
13257642920

push

github

web-flow
Merge pull request #190 from huandu/feature/prevent-syntax-error-with-zero-values

Ignore empty values and expressions to prevent syntax error

44 of 46 new or added lines in 5 files covered. (95.65%)

4 existing lines in 1 file now uncovered.

3475 of 3592 relevant lines covered (96.74%)

1.09 hits per line

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

94.74
/whereclause.go
1
// Copyright 2018 Huan Du. All rights reserved.
2
// Licensed under the MIT license that can be found in the LICENSE file.
3

4
package sqlbuilder
5

6
// WhereClause is a Builder for WHERE clause.
7
// All builders which support `WHERE` clause have an anonymous `WhereClause` field,
8
// in which the conditions are stored.
9
//
10
// WhereClause can be shared among multiple builders.
11
// However, it is not thread-safe.
12
type WhereClause struct {
13
        flavor  Flavor
14
        clauses []clause
15
}
16

17
var _ Builder = new(WhereClause)
18

19
// NewWhereClause creates a new WhereClause.
20
func NewWhereClause() *WhereClause {
1✔
21
        return &WhereClause{}
1✔
22
}
1✔
23

24
// CopyWhereClause creates a copy of the whereClause.
25
func CopyWhereClause(whereClause *WhereClause) *WhereClause {
1✔
26
        clauses := make([]clause, len(whereClause.clauses))
1✔
27
        copy(clauses, whereClause.clauses)
1✔
28

1✔
29
        return &WhereClause{
1✔
30
                flavor:  whereClause.flavor,
1✔
31
                clauses: clauses,
1✔
32
        }
1✔
33
}
1✔
34

35
type clause struct {
36
        args     *Args
37
        andExprs []string
38
}
39

40
func (c *clause) Build(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) {
1✔
41
        exprs := filterEmptyStrings(c.andExprs)
1✔
42

1✔
43
        if len(exprs) == 0 {
1✔
NEW
44
                return
×
NEW
45
        }
×
46

47
        buf := newStringBuilder()
1✔
48
        buf.WriteStrings(exprs, " AND ")
1✔
49
        sql, args = c.args.CompileWithFlavor(buf.String(), flavor, initialArg...)
1✔
50
        return
1✔
51
}
52

53
// whereClauseProxy is a proxy for WhereClause.
54
// It's useful when the WhereClause in a build can be changed.
55
type whereClauseProxy struct {
56
        *WhereClause
57
}
58

59
var _ Builder = new(whereClauseProxy)
60

61
// BuildWithFlavor builds a WHERE clause with the specified flavor and initial arguments.
62
func (wc *WhereClause) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) {
1✔
63
        if len(wc.clauses) == 0 {
2✔
64
                return "", nil
1✔
65
        }
1✔
66

67
        buf := newStringBuilder()
1✔
68
        buf.WriteLeadingString("WHERE ")
1✔
69

1✔
70
        sql, args = wc.clauses[0].Build(flavor, initialArg...)
1✔
71
        buf.WriteString(sql)
1✔
72

1✔
73
        for _, clause := range wc.clauses[1:] {
2✔
74
                buf.WriteString(" AND ")
1✔
75
                sql, args = clause.Build(flavor, args...)
1✔
76
                buf.WriteString(sql)
1✔
77
        }
1✔
78

79
        return buf.String(), args
1✔
80
}
81

82
// Build returns compiled WHERE clause string and args.
83
func (wc *WhereClause) Build() (sql string, args []interface{}) {
1✔
84
        return wc.BuildWithFlavor(wc.flavor)
1✔
85
}
1✔
86

87
// SetFlavor sets the flavor of compiled sql.
88
// When the WhereClause belongs to a builder, the flavor of the builder will be used when building SQL.
89
func (wc *WhereClause) SetFlavor(flavor Flavor) (old Flavor) {
1✔
90
        old = wc.flavor
1✔
91
        wc.flavor = flavor
1✔
92
        return
1✔
93
}
1✔
94

95
// Flavor returns flavor of clause
96
func (wc *WhereClause) Flavor() Flavor {
1✔
97
        return wc.flavor
1✔
98
}
1✔
99

100
// AddWhereExpr adds an AND expression to WHERE clause with the specified arguments.
101
func (wc *WhereClause) AddWhereExpr(args *Args, andExpr ...string) *WhereClause {
1✔
102
        if len(andExpr) == 0 {
2✔
103
                return wc
1✔
104
        }
1✔
105

106
        andExprsBytesLen := estimateStringsBytes(andExpr)
1✔
107

1✔
108
        if andExprsBytesLen == 0 {
2✔
109
                return wc
1✔
110
        }
1✔
111

112
        // Merge with last clause if possible.
113
        if len(wc.clauses) > 0 {
2✔
114
                lastClause := &wc.clauses[len(wc.clauses)-1]
1✔
115

1✔
116
                if lastClause.args == args {
2✔
117
                        lastClause.andExprs = append(lastClause.andExprs, andExpr...)
1✔
118
                        return wc
1✔
119
                }
1✔
120
        }
121

122
        wc.clauses = append(wc.clauses, clause{
1✔
123
                args:     args,
1✔
124
                andExprs: andExpr,
1✔
125
        })
1✔
126
        return wc
1✔
127
}
128

129
// AddWhereClause adds all clauses in the whereClause to the wc.
130
func (wc *WhereClause) AddWhereClause(whereClause *WhereClause) *WhereClause {
1✔
131
        if whereClause == nil {
1✔
132
                return wc
×
133
        }
×
134

135
        wc.clauses = append(wc.clauses, whereClause.clauses...)
1✔
136
        return wc
1✔
137
}
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

© 2025 Coveralls, Inc