2009-02-15 13:24:14 +00:00
|
|
|
# --
|
|
|
|
# Copyright (C) 2008-2009 10gen Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# ++
|
|
|
|
|
2008-11-22 01:00:51 +00:00
|
|
|
require 'mongo/message/message'
|
|
|
|
require 'mongo/message/opcodes'
|
2009-10-06 23:20:21 +00:00
|
|
|
require 'mongo/util/conversions'
|
2008-12-09 20:06:35 +00:00
|
|
|
require 'mongo/util/ordered_hash'
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
module Mongo
|
|
|
|
class QueryMessage < Message
|
2009-10-06 23:20:21 +00:00
|
|
|
include Mongo::Conversions
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
attr_reader :query
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
def initialize(db_name, collection_name, query)
|
|
|
|
super(OP_QUERY)
|
|
|
|
@query = query
|
2009-09-10 22:02:03 +00:00
|
|
|
@collection_name = collection_name
|
2009-10-14 18:38:44 +00:00
|
|
|
write_int(@query.query_opts)
|
2009-08-20 14:50:48 +00:00
|
|
|
write_string("#{db_name}.#{collection_name}")
|
|
|
|
write_int(query.number_to_skip)
|
2009-09-30 14:49:08 +00:00
|
|
|
write_int(query.number_to_return)
|
2009-08-20 14:50:48 +00:00
|
|
|
sel = query.selector
|
|
|
|
if query.contains_special_fields
|
|
|
|
sel = OrderedHash.new
|
|
|
|
sel['query'] = query.selector
|
2009-10-08 14:02:54 +00:00
|
|
|
if query.order_by
|
2009-10-06 23:20:21 +00:00
|
|
|
order_by = query.order_by
|
|
|
|
sel['orderby'] = case order_by
|
|
|
|
when String then string_as_sort_parameters(order_by)
|
|
|
|
when Symbol then symbol_as_sort_parameters(order_by)
|
|
|
|
when Array then array_as_sort_parameters(order_by)
|
2009-08-20 14:50:48 +00:00
|
|
|
when Hash # Should be an ordered hash, but this message doesn't care
|
2009-10-06 23:20:21 +00:00
|
|
|
warn_if_deprecated(order_by)
|
|
|
|
order_by
|
2009-08-20 14:50:48 +00:00
|
|
|
else
|
2009-10-08 14:02:54 +00:00
|
|
|
raise InvalidSortValueError.new("illegal order_by: is a #{query.order_by.class.name}, must be String, Array, Hash, or OrderedHash")
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-12-08 16:38:42 +00:00
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
sel['$hint'] = query.hint if query.hint && query.hint.length > 0
|
|
|
|
sel['$explain'] = true if query.explain
|
|
|
|
sel['$snapshot'] = true if query.snapshot
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
write_doc(sel)
|
|
|
|
write_doc(query.fields) if query.fields
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_key(key)
|
|
|
|
@first_key = key
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
2009-09-10 22:02:03 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
"db.#{@collection_name}.#{@query}"
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
end
|