Skip to content

Commit 6426bec

Browse files
authored
Statement shouldn't inherit from Hashable (#203)
* `Statement` shouldn't inherit from `Hashable` We made `Statement: Hashable` for convenience in holding onto statements in a shared key for SQLiteData, but that isn't really necessary, since we can always call out to the underlying query fragment to get a hashable value. This conformance unfortunately can cause bugs, including `==` resolving to the equality operator when a query operator was expected. Another bug is that `@Table @Selection` types conform to `Statement`, and this equatable implementation was preferred over the default synthesized version, making `Table` values equatable strictly by the underlying query, which meant `@Ephemeral` fields weren't taken into account at all. With 0.20.0, all `@Table` applications introduced a `Statement` conformance, and so this issue becomes much more widespread. While this change can break SQLiteData if someone upgrades StructuredQueries before upgrading SQLiteData with the changes from pointfreeco/sqlite-data#245. * fix
1 parent abb5084 commit 6426bec

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// A type that represents a full SQL query.
2-
public protocol Statement<QueryValue>: QueryExpression, Hashable {
2+
public protocol Statement<QueryValue>: QueryExpression {
33
/// A type representing the table being queried.
44
associatedtype From: Table
55

@@ -15,13 +15,3 @@ extension Statement {
1515
"(\(.newline)\(query.indented())\(.newline))"
1616
}
1717
}
18-
19-
extension Statement {
20-
public static func == (lhs: Self, rhs: Self) -> Bool {
21-
lhs.query == rhs.query
22-
}
23-
24-
public func hash(into hasher: inout Hasher) {
25-
hasher.combine(query)
26-
}
27-
}

Tests/StructuredQueriesTests/EphemeralTests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ extension SnapshotTests {
1717
"""
1818
}
1919
}
20+
21+
@Test func equality() {
22+
#expect(TestTable(displayName: "Blob Jr") != TestTable(displayName: "Blob Sr"))
23+
}
2024
}
2125
}
2226

23-
@Table private struct TestTable {
27+
@Table private struct TestTable: Equatable {
2428
var firstName = ""
2529
var lastName = ""
2630
@Ephemeral

0 commit comments

Comments
 (0)