Skip to content

Commit eae5701

Browse files
aide-cloud胡标
andauthored
feat: add primaryKeyColumn and primaryKeyColumn type
Co-authored-by: 胡标 <biao.hu@baishan.com>
1 parent 5e3f623 commit eae5701

File tree

8 files changed

+79
-47
lines changed

8 files changed

+79
-47
lines changed

constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ const (
2121
Comma = ","
2222
LeftBracket = "("
2323
RightBracket = ")"
24+
PK = "id"
2425
)

example/base/update_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
func TestUpdateById(t *testing.T) {
2727
user := &User{ID: 1, Username: "zhangsan", Password: "123456", Age: 18, Score: 100, Dept: "A部门asdfasdf"}
28-
result := gplus.UpdateById(user)
28+
result := gplus.UpdateById(user, user.ID)
2929
if result.Error != nil {
3030
fmt.Println(result.Error)
3131
}

example/common/remove_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestRemoveById(t *testing.T) {
2929
}
3030

3131
func TestRemoveByIds(t *testing.T) {
32-
var ids []int
32+
var ids []int64
3333
ids = append(ids, 5)
3434
ids = append(ids, 6)
3535
resultDb := userDao.RemoveByIds(ids)

example/common/update_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
func TestUpdateById(t *testing.T) {
2626
user4 := &User{ID: 4, Username: "zhangsan666", Password: "123456", Age: 13, Score: 12, Dept: "导弹部门"}
27-
userDao.UpdateById(user4)
27+
userDao.UpdateById(user4, user4.ID)
2828
}
2929

3030
func TestUpdate(t *testing.T) {

example/common/user_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import "github.com/acmestack/gorm-plus/gplus"
2222
var userDao = NewUserDao[User]()
2323

2424
type UserDao[T any] struct {
25-
gplus.CommonDao[T]
25+
gplus.CommonDao[T, int64]
2626
}
2727

2828
func NewUserDao[T any]() *UserDao[T] {

gplus/base_dao.go

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package gplus
1919

2020
import (
21+
"github.com/acmestack/gorm-plus/constants"
2122
"gorm.io/gorm"
2223
)
2324

@@ -40,31 +41,43 @@ func NewPage[T any](current, size int) *Page[T] {
4041
}
4142

4243
func Insert[T any](entity *T) *gorm.DB {
43-
resultDb := gormDb.Create(&entity)
44+
resultDb := gormDb.Create(entity)
4445
return resultDb
4546
}
4647

47-
func InsertBatch[T any](entities any) *gorm.DB {
48+
func InsertBatch[T any](entities []*T) *gorm.DB {
49+
if len(entities) == 0 {
50+
return gormDb
51+
}
4852
resultDb := gormDb.CreateInBatches(entities, defaultBatchSize)
4953
return resultDb
5054
}
5155

52-
func InsertBatchSize[T any](entities any, batchSize int) *gorm.DB {
56+
func InsertBatchSize[T any](entities []*T, batchSize int) *gorm.DB {
57+
if len(entities) == 0 {
58+
return gormDb
59+
}
5360
if batchSize <= 0 {
5461
batchSize = defaultBatchSize
5562
}
5663
resultDb := gormDb.CreateInBatches(entities, batchSize)
5764
return resultDb
5865
}
5966

60-
func DeleteById[T any](id any) *gorm.DB {
61-
resultDb := gormDb.Delete(new(T), id)
67+
func DeleteById[T any, K PrimaryKey](id K, primaryKeyColumn ...string) *gorm.DB {
68+
var entity T
69+
resultDb := gormDb.Where(getPKColumn(primaryKeyColumn), id).Delete(&entity)
6270
return resultDb
6371
}
6472

65-
func DeleteByIds[T any](ids any) *gorm.DB {
66-
var entities []T
67-
resultDb := gormDb.Delete(&entities, ids)
73+
func DeleteByIds[T any, K PrimaryKey](ids []K, primaryKeyColumn ...string) *gorm.DB {
74+
if len(ids) == 0 {
75+
return gormDb
76+
}
77+
78+
q := NewQuery[T]()
79+
q.In(getPKColumn(primaryKeyColumn), ids)
80+
resultDb := Delete[T](q)
6881
return resultDb
6982
}
7083

@@ -74,8 +87,8 @@ func Delete[T any](q *Query[T]) *gorm.DB {
7487
return resultDb
7588
}
7689

77-
func UpdateById[T any](entity *T) *gorm.DB {
78-
resultDb := gormDb.Model(&entity).Updates(&entity)
90+
func UpdateById[T any, K PrimaryKey](entity *T, id K, primaryKeyColumn ...string) *gorm.DB {
91+
resultDb := gormDb.Model(&entity).Where(getPKColumn(primaryKeyColumn), id).Updates(entity)
7992
return resultDb
8093
}
8194

@@ -84,29 +97,29 @@ func Update[T any](q *Query[T]) *gorm.DB {
8497
return resultDb
8598
}
8699

87-
func SelectById[T any](id any) (*T, *gorm.DB) {
88-
var entity *T
100+
func SelectById[T any, K PrimaryKey](id K) (*T, *gorm.DB) {
101+
var entity T
89102
resultDb := gormDb.Take(&entity, id)
90103
if resultDb.RowsAffected == 0 {
91104
return nil, resultDb
92105
}
93-
return entity, resultDb
106+
return &entity, resultDb
94107
}
95108

96-
func SelectByIds[T any](ids any) ([]*T, *gorm.DB) {
97-
var results []*T
98-
resultDb := gormDb.Find(&results, ids)
99-
return results, resultDb
109+
func SelectByIds[T any, K PrimaryKey](ids []K, primaryKeyColumn ...string) ([]*T, *gorm.DB) {
110+
q := NewQuery[T]()
111+
q.In(getPKColumn(primaryKeyColumn), ids)
112+
return SelectList[T](q)
100113
}
101114

102115
func SelectOne[T any](q *Query[T]) (*T, *gorm.DB) {
103-
var entity *T
116+
var entity T
104117
resultDb := buildCondition(q)
105118
resultDb.Take(&entity)
106119
if resultDb.RowsAffected == 0 {
107120
return nil, resultDb
108121
}
109-
return entity, resultDb
122+
return &entity, resultDb
110123
}
111124

112125
func SelectList[T any](q *Query[T]) ([]*T, *gorm.DB) {
@@ -211,3 +224,11 @@ func buildCondition[T any](q *Query[T]) *gorm.DB {
211224
}
212225
return resultDb
213226
}
227+
228+
// getPKColumn 获取主键key
229+
func getPKColumn(primaryKeyColumn []string) string {
230+
if len(primaryKeyColumn) > 0 {
231+
return primaryKeyColumn[0]
232+
}
233+
return constants.PK
234+
}

gplus/common_dao.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,77 +21,82 @@ import (
2121
"gorm.io/gorm"
2222
)
2323

24-
type CommonDao[T any] struct {
24+
type CommonDao[T any, K PrimaryKey] struct {
25+
pkColumn string
2526
}
2627

27-
func (service CommonDao[T]) Db() *gorm.DB {
28+
func NewCommonDao[T any, K PrimaryKey](pk string) *CommonDao[T, K] {
29+
return &CommonDao[T, K]{pkColumn: pk}
30+
}
31+
32+
func (service CommonDao[T, K]) Db() *gorm.DB {
2833
return gormDb
2934
}
3035

31-
func (service CommonDao[T]) Save(entity *T) *gorm.DB {
36+
func (service CommonDao[T, K]) Save(entity *T) *gorm.DB {
3237
return Insert[T](entity)
3338
}
3439

35-
func (service CommonDao[T]) SaveBatch(entities any) *gorm.DB {
40+
func (service CommonDao[T, K]) SaveBatch(entities []*T) *gorm.DB {
3641
return InsertBatch[T](entities)
3742
}
3843

39-
func (service CommonDao[T]) SaveBatchSize(entities any, batchSize int) *gorm.DB {
44+
func (service CommonDao[T, K]) SaveBatchSize(entities []*T, batchSize int) *gorm.DB {
4045
return InsertBatchSize[T](entities, batchSize)
4146
}
4247

43-
func (service CommonDao[T]) RemoveById(id any) *gorm.DB {
44-
return DeleteById[T](id)
48+
func (service CommonDao[T, K]) RemoveById(id any) *gorm.DB {
49+
return DeleteById[T, K](id, service.pkColumn)
4550
}
4651

47-
func (service CommonDao[T]) RemoveByIds(ids any) *gorm.DB {
48-
return DeleteByIds[T](ids)
52+
func (service CommonDao[T, K]) RemoveByIds(ids []K) *gorm.DB {
53+
return DeleteByIds[T, K](ids)
4954
}
5055

51-
func (service CommonDao[T]) Remove(q *Query[T]) *gorm.DB {
56+
func (service CommonDao[T, K]) Remove(q *Query[T]) *gorm.DB {
5257
return Delete[T](q)
5358
}
5459

55-
func (service CommonDao[T]) UpdateById(entity *T) *gorm.DB {
56-
return UpdateById[T](entity)
60+
func (service CommonDao[T, K]) UpdateById(entity *T, id K) *gorm.DB {
61+
return UpdateById[T, K](entity, id, service.pkColumn)
5762
}
5863

59-
func (service CommonDao[T]) Update(q *Query[T]) *gorm.DB {
64+
func (service CommonDao[T, K]) Update(q *Query[T]) *gorm.DB {
6065
return Update[T](q)
6166
}
6267

63-
func (service CommonDao[T]) GetById(id any) (*T, *gorm.DB) {
64-
return SelectById[T](id)
68+
func (service CommonDao[T, K]) GetById(id K) (*T, *gorm.DB) {
69+
return SelectById[T, K](id)
6570
}
6671

67-
func (service CommonDao[T]) GetOne(q *Query[T]) (*T, *gorm.DB) {
72+
func (service CommonDao[T, K]) GetOne(q *Query[T]) (*T, *gorm.DB) {
6873
return SelectOne[T](q)
6974
}
7075

71-
func (service CommonDao[T]) ListAll() ([]*T, *gorm.DB) {
76+
func (service CommonDao[T, K]) ListAll() ([]*T, *gorm.DB) {
7277
return SelectList[T](nil)
7378
}
7479

75-
func (service CommonDao[T]) List(q *Query[T]) ([]*T, *gorm.DB) {
80+
func (service CommonDao[T, K]) List(q *Query[T]) ([]*T, *gorm.DB) {
7681
return SelectList[T](q)
7782
}
7883

79-
func (service CommonDao[T]) ListByIds(ids any) ([]*T, *gorm.DB) {
80-
return SelectByIds[T](ids)
84+
func (service CommonDao[T, K]) ListByIds(ids []K) ([]*T, *gorm.DB) {
85+
return SelectByIds[T, K](ids)
8186
}
8287

83-
func (service CommonDao[T]) PageAll(page *Page[T]) (*Page[T], *gorm.DB) {
88+
func (service CommonDao[T, K]) PageAll(page *Page[T]) (*Page[T], *gorm.DB) {
8489
return SelectPage[T](page, nil)
8590
}
8691

87-
func (service CommonDao[T]) Page(page *Page[T], q *Query[T]) (*Page[T], *gorm.DB) {
92+
func (service CommonDao[T, K]) Page(page *Page[T], q *Query[T]) (*Page[T], *gorm.DB) {
8893
return SelectPage[T](page, q)
8994
}
9095

91-
func (service CommonDao[T]) CountAll() (int64, *gorm.DB) {
96+
func (service CommonDao[T, K]) CountAll() (int64, *gorm.DB) {
9297
return SelectCount[T](nil)
9398
}
9499

95-
func (service CommonDao[T]) Count(q *Query[T]) (int64, *gorm.DB) {
100+
func (service CommonDao[T, K]) Count(q *Query[T]) (int64, *gorm.DB) {
96101
return SelectCount[T](q)
97102
}

gplus/type.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package gplus
2+
3+
type PrimaryKey interface {
4+
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~string
5+
}

0 commit comments

Comments
 (0)