Create create hook
(config *Config)
| 35 | |
| 36 | // Create create hook |
| 37 | func Create(config *Config) func(db *gorm.DB) { |
| 38 | supportReturning := utils.Contains(config.CreateClauses, "RETURNING") |
| 39 | |
| 40 | return func(db *gorm.DB) { |
| 41 | if db.Error != nil { |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | if db.Statement.Schema != nil { |
| 46 | if !db.Statement.Unscoped { |
| 47 | for _, c := range db.Statement.Schema.CreateClauses { |
| 48 | db.Statement.AddClause(c) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if supportReturning && len(db.Statement.Schema.FieldsWithDefaultDBValue) > 0 { |
| 53 | if _, ok := db.Statement.Clauses["RETURNING"]; !ok { |
| 54 | fromColumns := make([]clause.Column, 0, len(db.Statement.Schema.FieldsWithDefaultDBValue)) |
| 55 | for _, field := range db.Statement.Schema.FieldsWithDefaultDBValue { |
| 56 | if field.Readable { |
| 57 | fromColumns = append(fromColumns, clause.Column{Name: field.DBName}) |
| 58 | } |
| 59 | } |
| 60 | if len(fromColumns) > 0 { |
| 61 | db.Statement.AddClause(clause.Returning{Columns: fromColumns}) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if db.Statement.SQL.Len() == 0 { |
| 68 | db.Statement.SQL.Grow(180) |
| 69 | db.Statement.AddClauseIfNotExists(clause.Insert{}) |
| 70 | db.Statement.AddClause(ConvertToCreateValues(db.Statement)) |
| 71 | |
| 72 | db.Statement.Build(db.Statement.BuildClauses...) |
| 73 | } |
| 74 | |
| 75 | isDryRun := !db.DryRun && db.Error == nil |
| 76 | if !isDryRun { |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | ok, mode := hasReturning(db, supportReturning) |
| 81 | if ok { |
| 82 | if c, ok := db.Statement.Clauses["ON CONFLICT"]; ok { |
| 83 | onConflict, _ := c.Expression.(clause.OnConflict) |
| 84 | if onConflict.DoNothing { |
| 85 | mode |= gorm.ScanOnConflictDoNothing |
| 86 | } else if len(onConflict.DoUpdates) > 0 || onConflict.UpdateAll { |
| 87 | mode |= gorm.ScanUpdate |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | rows, err := db.Statement.ConnPool.QueryContext( |
| 92 | db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars..., |
| 93 | ) |
| 94 | if db.AddError(err) == nil { |
no test coverage detected