Skip to content

Commit 303c289

Browse files
committed
Collation in clause.OrderDef is now a string
It is now always quoted when writing the query
1 parent f4fc03f commit 303c289

File tree

7 files changed

+14
-48
lines changed

7 files changed

+14
-48
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4545
### Changed
4646

4747
- Updated error constant generation to employ specific error types for making error matching easier. (thanks @mbezhanov)
48-
- Fix random value generation for pq.Float64Array factory (thanks @felipeparaujo)
48+
- Collation in `clause.OrderDef` is now a string not an expression and is always quoted
4949

5050
### Removed
5151

5252
- Remove redundatnt type parameters from `orm.ExecQuery`.
5353

54+
### Fixed
55+
56+
- Fix random value generation for pq.Float64Array factory (thanks @felipeparaujo)
57+
5458
## [v0.29.0] - 2024-11-20
5559

5660
### Added

clause/order_by.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type OrderDef struct {
2828
Expression any
2929
Direction string // ASC | DESC | USING operator
3030
Nulls string // FIRST | LAST
31-
Collation bob.Expression
31+
Collation string
3232
}
3333

3434
func (o OrderDef) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
@@ -37,11 +37,9 @@ func (o OrderDef) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, star
3737
return nil, err
3838
}
3939

40-
if o.Collation != nil {
41-
_, err = o.Collation.WriteSQL(ctx, w, d, start)
42-
if err != nil {
43-
return nil, err
44-
}
40+
if o.Collation != "" {
41+
w.Write([]byte(" COLLATE "))
42+
d.WriteQuoted(w, o.Collation)
4543
}
4644

4745
if o.Direction != "" {

dialect/mysql/dialect/mods.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dialect
22

33
import (
44
"context"
5-
"fmt"
65
"io"
76

87
"github.com/stephenafamo/bob"
@@ -261,17 +260,6 @@ func (j JoinChain[Q]) Using(using ...string) bob.Mod[Q] {
261260
return mods.Join[Q](jo)
262261
}
263262

264-
type collation struct {
265-
name string
266-
}
267-
268-
func (c collation) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, _ int) ([]any, error) {
269-
if _, err := fmt.Fprintf(w, " COLLATE %s", c.name); err != nil {
270-
return nil, err
271-
}
272-
return nil, nil
273-
}
274-
275263
type OrderBy[Q interface{ AppendOrder(clause.OrderDef) }] func() clause.OrderDef
276264

277265
func (s OrderBy[Q]) Apply(q Q) {
@@ -298,7 +286,7 @@ func (o OrderBy[Q]) Desc() OrderBy[Q] {
298286

299287
func (o OrderBy[Q]) Collate(collationName string) OrderBy[Q] {
300288
order := o()
301-
order.Collation = collation{name: collationName}
289+
order.Collation = collationName
302290

303291
return OrderBy[Q](func() clause.OrderDef {
304292
return order

dialect/mysql/select_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestSelect(t *testing.T) {
109109
sm.From("users"),
110110
sm.OrderBy("name").Collate("utf8mb4_bg_0900_as_cs").Asc(),
111111
),
112-
ExpectedSQL: "SELECT id, name FROM users ORDER BY name COLLATE utf8mb4_bg_0900_as_cs ASC",
112+
ExpectedSQL: "SELECT id, name FROM users ORDER BY name COLLATE `utf8mb4_bg_0900_as_cs` ASC",
113113
},
114114
}
115115

dialect/psql/dialect/mods.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,6 @@ func (j CrossJoinChain[Q]) As(alias string, columns ...string) bob.Mod[Q] {
219219
})
220220
}
221221

222-
type collation struct {
223-
name string
224-
}
225-
226-
func (c collation) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, _ int) ([]any, error) {
227-
if _, err := w.Write([]byte(" COLLATE ")); err != nil {
228-
return nil, err
229-
}
230-
d.WriteQuoted(w, c.name)
231-
return nil, nil
232-
}
233-
234222
type OrderBy[Q interface{ AppendOrder(clause.OrderDef) }] func() clause.OrderDef
235223

236224
func (s OrderBy[Q]) Apply(q Q) {
@@ -284,7 +272,7 @@ func (o OrderBy[Q]) NullsLast() OrderBy[Q] {
284272

285273
func (o OrderBy[Q]) Collate(collationName string) OrderBy[Q] {
286274
order := o()
287-
order.Collation = collation{name: collationName}
275+
order.Collation = collationName
288276

289277
return OrderBy[Q](func() clause.OrderDef {
290278
return order

dialect/sqlite/dialect/mods.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dialect
22

33
import (
44
"context"
5-
"fmt"
65
"io"
76

87
"github.com/stephenafamo/bob"
@@ -251,17 +250,6 @@ func CrossJoin[Q Joinable](e any) CrossJoinChain[Q] {
251250
})
252251
}
253252

254-
type collation struct {
255-
name string
256-
}
257-
258-
func (c collation) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, _ int) ([]any, error) {
259-
if _, err := fmt.Fprintf(w, " COLLATE %s", c.name); err != nil {
260-
return nil, err
261-
}
262-
return nil, nil
263-
}
264-
265253
type OrderBy[Q interface{ AppendOrder(clause.OrderDef) }] func() clause.OrderDef
266254

267255
func (s OrderBy[Q]) Apply(q Q) {
@@ -270,7 +258,7 @@ func (s OrderBy[Q]) Apply(q Q) {
270258

271259
func (o OrderBy[Q]) Collate(collationName string) OrderBy[Q] {
272260
order := o()
273-
order.Collation = &collation{name: collationName}
261+
order.Collation = collationName
274262

275263
return OrderBy[Q](func() clause.OrderDef {
276264
return order

dialect/sqlite/select_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestSelect(t *testing.T) {
116116
sm.From("users"),
117117
sm.OrderBy("name").Collate("NOCASE").Asc(),
118118
),
119-
ExpectedSQL: `SELECT id, name FROM users ORDER BY name COLLATE NOCASE ASC`,
119+
ExpectedSQL: `SELECT id, name FROM users ORDER BY name COLLATE "NOCASE" ASC`,
120120
},
121121
"with cross join": {
122122
Query: sqlite.Select(

0 commit comments

Comments
 (0)