Skip to content

Commit

Permalink
Add have any criteria (#1042)
Browse files Browse the repository at this point in the history
* Implement allow_nulls_for and forbid_nulls_for

* Remove leftover macro, fix ordering of change default with respect to change type

* Implement have_any? criteria

* Add change nullability statement (#1041)

* Implement allow_nulls_for and forbid_nulls_for

* Remove leftover macro, fix ordering of change default with respect to change type

* Prevent users from assuming boolean result

* Implement allow_nulls_for and forbid_nulls_for

* Remove leftover macro, fix ordering of change default with respect to change type

* Implement have_any? criteria

* Prevent users from assuming boolean result
  • Loading branch information
davidepaolotua authored May 17, 2024
1 parent 1326a8e commit c12d506
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
31 changes: 31 additions & 0 deletions spec/avram/array_column_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,35 @@ describe "Array Columns" do
bucket = SaveBucket.update!(bucket, enums: [Bucket::Size::Small])
bucket.enums.should eq([Bucket::Size::Small])
end

describe "the #have_any method" do
it "returns the records with have at least one element of the provided ones" do
BucketFactory.new.numbers([1, 2]).create
BucketFactory.new.numbers([1, 3]).create

bucket = BucketQuery.new.numbers.have_any([1, 2, 3]).select_count
bucket.should eq 2

bucket = BucketQuery.new.numbers.have_any([1, 3]).select_count
bucket.should eq 2

bucket = BucketQuery.new.numbers.have_any([3, 4]).select_count
bucket.should eq 1

bucket = BucketQuery.new.numbers.have_any([4]).select_count
bucket.should eq 0
end

it "returns nothing with an empty array" do
BucketFactory.new.numbers([1, 2]).create
bucket = BucketQuery.new.numbers.have_any([] of Int64).select_count
bucket.should eq 0
end

it "negates with not" do
BucketFactory.new.numbers([1, 2]).create
bucket = BucketQuery.new.numbers.not.have_any([3]).select_count
bucket.should eq 1
end
end
end
2 changes: 2 additions & 0 deletions spec/avram/where_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ describe Avram::Where do
should_negate(Avram::Where::In, Avram::Where::NotIn)
should_negate(Avram::Where::NotIn, Avram::Where::In)
should_negate(Avram::Where::Null, Avram::Where::NotNull)
should_negate(Avram::Where::Any, Avram::Where::NotAny)
should_negate(Avram::Where::NotAny, Avram::Where::Any)
end
end
5 changes: 5 additions & 0 deletions src/avram/criteria.cr
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ class Avram::Criteria(T, V)
add_clause(Avram::Where::In.new(column, values))
end

def have_any(values) : T
values = values.map { |value| V.adapter.to_db!(value) }
add_clause(Avram::Where::Any.new(column, values))
end

# :nodoc:
def private_distinct_on : T
rows.tap &.query.distinct_on(column)
Expand Down
28 changes: 28 additions & 0 deletions src/avram/where.cr
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,34 @@ module Avram::Where
end
end

class Any < ValueHoldingSqlClause
def operator : String
"&&"
end

def negated : NotAny
NotAny.new(column, value)
end

def prepare(placeholder_supplier : Proc(String)) : String
"#{column} #{operator} (#{placeholder_supplier.call})"
end
end

class NotAny < ValueHoldingSqlClause
def operator : String
"&&"
end

def negated : Any
Any.new(column, value)
end

def prepare(placeholder_supplier : Proc(String)) : String
"NOT(#{column} #{operator} (#{placeholder_supplier.call}))"
end
end

class Includes < ValueHoldingSqlClause
def operator : String
"= ANY"
Expand Down

0 comments on commit c12d506

Please sign in to comment.