mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-21 17:09:42 +01:00
support compound indexes and unique constraints on CoreStore properties
This commit is contained in:
@@ -292,7 +292,6 @@ public final class CoreStoreSchema: DynamicSchema {
|
||||
description.name = attribute.keyPath
|
||||
description.attributeType = Swift.type(of: attribute).attributeType
|
||||
description.isOptional = attribute.isOptional
|
||||
description.isIndexed = attribute.isIndexed
|
||||
description.defaultValue = attribute.defaultValue()
|
||||
description.isTransient = attribute.isTransient
|
||||
description.allowsExternalBinaryDataStorage = attribute.allowsExternalBinaryDataStorage
|
||||
@@ -442,6 +441,53 @@ public final class CoreStoreSchema: DynamicSchema {
|
||||
entityDescription: entityDescription
|
||||
)
|
||||
}
|
||||
for (entity, entityDescription) in entityDescriptionsByEntity {
|
||||
|
||||
let uniqueConstraints = entity.uniqueConstraints.filter({ !$0.isEmpty })
|
||||
if !uniqueConstraints.isEmpty {
|
||||
|
||||
CoreStore.assert(
|
||||
entityDescription.superentity == nil,
|
||||
"Uniqueness constraints must be defined at the highest level possible."
|
||||
)
|
||||
entityDescription.uniquenessConstraints = entity.uniqueConstraints.map { $0.map { $0 as NSString } }
|
||||
}
|
||||
guard !entity.indexes.isEmpty else {
|
||||
|
||||
continue
|
||||
}
|
||||
defer {
|
||||
|
||||
entityDescription.coreStoreEntity = entity // reserialize
|
||||
}
|
||||
let attributesByName = entityDescription.attributesByName
|
||||
if #available(iOS 11.0, OSX 10.13, watchOS 4.0, tvOS 11.0, *) {
|
||||
|
||||
entityDescription.indexes = entity.indexes.map { (compoundIndexes) in
|
||||
|
||||
return NSFetchIndexDescription.init(
|
||||
name: "_CoreStoreSchema_indexes_\(entityDescription.name!)_\(compoundIndexes.joined(separator: "-"))",
|
||||
elements: compoundIndexes.map { (keyPath) in
|
||||
|
||||
return NSFetchIndexElementDescription(
|
||||
property: attributesByName[keyPath]!,
|
||||
collationType: .binary
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
entityDescription.compoundIndexes = entity.indexes.map { (compoundIndexes) in
|
||||
|
||||
return compoundIndexes.map { (keyPath) in
|
||||
|
||||
return attributesByName[keyPath]!
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static func fourthPassSynthesizeManagedObjectClasses(for entityDescriptionsByEntity: [DynamicEntity: NSEntityDescription], allCustomGettersSetters: [DynamicEntity: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter]]) {
|
||||
|
||||
Reference in New Issue
Block a user