Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bob is not working with pgx array types #90

Open
hiendaovinh opened this issue Jul 26, 2023 · 3 comments
Open

Bob is not working with pgx array types #90

hiendaovinh opened this issue Jul 26, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@hiendaovinh
Copy link

hiendaovinh commented Jul 26, 2023

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
Copy link
Owner

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.

@stephenafamo stephenafamo added the enhancement New feature or request label Jul 26, 2023
@stephenafamo stephenafamo changed the title Bob is not working with postgresql array Bob is not working with pgx array types Aug 4, 2023
@hiendaovinh
Copy link
Author

I believe this will be fixed soon.

Ref:
golang/go#67546
jackc/pgx#2029
jackc/pgx#1779

@stephenafamo
Copy link
Owner

That would make a lot of things simpler 👍🏾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants