Skip to content

Commit 197849a

Browse files
committed
Fix #any? and #none? on empty QueryBuilders
It was always adding a `WHERE` clause to the query, regardless of whether there were any conditions.
1 parent 5a96b59 commit 197849a

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

spec/interro_spec.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,9 @@ describe Interro do
663663
matching.none?.should eq false
664664
empty.any?.should eq false
665665
empty.none?.should eq true
666+
# Check whether we can do it on an empty QueryBuilder
667+
UserQuery.new.any?.should eq true
668+
UserQuery.new.none?.should eq false
666669
end
667670

668671
describe "transactions" do

src/query_builder.cr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,15 @@ module Interro
508508
sql = String.build do |str|
509509
str << "SELECT 1 AS one"
510510
str << " FROM " << sql_table_name
511+
511512
if join = join_clause
512513
join.each(&.to_sql(str))
513514
end
514515

515-
str << " WHERE " << @where_clause.try(&.to_sql)
516+
if where = where_clause
517+
str << " WHERE " << where.to_sql
518+
end
519+
516520
str << " LIMIT 1"
517521
end
518522

0 commit comments

Comments
 (0)