Skip to content

Commit

Permalink
Update postgres.go
Browse files Browse the repository at this point in the history
add column name to the result of function query.
  • Loading branch information
Jack397419 committed Oct 27, 2023
1 parent 896679e commit 4aa1955
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion bindings/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4aa1955

Please sign in to comment.