Bussiere
Bussiere

Reputation: 1144

Migrating problem using psql array for pq to pgx

Here is my old code:

cardinalPoints := []string{"75101", "75110", "75112", "75115", "92062"}
query = `SELECT * FROM  "DataCom_travel" WHERE ((com1 = $1 AND com2 IN ($2)) OR (com1 IN ($2) AND com2 = $1));`
err := db.Select(&dataComTravel, query, "92050", pq.Array(cardinalPoints))

How to do it with pgx?

pgx.Array seems not working.

Upvotes: 2

Views: 4415

Answers (1)

Ezequiel Muns
Ezequiel Muns

Reputation: 7742

pq.Array will work with pgx as well. This is because all it does is provide an sql.Scanner/driver.Valuer implementation wrapping regular go slices. These are generic interfaces.

If you didn’t want to import both pgx and pq, you could copy the pq.Array and related code over to your project and create your own version. Just make sure the licensing of pq allows for that.

EDIT: Also, just read this, which says that pgx supports string slice mapping directly. So using plain cardinalPoints should work.

Upvotes: 6

Related Questions