Skip to content

Commit 3d7bede

Browse files
authored
test: Add some new tests (#52)
1 parent 3649cbd commit 3d7bede

File tree

8 files changed

+1113
-278
lines changed

8 files changed

+1113
-278
lines changed

gplus/cache.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ func getColumnNameMap(model any) map[uintptr]string {
7070
func GetModel[T any]() *T {
7171
modelTypeStr := reflect.TypeOf((*T)(nil)).Elem().String()
7272
if model, ok := modelInstanceCache.Load(modelTypeStr); ok {
73-
return model.(*T)
73+
m, isReal := model.(*T)
74+
if isReal {
75+
return m
76+
}
7477
}
7578
t := new(T)
7679
Cache(t)

gplus/dao.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ func Init(db *gorm.DB) {
3535
}
3636

3737
type Page[T any] struct {
38-
Current int
39-
Size int
40-
Total int64
41-
Records []*T
38+
Current int
39+
Size int
40+
Total int64
41+
Records []*T
42+
RecordsMap []T
4243
}
4344

4445
type Dao[T any] struct{}
@@ -100,15 +101,22 @@ func DeleteByIds[T any](ids any, opts ...OptionFunc) *gorm.DB {
100101

101102
// Delete 根据条件删除记录
102103
func Delete[T any](q *QueryCond[T], opts ...OptionFunc) *gorm.DB {
103-
db := getDb(opts...)
104104
var entity T
105-
resultDb := db.Where(q.queryBuilder.String(), q.queryArgs...).Delete(&entity)
105+
resultDb := buildCondition[T](q, opts...)
106+
resultDb.Delete(&entity)
106107
return resultDb
107108
}
108109

109-
// UpdateById 根据 ID 更新
110+
// UpdateById 根据 ID 更新,默认零值不更新
110111
func UpdateById[T any](entity *T, opts ...OptionFunc) *gorm.DB {
111112
db := getDb(opts...)
113+
resultDb := db.Model(entity).Updates(entity)
114+
return resultDb
115+
}
116+
117+
// UpdateZeroById 根据 ID 零值更新
118+
func UpdateZeroById[T any](entity *T, opts ...OptionFunc) *gorm.DB {
119+
db := getDb(opts...)
112120

113121
// 如果用户没有设置选择更新的字段,默认更新所有的字段,包括零值更新
114122
updateAllIfNeed(entity, opts, db)
@@ -131,8 +139,8 @@ func updateAllIfNeed(entity any, opts []OptionFunc, db *gorm.DB) {
131139

132140
// Update 根据 Map 更新
133141
func Update[T any](q *QueryCond[T], opts ...OptionFunc) *gorm.DB {
134-
db := getDb(opts...)
135-
resultDb := db.Model(new(T)).Where(q.queryBuilder.String(), q.queryArgs...).Updates(&q.updateMap)
142+
resultDb := buildCondition[T](q, opts...)
143+
resultDb.Updates(&q.updateMap)
136144
return resultDb
137145
}
138146

@@ -196,9 +204,9 @@ func SelectCount[T any](q *QueryCond[T], opts ...OptionFunc) (int64, *gorm.DB) {
196204
}
197205

198206
// Exists 根据条件判断记录是否存在
199-
func Exists[T any](q *QueryCond[T], opts ...OptionFunc) (bool, error) {
200-
_, dbRes := SelectOne[T](q, opts...)
201-
return dbRes.RowsAffected > 0, dbRes.Error
207+
func Exists[T any](q *QueryCond[T], opts ...OptionFunc) (bool, *gorm.DB) {
208+
count, resultDb := SelectCount[T](q, opts...)
209+
return count > 0, resultDb
202210
}
203211

204212
// SelectPageGeneric 根据传入的泛型封装分页记录
@@ -215,10 +223,16 @@ func SelectPageGeneric[T any, R any](page *Page[R], q *QueryCond[T], opts ...Opt
215223
page.Total = total
216224
}
217225
resultDb := buildCondition(q, opts...)
218-
var results []R
219-
resultDb.Scopes(paginate(page)).Scan(&results)
220-
for _, m := range results {
221-
page.Records = append(page.Records, &m)
226+
var r R
227+
switch any(r).(type) {
228+
case map[string]any:
229+
var results []R
230+
resultDb.Scopes(paginate(page)).Scan(&results)
231+
page.RecordsMap = results
232+
default:
233+
var results []*R
234+
resultDb.Scopes(paginate(page)).Scan(&results)
235+
page.Records = results
222236
}
223237
return page, resultDb
224238
}

0 commit comments

Comments
 (0)