<commit_before>package gorm

import (
	"fmt"
	"strings"
	"time"
)

func BeforeCreate(scope *Scope) {
	scope.CallMethod("BeforeSave")
	scope.CallMethod("BeforeCreate")
}

func UpdateTimeStampWhenCreate(scope *Scope) {
	if !scope.HasError() {
		now := time.Now()
		scope.SetColumn("CreatedAt", now)
		scope.SetColumn("UpdatedAt", now)
	}
}

func Create(scope *Scope) {
	defer scope.Trace(time.Now())

	if !scope.HasError() {
		// set create sql
		var sqls, columns []string

		for _, field := range scope.Fields() {
			if len(field.SqlTag) > 0 && !field.IsIgnored && (field.DBName != scope.PrimaryKey() || !scope.PrimaryKeyZero()) {
				columns = append(columns, scope.Quote(field.DBName))
				sqls = append(sqls, scope.AddToVars(field.Value))
			}
		}

		if len(columns) == 0 {
			scope.Raw(fmt.Sprintf("INSERT INTO %v DEFAULT VALUES %v",
				scope.QuotedTableName(),
				scope.Dialect().ReturningStr(scope.PrimaryKey()),
			))
		} else {
			scope.Raw(fmt.Sprintf(
				"INSERT INTO %v (%v) VALUES (%v) %v",
				scope.QuotedTableName(),
				strings.Join(columns, ","),
				strings.Join(sqls, ","),
				scope.Dialect().ReturningStr(scope.PrimaryKey()),
			))
		}

		// execute create sql
		var id interface{}
		if scope.Dialect().SupportLastInsertId() {
			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
					}
				}
			}
		} else {
			if scope.Err(scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&id)) == nil {
				scope.db.RowsAffected = 1
			}
		}

		if !scope.HasError() {
			scope.SetColumn(scope.PrimaryKey(), id)
		}
	}
}

func AfterCreate(scope *Scope) {
	scope.CallMethod("AfterCreate")
	scope.CallMethod("AfterSave")
}

func init() {
	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:commit_or_rollback_transaction", CommitOrRollbackTransaction)
	DefaultCallback.Create().Register("gorm:after_create", AfterCreate)
}<commit_msg>Social: add doc for recently added change<commit_after>package gorm

import (
	"fmt"
	"strings"
	"time"
)

func BeforeCreate(scope *Scope) {
	scope.CallMethod("BeforeSave")
	scope.CallMethod("BeforeCreate")
}

func UpdateTimeStampWhenCreate(scope *Scope) {
	if !scope.HasError() {
		now := time.Now()
		scope.SetColumn("CreatedAt", now)
		scope.SetColumn("UpdatedAt", now)
	}
}

func Create(scope *Scope) {
	defer scope.Trace(time.Now())

	if !scope.HasError() {
		// set create sql
		var sqls, columns []string

		for _, field := range scope.Fields() {
			if len(field.SqlTag) > 0 && !field.IsIgnored && (field.DBName != scope.PrimaryKey() || !scope.PrimaryKeyZero()) {
				columns = append(columns, scope.Quote(field.DBName))
				sqls = append(sqls, scope.AddToVars(field.Value))
			}
		}

		if len(columns) == 0 {
			scope.Raw(fmt.Sprintf("INSERT INTO %v DEFAULT VALUES %v",
				scope.QuotedTableName(),
				scope.Dialect().ReturningStr(scope.PrimaryKey()),
			))
		} else {
			scope.Raw(fmt.Sprintf(
				"INSERT INTO %v (%v) VALUES (%v) %v",
				scope.QuotedTableName(),
				strings.Join(columns, ","),
				strings.Join(sqls, ","),
				scope.Dialect().ReturningStr(scope.PrimaryKey()),
			))
		}

		// execute create sql
		var id interface{}
		if scope.Dialect().SupportLastInsertId() {
			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
					}
				}
			}
		} else {
			if scope.Err(scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&id)) == nil {
				scope.db.RowsAffected = 1
			}
		}

		if !scope.HasError() {
			scope.SetColumn(scope.PrimaryKey(), id)
		}
	}
}

func AfterCreate(scope *Scope) {
	scope.CallMethod("AfterCreate")
	scope.CallMethod("AfterSave")
}

func init() {
	// this is different version from gorm itself
	// if you are updating the source itself, please be sure that
	// before_create and before_update functions are first and last
	// functions, we are trying to merge this change to original repo
	// this will stay here up until we make it
	DefaultCallback.Create().Register("gorm:before_create", BeforeCreate)
	DefaultCallback.Create().Register("gorm:begin_transaction", BeginTransaction)
	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:commit_or_rollback_transaction", CommitOrRollbackTransaction)
	DefaultCallback.Create().Register("gorm:after_create", AfterCreate)

}
{'fraction_non_alphanumeric': 0.15420473615513705, 'fraction_numerical': 0.0011187768040275966, 'mean_word_length': 4.473469387755102, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5004794', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1751, 'n_words': 332}