2008-11-22 01:00:51 +00:00
|
|
|
require 'mongo/message/message'
|
|
|
|
require 'mongo/message/opcodes'
|
2008-12-09 20:06:35 +00:00
|
|
|
require 'mongo/util/ordered_hash'
|
2008-11-22 01:00:51 +00:00
|
|
|
|
|
|
|
module XGen
|
|
|
|
module Mongo
|
|
|
|
module Driver
|
|
|
|
|
|
|
|
class QueryMessage < Message
|
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
attr_reader :query
|
|
|
|
|
2008-12-08 16:38:42 +00:00
|
|
|
def initialize(db_name, collection_name, query)
|
2008-11-22 01:00:51 +00:00
|
|
|
super(OP_QUERY)
|
2009-01-13 19:02:16 +00:00
|
|
|
@query = query
|
2008-11-22 01:00:51 +00:00
|
|
|
write_int(0)
|
2008-12-08 16:38:42 +00:00
|
|
|
write_string("#{db_name}.#{collection_name}")
|
2008-11-22 01:00:51 +00:00
|
|
|
write_int(query.number_to_skip)
|
|
|
|
write_int(query.number_to_return)
|
2008-12-09 20:06:35 +00:00
|
|
|
sel = query.selector
|
2009-01-14 15:42:56 +00:00
|
|
|
if query.contains_special_fields
|
2008-12-09 20:06:35 +00:00
|
|
|
sel = OrderedHash.new
|
|
|
|
sel['query'] = query.selector
|
2009-01-14 15:42:56 +00:00
|
|
|
if query.order_by && query.order_by.length > 0
|
|
|
|
sel['orderby'] = case query.order_by
|
|
|
|
when String
|
|
|
|
{query.order_by => 1}
|
|
|
|
when Array
|
|
|
|
h = OrderedHash.new
|
2009-01-15 17:33:20 +00:00
|
|
|
query.order_by.each { |ob|
|
|
|
|
case ob
|
|
|
|
when String
|
|
|
|
h[ob] = 1
|
|
|
|
when Hash # should have one entry; will handle all
|
|
|
|
ob.each { |k,v| h[k] = v }
|
|
|
|
else
|
|
|
|
raise "illegal query order_by value #{query.order_by.inspect}"
|
|
|
|
end
|
|
|
|
}
|
2009-01-14 15:42:56 +00:00
|
|
|
h
|
|
|
|
when Hash # Should be an ordered hash, but this message doesn't care
|
|
|
|
query.order_by
|
|
|
|
else
|
|
|
|
raise "illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if query.hint_fields && query.hint_fields.length > 0
|
|
|
|
hints = OrderedHash.new
|
|
|
|
query.hint_fields.each { |hf| hints[hf] = 1 }
|
|
|
|
sel['$hint'] = hints
|
|
|
|
end
|
|
|
|
if query.explain
|
|
|
|
sel['$explain'] = true
|
|
|
|
end
|
2008-12-09 20:06:35 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
write_doc(sel)
|
2008-11-22 01:00:51 +00:00
|
|
|
write_doc(query.fields) if query.fields
|
|
|
|
end
|
2008-12-08 16:38:42 +00:00
|
|
|
|
|
|
|
def first_key(key)
|
|
|
|
@first_key = key
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|