Skip to content

db.pg: Support returning Result type with column names#25737

Merged
spytheman merged 4 commits into
vlang:masterfrom
omerrob:pg-result-and-mapper
Nov 19, 2025
Merged

db.pg: Support returning Result type with column names#25737
spytheman merged 4 commits into
vlang:masterfrom
omerrob:pg-result-and-mapper

Conversation

@omerrob

@omerrob omerrob commented Nov 14, 2025

Copy link
Copy Markdown
Contributor

Currently, db.pg queries return []Row, which is just

pub struct Row {
pub mut:
	vals []?string
}

Then the developer needs to map the values based on ordinal places.

I understand that this was built with the ORM in mind, and the ORM maintains a mapping of the ordinal places with the struct members.

Sometimes we don't want to use the ORM or can't because we use a lot of joins and CTEs (WITH clauses). In that case, it is convenient to have the column names in order to map the query.

This PR adds the Result struct:

pub struct Result {
pub:
	cols map[string]int
	rows []Row
}

and adds matching fn *_result() functions to the currently implemented functions.

It also adds a quality-of-life mapping method to Result:

fn (res Result) as_structs[T](mapper fn (Result, Row) !T) ![]T

that accepts a mapper function to return a typed struct.

@spytheman

spytheman commented Nov 14, 2025

Copy link
Copy Markdown
Contributor

https://github.com/vlang/v/actions/runs/19352895759/job/55368240782?pr=25737 shows that the PR needs the new pub APIs to be documented.
Please add a small comment on top of each, starting with the function/method name.

@omerrob

omerrob commented Nov 17, 2025

Copy link
Copy Markdown
Contributor Author

Added function documentation.

@JalonSolov

Copy link
Copy Markdown
Collaborator

None of those "Unhandled Exception" codes are valid codes.

@omerrob

omerrob commented Nov 17, 2025

Copy link
Copy Markdown
Contributor Author

Sorry, not sure what you mean by "Unhandled Exception" codes.

@JalonSolov

Copy link
Copy Markdown
Collaborator

Not you - in the CI failure logs.

@spytheman

Copy link
Copy Markdown
Contributor

The CI is a bit flaky unfortunately in the last few days :-| .

@medvednikov medvednikov reopened this Nov 18, 2025
@JalonSolov

Copy link
Copy Markdown
Collaborator

Merge master again. Should take care of the Windows failures, at least.

* master:
  cgen: fix dump(ptr_to_fixed_array_value) - it now returns the argument directly (thanks @tankf33der) (vlang#25764)
  type_resolver: clear option flag if present in comptime $for (fix vlang#25761) (vlang#25763)
  db.sqlite: update windows version to https://sqlite.org/2025/sqlite-amalgamation-3510000.zip (vlang#25766)
  builder,pref: add  `-file-list` support (implement feature vlang#25707) (vlang#25749)

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent work.

@spytheman

spytheman commented Nov 19, 2025

Copy link
Copy Markdown
Contributor

Thank you @omerrob 🙇🏻 .

@spytheman spytheman merged commit 8a48859 into vlang:master Nov 19, 2025
73 of 74 checks passed
@omerrob omerrob deleted the pg-result-and-mapper branch November 19, 2025 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants