Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Sources/StructuredQueriesCore/Statements/Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ extension Table {
) -> Select<Int, Self, ()> {
Where().count(filter: filter)
}

/// A select statement for if this table contains any rows.
///
/// - Returns: A select statement that selects `exists()`.
public static func exists() -> some PartialSelectStatement<Bool> {
ExistsSelect<Self, _>(statement: all)
}
}

public struct _SelectClauses: Sendable {
Expand Down Expand Up @@ -1525,3 +1532,11 @@ private struct CopyOnWrite<Value> {
extension CopyOnWrite: Sendable where Value: Sendable {}

extension CopyOnWrite.Storage: @unchecked Sendable where Value: Sendable {}

private struct ExistsSelect<From: Table, S: Statement>: SelectStatement {
typealias QueryValue = Bool
let statement: S
var query: QueryFragment {
"SELECT EXISTS\(statement)"
}
}
17 changes: 17 additions & 0 deletions Tests/StructuredQueriesTests/SelectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,23 @@ extension SnapshotTests {
}
}
}

@Test func exists() {
assertQuery(Reminder.exists()) {
"""
SELECT EXISTS(
SELECT "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title"
FROM "reminders"
)
"""
}results: {
"""
┌──────┐
│ true │
└──────┘
"""
}
}
}
}

Expand Down