adjust code for Create
This commit is contained in:
		
							parent
							
								
									e841ee0b7f
								
							
						
					
					
						commit
						029c663d02
					
				@ -65,66 +65,81 @@ func Create(config *Config) func(db *gorm.DB) {
 | 
				
			|||||||
			db.Statement.Build(db.Statement.BuildClauses...)
 | 
								db.Statement.Build(db.Statement.BuildClauses...)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if !db.DryRun && db.Error == nil {
 | 
							isDryRun := !db.DryRun && db.Error == nil
 | 
				
			||||||
 | 
							if !isDryRun {
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if ok, mode := hasReturning(db, supportReturning); ok {
 | 
							ok, mode := hasReturning(db, supportReturning)
 | 
				
			||||||
				if c, ok := db.Statement.Clauses["ON CONFLICT"]; ok {
 | 
							if ok {
 | 
				
			||||||
					if onConflict, _ := c.Expression.(clause.OnConflict); onConflict.DoNothing {
 | 
								if c, ok := db.Statement.Clauses["ON CONFLICT"]; ok {
 | 
				
			||||||
						mode |= gorm.ScanOnConflictDoNothing
 | 
									if onConflict, _ := c.Expression.(clause.OnConflict); onConflict.DoNothing {
 | 
				
			||||||
					}
 | 
										mode |= gorm.ScanOnConflictDoNothing
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil {
 | 
								}
 | 
				
			||||||
					gorm.Scan(rows, db, mode)
 | 
					 | 
				
			||||||
					rows.Close()
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			} else {
 | 
					 | 
				
			||||||
				result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if err != nil {
 | 
								rows, err := db.Statement.ConnPool.QueryContext(
 | 
				
			||||||
					db.AddError(err)
 | 
									db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
 | 
				
			||||||
					return
 | 
								)
 | 
				
			||||||
				}
 | 
								if db.AddError(err) == nil {
 | 
				
			||||||
 | 
									gorm.Scan(rows, db, mode)
 | 
				
			||||||
 | 
									rows.Close()
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				db.RowsAffected, _ = result.RowsAffected()
 | 
								return
 | 
				
			||||||
				if db.RowsAffected != 0 && db.Statement.Schema != nil &&
 | 
							}
 | 
				
			||||||
					db.Statement.Schema.PrioritizedPrimaryField != nil && db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
 | 
					 | 
				
			||||||
					if insertID, err := result.LastInsertId(); err == nil && insertID > 0 {
 | 
					 | 
				
			||||||
						switch db.Statement.ReflectValue.Kind() {
 | 
					 | 
				
			||||||
						case reflect.Slice, reflect.Array:
 | 
					 | 
				
			||||||
							if config.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(rv)
 | 
							result, err := db.Statement.ConnPool.ExecContext(
 | 
				
			||||||
									if isZero {
 | 
								db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
 | 
				
			||||||
										db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
 | 
							)
 | 
				
			||||||
										insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
 | 
							if err != nil {
 | 
				
			||||||
									}
 | 
								db.AddError(err)
 | 
				
			||||||
								}
 | 
								return
 | 
				
			||||||
							} 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(rv); isZero {
 | 
							db.RowsAffected, _ = result.RowsAffected()
 | 
				
			||||||
										db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
 | 
							if db.RowsAffected != 0 && db.Statement.Schema != nil &&
 | 
				
			||||||
										insertID += db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
 | 
								db.Statement.Schema.PrioritizedPrimaryField != nil &&
 | 
				
			||||||
									}
 | 
								db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
 | 
				
			||||||
								}
 | 
								insertID, err := result.LastInsertId()
 | 
				
			||||||
							}
 | 
								insertOk := err == nil && insertID > 0
 | 
				
			||||||
						case reflect.Struct:
 | 
								if !insertOk {
 | 
				
			||||||
							if _, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.ReflectValue); isZero {
 | 
									db.AddError(err)
 | 
				
			||||||
								db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.ReflectValue, insertID)
 | 
									return
 | 
				
			||||||
							}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								switch db.Statement.ReflectValue.Kind() {
 | 
				
			||||||
 | 
								case reflect.Slice, reflect.Array:
 | 
				
			||||||
 | 
									if config.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(rv)
 | 
				
			||||||
 | 
											if isZero {
 | 
				
			||||||
 | 
												db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
 | 
				
			||||||
 | 
												insertID -= db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					} else {
 | 
					 | 
				
			||||||
						db.AddError(err)
 | 
					 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
 | 
									} 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(rv); isZero {
 | 
				
			||||||
 | 
												db.Statement.Schema.PrioritizedPrimaryField.Set(rv, insertID)
 | 
				
			||||||
 | 
												insertID += db.Statement.Schema.PrioritizedPrimaryField.AutoIncrementIncrement
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								case reflect.Struct:
 | 
				
			||||||
 | 
									_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.ReflectValue)
 | 
				
			||||||
 | 
									if isZero {
 | 
				
			||||||
 | 
										db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.ReflectValue, insertID)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user