Skip to content

Commit b374583

Browse files
committed
wip
1 parent cb85c77 commit b374583

File tree

7 files changed

+90
-15
lines changed

7 files changed

+90
-15
lines changed

Sources/StructuredQueries/Macros.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import StructuredQueriesCore
1010
PartialSelectStatement,
1111
PrimaryKeyedTable,
1212
names: named(From),
13-
// named(all),
1413
named(columns),
1514
named(init(_:)),
1615
named(init(decoder:)),

Sources/StructuredQueriesCore/Statements/CommonTableExpression.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,28 @@ public struct With<QueryValue>: Statement {
2929
}
3030

3131
public var query: QueryFragment {
32+
guard !statement.isEmpty else { return "" }
33+
let cteFragments = ctes.compactMap(\.queryFragment.presence)
34+
guard !cteFragments.isEmpty else { return "" }
3235
var query: QueryFragment = "WITH "
3336
query.append(
34-
"\(ctes.map(\.queryFragment).joined(separator: ", "))\(.newlineOrSpace)\(statement)"
37+
"\(cteFragments.joined(separator: ", "))\(.newlineOrSpace)\(statement)"
3538
)
3639
return query
3740
}
3841
}
3942

43+
extension QueryFragment {
44+
fileprivate var presence: Self? { isEmpty ? nil : self }
45+
}
46+
4047
public struct CommonTableExpressionClause: QueryExpression {
4148
public typealias QueryValue = ()
4249
let tableName: QueryFragment
4350
let select: QueryFragment
4451
public var queryFragment: QueryFragment {
45-
"\(tableName) AS (\(.newline)\(select.indented())\(.newline))"
52+
guard !select.isEmpty else { return "" }
53+
return "\(tableName) AS (\(.newline)\(select.indented())\(.newline))"
4654
}
4755
}
4856

Sources/StructuredQueriesCore/Statements/Delete.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extension Table {
88
///
99
/// - Returns: A delete statement.
1010
public static func delete() -> DeleteOf<Self> {
11-
Delete()
11+
Delete(isEmpty: false)
1212
}
1313
}
1414

