diff options
Diffstat (limited to 'select.go')
| -rw-r--r-- | select.go | 18 |
1 files changed, 12 insertions, 6 deletions
@@ -11,6 +11,7 @@ import ( "reflect" . "go.xyrillian.de/gg/option" + "go.xyrillian.de/oblast/handle" ) // Select executes the provided SQL query and fills an instance of the record type R for each row in the result set, @@ -103,8 +104,8 @@ func (s Store[R]) SelectWhere(ctx context.Context, db Handle, partialQuery strin return result, newIOError(err, "Rows.Err", rows.Err()) } -func startSelectQuery(ctx context.Context, db Handle, plan plan, query string, args ...any) (*sql.Rows, [][]int, error) { - rows, err := db.QueryContext(ctx, query, args...) +func startSelectQuery(ctx context.Context, db Handle, plan plan, query string, args ...any) (handle.Rows, [][]int, error) { + rows, err := db.Query(ctx, query, args) if err != nil { return nil, nil, fmt.Errorf("during Query(): %w", err) } @@ -130,19 +131,19 @@ func startSelectQuery(ctx context.Context, db Handle, plan plan, query string, a return rows, indexes, nil } -func startSelectWhereQuery(ctx context.Context, db Handle, plan plan, partialQuery string, args ...any) (rows *sql.Rows, indexes [][]int, err error) { +func startSelectWhereQuery(ctx context.Context, db Handle, plan plan, partialQuery string, args ...any) (rows handle.Rows, indexes [][]int, err error) { if plan.Select.Query == "" { return nil, nil, errors.New("cannot execute SelectWhere() because query could not be autogenerated") } query := plan.Select.Query + partialQuery - rows, err = db.QueryContext(ctx, query, args...) + rows, err = db.Query(ctx, query, args) if err != nil { err = fmt.Errorf("during Query(): %w", err) } return rows, plan.Select.ScanIndexes, err } -func collectRow(rows *sql.Rows, plan plan, v reflect.Value, slots []any, indexes [][]int) error { +func collectRow(rows handle.Rows, plan plan, v reflect.Value, slots []any, indexes [][]int) error { for _, index := range plan.IndexesOfTransparentPointerStructs { f := v.FieldByIndex(index) f.Set(reflect.New(f.Type().Elem())) @@ -239,7 +240,12 @@ func selectOne(ctx context.Context, db Handle, plan plan, v reflect.Value, query for idx, index := range plan.Select.ScanIndexes { slots[idx] = v.FieldByIndex(index).Addr().Interface() } - return db.QueryRowContext(ctx, query, args...).Scan(slots...) + stmt, err := db.Prepare(ctx, query, false) + if err != nil { + return err + } + err = stmt.QueryRow(ctx, args, slots) + return newIOError(err, "Stmt.Close", stmt.Close()) } func noRowsToNone[R any](record R, err error) (Option[R], error) { |
