Bob is not working with pgx array types
hiendaovinh opened this issue · 1 comments
hiendaovinh commented
Hi, I was trying to use bob with pgx. When I try to fetch items from QueryContext it gave me this error
pq: unable to parse array; expected '{' at offset 0
I believe this is a problem with scanner. Please take a look.
type BobExecutor interface {
QueryContext(ctx context.Context, query string, args ...any) (scan.Rows, error)
ExecContext(context.Context, string, ...any) (sql.Result, error)
}
type PGXPool interface {
Begin(ctx context.Context) (pgx.Tx, error)
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
}
type BobExecutorPgx struct {
pool PGXPool
}
type rows struct {
pgx.Rows
}
func (r rows) Close() error {
r.Rows.Close()
return nil
}
func (r rows) Columns() ([]string, error) {
fields := r.FieldDescriptions()
cols := make([]string, len(fields))
for i, field := range fields {
cols[i] = field.Name
}
return cols, nil
}
func (v *BobExecutorPgx) QueryContext(ctx context.Context, query string, args ...any) (scan.Rows, error) {
r, err := v.pool.Query(ctx, query, args...)
if err != nil {
return nil, err
}
return rows{r}, nil
}
func (v *BobExecutorPgx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
tag, err := v.pool.Exec(ctx, query, args...)
if err != nil {
return nil, err
}
return driver.RowsAffected(tag.RowsAffected()), err
}
stephenafamo commented
My immediate guess is that this is happening because the array types in pgx do not implement sql.Scanner
.
If using github.com/stephenafamo/scan
directly, it is possible to add the custom pgx types using WithScannableTypes
. However, the code generated by Bob does not account for this.