Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions lib/couchbase-orm/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@ def to_s
"CouchbaseOrm_Relation: #{@model} where:#{@where.inspect} order:#{@order.inspect} limit: #{@limit} strict_loading: #{@strict_loading}"
end

# Inlines condition values into the statement (for display/debugging).
def to_n1ql
bucket_name = @model.bucket.name
where = build_where_with_params(nil)
order = build_order
limit = build_limit
"select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}"
build_select(nil)
end

# Binds condition values as positional parameters ($1, $2, ...).
def to_n1ql_with_params
bucket_name = @model.bucket.name
params = []
where = build_where_with_params(params)
order = build_order
limit = build_limit
["select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}", params]
[build_select(params), params]
end

# The single seam through which every relation statement reaches the
# cluster, so query options (N1ql.config merged with #with) and logging
# are applied in one place.
def execute(n1ql_query, params = [])
result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params))
CouchbaseOrm.logger.debug { "Relation query: #{n1ql_query} params: #{params.inspect} return #{result.rows.to_a.length} rows" }
Expand Down Expand Up @@ -76,17 +73,12 @@ def with(opts = {})
end

def first
n1ql_query, params = self.limit(1).to_n1ql_with_params
result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params))
return unless (first_id = result.rows.to_a.first)

@model.find(first_id, with_strict_loading: @strict_loading)
first_id = limit(1).ids.first
@model.find(first_id, with_strict_loading: @strict_loading) if first_id
end

def last
n1ql_query, params = to_n1ql_with_params
result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params))
last_id = result.rows.to_a.last
last_id = ids.last
@model.find(last_id, with_strict_loading: @strict_loading) if last_id
end

Expand Down Expand Up @@ -172,6 +164,10 @@ def build_limit
@limit ? "limit #{@limit}" : ""
end

def build_select(params)
"select raw meta().id from `#{@model.bucket.name}` where #{build_where_with_params(params)} order by #{build_order} #{build_limit}"
end

def initializer_arguments
{ model: @model, order: @order, where: @where, limit: @limit, strict_loading: @strict_loading, query_options: @query_options }
end
Expand Down
39 changes: 39 additions & 0 deletions spec/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,5 +534,44 @@ def self.active
expect(result).to match_array([m1])
end
end

describe "query execution seam" do
let(:relation) { RelationModel.where(active: true).order(age: :asc).with(adhoc: false, client_context_id: "relation-seam-spec") }

before do
@young = RelationModel.create!(active: true, age: 10)
@old = RelationModel.create!(active: true, age: 30)
RelationModel.create!(active: false, age: 20)
end

{
ids: ->(r) { r.ids },
first: ->(r) { r.first },
last: ->(r) { r.last },
count: ->(r) { r.count },
to_a: ->(r) { r.to_a },
update_all: ->(r) { r.update_all(name: "touched") },
}.each do |terminal, call|
it "routes #{terminal} through #execute with the relation's query options" do
expect_any_instance_of(CouchbaseOrm::Relation::CouchbaseOrm_Relation).to receive(:execute).once.and_call_original
expect(Couchbase::Options::Query).to receive(:new)
.with(hash_including(adhoc: false, client_context_id: "relation-seam-spec", scan_consistency: :request_plus))
.once.and_call_original
call.call(relation)
end
end

it "returns the first and last records" do
expect(relation.first).to eq @young
expect(relation.last).to eq @old
end

it "builds the same statement with inlined values or positional parameters" do
inlined = relation.to_n1ql
parameterized, params = relation.send(:to_n1ql_with_params)
expect(params).to eq([true])
expect(parameterized.sub("$1", "true")).to eq(inlined)
end
end
end

Loading