Skip to content

Commit 41f2209

Browse files
committed
Add static Table.exists()
1 parent b90c144 commit 41f2209

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Sources/StructuredQueriesCore/Operators.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,15 @@ extension Statement {
921921
}
922922
}
923923

924+
extension Table {
925+
/// Returns a predicate expression indicating whether this table contains any element.
926+
///
927+
/// - Returns: A predicate expression indicating whether this subquery contains any element.
928+
public static func exists() -> some QueryExpression<Bool> {
929+
SQLQueryExpression("EXISTS \(all)")
930+
}
931+
}
932+
924933
private struct UnaryOperator<QueryValue>: QueryExpression {
925934
let `operator`: QueryFragment
926935
let base: QueryFragment

Tests/StructuredQueriesTests/OperatorsTests.swift

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

570570
@Test func exists() {
571+
assertQuery(Values(Reminder.exists())) {
572+
"""
573+
SELECT EXISTS SELECT "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title"
574+
FROM "reminders"
575+
"""
576+
} results: {
577+
"""
578+
near "SELECT": syntax error
579+
"""
580+
}
571581
assertQuery(Values(Reminder.where { $0.id == 1 }.exists())) {
572582
"""
573583
SELECT EXISTS (

0 commit comments

Comments
 (0)