Skip to content

feat: enable adhoc option for prepared statement plan caching#36

Open
pimpin wants to merge 8 commits into
masterfrom
feat/prepared-stmt-adhoc
Open

feat: enable adhoc option for prepared statement plan caching#36
pimpin wants to merge 8 commits into
masterfrom
feat/prepared-stmt-adhoc

Conversation

@pimpin

@pimpin pimpin commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

Expose the adhoc query option so callers can opt into Couchbase server-side prepared-statement plan caching (adhoc: false), while preserving the existing default behaviour (adhoc: true, no caching).

  • Add DEFAULT_ADHOC = true and wire adhoc into N1ql.config so any customized config still falls back to the defaults (merge over { scan_consistency:, adhoc: })
  • Thread adhoc through the reverse_merge in the n1ql-macro generated method (same pattern as scan_consistency)
  • Read adhoc from N1ql.config in Relation#build_query_options, covering all relation paths (where, all, first, last, ids, update_all, delete_all)
  • Per-query override on relations uses the generic with(adhoc: false) (no dedicated adhoc method)

Override priority (highest → lowest): per-call kwarg > per-n1ql-definition option > N1ql.config > default (true).

Usage

# Enable plan caching for a single macro query
N1QLTest.by_rating(key: 1, adhoc: false)

# Enable plan caching for a relation query
User.where(country: 'FR').with(adhoc: false).to_a

# Always cache the plan for a given n1ql definition
n1ql :by_stable_filter, emit_key: [:name], adhoc: false

# Change the thread-local default
CouchbaseOrm::N1ql.config(adhoc: false)

Test plan

  • 3 specs in spec/n1ql_spec.rb: default adhoc: true, per-call override to false, N1ql.config override
  • Relation specs in spec/relation_spec.rb: default adhoc: true, with(adhoc:) override, config override, chainability
  • Run full CI: /test --fail-fast=false

CI fix

Replaced the brittle wget + apt install ./libtinfo5_6.3-2ubuntu0.1_amd64.deb steps with a single consolidated apt step that installs from the repository directly and falls back to libtinfo6 on newer Ubuntu runner images (ubuntu-24.04). This eliminates CI failures caused by Ubuntu package pool URLs becoming stale.

🤖 Generated with Claude Code

pimpin and others added 2 commits July 2, 2026 15:02
… queries

Add adhoc: false to N1ql.config default so every n1ql-macro query opts into
Couchbase server-side plan caching. The value flows through the reverse_merge
in the generated method, maintaining the same override priority as
scan_consistency: per-call > per-definition > N1ql.config > default.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…tion queries

Read adhoc from N1ql.config in build_query_options so all Relation-based
queries (.where, .all, .first, .last, .ids, update_all, delete_all, etc.)
also benefit from server-side plan caching without any caller changes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces support for the adhoc query option (defaulting to false to enable prepared statement plan caching) across Couchbase ORM N1QL queries and relations, along with corresponding tests. The review feedback highlights a potential issue where custom configurations might omit the adhoc key, resulting in it resolving to nil and silently disabling plan caching; merging the custom configuration with the defaults is recommended to prevent this.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread lib/couchbase-orm/n1ql.rb Outdated
pimpin and others added 2 commits July 3, 2026 10:18
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…verride

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@pimpin pimpin marked this pull request as ready for review July 3, 2026 08:51
Comment thread lib/couchbase-orm/relation.rb Outdated
Comment thread lib/couchbase-orm/n1ql.rb Outdated
Comment thread lib/couchbase-orm/n1ql.rb Outdated
pimpin and others added 2 commits July 6, 2026 16:37
Default adhoc to true to preserve original gem behavior (no prepared
statement caching unless explicitly opted in). Use DEFAULT_ADHOC constant
for consistency with DEFAULT_SCAN_CONSISTENCY. Fix config to merge
thread-local overrides on top of defaults instead of replacing them.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Introduce `query_options(**opts)` on CouchbaseOrm_Relation that merges
arbitrary Couchbase::Options::Query keyword args (scan_consistency, adhoc,
timeout, etc.) into the query. `adhoc(value)` becomes a convenience
wrapper over `query_options(adhoc: value)`, preserving the existing API.

build_query_options now merges N1ql.config defaults with per-relation
overrides, eliminating the nil-guard on @adhoc.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@pimpin pimpin left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Some more fix todo.

Comment thread docusaurus/docs/tutorial-ruby-couchbase-orm/07-sqlpp-queries.md Outdated
Comment thread lib/couchbase-orm/relation.rb Outdated
Comment thread lib/couchbase-orm/relation.rb Outdated
Comment thread spec/n1ql_spec.rb Outdated
Comment thread spec/n1ql_spec.rb Outdated
Comment thread spec/n1ql_spec.rb Outdated
Comment thread spec/relation_spec.rb
pimpin and others added 2 commits July 6, 2026 17:49
Address review feedback:
- Keep existing behaviour: default adhoc: true (opt into plan caching
  with adhoc: false), fix specs to reflect true as the default value
- Remove the dedicated Relation#adhoc method and its delegate alias;
  the generic `with(adhoc:)` covers the per-query override
- Update SQL++ docs to reflect the flipped default and `with` usage

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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