ObjectSchema.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /**
  21. This class represents Realm model object schemas.
  22. When using Realm, `ObjectSchema` instances allow performing migrations and introspecting the database's schema.
  23. Object schemas map to tables in the core database.
  24. */
  25. public struct ObjectSchema: CustomStringConvertible {
  26. // MARK: Properties
  27. internal let rlmObjectSchema: RLMObjectSchema
  28. /**
  29. An array of `Property` instances representing the managed properties of a class described by the schema.
  30. - see: `Property`
  31. */
  32. public var properties: [Property] {
  33. return rlmObjectSchema.properties.map { Property($0) }
  34. }
  35. /// The name of the class the schema describes.
  36. public var className: String { return rlmObjectSchema.className }
  37. /// The property which serves as the primary key for the class the schema describes, if any.
  38. public var primaryKeyProperty: Property? {
  39. if let rlmProperty = rlmObjectSchema.primaryKeyProperty {
  40. return Property(rlmProperty)
  41. }
  42. return nil
  43. }
  44. /// A human-readable description of the properties contained in the object schema.
  45. public var description: String { return rlmObjectSchema.description }
  46. // MARK: Initializers
  47. internal init(_ rlmObjectSchema: RLMObjectSchema) {
  48. self.rlmObjectSchema = rlmObjectSchema
  49. }
  50. // MARK: Property Retrieval
  51. /// Returns the property with the given name, if it exists.
  52. public subscript(propertyName: String) -> Property? {
  53. if let rlmProperty = rlmObjectSchema[propertyName] {
  54. return Property(rlmProperty)
  55. }
  56. return nil
  57. }
  58. }
  59. // MARK: Equatable
  60. extension ObjectSchema: Equatable {
  61. /// Returns whether the two object schemas are equal.
  62. public static func == (lhs: ObjectSchema, rhs: ObjectSchema) -> Bool {
  63. return lhs.rlmObjectSchema.isEqual(to: rhs.rlmObjectSchema)
  64. }
  65. }