You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add OrderByAsc and OrderByDesc methods to all builders
Implement new OrderByAsc(col) and OrderByDesc(col) methods for
SelectBuilder, UnionBuilder, UpdateBuilder, and DeleteBuilder. These
methods allow chaining multiple ORDER BY columns with different sort
directions, addressing the limitation where the old OrderBy/Asc/Desc
pattern could only apply a single direction to all columns.
Key changes:
- Add OrderByAsc(col string) and OrderByDesc(col string) to all four builders
- Each method appends the column with explicit ASC/DESC suffix
- Support chaining for complex ordering: OrderByDesc("score").OrderByAsc("name")
- Deprecate OrderBy/Asc/Desc methods with clear migration guidance
- Add comprehensive tests (TestSelectBuilder_OrderByAscDesc)
- Add example functions demonstrating usage
- Update all existing examples to use new API
- Update README.md with new "Build ORDER BY clause" section
This design is inspired by MyBatis-Plus and provides a more intuitive
and flexible API for handling multiple ORDER BY columns with different
sorting directions.
fix#214
-[Build SQL for different systems](#build-sql-for-different-systems)
15
16
-[Using `Struct` as a light weight ORM](#using-struct-as-a-light-weight-orm)
16
17
-[Nested SQL](#nested-sql)
@@ -203,6 +204,26 @@ fmt.Println(ub)
203
204
204
205
Refer to the [WhereClause](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#WhereClause) examples to learn its usage.
205
206
207
+
### Build `ORDER BY` clause
208
+
209
+
The `ORDER BY` clause is commonly used to sort query results. This package provides convenient methods to build `ORDER BY` clauses with proper ordering directions.
210
+
211
+
For scenarios where you need to order by multiple columns with different directions (ASC/DESC), use `OrderByAsc` and `OrderByDesc` methods. These methods can be chained to add multiple columns with their specific ordering.
212
+
213
+
```go
214
+
sb:= sqlbuilder.NewSelectBuilder()
215
+
sb.Select("id", "name", "score").From("users")
216
+
sb.OrderByDesc("score").OrderByAsc("name")
217
+
218
+
sql, args:= sb.Build()
219
+
fmt.Println(sql)
220
+
221
+
// Output:
222
+
// SELECT id, name, score FROM users ORDER BY score DESC, name ASC
223
+
```
224
+
225
+
The older `OrderBy` method combined with `Asc`/`Desc` is still available but deprecated, as it only supports a single ordering direction for all columns. The new `OrderByAsc` and `OrderByDesc` methods provide more flexibility and clarity when working with multiple columns.
226
+
206
227
### Build SQL for different systems
207
228
208
229
SQL syntax and parameter placeholders can differ across systems. To address these variations, this package introduces a concept termed "flavor".
@@ -412,7 +433,7 @@ var baseUserSelect = sqlbuilder.NewSelectBuilder().
0 commit comments