Refactor callbck_create

This commit is contained in:
Jinzhu 2014-12-13 10:46:16 +08:00
parent e30cf3bbd1
commit 5823670269
1 changed files with 3 additions and 11 deletions

View File

@ -60,21 +60,13 @@ func Create(scope *Scope) {
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
}
scope.db.RowsAffected, _ = result.RowsAffected()
}
}
} 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 = 1
}
}
if results, err := scope.DB().Exec(scope.Sql, scope.SqlVars...); err != nil {
scope.db.RowsAffected, _ = results.RowsAffected()
}
} else {
if scope.Err(scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&id)) == nil {