From 4aa1955493f17a9e511a201fd5a19f62a5cda2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=87=95=E9=A3=9E=20=E6=A2=81?= <397419@qq.com> Date: Fri, 27 Oct 2023 14:53:00 +0800 Subject: [PATCH] Update postgres.go add column name to the result of function query. --- bindings/postgres/postgres.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bindings/postgres/postgres.go b/bindings/postgres/postgres.go index 3a0ab98252..0f20c052b3 100644 --- a/bindings/postgres/postgres.go +++ b/bindings/postgres/postgres.go @@ -186,13 +186,21 @@ func (p *Postgres) query(ctx context.Context, sql string, args ...any) (result [ return nil, fmt.Errorf("error executing query: %w", err) } + cols := rows.FieldDescriptions() + rs := make([]any, 0) for rows.Next() { val, rowErr := rows.Values() if rowErr != nil { return nil, fmt.Errorf("error reading result '%v': %w", rows.Err(), rowErr) } - rs = append(rs, val) //nolint:asasalint + + r := map[string]interface{}{} + for i, col := range cols { + r[string(col.Name)] = val[i] + } + + rs = append(rs, r) //nolint:asasalint } result, err = json.Marshal(rs)