Conversation
|
Can you give an example of what you're trying to scan into? |
|
What I'm trying to do is scan an array of arbitrary types. I'm representing it in the DB as |
|
When array support was added in #466, the The unexported type Bytes []byte
func (b *Bytes) Scan(src interface{}) error {
*b = append(*b, src.([]byte)...)
return nil
}
var result []Bytes
err := db.QueryRow(`SELECT ARRAY['{}', '"a"', '9']::jsonb[]`).Scan(pq.Array(&result))In your case, something like type JSONB json.RawMessage
func (j *JSONB) Scan(src interface{}) error {
*j = append(*j, src.([]byte)...)
return nil
}
var result []JSONB
err := db.QueryRow(`SELECT ARRAY['{}', '"a"', '9']::jsonb[]`).Scan(pq.Array(&result))Keep in mind that all PostgreSQL arrays can contain nulls... You might be happiest with the built-in types: var result []sql.NullString
err := db.QueryRow(`SELECT ARRAY['{}', '"a"', '9', NULL]::jsonb[]`).Scan(pq.Array(&result)) |
|
I would rather not export this as it means it can never be changed. PostgreSQL arrays have some rather odd bits where you can do things like And using type parameters to make this sort of thing a bit easier is probably a better way forward regardless. |
Fixes #602
This function is needed for writing custom Scan functions to parse a
JSONB[]column.