Skip to content

orm: add execute method to Connection interface for raw SQL queries - #27416

Merged
medvednikov merged 12 commits into
vlang:masterfrom
Jengro777:orm_exec
Jun 26, 2026
Merged

orm: add execute method to Connection interface for raw SQL queries#27416
medvednikov merged 12 commits into
vlang:masterfrom
Jengro777:orm_exec

Conversation

@Jengro777

Copy link
Copy Markdown
Contributor

Summary

Add execute(query string) ![]orm.Row to the orm.Connection interface,
enabling users to run raw SQL queries through the ORM abstraction and
receive results as a driver-agnostic []orm.Row.

All three DB backends (mysql, pg, sqlite) implement the method, and the
orm.DB / orm.Tx wrappers expose it with proper transaction-active checks.

Behavior change

Yes — new public API:

  • orm.Connection.execute(query string) ![]orm.Row
  • orm.DB.execute(query string) ![]orm.Row
  • orm.Tx.execute(query string) ![]orm.Row
  • orm.Row struct with vals []string and names []string fields

The interface addition is a breaking change for any external type
implementing orm.Connection; they must add execute to compile.

Why execute instead of exec?

The new Connection.execute method returns ![]orm.Row, a driver-agnostic
row representation. The name execute (rather than exec) was chosen deliberately.

fix #27415

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6a41aa23be

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread vlib/db/pg/orm.v
Comment thread vlib/db/mysql/orm.c.v Outdated
@Jengro777

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4231bf9a7d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread vlib/db/pg/orm.v Outdated
Jengro777 and others added 4 commits June 12, 2026 09:04
Add a new `Row` struct for representing raw SQL result rows, and an
`execute` method on the `Connection` interface that returns `![]Row`.

All three DB backends (mysql, pg, sqlite) implement the new method;
sqlite also populates column names from the underlying driver Row type.

The `orm.DB` and `orm.Tx` wrappers expose `execute` as well, including
transaction-active checks via `ensure_active`.

Update mock implementations and add integration tests for the execute
path through scope and transaction wrappers.
Addresses the Codex review feedback: the PG backend now uses
exec_result (which returns pg.Result with cols metadata) instead
of exec, and the MySQL backend uses the raw result/fields API
to extract column names. Both populate orm.Row.names, matching
the SQLite implementation.

Co-authored-by: Codex <codex@vlang.io>
Instead of reconstructing column names from the pg.Result.cols map
(which collapses duplicate names), add a names []string field to
Result populated by ordinal position in res_to_result, and use
res.names directly in the ORM execute implementation.

The map-based approach could produce a names array shorter than the
actual column count when a query returns duplicate column labels
(e.g. SELECT a.id, b.id FROM ...), causing runtime panics or
misaligned names.
@Jengro777

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e243873cfb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread vlib/db/mysql/orm.c.v
Comment thread vlib/db/mysql/orm.c.v
Addresses two Codex review findings:

- Check get_errno when mysql_store_result returns NULL before treating
  it as a no-result statement; a NULL result with a non-zero errno
  means the result set read failed, not that there are zero rows.

- Free the MYSQL_RES allocated by mysql_store_result via defer so that
  repeated execute calls do not leak native result-set memory.
@Jengro777

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1a458a24da

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread vlib/db/pg/pg.c.v Outdated
@Jengro777
Jengro777 marked this pull request as draft June 12, 2026 01:38
res_to_result collected column names inside the row loop with an
if i == 0 guard, so names and cols were empty when PQntuples
returned zero (e.g. SELECT ... WHERE false). Move column name
collection before the row loop so metadata is always populated.

Add regression tests: synthetic tests for the Result data model
with zero tuples + column names, and an integration test that
exercises the full res_to_result path via SELECT ... WHERE false.
@Jengro777

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: cfedd41d25

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@Jengro777
Jengro777 marked this pull request as ready for review June 12, 2026 11:57

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cfedd41d25

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread vlib/orm/orm.v
@JalonSolov

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c260110d03

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread vlib/orm/orm_generic_where_shadow_test.v Outdated
The previous commit (afd648b) renamed local variables from
to  to work around a Windows GCC shadow-warning issue,
but this defeat the purpose of the test, which specifically guards the
 path where a local variable shadows the field.

This reverts the rename, restoring the original name-shadowing
regression test as written in commit c2f124c.
@Jengro777

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 003a9cdfbb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@medvednikov
medvednikov merged commit f581cbb into vlang:master Jun 26, 2026
84 checks passed
@Jengro777
Jengro777 deleted the orm_exec branch June 29, 2026 02:44
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.

orm.Connection interface lacks exec() method, forcing raw SQL execution to bypass the interface

3 participants