Repository navigation
Expand file tree
/
Copy pathPredicate.swift
More file actions
348 lines (318 loc) · 14.1 KB
/
Copy pathPredicate.swift
File metadata and controls
348 lines (318 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// Predicate.swift
// CoreModel-SQLite
//
// Created by Alsey Coleman Miller on 7/4/25.
//
import Foundation
import CoreModel
import SQLite
/// A SQL fragment with its positional bindings.
internal struct SQLFragment {
var sql: String
var bindings: [Binding?]
}
internal extension FetchRequest.Predicate {
/// Translate the predicate into a SQL `WHERE` clause fragment.
func sqlFragment(for entity: EntityDescription, model: Model) throws -> SQLFragment {
switch self {
case let .value(value):
return SQLFragment(sql: value ? "1" : "0", bindings: [])
case let .compound(compound):
return try compound.sqlFragment(for: entity, model: model)
case let .comparison(comparison):
return try comparison.sqlFragment(for: entity, model: model, predicate: self)
}
}
}
internal extension FetchRequest.Predicate.Compound {
func sqlFragment(for entity: EntityDescription, model: Model) throws -> SQLFragment {
switch self {
case let .and(subpredicates):
return try .joined(subpredicates, separator: " AND ", entity: entity, model: model)
case let .or(subpredicates):
return try .joined(subpredicates, separator: " OR ", entity: entity, model: model)
case let .not(subpredicate):
let fragment = try subpredicate.sqlFragment(for: entity, model: model)
return SQLFragment(sql: "NOT (" + fragment.sql + ")", bindings: fragment.bindings)
}
}
}
private extension SQLFragment {
static func joined(
_ predicates: [FetchRequest.Predicate],
separator: String,
entity: EntityDescription,
model: Model
) throws -> SQLFragment {
guard predicates.isEmpty == false else {
return SQLFragment(sql: "1", bindings: [])
}
let fragments = try predicates.map { try $0.sqlFragment(for: entity, model: model) }
return SQLFragment(
sql: "(" + fragments.map(\.sql).joined(separator: separator) + ")",
bindings: fragments.flatMap(\.bindings)
)
}
}
internal extension FetchRequest.Predicate.Comparison {
func sqlFragment(
for entity: EntityDescription,
model: Model,
predicate: FetchRequest.Predicate
) throws -> SQLFragment {
// `function(...) <operator> constant` comparisons compile to a SQL function call.
if case let .function(function) = left {
guard modifier == nil else {
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
let functionFragment = try function.sqlFragment(for: entity, predicate: predicate)
switch type {
case .lessThan, .lessThanOrEqualTo, .greaterThan, .greaterThanOrEqualTo:
let value = try right.constantBinding(predicate: predicate)
return SQLFragment(
sql: "\(functionFragment.sql) \(type.rawValue) ?",
bindings: functionFragment.bindings + [value]
)
case .equalTo, .notEqualTo:
let value = try right.constantBinding(predicate: predicate)
let sqlOperator = (type == .equalTo) ? "=" : "<>"
guard let value else {
let nullOperator = (type == .equalTo) ? "IS NULL" : "IS NOT NULL"
return SQLFragment(sql: "\(functionFragment.sql) \(nullOperator)", bindings: functionFragment.bindings)
}
return SQLFragment(
sql: "\(functionFragment.sql) \(sqlOperator) ?",
bindings: functionFragment.bindings + [value]
)
default:
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
}
// Only `keyPath <operator> constant` comparisons map directly to columns.
guard case let .keyPath(keyPath) = left else {
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
let property = PropertyKey(rawValue: keyPath.rawValue)
// to-many relationships are not columns; translate to a membership subquery
if let relationship = entity.relationships.first(where: { $0.id == property && $0.type == .toMany }) {
return try toManySQLFragment(
relationship,
entity: entity,
model: model,
predicate: predicate
)
}
guard modifier == nil else {
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
let column = try entity.validateColumn(
property,
predicate: predicate
)
switch type {
case .lessThan, .lessThanOrEqualTo, .greaterThan, .greaterThanOrEqualTo:
let value = try right.constantBinding(predicate: predicate)
return SQLFragment(sql: "\(column) \(type.rawValue) ?", bindings: [value])
case .equalTo, .notEqualTo:
let value = try right.constantBinding(predicate: predicate)
let sqlOperator = (type == .equalTo) ? "=" : "<>"
guard let value else {
let nullOperator = (type == .equalTo) ? "IS NULL" : "IS NOT NULL"
return SQLFragment(sql: "\(column) \(nullOperator)", bindings: [])
}
let collation = options.contains(.caseInsensitive) ? " COLLATE NOCASE" : ""
return SQLFragment(sql: "\(column) \(sqlOperator) ?\(collation)", bindings: [value])
case .beginsWith:
let pattern = try right.likePattern(predicate: predicate)
return .like(column: column, pattern: pattern + "%")
case .endsWith:
let pattern = try right.likePattern(predicate: predicate)
return .like(column: column, pattern: "%" + pattern)
case .contains:
let pattern = try right.likePattern(predicate: predicate)
return .like(column: column, pattern: "%" + pattern + "%")
case .like:
// Translate Cocoa-style wildcards (`*`, `?`) to SQL (`%`, `_`).
let pattern = try right.likePattern(predicate: predicate)
.replacingOccurrences(of: "*", with: "%")
.replacingOccurrences(of: "?", with: "_")
return .like(column: column, pattern: pattern)
case .in:
let values = try right.constantBindings(predicate: predicate)
guard values.isEmpty == false else {
return SQLFragment(sql: "0", bindings: [])
}
let placeholders = repeatElement("?", count: values.count).joined(separator: ", ")
return SQLFragment(sql: "\(column) IN (\(placeholders))", bindings: values)
case .between:
let values = try right.constantBindings(predicate: predicate)
guard values.count == 2 else {
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
return SQLFragment(sql: "\(column) BETWEEN ? AND ?", bindings: values)
case .matches:
// Regular expressions require a custom SQL function.
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
}
/// Translate a comparison against a to-many relationship into a membership subquery.
///
/// Matches CoreData's `ANY <relationship> IN <objects>` / `ANY <relationship> == <object>`
/// semantics: the row matches if at least one linked destination object is in the given
/// set. One-to-many relationships resolve through the inverse foreign key on the
/// destination table; many-to-many through the join table.
private func toManySQLFragment(
_ relationship: Relationship,
entity: EntityDescription,
model: Model,
predicate: FetchRequest.Predicate
) throws -> SQLFragment {
// `.any` is the only modifier with a direct SQL translation (`.all` would
// require set equality); no modifier is treated as `.any`, like NSPredicate.
guard modifier == nil || modifier == .any else {
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
let destinationIDs: [Binding?]
switch type {
case .in:
destinationIDs = try right.constantBindings(predicate: predicate)
case .equalTo, .contains:
destinationIDs = [try right.constantBinding(predicate: predicate)]
default:
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
guard destinationIDs.isEmpty == false else {
return SQLFragment(sql: "0", bindings: [])
}
let placeholders = repeatElement("?", count: destinationIDs.count).joined(separator: ", ")
let primaryKey = SQLiteDatabase.primaryKeyColumn.quotedIdentifier
switch try model.inverseType(of: relationship) {
case .toOne:
// one-to-many: the destination table holds the foreign key back to this entity
let destinationTable = relationship.destinationEntity.rawValue.quotedIdentifier
let foreignKey = relationship.inverseRelationship.rawValue.quotedIdentifier
return SQLFragment(
sql: "\(primaryKey) IN (SELECT \(foreignKey) FROM \(destinationTable) WHERE \(primaryKey) IN (\(placeholders)))",
bindings: destinationIDs
)
case .toMany:
let joinTable = JoinTable(entity: entity.id, relationship: relationship)
let table = joinTable.name.quotedIdentifier
let thisColumn = joinTable.thisColumn.quotedIdentifier
let otherColumn = joinTable.otherColumn.quotedIdentifier
var sql = "\(primaryKey) IN (SELECT \(thisColumn) FROM \(table) WHERE \(otherColumn) IN (\(placeholders))"
var bindings = destinationIDs
if joinTable.isSymmetric {
sql += " UNION SELECT \(otherColumn) FROM \(table) WHERE \(thisColumn) IN (\(placeholders))"
bindings += destinationIDs
}
sql += ")"
return SQLFragment(sql: sql, bindings: bindings)
}
}
}
private extension SQLFragment {
static func like(column: String, pattern: String) -> SQLFragment {
SQLFragment(sql: "\(column) LIKE ? ESCAPE '\\'", bindings: [pattern])
}
}
internal extension FetchRequest.Predicate.FunctionExpression {
/// Translate a function call expression into a SQL function-call fragment,
/// e.g. `myFunction("lat", "lon", ?, ?)`.
func sqlFragment(
for entity: EntityDescription,
predicate: FetchRequest.Predicate
) throws -> SQLFragment {
let argumentFragments = try arguments.map {
try $0.argumentSQLFragment(for: entity, predicate: predicate)
}
return SQLFragment(
sql: "\(name)(" + argumentFragments.map(\.sql).joined(separator: ", ") + ")",
bindings: argumentFragments.flatMap(\.bindings)
)
}
}
private extension FetchRequest.Predicate.Expression {
/// The expression as a SQL fragment suitable for use as a function argument
/// (a column reference, a constant placeholder, or a nested function call).
func argumentSQLFragment(
for entity: EntityDescription,
predicate: FetchRequest.Predicate
) throws -> SQLFragment {
switch self {
case let .keyPath(keyPath):
let column = try entity.validateColumn(PropertyKey(rawValue: keyPath.rawValue), predicate: predicate)
return SQLFragment(sql: column, bindings: [])
case let .function(function):
return try function.sqlFragment(for: entity, predicate: predicate)
case .attribute, .relationship:
return SQLFragment(sql: "?", bindings: [try constantBinding(predicate: predicate)])
}
}
/// The expression as a single constant binding.
func constantBinding(predicate: FetchRequest.Predicate) throws -> Binding? {
switch self {
case let .attribute(value):
return value.binding
case let .relationship(value):
switch value {
case .null:
return nil
case let .toOne(objectID):
return objectID.rawValue
case .toMany:
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
case .keyPath, .function:
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
}
/// The expression as a list of constant bindings (for `IN` and `BETWEEN`).
func constantBindings(predicate: FetchRequest.Predicate) throws -> [Binding?] {
switch self {
case let .relationship(.toMany(objectIDs)):
return objectIDs.map { $0.rawValue }
default:
return [try constantBinding(predicate: predicate)]
}
}
/// The expression as a `LIKE` pattern, escaping SQL wildcard characters.
func likePattern(predicate: FetchRequest.Predicate) throws -> String {
guard case let .attribute(.string(value)) = self else {
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
return value
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "%", with: "\\%")
.replacingOccurrences(of: "_", with: "\\_")
}
}
internal extension EntityDescription {
/// Validate that the key path refers to a column of this entity's table
/// and return the quoted column name.
func validateColumn(
_ property: PropertyKey,
predicate: FetchRequest.Predicate
) throws -> String {
guard hasColumn(for: property) else {
throw SQLiteDatabaseError.invalidPredicate(predicate)
}
return property.rawValue.quotedIdentifier
}
func hasColumn(for property: PropertyKey) -> Bool {
if property.rawValue == SQLiteDatabase.primaryKeyColumn {
return true
}
if attributes.contains(where: { $0.id == property }) {
return true
}
return relationships.contains(where: { $0.id == property && $0.type == .toOne })
}
}
internal extension String {
/// Quote as a SQL identifier.
var quotedIdentifier: String {
"\"" + replacingOccurrences(of: "\"", with: "\"\"") + "\""
}
}