Skip to content

Commit 9bd3ffa

Browse files
authored
feat: add some map function (#21)
1 parent 17b0a45 commit 9bd3ffa

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

gplus/base_dao.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ func Delete[T any](q *Query[T]) *gorm.DB {
8686
return resultDb
8787
}
8888

89+
func DeleteByMap[T any](q *Query[T]) *gorm.DB {
90+
for k, v := range q.ConditionMap {
91+
columnName := q.getColumnName(k)
92+
q.Eq(columnName, v)
93+
}
94+
var entity T
95+
resultDb := gormDb.Where(q.QueryBuilder.String(), q.QueryArgs...).Delete(&entity)
96+
return resultDb
97+
}
98+
8999
func UpdateById[T any](entity *T) *gorm.DB {
90100
resultDb := gormDb.Model(entity).Updates(entity)
91101
return resultDb
@@ -130,6 +140,20 @@ func SelectListModel[T any, R any](q *Query[T]) ([]*R, *gorm.DB) {
130140
return results, resultDb
131141
}
132142

143+
func SelectListByMap[T any](q *Query[T]) ([]*T, *gorm.DB) {
144+
resultDb := buildCondition(q)
145+
var results []*T
146+
resultDb.Find(&results)
147+
return results, resultDb
148+
}
149+
150+
func SelectListMaps[T any](q *Query[T]) ([]map[string]any, *gorm.DB) {
151+
resultDb := buildCondition(q)
152+
var results []map[string]any
153+
resultDb.Find(&results)
154+
return results, resultDb
155+
}
156+
133157
func SelectPage[T any](page *Page[T], q *Query[T]) (*Page[T], *gorm.DB) {
134158
total, countDb := SelectCount[T](q)
135159
if countDb.Error != nil {
@@ -156,6 +180,21 @@ func SelectPageModel[T any, R any](page *Page[R], q *Query[T]) (*Page[R], *gorm.
156180
return page, resultDb
157181
}
158182

183+
func SelectPageMaps[T any](page *Page[map[string]any], q *Query[T]) (*Page[map[string]any], *gorm.DB) {
184+
total, countDb := SelectCount[T](q)
185+
if countDb.Error != nil {
186+
return page, countDb
187+
}
188+
page.Total = total
189+
resultDb := buildCondition(q)
190+
var results []map[string]any
191+
resultDb.Scopes(paginate(page)).Find(&results)
192+
for _, m := range results {
193+
page.Records = append(page.Records, &m)
194+
}
195+
return page, resultDb
196+
}
197+
159198
func SelectCount[T any](q *Query[T]) (int64, *gorm.DB) {
160199
var count int64
161200
resultDb := buildCondition(q)
@@ -204,6 +243,15 @@ func buildCondition[T any](q *Query[T]) *gorm.DB {
204243
resultDb.Where(q.QueryBuilder.String(), q.QueryArgs...)
205244
}
206245

246+
if len(q.ConditionMap) > 0 {
247+
var condMap = make(map[string]any)
248+
for k, v := range q.ConditionMap {
249+
columnName := q.getColumnName(k)
250+
condMap[columnName] = v
251+
}
252+
resultDb.Where(condMap)
253+
}
254+
207255
if q.OrderBuilder.Len() > 0 {
208256
resultDb.Order(q.OrderBuilder.String())
209257
}

gplus/query.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ type Query[T any] struct {
4141
LastCond string
4242
UpdateMap map[string]any
4343
ColumnNameMap map[uintptr]string
44+
ConditionMap map[any]any
4445
}
4546

4647
func NewQuery[T any]() (*Query[T], *T) {
4748
q := &Query[T]{}
4849
return q, q.buildColumnNameMap()
4950
}
5051

52+
func NewQueryMap[T any]() (*Query[T], *T) {
53+
q := &Query[T]{}
54+
q.ConditionMap = make(map[any]any)
55+
return q, q.buildColumnNameMap()
56+
}
57+
5158
func (q *Query[T]) Eq(column any, val any) *Query[T] {
5259
q.addCond(column, val, constants.Eq)
5360
return q

0 commit comments

Comments
 (0)