dialect/sqlscan: add ScanInt64 to be used by sqlgraph

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/213

Reviewed By: alexsn

Differential Revision: D18763694

fbshipit-source-id: 890b35fcc2a28914b276ce65477788b4ddaeebf9
This commit is contained in:
Ariel Mashraki
2019-12-03 01:43:34 -08:00
committed by Facebook Github Bot
parent a4fac2db3b
commit a5e4a9cf54
4 changed files with 86 additions and 40 deletions

View File

@@ -351,25 +351,14 @@ func insertLastID(ctx context.Context, tx dialect.ExecQuerier, insert *InsertBui
return 0, err
}
defer rows.Close()
if !rows.Next() {
return 0, fmt.Errorf("no rows found for query: %v", query)
}
var id int64
if err := rows.Scan(&id); err != nil {
return 0, err
}
return id, nil
return ScanInt64(rows)
}
// MySQL, SQLite, etc.
var res sql.Result
if err := tx.Exec(ctx, query, args, &res); err != nil {
return 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
return id, nil
return res.LastInsertId()
}
// rollback calls to tx.Rollback and wraps the given error with the rollback error if occurred.