Skip to content

Commit dfaa965

Browse files
committed
feat: add order
1 parent 0dffae9 commit dfaa965

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

constants/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
package constants
2+
3+
const (
4+
COMMA = ","
5+
)

constants/keyword.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ const (
1212
Ge = ">="
1313
Lt = "<"
1414
Le = "<="
15+
Desc = "DESC"
16+
Asc = "ASC"
1517
)

gormplus/mapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func SelectOne[T any](q *Query[T]) (*gorm.DB, T) {
8181

8282
func SelectList[T any](q *Query[T]) (*gorm.DB, []T) {
8383
var results []T
84-
resultDb := GormDb.Select(q.Columns).Where(q.QueryBuilder.String(), q.Args...).Find(&results)
84+
resultDb := GormDb.Order(q.OrderBuilder.String()).Select(q.Columns).Where(q.QueryBuilder.String(), q.Args...).Find(&results)
8585
return resultDb, results
8686
}
8787

gormplus/mapper_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"fmt"
55
"gorm.io/driver/mysql"
66
"gorm.io/gorm"
7+
"gorm.io/gorm/logger"
78
"testing"
89
)
910

1011
func init() {
1112
dsn := "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
1213
var err error
13-
GormDb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
14+
GormDb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
15+
Logger: logger.Default.LogMode(logger.Info),
16+
})
1417
if err != nil {
1518
fmt.Println(err)
1619
}
@@ -23,7 +26,7 @@ type Test1 struct {
2326
}
2427

2528
func TestSave(t *testing.T) {
26-
test1 := Test1{Code: "D455", Price: 100}
29+
test1 := Test1{Code: "D455", Price: 200}
2730
resultDb := Insert(&test1)
2831
fmt.Println(resultDb)
2932
fmt.Println(test1)
@@ -113,7 +116,7 @@ func TestSelectOne(t *testing.T) {
113116

114117
func TestSelectList(t *testing.T) {
115118
q := Query[Test1]{}
116-
q.Eq("price", 200).Select("code", "price")
119+
q.OrderByDesc("price").OrderByAsc("code")
117120
db, result := SelectList(&q)
118121
fmt.Println(db.RowsAffected)
119122
fmt.Println(result)

gormplus/query.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
type Query[T any] struct {
1010
Columns []string
11+
OrderBuilder strings.Builder
1112
QueryBuilder strings.Builder
1213
Args []any
1314
LastCond string
@@ -91,6 +92,16 @@ func (q *Query[T]) Select(columns ...string) *Query[T] {
9192
return q
9293
}
9394

95+
func (q *Query[T]) OrderByDesc(columns ...string) *Query[T] {
96+
q.buildOrder(constants.Desc, columns...)
97+
return q
98+
}
99+
100+
func (q *Query[T]) OrderByAsc(columns ...string) *Query[T] {
101+
q.buildOrder(constants.Asc, columns...)
102+
return q
103+
}
104+
94105
func (q *Query[T]) addCond(column string, val any, condType string) {
95106
if q.LastCond != constants.And && q.LastCond != constants.Or && q.QueryBuilder.Len() > 0 {
96107
q.QueryBuilder.WriteString(constants.And)
@@ -102,3 +113,14 @@ func (q *Query[T]) addCond(column string, val any, condType string) {
102113
q.LastCond = ""
103114
q.Args = append(q.Args, val)
104115
}
116+
117+
func (q *Query[T]) buildOrder(orderType string, columns ...string) {
118+
for _, v := range columns {
119+
if q.OrderBuilder.Len() > 0 {
120+
q.OrderBuilder.WriteString(constants.COMMA)
121+
}
122+
q.OrderBuilder.WriteString(v)
123+
q.OrderBuilder.WriteString(" ")
124+
q.OrderBuilder.WriteString(orderType)
125+
}
126+
}

0 commit comments

Comments
 (0)