ObjectSchema.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. @frozen 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. /// Whether this object is embedded.
  41. public var isEmbedded: Bool { return rlmObjectSchema.isEmbedded }
  42. /// Whether this object is asymmetric.
  43. public var isAsymmetric: Bool { return rlmObjectSchema.isAsymmetric }
  44. /// The property which serves as the primary key for the class the schema describes, if any.
  45. public var primaryKeyProperty: Property? {
  46. if let rlmProperty = rlmObjectSchema.primaryKeyProperty {
  47. return Property(rlmProperty)
  48. }
  49. return nil
  50. }
  51. /// A human-readable description of the properties contained in the object schema.
  52. public var description: String { return rlmObjectSchema.description }
  53. // MARK: Initializers
  54. internal init(_ rlmObjectSchema: RLMObjectSchema) {
  55. self.rlmObjectSchema = rlmObjectSchema
  56. }
  57. // MARK: Property Retrieval
  58. /// Returns the property with the given name, if it exists.
  59. public subscript(propertyName: String) -> Property? {
  60. if let rlmProperty = rlmObjectSchema[propertyName] {
  61. return Property(rlmProperty)
  62. }
  63. return nil
  64. }
  65. }
  66. // MARK: Equatable
  67. extension ObjectSchema: Equatable {
  68. /// Returns whether the two object schemas are equal.
  69. public static func == (lhs: ObjectSchema, rhs: ObjectSchema) -> Bool {
  70. return lhs.rlmObjectSchema.isEqual(to: rhs.rlmObjectSchema)
  71. }
  72. }