diff options
Diffstat (limited to 'query.go')
| -rw-r--r-- | query.go | 23 |
1 files changed, 11 insertions, 12 deletions
@@ -72,13 +72,12 @@ func (s preparedStatement) QueryRow(args ...any) *sql.Row { // // Fields that are declared with the "auto" tag will not be written into the DB, // and instead their value (as auto-generated by the DB on insert) will be placed in the record. -// On success, returns the original set of records, updated thusly. // // Returns an error if [NewStore] was called without the [TableNameIs] option, which is required to generate a query for this method. // // Returns an error if any of the `records` has a non-zero value in any column marked as `db:",auto"`. // Records that already exist in the database should be handled with [Store.Update] instead. -func (s Store[R]) Insert(db Handle, records ...R) ([]R, error) { +func (s Store[R]) Insert(db Handle, records ...*R) error { // NOTE: This function body should be as short as possible to reduce the binary size after monomorphization. // Any expression that does not depend on type R should be factored out into a reusable function. if s.dialect.UsesLastInsertID() || len(s.plan.Insert.ScanIndexes) == 0 { @@ -88,13 +87,13 @@ func (s Store[R]) Insert(db Handle, records ...R) ([]R, error) { } } -func (s Store[R]) insertUsingLastInsertID(db Handle, records []R) (returnedRecords []R, returnedError error) { +func (s Store[R]) insertUsingLastInsertID(db Handle, records []*R) (returnedError error) { // NOTE: This function body should be as short as possible to reduce the binary size after monomorphization. // Any expression that does not depend on type R should be factored out into a reusable function. stmt, err := prepare(db, s.plan.Insert.Query, "Insert", len(records)) if err != nil { - return nil, err + return err } defer func() { returnedError = newIOError(returnedError, "Stmt.Close", stmt.Close()) @@ -109,14 +108,14 @@ func (s Store[R]) insertUsingLastInsertID(db Handle, records []R) (returnedRecor scanIndex = s.plan.Insert.ScanIndexes[0] } for idx := range records { - v := reflect.ValueOf(&records[idx]).Elem() + v := reflect.ValueOf(records[idx]).Elem() err := insertRecordUsingLastInsertID(v, idx, stmt, argumentIndexes, argumentSlots, scanIndex, s.plan) if err != nil { - return nil, newIOError(err, "Stmt.Close", stmt.Close()) + return newIOError(err, "Stmt.Close", stmt.Close()) } } - return records, newIOError(nil, "Stmt.Close", stmt.Close()) + return newIOError(nil, "Stmt.Close", stmt.Close()) } func insertRecordUsingLastInsertID(v reflect.Value, recordIndex int, stmt preparedStatement, argumentIndexes [][]int, argumentSlots []any, scanIndex []int, plan plan) error { @@ -152,13 +151,13 @@ func insertRecordUsingLastInsertID(v reflect.Value, recordIndex int, stmt prepar return nil } -func (s Store[R]) insertUsingReturningClause(db Handle, records []R) (returnedRecords []R, returnedError error) { +func (s Store[R]) insertUsingReturningClause(db Handle, records []*R) (returnedError error) { // NOTE: This function body should be as short as possible to reduce the binary size after monomorphization. // Any expression that does not depend on type R should be factored out into a reusable function. stmt, err := prepare(db, s.plan.Insert.Query, "Insert", len(records)) if err != nil { - return nil, err + return err } var ( @@ -169,14 +168,14 @@ func (s Store[R]) insertUsingReturningClause(db Handle, records []R) (returnedRe ) for idx := range records { - v := reflect.ValueOf(&records[idx]).Elem() + v := reflect.ValueOf(records[idx]).Elem() err := insertRecordUsingReturningClause(v, idx, stmt, argumentIndexes, argumentSlots, scanIndexes, scanSlots) if err != nil { - return nil, newIOError(err, "Stmt.Close", stmt.Close()) + return newIOError(err, "Stmt.Close", stmt.Close()) } } - return records, newIOError(nil, "Stmt.Close", stmt.Close()) + return newIOError(nil, "Stmt.Close", stmt.Close()) } func insertRecordUsingReturningClause(v reflect.Value, recordIndex int, stmt preparedStatement, argumentIndexes [][]int, argumentSlots []any, scanIndexes [][]int, scanSlots []any) error { |
