Skip to content

Commit 44a5dbf

Browse files
pimpinclaude
andcommitted
feat: enable prepared statement plan caching via adhoc: false in n1ql 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>
1 parent d7ce0c1 commit 44a5dbf

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

lib/couchbase-orm/n1ql.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def self.sanitize(value)
2323
def self.config(new_config = nil)
2424
Thread.current['__couchbaseorm_n1ql_config__'] = new_config if new_config
2525
Thread.current['__couchbaseorm_n1ql_config__'] || {
26-
scan_consistency: DEFAULT_SCAN_CONSISTENCY
26+
scan_consistency: DEFAULT_SCAN_CONSISTENCY,
27+
adhoc: false
2728
}
2829
end
2930

@@ -57,7 +58,10 @@ def n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **options)
5758
@indexes[name] = method_opts
5859

5960
singleton_class.__send__(:define_method, name) do |key: NO_VALUE, **opts, &result_modifier|
60-
opts = options.merge(opts).reverse_merge(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency])
61+
opts = options.merge(opts).reverse_merge(
62+
scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency],
63+
adhoc: CouchbaseOrm::N1ql.config[:adhoc]
64+
)
6165
values = key == NO_VALUE ? NO_VALUE : convert_values(method_opts[:emit_key], key)
6266
current_query = run_query(method_opts[:emit_key], values, query_fn, custom_order: custom_order, **opts.except(:include_docs, :key))
6367
if result_modifier

spec/n1ql_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ class N1QLTest < CouchbaseOrm::Base
196196
end
197197
end
198198

199+
it "should use adhoc: false by default for prepared statement plan caching" do
200+
expect(Couchbase::Options::Query).to receive(:new).with(hash_including(adhoc: false)).and_call_original
201+
N1QLTest.by_rating_reverse()
202+
end
203+
204+
it "should allow overriding adhoc per call" do
205+
expect(Couchbase::Options::Query).to receive(:new).with(hash_including(adhoc: true)).and_call_original
206+
N1QLTest.by_rating_reverse(adhoc: true)
207+
end
208+
209+
it "should respect N1ql.config adhoc setting" do
210+
default_config = CouchbaseOrm::N1ql.config
211+
CouchbaseOrm::N1ql.config({ adhoc: true })
212+
expect(Couchbase::Options::Query).to receive(:new).with(hash_including(adhoc: true)).and_call_original
213+
N1QLTest.by_rating_reverse()
214+
ensure
215+
CouchbaseOrm::N1ql.config(default_config)
216+
end
217+
199218
after(:all) do
200219
N1QLTest.delete_all
201220
end

0 commit comments

Comments
 (0)