@@ -23,7 +23,7 @@ extension PrimaryKeyedTable {
2323
/// - Parameter row: A row to delete.
2424
/// - Returns: A delete statement.
2525
public static func delete(_ row: Self) -> DeleteOf<Self> {
26-
Delete()
26+
delete()
2727
.where {
2828
$0.primaryKey.eq(TableColumns.PrimaryKey(queryOutput: row[keyPath: $0.primaryKey.keyPath]))
2929
}
@@ -36,9 +36,9 @@ extension PrimaryKeyedTable {
3636
///
3737
/// To learn more, see <doc:DeleteStatements>.
3838
public struct Delete<From: Table, Returning> {
39+
var isEmpty: Bool
3940
var `where`: [QueryFragment] = []
4041
var returning: [QueryFragment] = []
41-
var isEmpty = false
4242

4343
/// Adds a condition to a delete statement.
4444
///
@@ -108,6 +108,7 @@ public struct Delete<From: Table, Returning> {
108108
returning.append("\(quote: resultColumn.name)")
109109
}
110110
return Delete<From, (repeat each QueryValue)>(
111+
isEmpty: isEmpty,
111112
where: `where`,
112113
returning: Array(repeat each selection(From.columns))
113114
)
@@ -127,6 +128,7 @@ public struct Delete<From: Table, Returning> {
127128
returning.append("\(quote: resultColumn.name)")
128129
}
129130
return Delete<From, From>(
131+
isEmpty: isEmpty,
130132
where: `where`,
131133
returning: returning
132134
)

Sources/StructuredQueriesCore/Statements/Update.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension Table {
3737
or conflictResolution: ConflictResolution? = nil,
3838
set updates: (inout Updates<Self>) -> Void
3939
) -> UpdateOf<Self> {
40-
Update(conflictResolution: conflictResolution, updates: Updates(updates))
40+
Update(isEmpty: false, conflictResolution: conflictResolution, updates: Updates(updates))
4141
}
4242
}
4343

@@ -91,11 +91,11 @@ extension PrimaryKeyedTable {
9191
///
9292
/// To learn more, see <doc:UpdateStatements>.
9393
public struct Update<From: Table, Returning> {
94+
var isEmpty: Bool
9495
var conflictResolution: ConflictResolution?
9596
var updates: Updates<From>
9697
var `where`: [QueryFragment] = []
9798
var returning: [QueryFragment] = []
98-
var isEmpty = false
9999

100100
/// Adds a condition to an update statement.
101101
///
@@ -160,6 +160,7 @@ public struct Update<From: Table, Returning> {
160160
returning.append("\(quote: resultColumn.name)")
161161
}
162162
return Update<From, (repeat each QueryValue)>(
163+
isEmpty: false,
163164
conflictResolution: conflictResolution,
164165
updates: updates,
165166
where: `where`,
@@ -181,6 +182,7 @@ public struct Update<From: Table, Returning> {
181182
returning.append("\(quote: resultColumn.name)")
182183
}
183184
return Update<From, From>(
185+
isEmpty: isEmpty,
184186
conflictResolution: conflictResolution,
185187
updates: updates,
186188
where: `where`,

Sources/StructuredQueriesCore/Statements/Where.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ extension Where: SelectStatement {
479479
/// A delete statement for the filtered table.
480480
public func delete() -> DeleteOf<From> {
481481
Delete(
482-
where: scope == .unscoped ? predicates : From.all._selectClauses.where + predicates,
483-
isEmpty: scope == .empty
482+
isEmpty: scope == .empty,
483+
where: scope == .unscoped ? predicates : From.all._selectClauses.where + predicates
484484
)
485485
}
486486

@@ -495,10 +495,10 @@ extension Where: SelectStatement {
495495
set updates: (inout Updates<From>) -> Void
496496
) -> UpdateOf<From> {
497497
Update(
498+
isEmpty: scope == .empty,
498499
conflictResolution: conflictResolution,
499500
updates: Updates(updates),
500-
where: scope == .unscoped ? predicates : From.all._selectClauses.where + predicates,
501-
isEmpty: scope == .empty
501+
where: scope == .unscoped ? predicates : From.all._selectClauses.where + predicates
502502
)
503503
}
504504

Sources/StructuredQueriesMacros/TableMacro.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,6 @@ extension TableMacro: ExtensionMacro {
585585
"""
586586
public typealias From = Swift.Never
587587
""",
588-
// """
589-
// public static var all: Where<Self> { none }
590-
// """,
591588
])
592589
} else {
593590
initDecoder = """

Tests/StructuredQueriesTests/CommonTableExpressionTests.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,73 @@ extension SnapshotTests {
213213
}
214214
}
215215

216+
@Test func empty() {
217+
assertQuery(
218+
With {
219+
Reminder
220+
.where { !$0.isCompleted }
221+
.select { IncompleteReminder.Columns(isFlagged: $0.isFlagged, title: $0.title) }
222+
} query: {
223+
Reminder
224+
.none
225+
.delete()
226+
.returning(\.title)
227+
}
228+
) {
229+
"""
230+
231+
"""
232+
} results: {
233+
"""
234+
235+
"""
236+
}
237+
assertQuery(
238+
With {
239+
Reminder
240+
.none
241+
.where { !$0.isCompleted }
242+
.select { IncompleteReminder.Columns(isFlagged: $0.isFlagged, title: $0.title) }
243+
} query: {
244+
Reminder
245+
.delete()
246+
.returning(\.title)
247+
}
248+
) {
249+
"""
250+
251+
"""
252+
} results: {
253+
"""
254+
255+
"""
256+
}
257+
assertQuery(
258+
With {
259+
Reminder
260+
.none
261+
.where { !$0.isCompleted }
262+
.select { IncompleteReminder.Columns(isFlagged: $0.isFlagged, title: $0.title) }
263+
Reminder
264+
.where { !$0.isCompleted }
265+
.select { IncompleteReminder.Columns(isFlagged: $0.isFlagged, title: $0.title) }
266+
} query: {
267+
Reminder
268+
.none
269+
.delete()
270+
.returning(\.title)
271+
}
272+
) {
273+
"""
274+
275+
"""
276+
} results: {
277+
"""
278+
279+
"""
280+
}
281+
}
282+
216283
@Test func recursive() {
217284
assertQuery(
218285
With {

0 commit comments

Comments
 (0)