Skip to content

Commit 9fcd5a1

Browse files
authored
Merge branch 'main' into empty-queries
2 parents 64a48bb + 4f50c39 commit 9fcd5a1

File tree

5 files changed

+153
-6
lines changed

5 files changed

+153
-6
lines changed

Sources/StructuredQueriesCore/Statements/Delete.swift

Lines changed: 1 addition & 1 deletion
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(isEmpty: false)
11+
Where().delete()
1212
}
1313
}
1414

Sources/StructuredQueriesCore/Statements/Update.swift

Lines changed: 1 addition & 1 deletion
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(isEmpty: false, conflictResolution: conflictResolution, updates: Updates(updates))
40+
Where().update(or: conflictResolution, set: updates)
4141
}
4242
}
4343

Sources/StructuredQueriesCore/Statements/Where.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ extension Where: SelectStatement {
365365
/// - Parameter other: Another where clause.
366366
/// - Returns: A where clause that `AND`s the given where clauses together.
367367
public func and(_ other: Self) -> Self {
368+
guard !predicates.isEmpty else { return other }
369+
guard !other.predicates.isEmpty else { return self }
368370
var `where` = self
369371
`where`.predicates = [
370372
"""
@@ -381,6 +383,8 @@ extension Where: SelectStatement {
381383
/// - Parameter other: Another where clause.
382384
/// - Returns: A where clause that `OR`s the given where clauses together.
383385
public func or(_ other: Self) -> Self {
386+
guard !predicates.isEmpty else { return other }
387+
guard !other.predicates.isEmpty else { return self }
384388
var `where` = self
385389
`where`.predicates = [
386390
"""
@@ -397,7 +401,9 @@ extension Where: SelectStatement {
397401
/// - Returns: A where clause that `NOT`s this where clause.
398402
public func not() -> Self {
399403
var `where` = self
400-
`where`.predicates = ["NOT (\(`where`.predicates.joined(separator: " AND ")))"]
404+
`where`.predicates = [
405+
"NOT (\(predicates.isEmpty ? "1" : predicates.joined(separator: " AND ")))"
406+
]
401407
return `where`
402408
}
403409

Tests/StructuredQueriesTests/TableTests.swift

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ extension SnapshotTests {
129129
└─────────────────────────────────────────────┘
130130
"""
131131
}
132+
assertQuery(
133+
Row
134+
.delete()
135+
.where { $0.id > 0 }
136+
.returning(\.self)
137+
) {
138+
"""
139+
DELETE FROM "rows"
140+
WHERE NOT ("rows"."isDeleted") AND ("rows"."id" > 0)
141+
RETURNING "id", "isDeleted"
142+
"""
143+
} results: {
144+
"""
145+
146+
"""
147+
}
132148

133149
assertQuery(
134150
Row
@@ -157,8 +173,8 @@ extension SnapshotTests {
157173
@Test func update() throws {
158174
assertQuery(
159175
Row
160-
.where { $0.id > 0 }
161176
.update { $0.isDeleted.toggle() }
177+
.where { $0.id > 0 }
162178
.returning(\.self)
163179
) {
164180
"""
@@ -177,6 +193,23 @@ extension SnapshotTests {
177193
└─────────────────────────────────────────────┘
178194
"""
179195
}
196+
assertQuery(
197+
Row
198+
.where { $0.id > 0 }
199+
.update { $0.isDeleted.toggle() }
200+
.returning(\.self)
201+
) {
202+
"""
203+
UPDATE "rows"
204+
SET "isDeleted" = NOT ("rows"."isDeleted")
205+
WHERE NOT ("rows"."isDeleted") AND ("rows"."id" > 0)
206+
RETURNING "id", "isDeleted"
207+
"""
208+
} results: {
209+
"""
210+
211+
"""
212+
}
180213

181214
assertQuery(
182215
Row
@@ -386,8 +419,8 @@ extension SnapshotTests {
386419
@Test func delete() throws {
387420
assertQuery(
388421
Row
389-
.where { $0.id > 0 }
390422
.delete()
423+
.where { $0.id > 0 }
391424
.returning(\.self)
392425
) {
393426
"""
@@ -405,6 +438,22 @@ extension SnapshotTests {
405438
└────────────────────────────────────────────┘
406439
"""
407440
}
441+
assertQuery(
442+
Row
443+
.where { $0.id > 0 }
444+
.delete()
445+
.returning(\.self)
446+
) {
447+
"""
448+
DELETE FROM "rows"
449+
WHERE NOT ("rows"."isDeleted") AND ("rows"."id" > 0)
450+
RETURNING "id", "isDeleted"
451+
"""
452+
} results: {
453+
"""
454+
455+
"""
456+
}
408457

409458
assertQuery(
410459
Row
@@ -433,8 +482,8 @@ extension SnapshotTests {
433482
@Test func update() throws {
434483
assertQuery(
435484
Row
436-
.where { $0.id > 0 }
437485
.update { $0.isDeleted.toggle() }
486+
.where { $0.id > 0 }
438487
.returning(\.self)
439488
) {
440489
"""
@@ -453,6 +502,23 @@ extension SnapshotTests {
453502
└────────────────────────────────────────────┘
454503
"""
455504
}
505+
assertQuery(
506+
Row
507+
.where { $0.id > 0 }
508+
.update { $0.isDeleted.toggle() }
509+
.returning(\.self)
510+
) {
511+
"""
512+
UPDATE "rows"
513+
SET "isDeleted" = NOT ("rows"."isDeleted")
514+
WHERE NOT ("rows"."isDeleted") AND ("rows"."id" > 0)
515+
RETURNING "id", "isDeleted"
516+
"""
517+
} results: {
518+
"""
519+
520+
"""
521+
}
456522

457523
assertQuery(
458524
Row

Tests/StructuredQueriesTests/WhereTests.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@ extension SnapshotTests {
4040
└───┘
4141
"""
4242
}
43+
assertQuery(
44+
Reminder.all.and(Reminder.where(\.isFlagged)).count()
45+
) {
46+
"""
47+
SELECT count(*)
48+
FROM "reminders"
49+
WHERE "reminders"."isFlagged"
50+
"""
51+
} results: {
52+
"""
53+
┌───┐
54+
│ 2 │
55+
└───┘
56+
"""
57+
}
58+
assertQuery(
59+
Reminder.where(\.isFlagged).and(Reminder.all).count()
60+
) {
61+
"""
62+
SELECT count(*)
63+
FROM "reminders"
64+
WHERE "reminders"."isFlagged"
65+
"""
66+
} results: {
67+
"""
68+
┌───┐
69+
│ 2 │
70+
└───┘
71+
"""
72+
}
4373
}
4474

4575
@Test(.snapshots(record: .never)) func emptyResults() {
@@ -93,6 +123,36 @@ extension SnapshotTests {
93123
└───┘
94124
"""
95125
}
126+
assertQuery(
127+
Reminder.all.or(Reminder.where(\.isFlagged)).count()
128+
) {
129+
"""
130+
SELECT count(*)
131+
FROM "reminders"
132+
WHERE "reminders"."isFlagged"
133+
"""
134+
} results: {
135+
"""
136+
┌───┐
137+
│ 2 │
138+
└───┘
139+
"""
140+
}
141+
assertQuery(
142+
Reminder.where(\.isFlagged).or(Reminder.all).count()
143+
) {
144+
"""
145+
SELECT count(*)
146+
FROM "reminders"
147+
WHERE "reminders"."isFlagged"
148+
"""
149+
} results: {
150+
"""
151+
┌───┐
152+
│ 2 │
153+
└───┘
154+
"""
155+
}
96156
}
97157

98158
@Test func not() {
@@ -128,6 +188,21 @@ extension SnapshotTests {
128188
└───┘
129189
"""
130190
}
191+
assertQuery(
192+
Reminder.all.not().count()
193+
) {
194+
"""
195+
SELECT count(*)
196+
FROM "reminders"
197+
WHERE NOT (1)
198+
"""
199+
} results: {
200+
"""
201+
┌───┐
202+
│ 0 │
203+
└───┘
204+
"""
205+
}
131206
}
132207

133208
@Test func optionalBoolean() throws {

0 commit comments

Comments
 (0)