From 303c28993aeb74724da671662390d577d87873a0 Mon Sep 17 00:00:00 2001 From: Stephen Afam-Osemene Date: Mon, 9 Dec 2024 19:47:42 +0100 Subject: [PATCH] Collation in `clause.OrderDef` is now a string It is now always quoted when writing the query --- CHANGELOG.md | 6 +++++- clause/order_by.go | 10 ++++------ dialect/mysql/dialect/mods.go | 14 +------------- dialect/mysql/select_test.go | 2 +- dialect/psql/dialect/mods.go | 14 +------------- dialect/sqlite/dialect/mods.go | 14 +------------- dialect/sqlite/select_test.go | 2 +- 7 files changed, 14 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ffa0b89..06b7bae9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/clause/order_by.go b/clause/order_by.go index 6659189e..c0ff9df8 100644 --- a/clause/order_by.go +++ b/clause/order_by.go @@ -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) { @@ -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 != "" { diff --git a/dialect/mysql/dialect/mods.go b/dialect/mysql/dialect/mods.go index d9ea1a49..3e7b2948 100644 --- a/dialect/mysql/dialect/mods.go +++ b/dialect/mysql/dialect/mods.go @@ -2,7 +2,6 @@ package dialect import ( "context" - "fmt" "io" "github.com/stephenafamo/bob" @@ -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) { @@ -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 diff --git a/dialect/mysql/select_test.go b/dialect/mysql/select_test.go index 588c05a6..33598418 100644 --- a/dialect/mysql/select_test.go +++ b/dialect/mysql/select_test.go @@ -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", }, } diff --git a/dialect/psql/dialect/mods.go b/dialect/psql/dialect/mods.go index 8a520201..e5fbe621 100644 --- a/dialect/psql/dialect/mods.go +++ b/dialect/psql/dialect/mods.go @@ -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) { @@ -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 diff --git a/dialect/sqlite/dialect/mods.go b/dialect/sqlite/dialect/mods.go index 43ba07b2..19e02bc6 100644 --- a/dialect/sqlite/dialect/mods.go +++ b/dialect/sqlite/dialect/mods.go @@ -2,7 +2,6 @@ package dialect import ( "context" - "fmt" "io" "github.com/stephenafamo/bob" @@ -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) { @@ -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 diff --git a/dialect/sqlite/select_test.go b/dialect/sqlite/select_test.go index c44dceb5..4d20b867 100644 --- a/dialect/sqlite/select_test.go +++ b/dialect/sqlite/select_test.go @@ -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(