Skip to content

Commit 702c88c

Browse files
pimpinclaude
andcommitted
Add adhoc option to control prepared-statement caching
Default adhoc to true in N1ql.config to preserve current behavior (queries are not cached). When set to false, Couchbase will cache the parameterized query plan, improving performance for repeated queries. The option is forwarded to Couchbase::Options::Query in both n1ql and relation query paths. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 158fd9c commit 702c88c

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

lib/couchbase-orm/n1ql.rb

Lines changed: 5 additions & 3 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: true
2728
}
2829
end
2930

@@ -130,10 +131,11 @@ def run_query(keys, values, query_fn, custom_order: nil, descending: false, limi
130131
limit = build_limit(limit)
131132
n1ql_query = "select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}"
132133

133-
query_options = options.merge(positional_parameters: params)
134+
adhoc = options.delete(:adhoc) { CouchbaseOrm::N1ql.config[:adhoc] }
135+
query_options = options.merge(positional_parameters: params, adhoc: adhoc)
134136
result = cluster.query(n1ql_query, Couchbase::Options::Query.new(**query_options))
135137
CouchbaseOrm.logger.debug {
136-
"N1QL query: #{n1ql_query} params: #{params.inspect} return #{result.rows.to_a.length} rows with scan_consistency: #{options[:scan_consistency]}"
138+
"N1QL query: #{n1ql_query} params: #{params.inspect} return #{result.rows.to_a.length} rows with scan_consistency: #{options[:scan_consistency]} adhoc: #{adhoc}"
137139
}
138140
N1qlProxy.new(result)
139141
end

lib/couchbase-orm/relation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ def build_update_with_params(params, **cond)
236236
def build_query_options(positional_parameters: [])
237237
opts = { scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency] }
238238
opts[:positional_parameters] = positional_parameters unless positional_parameters.empty?
239+
adhoc = CouchbaseOrm::N1ql.config[:adhoc]
240+
opts[:adhoc] = adhoc unless adhoc.nil?
239241
Couchbase::Options::Query.new(**opts)
240242
end
241243

spec/n1ql_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,25 +174,25 @@ class N1QLTest < CouchbaseOrm::Base
174174
N1QLTest.by_rating_reverse()
175175
expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once) do |&block|
176176
msg = block ? block.call : nil
177-
msg == "N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=$1 order by name DESC params: [\"n1_ql_test\"] return 0 rows with scan_consistency: #{described_class::DEFAULT_SCAN_CONSISTENCY}"
177+
msg == "N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=$1 order by name DESC params: [\"n1_ql_test\"] return 0 rows with scan_consistency: #{described_class::DEFAULT_SCAN_CONSISTENCY} adhoc: true"
178178
end
179179
end
180180

181181
it "should log the set scan_consistency when n1ql query is executed with a specific scan_consistency" do
182182
allow(CouchbaseOrm.logger).to receive(:debug)
183183
default_n1ql_config = CouchbaseOrm::N1ql.config
184-
CouchbaseOrm::N1ql.config({ scan_consistency: :not_bounded })
184+
CouchbaseOrm::N1ql.config({ scan_consistency: :not_bounded, adhoc: true })
185185
N1QLTest.by_rating_reverse()
186186
expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once) do |&block|
187187
msg = block ? block.call : nil
188-
msg == "N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=$1 order by name DESC params: [\"n1_ql_test\"] return 0 rows with scan_consistency: not_bounded"
188+
msg == "N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=$1 order by name DESC params: [\"n1_ql_test\"] return 0 rows with scan_consistency: not_bounded adhoc: true"
189189
end
190190

191191
CouchbaseOrm::N1ql.config(default_n1ql_config)
192192
N1QLTest.by_rating_reverse()
193193
expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once) do |&block|
194194
msg = block ? block.call : nil
195-
msg == "N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=$1 order by name DESC params: [\"n1_ql_test\"] return 0 rows with scan_consistency: #{described_class::DEFAULT_SCAN_CONSISTENCY}"
195+
msg == "N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=$1 order by name DESC params: [\"n1_ql_test\"] return 0 rows with scan_consistency: #{described_class::DEFAULT_SCAN_CONSISTENCY} adhoc: true"
196196
end
197197
end
198198

0 commit comments

Comments
 (0)