简体   繁体   English

SQLX 扫描到结构数组

[英]SQLX scan into array of structs

I'm trying to implement a sql query that returns data in a way that jQuery Datatables expects while allowing users to customize the query (I'm doing alot of input validate to make sure there isn't any sql injection).The query is supposed to filter all of the edits by the object_name and editor display_name.我正在尝试实现一个 sql 查询,它以 jQuery Datatables 期望的方式返回数据,同时允许用户自定义查询(我正在做大量的输入验证以确保没有任何 sql 注入)。查询是应该按 object_name 和编辑器 display_name 过滤所有编辑。 Then of those edits it is supposed to get the last edit for each object based on it's perm_id (uid).然后在这些编辑中,它应该根据每个对象的 perm_id (uid) 获取最后的编辑。 Then it tries to fit everything into a jQuery Datatables friendly format that supports pagination.然后它会尝试将所有内容放入支持分页的 jQuery 数据表友好格式中。 I think the query itself is working I'm just not sure if sqlx supports the array_agg scanning into a struct.我认为查询本身正在运行 我只是不确定 sqlx 是否支持 array_agg 扫描到结构中。 When I try to scan it into the DataTableResponse struct I get this error.当我尝试将其扫描到 DataTableResponse 结构中时,出现此错误。

unsupported Scan, storing driver.Value type []uint8 into type *[]edits.Edit不支持的扫描,将 driver.Value 类型 []uint8 存储到类型 *[]edits.Edit

type Edit struct {
    Id                 *int            `db:"id" json:"id,omitempty"`
    UserId             *int            `db:"user_id" json:"user_id,omitempty"`
    PermId             *string         `db:"perm_id" json:"perm_id,omitempty"`
    ObjectName         *string        `db:"object_name,omitempty"`
    EditTypeId         *int            `db:"edit_type_id" json:"edit_type_id,omitempty"`
    EditDate           *time.Time      `db:"edit_date" json:"edit_date,omitempty"`

    DisplayName *string `db:"display_name" json:"display_name,omitempty"`
}

type DataTableResponse struct {
    Draw int `db:"draw" json:"draw"`
    RecordsTotal int `db:"records_total" json:"recordsTotal"`
    RecordsFiltered int `db:"records_filtered" json:"recordsFiltered"`
    Data []Edit `db:"data" json:"data"`
}


response := []DataTableResponse{}
err = db.Select(&response,
    fmt.Sprintf(`
        SELECT DISTINCT ON (results.perm_id) %d as draw, array_agg((%s)) as data,COUNT(DISTINCT perm_id) as records_total,COUNT(DISTINCT perm_id) - count(results.*) as records_filtered
        FROM (SELECT edit_metadata.*,user.display_name
                FROM edit_metadata INNER JOIN user 
                ON edit_metadata.user_id = user.id 
                WHERE user.display_name ilike $1 AND edit_metadata.object_name ilike $2 
                ORDER BY edit_metadata.edit_date ASC) 
        as results 
        GROUP BY results.perm_id,results.edit_date
        ORDER BY results.perm_id,%s %s
        LIMIT %s
        OFFSET %s`,draw, requestedFields, sortBy, sortDir,length, start),
    fmt.Sprintf("%%%s%%", c.Query("display_name")),
    fmt.Sprintf("%%%s%%", c.Query("object_name")))
if err != nil {
    log.Fatal(err)
}
c.JSON(http.StatusOK, response)

I have same issue with array of strings.我对字符串数组有同样的问题。 I found that sqlx scans PostgreSQL's arrays as a string in []uint8我发现 sqlx 将 PostgreSQL 的数组扫描为 []uint8 中的字符串

So you need to write your own Scanner/Parser.所以你需要编写自己的扫描器/解析器。

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

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