-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_incr.go
118 lines (101 loc) · 3.51 KB
/
auto_incr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package callbacks
import (
"reflect"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/utils"
comUtl "github.com/wfusion/gofusion/common/utils"
. "gorm.io/driver/mysql"
)
func CreateAutoIncr(db *gorm.DB, gormDialector gorm.Dialector, autoIncrIncr int64) {
withReturning := false
dialector := gormDialector.(*Dialector)
if !dialector.Config.SkipInitializeWithVersion && strings.Contains(dialector.ServerVersion, "MariaDB") {
withReturning = checkVersion(dialector.ServerVersion, "10.5")
}
lastInsertIDReversed := false
if !dialector.Config.DisableWithReturning && withReturning {
lastInsertIDReversed = true
}
comUtl.MustSuccess(
db.Callback().Create().Replace("gorm:create", func(db *gorm.DB) {
if db.Error != nil {
return
}
BuildCreateSQL(db)
isDryRun := !db.DryRun && db.Error == nil
if !isDryRun {
return
}
ok, mode := hasReturning(db, utils.Contains(db.Callback().Create().Clauses, "RETURNING"))
if ok {
if c, ok := db.Statement.Clauses["ON CONFLICT"]; ok {
if onConflict, _ := c.Expression.(clause.OnConflict); onConflict.DoNothing {
mode |= gorm.ScanOnConflictDoNothing
}
}
rows, err := db.Statement.ConnPool.QueryContext(
db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
)
if db.AddError(err) == nil {
defer func() { _ = db.AddError(rows.Close()) }()
gorm.Scan(rows, db, mode)
}
return
}
result, err := db.Statement.ConnPool.ExecContext(
db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
)
if err != nil {
_ = db.AddError(err)
return
}
db.RowsAffected, _ = result.RowsAffected()
if db.RowsAffected != 0 && db.Statement.Schema != nil &&
db.Statement.Schema.PrioritizedPrimaryField != nil &&
db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
insertID, err := result.LastInsertId()
insertOk := err == nil && insertID > 0
if !insertOk {
_ = db.AddError(err)
return
}
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
if lastInsertIDReversed {
for i := db.Statement.ReflectValue.Len() - 1; i >= 0; i-- {
rv := db.Statement.ReflectValue.Index(i)
if reflect.Indirect(rv).Kind() != reflect.Struct {
break
}
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, rv)
if isZero {
_ = db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, rv, insertID))
//insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
insertID -= autoIncrIncr
}
}
} else {
for i := 0; i < db.Statement.ReflectValue.Len(); i++ {
rv := db.Statement.ReflectValue.Index(i)
if reflect.Indirect(rv).Kind() != reflect.Struct {
break
}
if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, rv); isZero {
_ = db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, rv, insertID))
//insertID += db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
insertID += autoIncrIncr
}
}
}
case reflect.Struct:
_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, db.Statement.ReflectValue)
if isZero {
_ = db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, db.Statement.ReflectValue, insertID))
}
}
}
}),
)
}