Skip to content

refactor(codegen/adapter): consume RawQuery.encode via runtime.prepare#379

Merged
nao1215 merged 1 commit intomainfrom
refactor/issue-299-adapters-consume-rawquery
Apr 17, 2026
Merged

refactor(codegen/adapter): consume RawQuery.encode via runtime.prepare#379
nao1215 merged 1 commit intomainfrom
refactor/issue-299-adapters-consume-rawquery

Conversation

@nao1215
Copy link
Copy Markdown
Owner

@nao1215 nao1215 commented Apr 17, 2026

Summary

Rework the native pog / sqlight adapter codegen so generated query functions consume the same RawQuery.encode pipeline that powers runtime.prepare. Each adapter file now declares a single value_to_<driver> helper, and every query function walks the encoded List(Value) through list.fold, eliminating the per-parameter driver encoding calls and the ad-hoc slice expansion logic.

Changes

  • src/sqlode/codegen/adapter.gleam
    • Emit value_to_pog / value_to_sqlight helper at the top of the file, mapping runtime.Value → driver Value
    • New render_prepare_lines helper centralises the runtime.prepare(q, p) + list.fold scaffolding
    • Rewrite render_pog_query_call / render_pog_exec_last_id / render_sqlight_query_call / render_sqlight_exec_last_id to use the helper
    • Delete render_param_value_expr, render_list_param_value_expr, render_slice_expansion_line, render_pog_params_simple, render_pog_params_with_slices, render_sqlight_params_simple, render_sqlight_params_with_slices
    • Gate gleam/list import on has_params instead of has_slices
  • test/codegen_test.gleam
    • Update assertions for the new prepare + fold shape
    • Flip enum/array expectations: models.status_to_string(v) / pog.array(pog.text) now live in the params module, not the adapter

Design Decisions

  • A single value_to_<driver> helper per adapter file keeps the generated code small and recursive array support is a natural one-liner (pog.array(value_to_pog, vs)).
  • Slice expansion is already handled by runtime.prepare, so the adapter layer only needs to fold the resulting List(Value); this removes a large amount of conditional rendering.
  • Enum → string conversion and array encoding now belong to the params module output, so the adapter stays driver-agnostic and does not re-encode.

Limitations

  • sqlight has no native array type, so SqlArray falls through to sqlight.null() in value_to_sqlight. Array support for SQLite remains a separate concern.

Closes #299

Summary by CodeRabbit

  • Refactor
    • Updated SQL adapter code generation to use a unified parameter binding approach with driver-specific value conversion helpers.
    • Simplified parameter handling by removing placeholder expansion logic in favor of a prepare-and-fold pattern.
    • Updated test assertions to reflect the new code generation structure.

Replace per-parameter driver encoding calls with a single
value_to_<driver> helper plus runtime.prepare + list.fold, so native
adapters consume the same RawQuery encoding pipeline as the generic
runtime path.

- Emit a value_to_pog / value_to_sqlight helper at the top of each
  adapter file, mapping runtime.Value to the driver's Value type
- Generate each query function via runtime.prepare(q, p) followed by
  list.fold(values, query, fn(acc, v) { driver.parameter(acc, helper(v)) })
- Drop per-param render_param_value_expr, render_list_param_value_expr
  and render_slice_expansion_line; slice expansion now happens inside
  runtime.prepare
- Delegate enum-to-string and array conversion to the params module so
  the adapter layer stays driver-agnostic

Closes #299
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 17, 2026

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Free

Run ID: 0128140c-66c7-4727-ab9a-4d847c2ef1af

📥 Commits

Reviewing files that changed from the base of the PR and between 2620685 and 552ce0c.

📒 Files selected for processing (2)
  • src/sqlode/codegen/adapter.gleam
  • test/codegen_test.gleam

📝 Walkthrough

Walkthrough

The pull request refactors SQLode's adapter code generation, replacing slice placeholder expansion with a new runtime.prepare approach. It introduces per-adapter value-to-driver conversion helpers within the AdapterConfig and consolidates parameter binding into unified fold/map operations over a values list, removing prior per-parameter expression generation logic.

Changes

Cohort / File(s) Summary
Adapter Rendering Refactor
src/sqlode/codegen/adapter.gleam
Added value_to_driver_helper field to AdapterConfig. Reworked render_pog_query_call, render_pog_exec_last_id, render_sqlight_query_call, and render_sqlight_exec_last_id to use runtime.prepare(q, p) and fold/map operations over values instead of slice placeholder expansion. Removed render_param_value_expr, render_list_param_value_expr, and render_slice_expansion_line helper functions. Updated import gleam/list logic to depend on has_params rather than slice usage.
Test Assertion Updates
test/codegen_test.gleam
Updated adapter rendering assertions to expect runtime.prepare(q, p) calls and unified fold/map parameter binding patterns. Changed enum slice conversion expectations from per-parameter stringification to adapter-level helper invocations. Updated array-columns tests to expect recursive helper patterns instead of per-type pog.array(pog.*) calls.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Old placeholders fade away,
prepare and helpers save the day,
A fold, a map, so clean and bright,
Parameter binding now feels right!


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands and usage tips.

@nao1215 nao1215 merged commit dab0027 into main Apr 17, 2026
2 checks passed
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.

design: make native adapters consume RawQuery encoding instead of re-encoding params

1 participant