您已经知道array_agg 接受一个单个 参数并返回参数类型的数组。因此,如果您希望一行的所有列都包含在数组的元素中,您可以直接传入行引用,例如:
SELECT array_agg(b) FROM b
但是,如果您只想在数组元素中包含特定列,则可以使用 ROW 构造函数,例如:
SELECT array_agg(ROW(b.stuff, b.other)) FROM b
Go 的标准库为仅扫描标量值提供了开箱即用的支持。要扫描更复杂的值,例如任意对象和数组,必须寻找第 3 方解决方案,或者实现他们自己的 sql.Scanner。
为了能够实现您自己的sql.Scanner 并正确解析postgres 行数组,您首先需要知道postgres 使用什么格式来输出值,您可以通过使用psql 和一些直接查询来找到它:
-- simple values
SELECT ARRAY[ROW(123,'foo'),ROW(456,'bar')];
-- output: {"(123,foo)","(456,bar)"}
-- not so simple values
SELECT ARRAY[ROW(1,'a b'),ROW(2,'a,b'),ROW(3,'a",b'),ROW(4,'(a,b)'),ROW(5,'"','""')];
-- output: {"(1,\"a b\")","(2,\"a,b\")","(3,\"a\"\",b\")","(4,\"(a,b)\")","(5,\"\"\"\",\"\"\"\"\"\")"}
正如您所看到的,这可能会变得很复杂,但它是可解析的,语法看起来像这样:
{"(column_value[, ...])"[, ...]}
其中column_value 是不带引号的值,或者是带有转义双引号的带引号的值,并且这种带引号的值本身可以包含转义的双引号,但只能包含两个,即单个转义的双引号引用不会出现在column_value 中。所以解析器的粗略和不完整的实现可能看起来像这样:
注意:可能还有其他我不知道的语法规则,在解析过程中需要考虑。除此之外,下面的代码不能正确处理 NULL。
func parseRowArray(a []byte) (out [][]string) {
a = a[1 : len(a)-1] // drop surrounding curlies
for i := 0; i < len(a); i++ {
if a[i] == '"' { // start of row element
row := []string{}
i += 2 // skip over current '"' and the following '('
for j := i; j < len(a); j++ {
if a[j] == '\\' && a[j+1] == '"' { // start of quoted column value
var col string // column value
j += 2 // skip over current '\' and following '"'
for k := j; k < len(a); k++ {
if a[k] == '\\' && a[k+1] == '"' { // end of quoted column, maybe
if a[k+2] == '\\' && a[k+3] == '"' { // nope, just escaped quote
col += string(a[j:k]) + `"`
k += 3 // skip over `\"\` (the k++ in the for statement will skip over the `"`)
j = k + 1 // skip over `\"\"`
continue // go to k loop
} else { // yes, end of quoted column
col += string(a[j:k])
row = append(row, col)
j = k + 2 // skip over `\"`
break // go back to j loop
}
}
}
if a[j] == ')' { // row end
out = append(out, row)
i = j + 1 // advance i to j's position and skip the potential ','
break // go to back i loop
}
} else { // assume non quoted column value
for k := j; k < len(a); k++ {
if a[k] == ',' || a[k] == ')' { // column value end
col := string(a[j:k])
row = append(row, col)
j = k // advance j to k's position
break // go back to j loop
}
}
if a[j] == ')' { // row end
out = append(out, row)
i = j + 1 // advance i to j's position and skip the potential ','
break // go to back i loop
}
}
}
}
}
return out
}
试试playground。
通过类似的方式,您可以为您的 Go 条形图实现 sql.Scanner。
type BarList []*Bar
func (ls *BarList) Scan(src interface{}) error {
switch data := src.(type) {
case []byte:
a := praseRowArray(data)
res := make(BarList, len(a))
for i := 0; i < len(a); i++ {
bar := new(Bar)
// Here i'm assuming the parser produced a slice of at least two
// strings, if there are cases where this may not be the true you
// should add proper length checks to avoid unnecessary panics.
bar.Stuff = a[i][0]
bar.Other = a[i][1]
res[i] = bar
}
*ls = res
}
return nil
}
现在,如果您将Foo 类型中的Bars 字段的类型从[]*Bar 更改为BarList,您将能够直接将该字段的指针传递给(*sql.Row|*sql.Rows).Scan 调用:
rows.Scan(&f.Bars)
如果您不想更改字段的类型,您仍然可以通过在将指针传递给Scan 方法时转换指针来使其工作:
rows.Scan((*BarList)(&f.Bars))
JSON
Henry Woody 建议的 json 解决方案的 sql.Scanner 实现如下所示:
type BarList []*Bar
func (ls *BarList) Scan(src interface{}) error {
if b, ok := src.([]byte); ok {
return json.Unmarshal(b, ls)
}
return nil
}