Skip to content

Commit e4ec28f

Browse files
authored
refactor: refactor some methods and add some methods comments (#46)
1 parent 4f1a78b commit e4ec28f

File tree

2 files changed

+83
-54
lines changed

2 files changed

+83
-54
lines changed

gplus/dao.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type Page[T any] struct {
4343

4444
type Dao[T any] struct{}
4545

46-
func (dao Dao[T]) NewQuery() (*Query[T], *T) {
47-
q := &Query[T]{}
46+
func (dao Dao[T]) NewQuery() (*QueryCond[T], *T) {
47+
q := &QueryCond[T]{}
4848
return q, q.buildColumnNameMap()
4949
}
5050

@@ -99,15 +99,15 @@ func DeleteByIds[T any](ids any, opts ...OptionFunc) *gorm.DB {
9999
}
100100

101101
// Delete 根据条件删除记录
102-
func Delete[T any](q *Query[T], opts ...OptionFunc) *gorm.DB {
102+
func Delete[T any](q *QueryCond[T], opts ...OptionFunc) *gorm.DB {
103103
db := getDb(opts...)
104104
var entity T
105105
resultDb := db.Where(q.QueryBuilder.String(), q.QueryArgs...).Delete(&entity)
106106
return resultDb
107107
}
108108

109109
// DeleteByMap 根据Map删除记录
110-
func DeleteByMap[T any](q *Query[T], opts ...OptionFunc) *gorm.DB {
110+
func DeleteByMap[T any](q *QueryCond[T], opts ...OptionFunc) *gorm.DB {
111111
db := getDb(opts...)
112112
for k, v := range q.ConditionMap {
113113
columnName := getColumnName(k)
@@ -126,7 +126,7 @@ func UpdateById[T any](entity *T, opts ...OptionFunc) *gorm.DB {
126126
}
127127

128128
// Update 根据 Map 更新
129-
func Update[T any](q *Query[T], opts ...OptionFunc) *gorm.DB {
129+
func Update[T any](q *QueryCond[T], opts ...OptionFunc) *gorm.DB {
130130
db := getDb(opts...)
131131
resultDb := db.Model(new(T)).Where(q.QueryBuilder.String(), q.QueryArgs...).Updates(&q.UpdateMap)
132132
return resultDb
@@ -149,20 +149,20 @@ func SelectByIds[T any](ids any, opts ...OptionFunc) ([]*T, *gorm.DB) {
149149
}
150150

151151
// SelectOne 根据条件查询单条记录
152-
func SelectOne[T any](q *Query[T], opts ...OptionFunc) (*T, *gorm.DB) {
152+
func SelectOne[T any](q *QueryCond[T], opts ...OptionFunc) (*T, *gorm.DB) {
153153
var entity T
154154
resultDb := buildCondition(q, opts...)
155155
return &entity, resultDb.First(&entity)
156156
}
157157

158158
// Exists 根据条件判断记录是否存在
159-
func Exists[T any](q *Query[T], opts ...OptionFunc) (bool, error) {
159+
func Exists[T any](q *QueryCond[T], opts ...OptionFunc) (bool, error) {
160160
_, dbRes := SelectOne[T](q, opts...)
161161
return dbRes.RowsAffected > 0, dbRes.Error
162162
}
163163

164164
// SelectList 根据条件查询多条记录
165-
func SelectList[T any](q *Query[T], opts ...OptionFunc) ([]*T, *gorm.DB) {
165+
func SelectList[T any](q *QueryCond[T], opts ...OptionFunc) ([]*T, *gorm.DB) {
166166
resultDb := buildCondition(q, opts...)
167167
var results []*T
168168
resultDb.Find(&results)
@@ -172,31 +172,31 @@ func SelectList[T any](q *Query[T], opts ...OptionFunc) ([]*T, *gorm.DB) {
172172
// SelectListModel 根据条件查询多条记录
173173
// 第一个泛型代表数据库表实体
174174
// 第二个泛型代表返回记录实体
175-
func SelectListModel[T any, R any](q *Query[T], opts ...OptionFunc) ([]*R, *gorm.DB) {
175+
func SelectListModel[T any, R any](q *QueryCond[T], opts ...OptionFunc) ([]*R, *gorm.DB) {
176176
resultDb := buildCondition(q, opts...)
177177
var results []*R
178178
resultDb.Scan(&results)
179179
return results, resultDb
180180
}
181181

182182
// SelectListByMap 根据 Map 查询多条记录
183-
func SelectListByMap[T any](q *Query[T], opts ...OptionFunc) ([]*T, *gorm.DB) {
183+
func SelectListByMap[T any](q *QueryCond[T], opts ...OptionFunc) ([]*T, *gorm.DB) {
184184
resultDb := buildCondition(q, opts...)
185185
var results []*T
186186
resultDb.Find(&results)
187187
return results, resultDb
188188
}
189189

190190
// SelectListMaps 根据条件查询,返回Map记录
191-
func SelectListMaps[T any](q *Query[T], opts ...OptionFunc) ([]map[string]any, *gorm.DB) {
191+
func SelectListMaps[T any](q *QueryCond[T], opts ...OptionFunc) ([]map[string]any, *gorm.DB) {
192192
resultDb := buildCondition(q, opts...)
193193
var results []map[string]any
194194
resultDb.Find(&results)
195195
return results, resultDb
196196
}
197197

198198
// SelectPage 根据条件分页查询记录
199-
func SelectPage[T any](page *Page[T], q *Query[T], opts ...OptionFunc) (*Page[T], *gorm.DB) {
199+
func SelectPage[T any](page *Page[T], q *QueryCond[T], opts ...OptionFunc) (*Page[T], *gorm.DB) {
200200
option := getOption(opts)
201201

202202
// 如果需要分页忽略总数,不查询总数
@@ -218,7 +218,7 @@ func SelectPage[T any](page *Page[T], q *Query[T], opts ...OptionFunc) (*Page[T]
218218
// SelectPageModel 根据条件分页查询记录
219219
// 第一个泛型代表数据库表实体
220220
// 第二个泛型代表返回记录实体
221-
func SelectPageModel[T any, R any](page *Page[R], q *Query[T], opts ...OptionFunc) (*Page[R], *gorm.DB) {
221+
func SelectPageModel[T any, R any](page *Page[R], q *QueryCond[T], opts ...OptionFunc) (*Page[R], *gorm.DB) {
222222
option := getOption(opts)
223223
// 如果需要分页忽略总数,不查询总数
224224
if !option.IgnoreTotal {
@@ -236,7 +236,7 @@ func SelectPageModel[T any, R any](page *Page[R], q *Query[T], opts ...OptionFun
236236
}
237237

238238
// SelectPageMaps 根据条件分页查询,返回分页Map记录
239-
func SelectPageMaps[T any](page *Page[map[string]any], q *Query[T], opts ...OptionFunc) (*Page[map[string]any], *gorm.DB) {
239+
func SelectPageMaps[T any](page *Page[map[string]any], q *QueryCond[T], opts ...OptionFunc) (*Page[map[string]any], *gorm.DB) {
240240
option := getOption(opts)
241241
// 如果需要分页忽略总数,不查询总数
242242
if !option.IgnoreTotal {
@@ -256,7 +256,7 @@ func SelectPageMaps[T any](page *Page[map[string]any], q *Query[T], opts ...Opti
256256
}
257257

258258
// SelectCount 根据条件查询记录数量
259-
func SelectCount[T any](q *Query[T], opts ...OptionFunc) (int64, *gorm.DB) {
259+
func SelectCount[T any](q *QueryCond[T], opts ...OptionFunc) (int64, *gorm.DB) {
260260
var count int64
261261
resultDb := buildCondition(q, opts...)
262262
resultDb.Count(&count)
@@ -283,7 +283,7 @@ func paginate[T any](p *Page[T]) func(db *gorm.DB) *gorm.DB {
283283
}
284284
}
285285

286-
func buildCondition[T any](q *Query[T], opts ...OptionFunc) *gorm.DB {
286+
func buildCondition[T any](q *QueryCond[T], opts ...OptionFunc) *gorm.DB {
287287
db := getDb(opts...)
288288
resultDb := db.Model(new(T))
289289
if q != nil {

0 commit comments

Comments
 (0)