Schema.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. `Schema` instances represent collections of model object schemas managed by a Realm.
  22. When using Realm, `Schema` instances allow performing migrations and introspecting the database's schema.
  23. Schemas map to collections of tables in the core database.
  24. */
  25. public struct Schema: CustomStringConvertible {
  26. // MARK: Properties
  27. internal let rlmSchema: RLMSchema
  28. /**
  29. An array of `ObjectSchema`s for all object types in the Realm.
  30. This property is intended to be used during migrations for dynamic introspection.
  31. */
  32. public var objectSchema: [ObjectSchema] {
  33. return rlmSchema.objectSchema.map(ObjectSchema.init)
  34. }
  35. /// A human-readable description of the object schemas contained within.
  36. public var description: String { return rlmSchema.description }
  37. // MARK: Initializers
  38. internal init(_ rlmSchema: RLMSchema) {
  39. self.rlmSchema = rlmSchema
  40. }
  41. // MARK: ObjectSchema Retrieval
  42. /// Looks up and returns an `ObjectSchema` for the given class name in the Realm, if it exists.
  43. public subscript(className: String) -> ObjectSchema? {
  44. if let rlmObjectSchema = rlmSchema.schema(forClassName: className) {
  45. return ObjectSchema(rlmObjectSchema)
  46. }
  47. return nil
  48. }
  49. }
  50. // MARK: Equatable
  51. extension Schema: Equatable {
  52. /// Returns whether the two schemas are equal.
  53. public static func == (lhs: Schema, rhs: Schema) -> Bool {
  54. return lhs.rlmSchema.isEqual(to: rhs.rlmSchema)
  55. }
  56. }