Skip to content

Commit a382b18

Browse files
committed
Restructure code layout, add conjoin string for ORDER clause
1 parent 0af49c6 commit a382b18

File tree

3 files changed

+108
-95
lines changed

3 files changed

+108
-95
lines changed

clause.go

Lines changed: 86 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,18 @@ import (
55
"strings"
66
)
77

8-
type clauseKind uint
9-
10-
type fromClause struct {
11-
table string
12-
}
13-
14-
type intoClause struct {
15-
table string
16-
}
17-
18-
type limitClause int64
19-
20-
type offsetClause int64
21-
22-
type orderClause struct {
23-
cols []string
24-
dir string
25-
}
26-
27-
type returningClause struct {
28-
cols []string
29-
}
30-
31-
type setClause struct {
32-
col string
33-
expr Expr
34-
}
35-
36-
type unionClause struct {
37-
q Query
38-
}
39-
40-
type valuesClause struct {
41-
items []string
42-
args []interface{}
43-
}
44-
45-
type whereClause struct {
46-
conjunction string
47-
op string
48-
left Expr
49-
right Expr
50-
}
51-
52-
// Clause is a Query expression that will typically represent one of the
8+
// clause is a Query expression that will typically represent one of the
539
// following SQL clauses, FROM, LIMIT, OFFSET, ORDER BY, UNION, VALUES,
5410
// WHERE, RETURNING, and SET.
55-
type Clause interface {
11+
type clause interface {
5612
Expr
5713

58-
// Kind returns the kind of the current Clause.
59-
Kind() clauseKind
14+
// kind returns the kind of the current clause.
15+
kind() clauseKind
6016
}
6117

18+
type clauseKind uint
19+
6220
//go:generate stringer -type clauseKind -linecomment
6321
const (
6422
_FromClause clauseKind = iota // FROM
@@ -72,17 +30,6 @@ const (
7230
_SetClause // SET
7331
)
7432

75-
var (
76-
_ Clause = (*fromClause)(nil)
77-
_ Clause = (*limitClause)(nil)
78-
_ Clause = (*offsetClause)(nil)
79-
_ Clause = (*orderClause)(nil)
80-
_ Clause = (*unionClause)(nil)
81-
_ Clause = (*whereClause)(nil)
82-
_ Clause = (*returningClause)(nil)
83-
_ Clause = (*setClause)(nil)
84-
)
85-
8633
func realWhere(conjunction string, left Expr, op string, right Expr) Option {
8734
return func(q Query) Query {
8835
leftArgs := left.Args()
@@ -107,6 +54,24 @@ func realWhere(conjunction string, left Expr, op string, right Expr) Option {
10754
}
10855
}
10956

57+
// Where appends a WHERE clause to the Query. This will append the arguments
58+
// of the given expression to the Query too. By default this will use AND for
59+
// conjoining multiple WHERE clauses.
60+
func Where(col, op string, expr Expr) Option {
61+
return func(q Query) Query {
62+
return realWhere("AND", Ident(col), op, expr)(q)
63+
}
64+
}
65+
66+
// OrWhere appends a WHERE clause to the Query. This will append the arguments
67+
// of the given expression to the Query too. This will use OR for conjoining
68+
// with a preceding WHERE clause.
69+
func OrWhere(col, op string, expr Expr) Option {
70+
return func(q Query) Query {
71+
return realWhere("OR", Ident(col), op, expr)(q)
72+
}
73+
}
74+
11075
// From appends a FROM clause for the given table to the Query.
11176
func From(table string) Option {
11277
return func(q Query) Query {
@@ -201,60 +166,98 @@ func Values(vals ...interface{}) Option {
201166
}
202167
}
203168

204-
// Where appends a WHERE clause to the Query. This will append the arguments
205-
// of the given expression to the Query too. By default this will use AND for
206-
// conjoining multiple WHERE clauses.
207-
func Where(col, op string, expr Expr) Option {
208-
return func(q Query) Query {
209-
return realWhere("AND", Ident(col), op, expr)(q)
210-
}
169+
type fromClause struct {
170+
table string
211171
}
212172

213-
// OrWhere appends a WHERE clause to the Query. This will append the arguments
214-
// of the given expression to the Query too. This will use OR for conjoining
215-
// with a preceding WHERE clause.
216-
func OrWhere(col, op string, expr Expr) Option {
217-
return func(q Query) Query {
218-
return realWhere("OR", Ident(col), op, expr)(q)
219-
}
220-
}
173+
var _ clause = (*fromClause)(nil)
221174

222175
func (c fromClause) Args() []interface{} { return nil }
223176
func (c fromClause) Build() string { return c.table }
224-
func (c fromClause) Kind() clauseKind { return _FromClause }
177+
func (c fromClause) kind() clauseKind { return _FromClause }
178+
179+
type limitClause int64
180+
181+
var _ clause = (*limitClause)(nil)
225182

226183
func (c limitClause) Args() []interface{} { return nil }
227184
func (c limitClause) Build() string { return strconv.FormatInt(int64(c), 10) }
228-
func (c limitClause) Kind() clauseKind { return _LimitClause }
185+
func (c limitClause) kind() clauseKind { return _LimitClause }
186+
187+
type offsetClause int64
188+
189+
var _ clause = (*offsetClause)(nil)
229190

230191
func (c offsetClause) Args() []interface{} { return nil }
231192
func (c offsetClause) Build() string { return strconv.FormatInt(int64(c), 10) }
232-
func (c offsetClause) Kind() clauseKind { return _OffsetClause }
193+
func (c offsetClause) kind() clauseKind { return _OffsetClause }
194+
195+
type orderClause struct {
196+
cols []string
197+
dir string
198+
}
199+
200+
var _ clause = (*orderClause)(nil)
233201

234202
func (c orderClause) Args() []interface{} { return nil }
235203
func (c orderClause) Build() string { return strings.Join(c.cols, ", ") + " " + c.dir }
236-
func (c orderClause) Kind() clauseKind { return _OrderClause }
204+
func (c orderClause) kind() clauseKind { return _OrderClause }
205+
206+
type returningClause struct {
207+
cols []string
208+
}
209+
210+
var _ clause = (*returningClause)(nil)
237211

238212
func (c returningClause) Args() []interface{} { return nil }
239213
func (c returningClause) Build() string { return strings.Join(c.cols, ", ") }
240-
func (c returningClause) Kind() clauseKind { return _ReturningClause }
214+
func (c returningClause) kind() clauseKind { return _ReturningClause }
215+
216+
type setClause struct {
217+
col string
218+
expr Expr
219+
}
220+
221+
var _ clause = (*setClause)(nil)
241222

242223
func (c setClause) Args() []interface{} { return nil }
243224
func (c setClause) Build() string { return c.col + " = " + c.expr.Build() }
244-
func (c setClause) Kind() clauseKind { return _SetClause }
225+
func (c setClause) kind() clauseKind { return _SetClause }
226+
227+
type unionClause struct {
228+
q Query
229+
}
230+
231+
var _ clause = (*unionClause)(nil)
245232

246233
func (c unionClause) Args() []interface{} { return nil }
247-
func (c unionClause) Kind() clauseKind { return _UnionClause }
248234
func (c unionClause) Build() string { return c.q.buildInitial() }
235+
func (c unionClause) kind() clauseKind { return _UnionClause }
236+
237+
type valuesClause struct {
238+
items []string
239+
args []interface{}
240+
}
241+
242+
var _ clause = (*valuesClause)(nil)
249243

250244
func (c valuesClause) Args() []interface{} { return c.args }
251245
func (c valuesClause) Build() string { return "(" + strings.Join(c.items, ", ") + ")" }
252-
func (c valuesClause) Kind() clauseKind { return _ValuesClause }
246+
func (c valuesClause) kind() clauseKind { return _ValuesClause }
247+
248+
type whereClause struct {
249+
conjunction string
250+
op string
251+
left Expr
252+
right Expr
253+
}
254+
255+
var _ clause = (*whereClause)(nil)
253256

254257
func (c whereClause) Args() []interface{} { return nil }
255258

256259
func (c whereClause) Build() string {
257260
return c.left.Build() + " " + c.op + " " + c.right.Build()
258261
}
259262

260-
func (c whereClause) Kind() clauseKind { return _WhereClause }
263+
func (c whereClause) kind() clauseKind { return _WhereClause }

query.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Query struct {
1919
stmt statement
2020
table string
2121
exprs []Expr
22-
clauses []Clause
22+
clauses []clause
2323
args []interface{}
2424
}
2525

@@ -115,7 +115,7 @@ func Options(opts ...Option) Option {
115115

116116
// conj returns the string that should be used for conjoining multiple clauses
117117
// of the same type.
118-
func (q Query) conj(cl Clause) string {
118+
func (q Query) conj(cl clause) string {
119119
if cl == nil {
120120
return ""
121121
}
@@ -124,9 +124,11 @@ func (q Query) conj(cl Clause) string {
124124
case whereClause:
125125
return " " + v.conjunction + " "
126126
case unionClause:
127-
return " " + cl.Kind().String() + " "
127+
return " " + cl.kind().String() + " "
128128
case setClause, valuesClause:
129129
return ", "
130+
case orderClause:
131+
return ", "
130132
default:
131133
return " "
132134
}
@@ -169,8 +171,8 @@ func (q Query) buildInitial() string {
169171

170172
for i, cl := range q.clauses {
171173
var (
172-
prev Clause
173-
next Clause
174+
prev clause
175+
next clause
174176
)
175177

176178
if i > 0 {
@@ -181,7 +183,7 @@ func (q Query) buildInitial() string {
181183
next = q.clauses[i+1]
182184
}
183185

184-
kind := cl.Kind()
186+
kind := cl.kind()
185187

186188
if kind != _UnionClause {
187189
// Write the string of the clause kind only once, this avoids something
@@ -202,17 +204,16 @@ func (q Query) buildInitial() string {
202204
if next != nil {
203205
conj := q.conj(next)
204206

205-
// Determine if the clause needs wrapping in parentheses. We wrap
206-
// clauses under these conditions:
207-
//
208-
// - If the next clause is a different kind from the current one
209-
if next.Kind() == kind {
207+
// Clauses are wrapped if the next clause is different from the
208+
// current one, or if the conjoining string is different for the
209+
// next clause.
210+
if next.kind() == kind {
210211
wrap := false
211212

212213
if prev != nil {
213214
// Wrap the clause in parentheses if we have a different
214215
// conjunction string.
215-
wrap = (prev.Kind() == kind) && (conj != q.conj(cl))
216+
wrap = (prev.kind() == kind) && (conj != q.conj(cl))
216217
}
217218

218219
if wrap {

query_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ func Test_Query(t *testing.T) {
202202
Values("note 3", "another comment"),
203203
),
204204
},
205+
{
206+
"SELECT * FROM posts ORDER BY created_at DESC, author ASC",
207+
Select(
208+
Columns("*"),
209+
From("posts"),
210+
OrderDesc("created_at"),
211+
OrderAsc("author"),
212+
),
213+
},
205214
}
206215

207216
for i, test := range tests {

0 commit comments

Comments
 (0)