forked from Prosumma/CoreDataQueryInterface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.swift
More file actions
194 lines (157 loc) · 7.84 KB
/
Copy pathExtensions.swift
File metadata and controls
194 lines (157 loc) · 7.84 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
//
// Extensions.swift
// CoreDataQueryInterface
//
// Created by Gregory Higley on 9/25/16.
// Copyright © 2016 Prosumma LLC. All rights reserved.
//
import CoreData
import Foundation
extension NSManagedObjectContext {
public func from<M: NSManagedObject>(_ entity: M.Type = M.self) -> Query<M, M> where M: Entity {
return Query<M, M>().context(managedObjectContext: self)
}
public func cdqiNewEntity<M: NSManagedObject>(_ entity: M.Type = M.self, attributes: [String: Any] = [:], initialize: ((M) -> Void)? = nil) -> M {
let entity: M
if #available(iOS 10, macOS 10.12, tvOS 10, watchOS 3, *) {
entity = M(entity: M.entity(), insertInto: self)
} else {
let managedObjectModel = persistentStoreCoordinator!.managedObjectModel
entity = M(entity: M.cdqiEntity(managedObjectModel: managedObjectModel), insertInto: self)
}
entity.setValuesForKeys(attributes)
initialize?(entity)
return entity
}
}
extension ExpressionConvertible {
public func cdqiAverage(type: NSAttributeType? = nil) -> FunctionExpression {
return average(self, type: type)
}
public func cdqiAverage(alias name: String, type: NSAttributeType? = nil) -> PropertyConvertible {
return average(self, alias: name, type: type)
}
public func cdqiCount(type: NSAttributeType? = nil) -> FunctionExpression {
return count(self, type: type)
}
public func cdqiCount(alias name: String) -> PropertyConvertible {
return count(self, alias: name, type: .integer32AttributeType)
}
public func cdqiMin(type: NSAttributeType? = nil) -> FunctionExpression {
return min(self, type: type)
}
public func cdqiMin(alias name: String, type: NSAttributeType? = nil) -> PropertyConvertible {
return min(self, alias: name, type: type)
}
public func cdqiMax(type: NSAttributeType? = nil) -> FunctionExpression {
return max(self, type: type)
}
public func cdqiMax(alias name: String, type: NSAttributeType? = nil) -> PropertyConvertible {
return max(self, alias: name, type: type)
}
public func cdqiSum(type: NSAttributeType? = nil) -> FunctionExpression {
return sum(self, type: type)
}
public func cdqiSum(alias name: String, type: NSAttributeType? = nil) -> PropertyConvertible {
return sum(self, alias: name, type: type)
}
public func cdqiCompare(_ op: NSComparisonPredicate.Operator, _ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, op, rhs, options: options)
}
public func cdqiCompare(_ op: NSComparisonPredicate.Operator, _ rhs: Null, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, op, rhs, options: options)
}
public func cdqiEqualTo(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .equalTo, rhs, options: options)
}
public func cdqiEqualTo(_ rhs: Null, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .equalTo, rhs, options: options)
}
public func cdqiNotEqualTo(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .notEqualTo, rhs, options: options)
}
public func cdqiNotEqualTo(_ rhs: Null, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .notEqualTo, rhs, options: options)
}
public func cdqiLessThan(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .lessThan, rhs, options: options)
}
public func cdqiLessThanOrEqualTo(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .lessThanOrEqualTo, rhs, options: options)
}
public func cdqiGreaterThan(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .greaterThan, rhs, options: options)
}
public func cdqiGreaterThanOrEqualTo(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .greaterThanOrEqualTo, rhs, options: options)
}
public func cdqiAmong<R: Sequence>(_ rhs: R, options: NSComparisonPredicate.Options = []) -> NSPredicate where R.Iterator.Element: ExpressionConvertible {
let re = NSExpression(forAggregate: rhs.map{ $0.cdqiExpression })
return compare(self, .in, re, options: options)
}
public func cdqiBetween(_ lhs: ExpressionConvertible, and rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return between(self, lhs, and: rhs, options: options)
}
public func cdqiContains(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .contains, rhs, options: options)
}
public func cdqiLike(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .like, rhs, options: options)
}
public func cdqiBeginsWith(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .beginsWith, rhs, options: options)
}
public func cdqiEndsWith(_ rhs: ExpressionConvertible, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return compare(self, .endsWith, rhs, options: options)
}
public func cdqiMatches(_ rhs: String, options: NSComparisonPredicate.Options = []) -> NSPredicate {
return matches(self, rhs, options: options)
}
public func cdqiAlias(name: String, type: NSAttributeType) -> PropertyConvertible {
let property = NSExpressionDescription()
property.expression = cdqiExpression
property.name = name
property.expressionResultType = type
return property
}
public func cdqiAlias(name: String) -> PropertyConvertible {
var type: NSAttributeType = .undefinedAttributeType
if let typed = self as? Typed {
type = typed.cdqiType
}
return alias(self, name: name, type: type)
}
}
extension KeyPathExpressionConvertible {
public func cdqiAlias(type: NSAttributeType) -> PropertyConvertible {
return alias(self, name: cdqiName, type: type)
}
}
/**
Returns the name of the entity corresponding to the current class.
- parameter aClass: The class for which to retrieve the entity
- parameter managedObjectModel: The managed object model in which to look for the entity.
- returns: The name of the entity.
*/
public func entityNameForClass(aClass: AnyClass, inManagedObjectModel managedObjectModel: NSManagedObjectModel) -> String? {
struct Static {
static var entityCache = [String: String]()
static let entityCacheQueue = DispatchQueue(label: "com.prosumma.coredataqueryinterface.EntityCacheQueue")
}
let className = String(validatingUTF8: class_getName(aClass))!
var entityName: String?
Static.entityCacheQueue.sync {
entityName = Static.entityCache[className]
if entityName == nil {
entityName = managedObjectModel.entities.filter{ $0.managedObjectClassName == className }.first?.name
Static.entityCache[className] = entityName
}
}
return entityName
}
extension NSManagedObject {
public static func cdqiEntity(managedObjectModel: NSManagedObjectModel) -> NSEntityDescription {
let entityName = entityNameForClass(aClass: self, inManagedObjectModel: managedObjectModel)!
return managedObjectModel.entitiesByName[entityName]!
}
}