Merge branch 'go-gorm:master' into master

This commit is contained in:
piyongcai 2021-12-16 07:59:41 +08:00 committed by GitHub
commit cb6ffff60c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 50 additions and 28 deletions

View File

@ -9,5 +9,3 @@ jobs:
uses: actions/checkout@v2
- name: golangci-lint
uses: reviewdog/action-golangci-lint@v2
with:
golangci_lint_flags: '-E cyclop,unconvert,misspell,unparam,ineffassign,gocritic,prealloc,exportloopref,gosec'

11
.golangci.yml Normal file
View File

@ -0,0 +1,11 @@
linters:
enable:
- cyclop
- exportloopref
- gocritic
- gosec
- ineffassign
- misspell
- prealloc
- unconvert
- unparam

View File

@ -57,7 +57,7 @@ func Create(config *Config) func(db *gorm.DB) {
}
}
if db.Statement.SQL.String() == "" {
if db.Statement.SQL.Len() == 0 {
db.Statement.SQL.Grow(180)
db.Statement.AddClauseIfNotExists(clause.Insert{})
db.Statement.AddClause(ConvertToCreateValues(db.Statement))

View File

@ -118,13 +118,7 @@ func Delete(config *Config) func(db *gorm.DB) {
return
}
if db.Statement.Schema != nil && !db.Statement.Unscoped {
for _, c := range db.Statement.Schema.DeleteClauses {
db.Statement.AddClause(c)
}
}
if db.Statement.SQL.String() == "" {
if db.Statement.SQL.Len() == 0 {
db.Statement.SQL.Grow(100)
db.Statement.AddClauseIfNotExists(clause.Delete{})
@ -147,6 +141,15 @@ func Delete(config *Config) func(db *gorm.DB) {
}
db.Statement.AddClauseIfNotExists(clause.From{})
}
if db.Statement.Schema != nil {
for _, c := range db.Statement.Schema.DeleteClauses {
db.Statement.AddClause(c)
}
}
if db.Statement.SQL.Len() == 0 {
db.Statement.Build(db.Statement.BuildClauses...)
}

View File

@ -33,7 +33,7 @@ func BuildQuerySQL(db *gorm.DB) {
}
}
if db.Statement.SQL.String() == "" {
if db.Statement.SQL.Len() == 0 {
db.Statement.SQL.Grow(100)
clauseSelect := clause.Select{Distinct: db.Statement.Distinct}

View File

@ -59,13 +59,7 @@ func Update(config *Config) func(db *gorm.DB) {
return
}
if db.Statement.Schema != nil && !db.Statement.Unscoped {
for _, c := range db.Statement.Schema.UpdateClauses {
db.Statement.AddClause(c)
}
}
if db.Statement.SQL.String() == "" {
if db.Statement.SQL.Len() == 0 {
db.Statement.SQL.Grow(180)
db.Statement.AddClauseIfNotExists(clause.Update{})
if set := ConvertToAssignments(db.Statement); len(set) != 0 {
@ -73,6 +67,16 @@ func Update(config *Config) func(db *gorm.DB) {
} else if _, ok := db.Statement.Clauses["SET"]; !ok {
return
}
}
if db.Statement.Schema != nil {
for _, c := range db.Statement.Schema.UpdateClauses {
db.Statement.AddClause(c)
}
}
if db.Statement.SQL.Len() == 0 {
db.Statement.Build(db.Statement.BuildClauses...)
}

2
go.mod
View File

@ -4,5 +4,5 @@ go 1.14
require (
github.com/jinzhu/inflection v1.0.0
github.com/jinzhu/now v1.1.3
github.com/jinzhu/now v1.1.4
)

4
go.sum
View File

@ -1,4 +1,4 @@
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.3 h1:PlHq1bSCSZL9K0wUhbm2pGLoTWs2GwVhsP6emvGV/ZI=
github.com/jinzhu/now v1.1.3/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=

View File

@ -103,7 +103,7 @@ func (sd SoftDeleteUpdateClause) MergeClause(*clause.Clause) {
}
func (sd SoftDeleteUpdateClause) ModifyStatement(stmt *Statement) {
if stmt.SQL.String() == "" {
if stmt.SQL.Len() == 0 && !stmt.Statement.Unscoped {
if _, ok := stmt.Clauses["WHERE"]; stmt.DB.AllowGlobalUpdate || ok {
SoftDeleteQueryClause(sd).ModifyStatement(stmt)
}
@ -129,7 +129,7 @@ func (sd SoftDeleteDeleteClause) MergeClause(*clause.Clause) {
}
func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *Statement) {
if stmt.SQL.String() == "" {
if stmt.SQL.Len() == 0 && !stmt.Statement.Unscoped {
curTime := stmt.DB.NowFunc()
stmt.AddClause(clause.Set{{Column: clause.Column{Name: sd.Field.DBName}, Value: curTime}})
stmt.SetColumn(sd.Field.DBName, curTime, true)

View File

@ -5,14 +5,14 @@ go 1.14
require (
github.com/google/uuid v1.3.0
github.com/jackc/pgx/v4 v4.14.1 // indirect
github.com/jinzhu/now v1.1.3
github.com/jinzhu/now v1.1.4
github.com/lib/pq v1.10.4
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
gorm.io/driver/mysql v1.2.0
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b // indirect
gorm.io/driver/mysql v1.2.1
gorm.io/driver/postgres v1.2.3
gorm.io/driver/sqlite v1.2.6
gorm.io/driver/sqlserver v1.2.1
gorm.io/gorm v1.22.3
gorm.io/gorm v1.22.4
)
replace gorm.io/gorm => ../

View File

@ -645,7 +645,13 @@ func TestSave(t *testing.T) {
dryDB := DB.Session(&gorm.Session{DryRun: true})
stmt := dryDB.Save(&user).Statement
if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(stmt.SQL.String()) {
if !regexp.MustCompile(`.id. = .* AND .users.\..deleted_at. IS NULL`).MatchString(stmt.SQL.String()) {
t.Fatalf("invalid updating SQL, got %v", stmt.SQL.String())
}
dryDB = DB.Session(&gorm.Session{DryRun: true})
stmt = dryDB.Unscoped().Save(&user).Statement
if !regexp.MustCompile(`WHERE .id. = [^ ]+$`).MatchString(stmt.SQL.String()) {
t.Fatalf("invalid updating SQL, got %v", stmt.SQL.String())
}