Skip to content

Commit 1d0c57b

Browse files
committed
refactor: refactor the SelectXXX
1 parent 239a88c commit 1d0c57b

File tree

3 files changed

+91
-45
lines changed

3 files changed

+91
-45
lines changed

gormplus/mapper.go

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ func Init(db *gorm.DB) {
1111
gormDb = db
1212
}
1313

14-
type Page struct {
15-
Page int
16-
PageSize int
14+
type Page[T any] struct {
15+
Current int
16+
Size int
17+
Total int64
18+
Records []*T
19+
}
20+
21+
func NewPage[T any](current, size int) *Page[T] {
22+
return &Page[T]{Current: current, Size: size}
1723
}
1824

1925
func Insert[T any](entity *T) *gorm.DB {
@@ -61,47 +67,81 @@ func Update[T any](q *Query[T]) *gorm.DB {
6167
return resultDb
6268
}
6369

64-
func SelectById[T any](id any) (*gorm.DB, T) {
65-
var entity T
66-
resultDb := gormDb.Limit(1).Find(&entity, id)
67-
return resultDb, entity
70+
func SelectById[T any](id any) (*T, *gorm.DB) {
71+
var entity *T
72+
resultDb := gormDb.Take(&entity, id)
73+
if resultDb.RowsAffected == 0 {
74+
return nil, resultDb
75+
}
76+
return entity, resultDb
6877
}
6978

70-
func SelectByIds[T any](ids ...any) (*gorm.DB, []T) {
71-
var results []T
79+
func SelectByIds[T any](ids any) ([]*T, *gorm.DB) {
80+
var results []*T
7281
resultDb := gormDb.Find(&results, ids)
73-
return resultDb, results
82+
return results, resultDb
7483
}
7584

76-
func SelectOne[T any](q *Query[T]) (*gorm.DB, T) {
77-
var entity T
78-
resultDb := gormDb.Select(q.SelectColumns).Where(q.QueryBuilder.String(), q.QueryArgs...).Limit(1).Find(&entity)
79-
return resultDb, entity
85+
func SelectOne[T any](q *Query[T]) (*T, *gorm.DB) {
86+
var entity *T
87+
resultDb := buildCondition(q)
88+
resultDb.Take(&entity)
89+
if resultDb.RowsAffected == 0 {
90+
return nil, resultDb
91+
}
92+
return entity, resultDb
8093
}
8194

82-
func SelectList[T any](q *Query[T]) (*gorm.DB, []T) {
95+
func SelectList[T any](q *Query[T]) ([]*T, *gorm.DB) {
8396
resultDb := buildCondition(q)
84-
var results []T
97+
var results []*T
8598
resultDb.Find(&results)
86-
return resultDb, results
99+
return results, resultDb
100+
}
101+
102+
func SelectModelList[T any, R any](q *Query[T]) ([]*R, *gorm.DB) {
103+
resultDb := buildCondition(q)
104+
var results []*R
105+
resultDb.Scan(&results)
106+
return results, resultDb
87107
}
88108

89-
func SelectPage[T any](page Page, q *Query[T]) (*gorm.DB, []T) {
109+
func SelectPage[T any](page *Page[T], q *Query[T]) (*Page[T], *gorm.DB) {
110+
total, countDb := SelectCount[T](q)
111+
if countDb.Error != nil {
112+
return page, countDb
113+
}
114+
page.Total = total
90115
resultDb := buildCondition(q)
91-
var results []T
116+
var results []*T
92117
resultDb.Scopes(paginate(page)).Find(&results)
93-
return resultDb, results
118+
page.Records = results
119+
return page, resultDb
120+
}
121+
122+
func SelectModelPage[T any, R any](page *Page[R], q *Query[T]) (*Page[R], *gorm.DB) {
123+
total, countDb := SelectCount[T](q)
124+
if countDb.Error != nil {
125+
return page, countDb
126+
}
127+
page.Total = total
128+
resultDb := buildCondition(q)
129+
var results []*R
130+
resultDb.Scopes(paginate(page)).Scan(&results)
131+
page.Records = results
132+
return page, resultDb
94133
}
95134

96-
func SelectCount[T any](q *Query[T]) (*gorm.DB, int64) {
135+
func SelectCount[T any](q *Query[T]) (int64, *gorm.DB) {
97136
var count int64
98-
resultDb := gormDb.Model(new(T)).Where(q.QueryBuilder.String(), q.QueryArgs...).Count(&count)
99-
return resultDb, count
137+
resultDb := buildCondition(q)
138+
resultDb.Count(&count)
139+
return count, resultDb
100140
}
101141

102-
func paginate(p Page) func(db *gorm.DB) *gorm.DB {
103-
page := p.Page
104-
pageSize := p.PageSize
142+
func paginate[T any](p *Page[T]) func(db *gorm.DB) *gorm.DB {
143+
page := p.Current
144+
pageSize := p.Size
105145
return func(db *gorm.DB) *gorm.DB {
106146
if page <= 0 {
107147
page = 1

gormplus/mapper_test.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gormplus
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"gorm.io/driver/mysql"
76
"gorm.io/gorm"
@@ -93,13 +92,16 @@ func TestUpdate(t *testing.T) {
9392
}
9493

9594
func TestSelectById(t *testing.T) {
96-
db, result := SelectById[Test1](1)
97-
fmt.Println(db)
98-
fmt.Println(result)
95+
test1, resultDb := SelectById[Test1](1)
96+
fmt.Println(resultDb)
97+
fmt.Println(test1)
9998
}
10099

101100
func TestSelectByIds(t *testing.T) {
102-
db, result := SelectByIds[Test1](1, 2)
101+
var ids []int
102+
ids = append(ids, 1)
103+
ids = append(ids, 2)
104+
result, db := SelectByIds[Test1](ids)
103105
fmt.Println(db)
104106
fmt.Println(result)
105107
}
@@ -113,28 +115,28 @@ func TestSelectOne(t *testing.T) {
113115
}
114116

115117
func TestSelectList(t *testing.T) {
116-
db, result := SelectList[Test1](nil)
117-
fmt.Println(db.RowsAffected)
118-
for _, v := range result {
119-
marshal, _ := json.Marshal(v)
120-
fmt.Println(string(marshal))
121-
}
118+
/* db, result := SelectList[Test1](nil)
119+
fmt.Println(db.RowsAffected)
120+
for _, v := range result {
121+
marshal, _ := json.Marshal(v)
122+
fmt.Println(string(marshal))
123+
}*/
122124
}
123125

124126
func TestSelectPage(t *testing.T) {
125-
page := Page{Page: 1, PageSize: 10}
126-
db, result := SelectPage[Test1](page, nil)
127-
fmt.Println(db.RowsAffected)
128-
for _, v := range result {
129-
marshal, _ := json.Marshal(v)
130-
fmt.Println(string(marshal))
131-
}
127+
/* page := &Page{Current: 1, Size: 10}
128+
db, result := SelectPage[Test1](page, nil)
129+
fmt.Println(db.RowsAffected)
130+
for _, v := range result {
131+
marshal, _ := json.Marshal(v)
132+
fmt.Println(string(marshal))
133+
}*/
132134
}
133135

134136
func TestSelectCount(t *testing.T) {
135137
q := Query[Test1]{}
136138
q.Eq("price", 100)
137-
db, count := SelectCount(&q)
139+
count, db := SelectCount(&q)
138140
fmt.Println(db)
139141
fmt.Println(count)
140142
}

gormplus/query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type Query[T any] struct {
1919
UpdateMap map[string]any
2020
}
2121

22+
func NewQuery[T any]() *Query[T] {
23+
return &Query[T]{}
24+
}
25+
2226
func (q *Query[T]) Eq(column string, val any) *Query[T] {
2327
q.addCond(column, val, constants.Eq)
2428
return q

0 commit comments

Comments
 (0)