简体   繁体   English

将 PostgreSQL 字段(ARRAY 类型)扫描到 Go 结构的切片中

[英]Scan a PostgreSQL field (of ARRAY type) into a slice of Go structs

Let's say I have:假设我有:

type User struct {
    ID int64 `json:"id"
    Posts []Post `json:"posts"
}

type Post struct {
    ID int64 `json:"id"
    Text string `json:"text"
}

The SQL query: SQL 查询:

WITH temp AS (SELECT u.id AS user_id, p.id AS post_id, p.text AS post_text FROM users u JOIN posts p ON u.id=p.user_id)

SELECT user_id, ARRAY_AGG(ARRAY[post_id::text, post_text]) 
FROM temp
GROUP BY user_id
)

What I want is to scan rows from the query above into a slice of User objects:我想要的是从上面的查询中扫描行到用户对象的切片中:

 import (
    "context"
    "fmt"
    "github.com/jackc/pgx/v4/pgxpool"
    "github.com/lib/pq"
 )

 var out []User

    rows, _ := client.Query(context.Background(), query) // No error handling for brevity

    for rows.Next() {

        var u User

        if err := rows.Scan(&u.ID, pq.Array(&u.Posts)); err != nil {
            return
        }

        out = append(out, u)

    }

Pretty much expectedly, the code above fails with:几乎可以预料,上面的代码失败了:

pq: cannot convert ARRAY[4][2] to StringArray

This makes sense, but is there a way to read the SQL output into my slice of users?这是有道理的,但是有没有办法将 SQL output 读入我的用户群?

Scanning of multi-dimensional arrays of arbitrary types, like structs, is not supported by lib/pq . lib/pq不支持扫描任意类型的多维 arrays,如结构。 If you want to scan such an array you'll have to parse and decode it yourself in a custom sql.Scanner implementation.如果您想扫描这样的数组,您必须在自定义sql.Scanner实现中自己解析和解码它。

For example:例如:

type PostList []Post

func (ls *PostList) Scan(src any) error {
    var data []byte
    switch v := src.(type) {
    case string:
        data = []byte(v)
    case []byte:
        data = v
    }

    // The data var holds the multi-dimensional array value,
    // something like: {{"1","foo"}, {"2","bar"}, ...}
    // The above example is easy to parse but too simplistic,
    // the array is likely to be more complex and therefore
    // harder to parse, but not at all impossible if that's
    // what you want.

    return nil
}

If you want to learn more about the PostgreSQL array representation syntax, see:如果您想了解有关 PostgreSQL 数组表示语法的更多信息,请参阅:


An approach that does not require you to implement a parser for PostgreSQL arrays would be to build and pass JSON objects, instead of PostgreSQL arrays, to array_agg . An approach that does not require you to implement a parser for PostgreSQL arrays would be to build and pass JSON objects, instead of PostgreSQL arrays, to array_agg . The result of that would be a one-dimensional array with jsonb as the element type.其结果将是一个以jsonb作为元素类型的一维数组。

SELECT user_id, array_agg(jsonb_build_object('id', post_id, 'text', post_text)) 
FROM temp
GROUP BY user_id

Then the implementation of the custom sql.Scanner just needs to delegate to lib/pq.GenericArray and another, element-specific sql.Scanner , would delegate to encoding/json .然后自定义sql.Scanner的实现只需要委托给lib/pq.GenericArray和另一个特定于元素的sql.Scanner ,将委托给encoding/json

type PostList []Post

func (ls *PostList) Scan(src any) error {
    return pq.GenericArray{ls}.Scan(src)
}

func (p *Post) Scan(src any) error {
    var data []byte
    switch v := src.(type) {
    case string:
        data = []byte(v)
    case []byte:
        data = v
    }
    return json.Unmarshal(data, p)
}

type User struct {
    ID    int64    `json:"id"`
    Posts PostList `json:"posts"`
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM