Skip to content

Commit 519b7aa

Browse files
authored
Add static Table.exists() (#88)
* Add static `Table.exists()` * wip * wip * wip
1 parent 16faf66 commit 519b7aa

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Sources/StructuredQueriesCore/Operators.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,12 +913,21 @@ extension Statement where QueryValue: QueryBindable {
913913
}
914914
}
915915

916-
extension Statement {
916+
extension PartialSelectStatement {
917917
/// Returns a predicate expression indicating whether this subquery contains any element.
918918
///
919919
/// - Returns: A predicate expression indicating whether this subquery contains any element.
920920
public func exists() -> some QueryExpression<Bool> {
921-
SQLQueryExpression("EXISTS \(self)")
921+
SQLQueryExpression("EXISTS \(self.queryFragment)")
922+
}
923+
}
924+
925+
extension Table {
926+
/// Returns a predicate expression indicating whether this table contains any element.
927+
///
928+
/// - Returns: A predicate expression indicating whether this subquery contains any element.
929+
public static func exists() -> some QueryExpression<Bool> {
930+
all.exists()
922931
}
923932
}
924933

Tests/StructuredQueriesTests/OperatorsTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,20 @@ extension SnapshotTests {
568568
}
569569

570570
@Test func exists() {
571+
assertQuery(Values(Reminder.exists())) {
572+
"""
573+
SELECT EXISTS (
574+
SELECT "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title"
575+
FROM "reminders"
576+
)
577+
"""
578+
} results: {
579+
"""
580+
┌──────┐
581+
│ true │
582+
└──────┘
583+
"""
584+
}
571585
assertQuery(Values(Reminder.where { $0.id == 1 }.exists())) {
572586
"""
573587
SELECT EXISTS (

0 commit comments

Comments
 (0)