Skip to content

Commit 11e7df9

Browse files
committed
cleanup
1 parent 23e1072 commit 11e7df9

4 files changed

Lines changed: 34 additions & 35 deletions

File tree

Sources/SQLiteData/StructuredQueries+GRDB/CustomFunctions.swift

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@ extension Database {
2020
Unmanaged.passRetained(ScalarDatabaseFunctionDefinition(function)).toOpaque(),
2121
{ context, argumentCount, arguments in
2222
do {
23-
let function = Unmanaged<ScalarDatabaseFunctionDefinition>
23+
let definition = Unmanaged<ScalarDatabaseFunctionDefinition>
2424
.fromOpaque(sqlite3_user_data(context))
2525
.takeUnretainedValue()
26-
.function
27-
var decoder = SQLiteFunctionDecoder(
28-
name: function.name,
29-
argumentCount: argumentCount,
30-
arguments: arguments
31-
)
32-
try function
33-
.invoke(&decoder)
26+
definition.decoder.reset(argumentCount: argumentCount, arguments: arguments)
27+
try definition.function
28+
.invoke(&definition.decoder)
3429
.result(db: context)
3530
} catch {
3631
QueryBinding.invalid(error).result(db: context)
@@ -59,13 +54,9 @@ extension Database {
5954
nil,
6055
{ context, argumentCount, arguments in
6156
let function = AggregateDatabaseFunctionContext[context].takeUnretainedValue()
62-
var decoder = SQLiteFunctionDecoder(
63-
name: function.iterator.body.name,
64-
argumentCount: argumentCount,
65-
arguments: arguments
66-
)
57+
function.decoder.reset(argumentCount: argumentCount, arguments: arguments)
6758
do {
68-
try function.iterator.step(&decoder)
59+
try function.iterator.step(&function.decoder)
6960
} catch {
7061
sqlite3_result_error(context, error.localizedDescription, -1)
7162
}
@@ -118,8 +109,10 @@ extension DatabaseFunction {
118109

119110
private final class ScalarDatabaseFunctionDefinition {
120111
let function: any ScalarDatabaseFunction
112+
var decoder: SQLiteFunctionDecoder
121113
init(_ function: some ScalarDatabaseFunction) {
122114
self.function = function
115+
self.decoder = SQLiteFunctionDecoder(name: function.name)
123116
}
124117
}
125118

@@ -152,8 +145,10 @@ private final class AggregateDatabaseFunctionContext {
152145
}
153146
}
154147
let iterator: any AggregateDatabaseFunctionIteratorProtocol
148+
var decoder: SQLiteFunctionDecoder
155149
init(_ body: some AggregateDatabaseFunction) {
156150
self.iterator = AggregateDatabaseFunctionIterator(body)
151+
self.decoder = SQLiteFunctionDecoder(name: body.name)
157152
}
158153
}
159154

Sources/SQLiteData/StructuredQueries+GRDB/Decoding.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import GRDBSQLite
22

3-
#if !StrictDecoding
4-
import ConcurrencyExtras
5-
6-
let reportedTypeMismatches = LockIsolated<Set<String>>([])
7-
#endif
8-
93
@usableFromInline
104
func storageClassName(_ type: Int32) -> String {
115
switch type {

Sources/SQLiteData/StructuredQueries+GRDB/SQLiteFunctionDecoder.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ public import GRDBSQLite
33
public import StructuredQueriesCore
44

55
#if !StrictDecoding
6-
import ConcurrencyExtras
76
import IssueReporting
87
#endif
98

@@ -13,19 +12,29 @@ struct SQLiteFunctionDecoder: QueryDecoder {
1312
let name: String
1413

1514
@usableFromInline
16-
let argumentCount: Int32
15+
var argumentCount: Int32 = 0
1716

1817
@usableFromInline
19-
let arguments: UnsafeMutablePointer<OpaquePointer?>?
18+
var arguments: UnsafeMutablePointer<OpaquePointer?>?
2019

2120
@usableFromInline
2221
var currentIndex: Int32 = 0
2322

23+
#if !StrictDecoding
24+
@usableFromInline
25+
var reportedTypeMismatches: Set<Int32> = []
26+
#endif
27+
2428
@usableFromInline
25-
init(name: String, argumentCount: Int32, arguments: UnsafeMutablePointer<OpaquePointer?>?) {
29+
init(name: String) {
2630
self.name = name
31+
}
32+
33+
@usableFromInline
34+
mutating func reset(argumentCount: Int32, arguments: UnsafeMutablePointer<OpaquePointer?>?) {
2735
self.argumentCount = argumentCount
2836
self.arguments = arguments
37+
self.currentIndex = 0
2938
}
3039

3140
@inlinable
@@ -141,12 +150,11 @@ struct SQLiteFunctionDecoder: QueryDecoder {
141150
}
142151

143152
@usableFromInline
144-
func reportTypeMismatch(_ columnType: Any.Type) throws(QueryDecodingError) {
153+
mutating func reportTypeMismatch(_ columnType: Any.Type) throws(QueryDecodingError) {
145154
#if StrictDecoding
146155
throw QueryDecodingError.typeMismatch(columnType)
147156
#else
148-
let key = "\(currentIndex)|\(name)"
149-
guard reportedTypeMismatches.withValue({ $0.insert(key).inserted })
157+
guard reportedTypeMismatches.insert(currentIndex).inserted
150158
else { return }
151159
let value = arguments?[Int(currentIndex)]
152160
reportIssue(

Sources/SQLiteData/StructuredQueries+GRDB/SQLiteQueryDecoder.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ public import GRDBSQLite
33
public import StructuredQueriesCore
44

55
#if !StrictDecoding
6-
import ConcurrencyExtras
76
import IssueReporting
87
#endif
98

@@ -15,6 +14,11 @@ struct SQLiteQueryDecoder: QueryDecoder {
1514
@usableFromInline
1615
var currentIndex: Int32 = 0
1716

17+
#if !StrictDecoding
18+
@usableFromInline
19+
var reportedTypeMismatches: Set<Int32> = []
20+
#endif
21+
1822
@usableFromInline
1923
init(statement: OpaquePointer) {
2024
self.statement = statement
@@ -125,24 +129,22 @@ struct SQLiteQueryDecoder: QueryDecoder {
125129
}
126130

127131
@usableFromInline
128-
func reportTypeMismatch(_ columnType: Any.Type) throws(QueryDecodingError) {
132+
mutating func reportTypeMismatch(_ columnType: Any.Type) throws(QueryDecodingError) {
129133
#if StrictDecoding
130134
throw QueryDecodingError.typeMismatch(columnType)
131135
#else
132-
let sql = sqlite3_sql(statement).map { String(cString: $0) } ?? ""
133-
let key = "\(currentIndex)|\(sql)"
134-
guard reportedTypeMismatches.withValue({ $0.insert(key).inserted })
136+
guard reportedTypeMismatches.insert(currentIndex).inserted
135137
else { return }
136138
let columnName =
137139
sqlite3_column_name(statement, currentIndex)
138140
.map { " (\(String(cString: $0).debugDescription))" }
139141
?? ""
140142
reportIssue(
141143
"""
142-
Expected column \(currentIndex)\(columnName ?? "") to decode \(columnType), but found \
144+
Expected column \(currentIndex)\(columnName) to decode \(columnType), but found \
143145
\(storageClassName(sqlite3_column_type(statement, currentIndex))): ...
144146
145-
\(sql)
147+
\(sqlite3_sql(statement).map { String(cString: $0) } ?? "")
146148
"""
147149
)
148150
#endif

0 commit comments

Comments
 (0)