Pgx: Support StringArray?

Created on 11 Jun 2020  路  6Comments  路  Source: jackc/pgx

I know UntypedTextArray and TextArray exist, but they are quite cumbersome to use.

If there is a type StringArray []string, I could directly scan into a string slice like this:

var strs []string
Scan((*pgtype.StringArray)(&strs))

With UntypedTextArray, I have to define a new variable and assign .Elements to strs. With TextArray, it will be even more verbose.

Most helpful comment

Thanks for the heads up, I'm using the stdlib though. Currently I'm still using pq.StringArray, Wish I could completely transition to pgx by eliminating the final lib/pq piece that I rely on.

All 6 comments

In the native pgx interface you can already scan directly to a []string.

Thanks for the heads up, I'm using the stdlib though. Currently I'm still using pq.StringArray, Wish I could completely transition to pgx by eliminating the final lib/pq piece that I rely on.

I am using stdlib as well, because sqlx works with sql.DB.
When doing sqlx.Select into a struct, selecting postgres array into []string fails with the following error: unsupported Scan, storing driver.Value type string into type *[]string.
pgtype.TextArray works, though it is not really convenient.

Please add this somewhere.... maybe pgtype helpers?

import (
    . "github.com/jackc/pgtype"
)

type Strings []string

func (strings Strings) Array() TextArray {
    length := int32(len(strings))

    txtArray := TextArray{
        Elements:   []Text{},
        Dimensions: []ArrayDimension{{Length: length, LowerBound: 1}},
        Status:     Present,
    }

    for i := range strings {
        txtArray.Elements = append(txtArray.Elements, Text{
            String: strings[i],
            Status: Present,
        })
    }
    return txtArray
}

I am having the same issue just selecting int []int32 and getting unsupported Scan, storing driver.Value type string into type *[]int32. The OID being sent from Postgres seems to be 1007, which is Int4ArrayOID, the value is {}. Not sure what I am doing wrong, it seems scanning into a Go slice simply does not work?

Oh, I am using stdlib, does that make any difference and why?

Was this page helpful?
0 / 5 - 0 ratings