gorm/callback_create.go

107 lines
3.0 KiB
Go
Raw Normal View History

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) {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeCreate")
}
2014-01-27 18:36:08 +04:00
func UpdateTimeStampWhenCreate(scope *Scope) {
2014-01-26 08:41:37 +04:00
if !scope.HasError() {
now := NowFunc()
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) {
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
for _, field := range scope.Fields() {
2014-08-30 18:39:28 +04:00
if field.IsNormal && (!field.IsPrimaryKey || !scope.PrimaryKeyZero()) {
2014-11-17 12:38:32 +03:00
if field.DefaultValue != nil && field.IsBlank {
continue
}
2014-01-28 12:22:41 +04:00
columns = append(columns, scope.Quote(field.DBName))
2014-09-02 15:03:01 +04:00
sqls = append(sqls, scope.AddToVars(field.Field.Interface()))
2014-01-26 15:34:06 +04:00
}
}
returningKey := "*"
if scope.PrimaryKey() != "" {
returningKey = scope.PrimaryKey()
}
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(),
scope.Dialect().ReturningStr(scope.TableName(), 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, ","),
scope.Dialect().ReturningStr(scope.TableName(), 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
var id interface{}
if scope.Dialect().SupportLastInsertId() {
2014-06-05 13:58:14 +04:00
if result, err := scope.DB().Exec(scope.Sql, scope.SqlVars...); scope.Err(err) == nil {
id, err = result.LastInsertId()
if scope.Err(err) == nil {
if count, err := result.RowsAffected(); err == nil {
scope.db.RowsAffected = count
}
}
2014-01-26 08:41:37 +04:00
}
} else {
if scope.PrimaryKey() == "" {
if rows, err := scope.DB().Query(scope.Sql, scope.SqlVars...); err != nil {
//extract column name to get fields lenght
if names, columnsErr := rows.Columns(); columnsErr != nil {
ids := make([]interface{}, len(names))
if scope.Err(rows.Scan(ids...)) == nil {
scope.db.RowsAffected = int64(len(names))
}
}
}
} else {
if scope.Err(scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&id)) == nil {
scope.db.RowsAffected = 1
}
2014-06-09 04:17:20 +04:00
}
2014-01-26 08:41:37 +04:00
}
if scope.PrimaryKey() != "" && !scope.HasError() && scope.PrimaryKeyZero() {
2014-01-26 15:34:06 +04:00
scope.SetColumn(scope.PrimaryKey(), id)
}
2014-01-26 08:41:37 +04:00
}
}
func AfterCreate(scope *Scope) {
scope.CallMethod("AfterCreate")
scope.CallMethod("AfterSave")
}
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)
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
}