ObjectSchema.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import Foundation
  19. import Realm
  20. import Realm.Private
  21. /**
  22. This class represents Realm model object schemas.
  23. When using Realm, `ObjectSchema` instances allow performing migrations and introspecting the database's schema.
  24. Object schemas map to tables in the core database.
  25. */
  26. public struct ObjectSchema: CustomStringConvertible {
  27. // MARK: Properties
  28. internal let rlmObjectSchema: RLMObjectSchema
  29. /**
  30. An array of `Property` instances representing the managed properties of a class described by the schema.
  31. - see: `Property`
  32. */
  33. public var properties: [Property] {
  34. return rlmObjectSchema.properties.map { Property($0) }
  35. }
  36. /// The name of the class the schema describes.
  37. public var className: String { return rlmObjectSchema.className }
  38. /// The object class the schema describes.
  39. public var objectClass: AnyClass { return rlmObjectSchema.objectClass }
  40. /// The property which serves as the primary key for the class the schema describes, if any.
  41. public var primaryKeyProperty: Property? {
  42. if let rlmProperty = rlmObjectSchema.primaryKeyProperty {
  43. return Property(rlmProperty)
  44. }
  45. return nil
  46. }
  47. /// A human-readable description of the properties contained in the object schema.
  48. public var description: String { return rlmObjectSchema.description }
  49. // MARK: Initializers
  50. internal init(_ rlmObjectSchema: RLMObjectSchema) {
  51. self.rlmObjectSchema = rlmObjectSchema
  52. }
  53. // MARK: Property Retrieval
  54. /// Returns the property with the given name, if it exists.
  55. public subscript(propertyName: String) -> Property? {
  56. if let rlmProperty = rlmObjectSchema[propertyName] {
  57. return Property(rlmProperty)
  58. }
  59. return nil
  60. }
  61. }
  62. // MARK: Equatable
  63. extension ObjectSchema: Equatable {
  64. /// Returns whether the two object schemas are equal.
  65. public static func == (lhs: ObjectSchema, rhs: ObjectSchema) -> Bool {
  66. return lhs.rlmObjectSchema.isEqual(to: rhs.rlmObjectSchema)
  67. }
  68. }