Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres: add column name to the result of query function. #3201

Closed
wants to merge 8 commits into from
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 @@
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]

Check failure on line 200 in bindings/postgres/postgres.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

unnecessary conversion (unconvert)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, this is a breaking change. Currently, users expect to receive a slice, but with this, they will receive a map. This will break all existing users.

Perhaps this should be a configurable option?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Infact, the query function return a json, not a map or a slice: "result, err = json.Marshal(rs)", and the json should include column name,like the query function in mysql.go: "result, err := m.jsonify(rows)"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my point is: that is a breaking change. If users expect the JSON to contain an array, and you're now returning a dictionary, that will break applications.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your opinion, and we expect the JSON contain a dictionary,how to make a configurable option? Can you help me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to do this via request metadata which a user would need to specify to enable your new functionality, or introduce a new operation code. You can look at the code of other bindings to see how this can be done.

}

rs = append(rs, r) //nolint:asasalint
}

result, err = json.Marshal(rs)
Expand Down
Loading