Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Raise error in Repository#read if query is not valid; resolve #16 #295

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion lib/dm-core/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,11 @@ def lazy_load
tail = self.tail
query = self.query

resources = repository.read(query)
resources = begin
repository.read(query)
rescue Query::InvalidConditionsError
[]
end

# remove already known results
resources -= head if head.any?
Expand Down
12 changes: 10 additions & 2 deletions lib/dm-core/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,11 @@ def first(*args)
if limit_specified
all(query)
else
query.repository.read(query).first
begin
query.repository.read(query).first
rescue Query::InvalidConditionsError
nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required because the checks for invalid query do not actually represent an invalid query.

One such example: Article.first(id: nil) Should immediately return nil, nobody expects this to raise an error.

This checks validity by asking the property Article.properties[:id].valid?(nil). Obviously this is invalid (given this property is Serial/key).

end
end
end

Expand Down Expand Up @@ -416,7 +420,11 @@ def last(*args)
if limit_specified
all(query)
else
query.repository.read(query).last
begin
query.repository.read(query).last
rescue Query::InvalidConditionsError
nil
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/dm-core/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Query
include DataMapper::Assertions
extend Equalizer

InvalidConditionsError = Class.new(StandardError)

OPTIONS = [ :fields, :links, :conditions, :offset, :limit, :order, :unique, :add_reversed, :reload ].to_set.freeze

equalize :repository, :model, :sorted_fields, :links, :conditions, :order, :offset, :limit, :reload?, :unique?, :add_reversed?
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def create(resources)
#
# @api semipublic
def read(query)
return [] unless query.valid?
raise Query::InvalidConditionsError unless query.valid?
query.model.load(adapter.read(query), query)
end

Expand Down