diff options
| -rw-r--r-- | query.go | 4 | ||||
| -rw-r--r-- | select.go | 16 |
2 files changed, 8 insertions, 12 deletions
@@ -139,7 +139,7 @@ func (s Store[R]) Insert(db Handle, records ...R) (returnedRecords []R, returned // Returns [MissingRecordError] if any of the records does not exist in the database, that is, if for any of the records, the database contains no row with the same primary key values. // // Returns an error if [NewStore] was called without the [TableNameIs] or [PrimaryKeyIs] options, which are both required to generate a query for this method. -func (s Store[R]) Update(db Handle, records ...R) (returnedError error) { +func (s Store[R]) Update(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. @@ -184,7 +184,7 @@ func updateRecord(v reflect.Value, recordIndex int, stmt preparedStatement, argu // Delete executes an SQL DELETE statement for each of the provided records, using their primary keys to locate the respective table rows. // // Returns an error if [NewStore] was called without the [TableNameIs] or [PrimaryKeyIs] options, which are both required to generate a query for this method. -func (s Store[R]) Delete(db Handle, records ...R) (returnedError error) { +func (s Store[R]) Delete(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. // TODO: minimize @@ -73,31 +73,27 @@ func (s Store[R]) SelectWhere(db Handle, partialQuery string, args ...any) ([]R, return result, newIOError(err, "Rows.Err", rows.Err()) } -func startSelectQuery(db Handle, plan plan, query string, args ...any) (returnedRows *sql.Rows, indexes [][]int, returnedError error) { +func startSelectQuery(db Handle, plan plan, query string, args ...any) (*sql.Rows, [][]int, error) { rows, err := db.Query(query, args...) if err != nil { return nil, nil, fmt.Errorf("during Query(): %w", err) } - defer func() { - if returnedError != nil { - // NOTE: Not `returnedRows.Close()`! We may have `rows != nil && returnedRows == nil`. - returnedError = newIOError(returnedError, "Rows.Close", rows.Close()) - } - }() columnNames, err := rows.Columns() if err != nil { - return nil, nil, fmt.Errorf("during rows.Columns(): %w", err) + err = fmt.Errorf("during rows.Columns(): %w", err) + return nil, nil, newIOError(err, "Rows.Close", rows.Close()) } - indexes = make([][]int, len(columnNames)) + indexes := make([][]int, len(columnNames)) for idx, columnName := range columnNames { var ok bool indexes[idx], ok = plan.IndexByColumnName[columnName] if !ok { - return nil, nil, fmt.Errorf( + err := fmt.Errorf( "result has column %q in position %d, but no field in type %s has `db:%[1]q`", columnName, idx, plan.TypeName, ) + return nil, nil, newIOError(err, "Rows.Close", rows.Close()) } } |
