2014-01-26 08:41:37 +04:00
|
|
|
package gorm
|
|
|
|
|
2014-01-26 15:34:06 +04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-01-26 08:41:37 +04:00
|
|
|
func BeforeCreate(scope *Scope) {
|
2015-02-24 11:28:15 +03:00
|
|
|
scope.CallMethodWithErrorCheck("BeforeSave")
|
|
|
|
scope.CallMethodWithErrorCheck("BeforeCreate")
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
|
2014-01-27 18:36:08 +04:00
|
|
|
func UpdateTimeStampWhenCreate(scope *Scope) {
|
2014-01-26 08:41:37 +04:00
|
|
|
if !scope.HasError() {
|
2014-08-23 12:00:04 +04:00
|
|
|
now := NowFunc()
|
2014-05-12 13:52:47 +04:00
|
|
|
scope.SetColumn("CreatedAt", now)
|
|
|
|
scope.SetColumn("UpdatedAt", now)
|
2014-01-27 07:06:13 +04:00
|
|
|
}
|
|
|
|
}
|
2014-01-26 17:23:53 +04:00
|
|
|
|
2014-01-27 07:06:13 +04:00
|
|
|
func Create(scope *Scope) {
|
2016-01-13 09:58:30 +03:00
|
|
|
defer scope.trace(NowFunc())
|
2014-01-27 07:06:13 +04:00
|
|
|
|
|
|
|
if !scope.HasError() {
|
2014-01-26 15:34:06 +04:00
|
|
|
// set create sql
|
|
|
|
var sqls, columns []string
|
2015-03-12 12:47:31 +03:00
|
|
|
fields := scope.Fields()
|
|
|
|
for _, field := range fields {
|
2015-03-12 13:30:59 +03:00
|
|
|
if scope.changeableField(field) {
|
2015-03-12 12:47:31 +03:00
|
|
|
if field.IsNormal {
|
|
|
|
if !field.IsPrimaryKey || (field.IsPrimaryKey && !field.IsBlank) {
|
|
|
|
if !field.IsBlank || !field.HasDefaultValue {
|
|
|
|
columns = append(columns, scope.Quote(field.DBName))
|
|
|
|
sqls = append(sqls, scope.AddToVars(field.Field.Interface()))
|
2015-09-14 18:41:14 +03:00
|
|
|
} else if field.HasDefaultValue {
|
2015-12-04 13:41:28 +03:00
|
|
|
var hasDefaultValueColumns []string
|
|
|
|
if oldHasDefaultValueColumns, ok := scope.InstanceGet("gorm:force_reload_after_create_attrs"); ok {
|
|
|
|
hasDefaultValueColumns = oldHasDefaultValueColumns.([]string)
|
|
|
|
}
|
|
|
|
hasDefaultValueColumns = append(hasDefaultValueColumns, field.DBName)
|
|
|
|
scope.InstanceSet("gorm:force_reload_after_create_attrs", hasDefaultValueColumns)
|
2015-03-12 12:47:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
2015-07-30 09:26:48 +03:00
|
|
|
for _, dbName := range relationship.ForeignDBNames {
|
|
|
|
if relationField := fields[dbName]; !scope.changeableField(relationField) {
|
|
|
|
columns = append(columns, scope.Quote(relationField.DBName))
|
|
|
|
sqls = append(sqls, scope.AddToVars(relationField.Field.Interface()))
|
|
|
|
}
|
2015-03-12 12:47:31 +03:00
|
|
|
}
|
2014-11-15 20:32:35 +03:00
|
|
|
}
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 14:03:42 +03:00
|
|
|
returningKey := "*"
|
2015-03-11 06:28:30 +03:00
|
|
|
primaryField := scope.PrimaryField()
|
2015-02-17 15:19:47 +03:00
|
|
|
if primaryField != nil {
|
|
|
|
returningKey = scope.Quote(primaryField.DBName)
|
2014-12-08 13:33:30 +03:00
|
|
|
}
|
|
|
|
|
2014-04-29 13:42:10 +04:00
|
|
|
if len(columns) == 0 {
|
|
|
|
scope.Raw(fmt.Sprintf("INSERT INTO %v DEFAULT VALUES %v",
|
2014-06-03 13:15:05 +04:00
|
|
|
scope.QuotedTableName(),
|
2015-09-11 01:36:17 +03:00
|
|
|
scope.Dialect().ReturningStr(scope.QuotedTableName(), returningKey),
|
2014-04-29 13:42:10 +04:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
scope.Raw(fmt.Sprintf(
|
|
|
|
"INSERT INTO %v (%v) VALUES (%v) %v",
|
2014-06-03 13:15:05 +04:00
|
|
|
scope.QuotedTableName(),
|
2014-04-29 13:42:10 +04:00
|
|
|
strings.Join(columns, ","),
|
|
|
|
strings.Join(sqls, ","),
|
2015-09-11 01:36:17 +03:00
|
|
|
scope.Dialect().ReturningStr(scope.QuotedTableName(), returningKey),
|
2014-04-29 13:42:10 +04:00
|
|
|
))
|
|
|
|
}
|
2014-01-26 15:34:06 +04:00
|
|
|
|
|
|
|
// execute create sql
|
2014-01-26 08:41:37 +04:00
|
|
|
if scope.Dialect().SupportLastInsertId() {
|
2015-02-26 07:35:33 +03:00
|
|
|
if result, err := scope.SqlDB().Exec(scope.Sql, scope.SqlVars...); scope.Err(err) == nil {
|
2015-02-20 17:06:49 +03:00
|
|
|
id, err := result.LastInsertId()
|
2014-06-05 13:58:14 +04:00
|
|
|
if scope.Err(err) == nil {
|
2014-12-13 05:46:16 +03:00
|
|
|
scope.db.RowsAffected, _ = result.RowsAffected()
|
2015-06-01 06:04:11 +03:00
|
|
|
if primaryField != nil && primaryField.IsBlank {
|
2015-02-23 04:40:39 +03:00
|
|
|
scope.Err(scope.SetColumn(primaryField, id))
|
2015-02-20 17:06:49 +03:00
|
|
|
}
|
2014-06-05 13:58:14 +04:00
|
|
|
}
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
} else {
|
2015-02-17 15:19:47 +03:00
|
|
|
if primaryField == nil {
|
2015-06-04 14:47:25 +03:00
|
|
|
if results, err := scope.SqlDB().Exec(scope.Sql, scope.SqlVars...); err == nil {
|
2014-12-13 05:46:16 +03:00
|
|
|
scope.db.RowsAffected, _ = results.RowsAffected()
|
2015-06-04 15:23:57 +03:00
|
|
|
} else {
|
|
|
|
scope.Err(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := scope.Err(scope.SqlDB().QueryRow(scope.Sql, scope.SqlVars...).Scan(primaryField.Field.Addr().Interface())); err == nil {
|
|
|
|
scope.db.RowsAffected = 1
|
|
|
|
} else {
|
|
|
|
scope.Err(err)
|
2014-12-08 21:00:02 +03:00
|
|
|
}
|
2014-06-09 04:17:20 +04:00
|
|
|
}
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 18:41:14 +03:00
|
|
|
func ForceReloadAfterCreate(scope *Scope) {
|
2015-12-04 13:41:28 +03:00
|
|
|
if columns, ok := scope.InstanceGet("gorm:force_reload_after_create_attrs"); ok {
|
|
|
|
scope.DB().New().Select(columns.([]string)).First(scope.Value)
|
2015-09-14 18:41:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-26 08:41:37 +04:00
|
|
|
func AfterCreate(scope *Scope) {
|
2015-02-24 11:28:15 +03:00
|
|
|
scope.CallMethodWithErrorCheck("AfterCreate")
|
|
|
|
scope.CallMethodWithErrorCheck("AfterSave")
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2014-01-29 06:28:20 +04:00
|
|
|
DefaultCallback.Create().Register("gorm:begin_transaction", BeginTransaction)
|
|
|
|
DefaultCallback.Create().Register("gorm:before_create", BeforeCreate)
|
|
|
|
DefaultCallback.Create().Register("gorm:save_before_associations", SaveBeforeAssociations)
|
|
|
|
DefaultCallback.Create().Register("gorm:update_time_stamp_when_create", UpdateTimeStampWhenCreate)
|
|
|
|
DefaultCallback.Create().Register("gorm:create", Create)
|
2015-09-14 18:41:14 +03:00
|
|
|
DefaultCallback.Create().Register("gorm:force_reload_after_create", ForceReloadAfterCreate)
|
2014-01-29 06:28:20 +04:00
|
|
|
DefaultCallback.Create().Register("gorm:save_after_associations", SaveAfterAssociations)
|
|
|
|
DefaultCallback.Create().Register("gorm:after_create", AfterCreate)
|
|
|
|
DefaultCallback.Create().Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|