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
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
if query.order_by
|
|
|
|
sel = OrderedHash.new
|
|
|
|
sel['query'] = query.selector
|
|
|
|
sel['orderby'] = case query.order_by
|
|
|
|
when Array
|
|
|
|
if query.order_by.empty? # Empty array of order_by values
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
case query.order_by[0]
|
|
|
|
when Hash # Array of hashes
|
|
|
|
query.order_by
|
|
|
|
else # ['a', 'b']
|
|
|
|
query.order_by.collect { |v| {v => 1} } # Assume ascending order for all values
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when Hash # Should be an ordered hash, but this message doesn't care
|
|
|
|
a = []
|
|
|
|
query.order_by.each { |k,v| a << {k => v }}
|
|
|
|
a
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|