github.com/wfusion/gofusion@v1.1.14/db/callbacks/build_sql.go (about) 1 package callbacks 2 3 import ( 4 "reflect" 5 6 "gorm.io/gorm" 7 "gorm.io/gorm/callbacks" 8 "gorm.io/gorm/clause" 9 "gorm.io/gorm/schema" 10 "gorm.io/gorm/utils" 11 12 "github.com/wfusion/gofusion/db/softdelete" 13 ) 14 15 func BuildQuerySQL(db *gorm.DB) { 16 callbacks.BuildQuerySQL(db) 17 } 18 19 func BuildCreateSQL(db *gorm.DB) { 20 supportReturning := utils.Contains(db.Callback().Create().Clauses, "RETURNING") 21 if db.Statement.Schema != nil { 22 if !db.Statement.Unscoped { 23 for _, c := range db.Statement.Schema.CreateClauses { 24 db.Statement.AddClause(c) 25 } 26 } 27 28 if supportReturning && len(db.Statement.Schema.FieldsWithDefaultDBValue) > 0 { 29 if _, ok := db.Statement.Clauses["RETURNING"]; !ok { 30 fromColumns := make([]clause.Column, 0, len(db.Statement.Schema.FieldsWithDefaultDBValue)) 31 for _, field := range db.Statement.Schema.FieldsWithDefaultDBValue { 32 fromColumns = append(fromColumns, clause.Column{Name: field.DBName}) 33 } 34 db.Statement.AddClause(clause.Returning{Columns: fromColumns}) 35 } 36 } 37 } 38 39 if db.Statement.SQL.Len() == 0 { 40 db.Statement.SQL.Grow(180) 41 42 db.Statement.AddClauseIfNotExists(clause.Insert{}) 43 db.Statement.AddClause(callbacks.ConvertToCreateValues(db.Statement)) 44 delete(db.Statement.Clauses, clause.From{}.Name()) 45 46 db.Statement.Build(db.Statement.BuildClauses...) 47 } 48 } 49 50 func BuildUpdateSQL(db *gorm.DB) { 51 if db.Statement.Schema != nil { 52 for _, c := range db.Statement.Schema.UpdateClauses { 53 db.Statement.AddClause(c) 54 } 55 } 56 57 if db.Statement.SQL.Len() == 0 { 58 db.Statement.SQL.Grow(180) 59 db.Statement.AddClauseIfNotExists(clause.Update{}) 60 delete(db.Statement.Clauses, clause.From{}.Name()) 61 if _, ok := db.Statement.Clauses["SET"]; !ok { 62 if set := callbacks.ConvertToAssignments(db.Statement); len(set) != 0 { 63 db.Statement.AddClause(set) 64 } else { 65 return 66 } 67 } 68 69 db.Statement.Build(db.Statement.BuildClauses...) 70 } 71 } 72 73 func BuildDeleteSQL(db *gorm.DB) { 74 if db.Statement.Schema != nil { 75 for _, c := range db.Statement.Schema.DeleteClauses { 76 db.Statement.AddClause(c) 77 } 78 } 79 80 if db.Statement.SQL.Len() > 0 { 81 return 82 } 83 84 stmt := db.Statement 85 stmt.SQL.Grow(180) 86 if stmt.Schema != nil { 87 _, queryValues := schema.GetIdentityFieldValuesMap(stmt.Context, stmt.ReflectValue, stmt.Schema.PrimaryFields) 88 column, values := schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues) 89 90 if len(values) > 0 { 91 stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}}) 92 } 93 94 if stmt.ReflectValue.CanAddr() && stmt.Dest != stmt.Model && stmt.Model != nil { 95 _, queryValues = schema.GetIdentityFieldValuesMap(stmt.Context, reflect.ValueOf(stmt.Model), stmt.Schema.PrimaryFields) 96 column, values = schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues) 97 98 if len(values) > 0 { 99 stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}}) 100 } 101 } 102 } 103 104 if softdelete.IsClausesWithSoftDelete(db.Statement.Clauses) { 105 stmt.AddClauseIfNotExists(clause.Update{}) 106 delete(stmt.Clauses, clause.From{}.Name()) 107 stmt.Build(stmt.DB.Callback().Update().Clauses...) 108 } else { 109 stmt.AddClauseIfNotExists(clause.Delete{}) 110 stmt.AddClauseIfNotExists(clause.From{}) 111 stmt.Build(stmt.BuildClauses...) 112 } 113 }