Skip to content

feat: add an opt-in query_typed option for the postgres backends - #15

Closed
orlandoc01 wants to merge 2 commits into
mainfrom
query-typed
Closed

orlandoc01 wants to merge 2 commits into
mainfrom
query-typed

Conversation

@orlandoc01

Copy link
Copy Markdown
Owner

Summary

Adds an opt-in query_typed: true plugin option for the postgres, tokio-postgres, and deadpool-postgres backends (upstream tunamaguro/sqlc-gen-rust#61).

When enabled, plain static-query functions and -- :if dynamic-filter functions bind parameters by their PostgreSQL type OID and execute with the driver's query_typed* APIs, so a call is one round trip instead of prepare + execute. Dynamic-filter queries benefit most, since their SQL is built at runtime and could never be prepared ahead.

  • prepare_<fn> and <fn>_with are unchanged, so callers who want prepared statements keep them.
  • :exec / :execrows / :execresult drain a typed row stream and read rows_affected; the async backends need futures-util, which the _stream helpers already required.
  • Any query with a parameter that has no postgres_types::Type constant (user enums, domains, citext, hstore, pseudo-types such as any) is generated exactly as before, even with the flag on.
  • deadpool-postgres 0.14's GenericClient predates the typed methods, so its typed plain functions take the pooled Client; pooled transactions keep using prepare_<fn> / <fn>_with.
  • Output with the flag off is byte-identical to before.

The option is enabled in sqlc.yaml for the dynamic-filter and type-mapping postgres example crates so CI runs both the typed and fallback paths against a live database. Other example crates stay on the prepared path.

Also dedupes the postgres static/dynamic generators behind a bind mode, shares the dynamic bind-arm scaffolding across sqlx/rusqlite/postgres, and centralises test query parsing in test_support.

Test plan

  • just check-local
  • cargo test for the six typed example crates against a live PostgreSQL 17 (POSTGRES_DATABASE_URL)
  • New token tests: typed :one/:many/:exec/:execrows on all three backends, arrays, pg_catalog. spellings, zero-parameter queries, enum and any fallback, dynamic-filter typed helper, flag-off unchanged
  • New live test UpdateKnownTypes in the three type-mapping postgres crates binding arrays, timestamps, date, uuid, json/jsonb, bytea, text, bool, and float8 through the typed path

🤖 Generated with Claude Code

https://claude.ai/code/session_01MFwFfjtmH1EyBCTd1jf6C4

orlandoc01 and others added 2 commits September 13, 2026 01:19
Fixes the pg_catalog lowercase fallback, maps pseudo-types such as any
to the prepared path, and corrects character varying to VARCHAR. Merges
the typed static and dynamic generators into the existing ones behind a
bind mode, dedupes the dynamic bind arms and test query parsing across
backends, and adds live multi-type coverage to the type-mapping
postgres crates.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MFwFfjtmH1EyBCTd1jf6C4
@orlandoc01 orlandoc01 closed this Sep 19, 2026
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.

1 participant