Skip to content

Commit 7b94fde

Browse files
committed
Implement multiple string keys
1 parent 726c467 commit 7b94fde

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

lib/ruby_odata/service.rb

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,42 +180,60 @@ def build_collection_query_object(name, additional_parameters, *args)
180180
if args.empty?
181181
#nothing to add
182182
elsif args.size == 1
183-
if args.first.to_s =~ /\d+/
184-
id_metadata = find_id_metadata(name.to_s)
185-
root << build_id_path(args.first, id_metadata)
186-
else
187-
root << "(#{args.first})"
188-
end
183+
keys = get_keys_metadata(name.to_s)
184+
root << "(#{build_ids(keys, args.first).join(',')})"
189185
else
190186
root << "(#{args.join(',')})"
191187
end
192188
QueryBuilder.new(root, additional_parameters)
193189
end
194190

195-
# Finds the metadata associated with the given collection's first id property
196-
# Remarks: This is used for single item lookup queries using the ID, e.g. Products(1), not complex primary keys
191+
# Finds the metadata associated with the given collection's keys
197192
#
198193
# @param [String] collection_name the name of the collection
199-
def find_id_metadata(collection_name)
194+
def get_keys_metadata(collection_name)
200195
collection_data = @collections.fetch(collection_name)
201196
class_metadata = @class_metadata.fetch(collection_data[:type].to_s)
202-
key = class_metadata.select{|k,h| h.is_key }.collect{|k,h| h.name }[0]
203-
class_metadata[key]
197+
keys = class_metadata.select{|k,h| h.is_key }
198+
end
199+
200+
# Builds the ID expression of a given id for query
201+
#
202+
# @param [Object] id_value the actual value to be used
203+
# @param [PropertyMetadata] id_metadata the property metadata object for the id
204+
# Builds the IDs expression for the given ids for query
205+
#
206+
# @param [Hash] keys Hash of metadata for the keys
207+
# @param [Object/Hash] values
208+
def build_ids(keys, values)
209+
if keys.size == 1
210+
[ quote_id(values, keys.first[1]) ]
211+
elsif values.is_a?(Hash)
212+
ids = []
213+
keys.each_pair do |key, meta|
214+
v = values[key.to_sym]
215+
ids << "#{key}=#{quote_id(v, meta)}"
216+
end
217+
ids
218+
else
219+
values.to_a
220+
end
204221
end
205222

206223
# Builds the ID expression of a given id for query
207224
#
208225
# @param [Object] id_value the actual value to be used
209226
# @param [PropertyMetadata] id_metadata the property metadata object for the id
210-
def build_id_path(id_value, id_metadata)
227+
def quote_id(id_value, id_metadata)
211228
if id_metadata.type == "Edm.Int64"
212-
"(#{id_value}L)"
229+
"#{id_value}L"
213230
elsif id_metadata.type == "Edm.String"
214-
"('#{id_value}')"
231+
"'#{id_value}'"
215232
else
216-
"(#{id_value})"
233+
"#{id_value}"
217234
end
218235
end
236+
219237

220238
def set_options!(options)
221239
@options = options

0 commit comments

Comments
 (0)