Skip to content

Commit af0c087

Browse files
committed
refactor: use getPKColumn get primaryKey name
1 parent 0460f71 commit af0c087

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

constants/constants.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
package constants
1919

2020
const (
21-
Comma = ","
22-
LeftBracket = "("
23-
RightBracket = ")"
24-
PK = "id"
21+
Comma = ","
22+
LeftBracket = "("
23+
RightBracket = ")"
24+
DefaultPrimaryName = "id"
2525
)

gplus/base_dao.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ package gplus
2020
import (
2121
"github.com/acmestack/gorm-plus/constants"
2222
"gorm.io/gorm"
23+
"gorm.io/gorm/schema"
24+
"gorm.io/gorm/utils"
25+
"reflect"
2326
)
2427

2528
var gormDb *gorm.DB
@@ -64,15 +67,15 @@ func InsertBatchSize[T any](entities []*T, batchSize int) *gorm.DB {
6467
return resultDb
6568
}
6669

67-
func DeleteById[T any](id any, primaryKeyColumn ...string) *gorm.DB {
70+
func DeleteById[T any](id any) *gorm.DB {
6871
var entity T
69-
resultDb := gormDb.Where(getPKColumn(primaryKeyColumn), id).Delete(&entity)
72+
resultDb := gormDb.Where(getPKColumn[T](), id).Delete(&entity)
7073
return resultDb
7174
}
7275

73-
func DeleteByIds[T any](ids any, primaryKeyColumn ...string) *gorm.DB {
76+
func DeleteByIds[T any](ids any) *gorm.DB {
7477
q := NewQuery[T]()
75-
q.In(getPKColumn(primaryKeyColumn), ids)
78+
q.In(getPKColumn[T](), ids)
7679
resultDb := Delete[T](q)
7780
return resultDb
7881
}
@@ -83,8 +86,8 @@ func Delete[T any](q *Query[T]) *gorm.DB {
8386
return resultDb
8487
}
8588

86-
func UpdateById[T any](entity *T, id any, primaryKeyColumn ...string) *gorm.DB {
87-
resultDb := gormDb.Model(&entity).Where(getPKColumn(primaryKeyColumn), id).Updates(entity)
89+
func UpdateById[T any](entity *T, id any) *gorm.DB {
90+
resultDb := gormDb.Model(&entity).Where(getPKColumn[T](), id).Updates(entity)
8891
return resultDb
8992
}
9093

@@ -102,9 +105,9 @@ func SelectById[T any](id any) (*T, *gorm.DB) {
102105
return &entity, resultDb
103106
}
104107

105-
func SelectByIds[T any](ids any, primaryKeyColumn ...string) ([]*T, *gorm.DB) {
108+
func SelectByIds[T any](ids any) ([]*T, *gorm.DB) {
106109
q := NewQuery[T]()
107-
q.In(getPKColumn(primaryKeyColumn), ids)
110+
q.In(getPKColumn[T](), ids)
108111
return SelectList[T](q)
109112
}
110113

@@ -222,9 +225,22 @@ func buildCondition[T any](q *Query[T]) *gorm.DB {
222225
}
223226

224227
// getPKColumn 获取主键key
225-
func getPKColumn(primaryKeyColumn []string) string {
226-
if len(primaryKeyColumn) > 0 {
227-
return primaryKeyColumn[0]
228+
func getPKColumn[T any]() string {
229+
var entity T
230+
entityType := reflect.TypeOf(entity)
231+
numField := entityType.NumField()
232+
var columnName string
233+
for i := 0; i < numField; i++ {
234+
field := entityType.Field(i)
235+
tagSetting := schema.ParseTagSetting(field.Tag.Get("gorm"), ";")
236+
isPrimaryKey := utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"])
237+
if isPrimaryKey {
238+
columnName = tagSetting["COLUMN"]
239+
break
240+
}
241+
}
242+
if columnName == "" {
243+
return constants.DefaultPrimaryName
228244
}
229-
return constants.PK
245+
return columnName
230246
}

0 commit comments

Comments
 (0)