Skip to content

Commit

Permalink
Collation in clause.OrderDef is now a string
Browse files Browse the repository at this point in the history
It is now always quoted when writing the query
  • Loading branch information
stephenafamo committed Dec 10, 2024
1 parent f4fc03f commit 303c289
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 48 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

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

### Removed

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

### Fixed

- Fix random value generation for pq.Float64Array factory (thanks @felipeparaujo)

## [v0.29.0] - 2024-11-20

### Added
Expand Down
10 changes: 4 additions & 6 deletions clause/order_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type OrderDef struct {
Expression any
Direction string // ASC | DESC | USING operator
Nulls string // FIRST | LAST
Collation bob.Expression
Collation string
}

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

if o.Collation != nil {
_, err = o.Collation.WriteSQL(ctx, w, d, start)
if err != nil {
return nil, err
}
if o.Collation != "" {
w.Write([]byte(" COLLATE "))
d.WriteQuoted(w, o.Collation)
}

if o.Direction != "" {
Expand Down
14 changes: 1 addition & 13 deletions dialect/mysql/dialect/mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dialect

import (
"context"
"fmt"
"io"

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

type collation struct {
name string
}

func (c collation) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, _ int) ([]any, error) {
if _, err := fmt.Fprintf(w, " COLLATE %s", c.name); err != nil {
return nil, err
}
return nil, nil
}

type OrderBy[Q interface{ AppendOrder(clause.OrderDef) }] func() clause.OrderDef

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

func (o OrderBy[Q]) Collate(collationName string) OrderBy[Q] {
order := o()
order.Collation = collation{name: collationName}
order.Collation = collationName

return OrderBy[Q](func() clause.OrderDef {
return order
Expand Down
2 changes: 1 addition & 1 deletion dialect/mysql/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestSelect(t *testing.T) {
sm.From("users"),
sm.OrderBy("name").Collate("utf8mb4_bg_0900_as_cs").Asc(),
),
ExpectedSQL: "SELECT id, name FROM users ORDER BY name COLLATE utf8mb4_bg_0900_as_cs ASC",
ExpectedSQL: "SELECT id, name FROM users ORDER BY name COLLATE `utf8mb4_bg_0900_as_cs` ASC",
},
}

Expand Down
14 changes: 1 addition & 13 deletions dialect/psql/dialect/mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,6 @@ func (j CrossJoinChain[Q]) As(alias string, columns ...string) bob.Mod[Q] {
})
}

type collation struct {
name string
}

func (c collation) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, _ int) ([]any, error) {
if _, err := w.Write([]byte(" COLLATE ")); err != nil {
return nil, err
}
d.WriteQuoted(w, c.name)
return nil, nil
}

type OrderBy[Q interface{ AppendOrder(clause.OrderDef) }] func() clause.OrderDef

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

func (o OrderBy[Q]) Collate(collationName string) OrderBy[Q] {
order := o()
order.Collation = collation{name: collationName}
order.Collation = collationName

return OrderBy[Q](func() clause.OrderDef {
return order
Expand Down
14 changes: 1 addition & 13 deletions dialect/sqlite/dialect/mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dialect

import (
"context"
"fmt"
"io"

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

type collation struct {
name string
}

func (c collation) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, _ int) ([]any, error) {
if _, err := fmt.Fprintf(w, " COLLATE %s", c.name); err != nil {
return nil, err
}
return nil, nil
}

type OrderBy[Q interface{ AppendOrder(clause.OrderDef) }] func() clause.OrderDef

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

func (o OrderBy[Q]) Collate(collationName string) OrderBy[Q] {
order := o()
order.Collation = &collation{name: collationName}
order.Collation = collationName

return OrderBy[Q](func() clause.OrderDef {
return order
Expand Down
2 changes: 1 addition & 1 deletion dialect/sqlite/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestSelect(t *testing.T) {
sm.From("users"),
sm.OrderBy("name").Collate("NOCASE").Asc(),
),
ExpectedSQL: `SELECT id, name FROM users ORDER BY name COLLATE NOCASE ASC`,
ExpectedSQL: `SELECT id, name FROM users ORDER BY name COLLATE "NOCASE" ASC`,
},
"with cross join": {
Query: sqlite.Select(
Expand Down

0 comments on commit 303c289

Please sign in to comment.