Skip to content
Merged
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
9 changes: 0 additions & 9 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Creates a common table expression that can be used to factor subqueries, or create hierarchical
/// or recursive queries of trees and graphs.
public struct With<QueryValue>: Statement, Sendable {
public struct With<Base: Statement>: Statement, Sendable {
public typealias QueryValue = Base.QueryValue
public typealias From = Never

var ctes: [CommonTableExpressionClause]
Expand All @@ -9,7 +10,7 @@ public struct With<QueryValue>: Statement, Sendable {
@_disfavoredOverload
public init(
@CommonTableExpressionBuilder _ ctes: () -> [CommonTableExpressionClause],
query statement: () -> some Statement<QueryValue>
query statement: () -> Base
) {
self.ctes = ctes()
self.statement = statement().query
Expand All @@ -22,7 +23,21 @@ public struct With<QueryValue>: Statement, Sendable {
where
S.QueryValue == (),
S.Joins == (repeat each J),
QueryValue == (S.From, repeat each J)
Base == Select<(S.From, repeat each J), S.From, (repeat each J)>
{
self.ctes = ctes()
self.statement = statement().query
}

@_disfavoredOverload
public init<S: SelectStatement>(
@CommonTableExpressionBuilder _ ctes: () -> [CommonTableExpressionClause],
query statement: () -> S
)
where
S.QueryValue == (),
S.Joins == (),
Base == Select<S.From, S.From, ()>
{
self.ctes = ctes()
self.statement = statement().query
Expand All @@ -40,6 +55,8 @@ public struct With<QueryValue>: Statement, Sendable {
}
}

extension With: PartialSelectStatement where Base: PartialSelectStatement {}

extension QueryFragment {
fileprivate var presence: Self? { isEmpty ? nil : self }
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/StructuredQueriesSQLiteCore/Views.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension Table where Self: _Selection {
/// - ifNotExists: Adds an `IF NOT EXISTS` clause to the `CREATE VIEW` statement.
/// - select: A statement describing the contents of the view.
/// - Returns: A temporary trigger.
public static func createTemporaryView<Selection: SelectStatement>(
public static func createTemporaryView<Selection: PartialSelectStatement>(
ifNotExists: Bool = false,
as select: Selection
) -> TemporaryView<Self, Selection>
Expand All @@ -21,7 +21,7 @@ extension Table where Self: _Selection {
/// This type of statement is returned from ``Table/createTemporaryView(ifNotExists:as:)``.
///
/// To learn more, see <doc:Views>.
public struct TemporaryView<View: Table & _Selection, Selection: SelectStatement>: Statement
public struct TemporaryView<View: Table & _Selection, Selection: PartialSelectStatement>: Statement
where Selection.QueryValue == View {
public typealias QueryValue = ()
public typealias From = Never
Expand Down
32 changes: 28 additions & 4 deletions Tests/StructuredQueriesTests/ViewsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,34 @@ extension SnapshotTests {
"""
}
}

@Test func ctes() {
assertQuery(
CompletedReminder.createTemporaryView(
as: With {
Reminder
.where(\.isCompleted)
.select { CompletedReminder.Columns(reminderID: $0.id, title: $0.title) }
} query: {
CompletedReminder.all
}
)
) {
"""
CREATE TEMPORARY VIEW
"completedReminders"
("reminderID", "title")
AS
WITH "completedReminders" AS (
SELECT "reminders"."id" AS "reminderID", "reminders"."title" AS "title"
FROM "reminders"
WHERE "reminders"."isCompleted"
)
SELECT "completedReminders"."reminderID", "completedReminders"."title"
FROM "completedReminders"
"""
}
}
}
}

Expand All @@ -70,7 +98,3 @@ private struct CompletedReminder {
let reminderID: Reminder.ID
let title: String
}

extension Table where Self: _Selection {
static func foo() {}
}