Skip to content

Commit 5a96b59

Browse files
committed
Add QueryBuilder#none?
1 parent 2612283 commit 5a96b59

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

spec/interro_spec.cr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,17 @@ describe Interro do
654654
UserQuery.new.search("search").should contain user
655655
end
656656

657+
it "can check whether any records match" do
658+
user = create_user
659+
matching = UserQuery.new.with_id(user.id)
660+
empty = UserQuery.new.with_id(UUID.v7)
661+
662+
matching.any?.should eq true
663+
matching.none?.should eq false
664+
empty.any?.should eq false
665+
empty.none?.should eq true
666+
end
667+
657668
describe "transactions" do
658669
it "commits without error" do
659670
user = nil

src/query_builder.cr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ module Interro
501501
end
502502

503503
def any? : Bool
504+
!none?
505+
end
506+
507+
def none? : Bool
504508
sql = String.build do |str|
505509
str << "SELECT 1 AS one"
506510
str << " FROM " << sql_table_name
@@ -512,7 +516,7 @@ module Interro
512516
str << " LIMIT 1"
513517
end
514518

515-
!!connection(CONFIG.read_db).query_one? sql, args: @args, as: Int32
519+
!connection(CONFIG.read_db).query_one? sql, args: @args, as: Int32
516520
end
517521

518522
protected def insert(**values) : T

0 commit comments

Comments
 (0)