QueryPredicate.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if SWIFT_PACKAGE
  17. @_exported import FirebaseFirestoreInternalWrapper
  18. #else
  19. @_exported import FirebaseFirestoreInternal
  20. #endif // SWIFT_PACKAGE
  21. /// Query predicates that can be used to filter results fetched by `FirestoreQuery`.
  22. ///
  23. /// Construct predicates using one of the following ways:
  24. ///
  25. /// let onlyFavourites: QueryPredicate = .whereField("isFavourite", isEqualTo: true)
  26. /// let onlyFavourites2: QueryPredicate = .isEqualTo("isFavourite", true)
  27. /// let onlyFavourites3: QueryPredicate = .where("isFavourite", isEqualTo: true)
  28. public enum QueryPredicate {
  29. case isEqualTo(_ field: String, _ value: Any)
  30. case isIn(_ field: String, _ values: [Any])
  31. case isNotIn(_ field: String, _ values: [Any])
  32. case arrayContains(_ field: String, _ value: Any)
  33. case arrayContainsAny(_ field: String, _ values: [Any])
  34. case isLessThan(_ field: String, _ value: Any)
  35. case isGreaterThan(_ field: String, _ value: Any)
  36. case isLessThanOrEqualTo(_ field: String, _ value: Any)
  37. case isGreaterThanOrEqualTo(_ field: String, _ value: Any)
  38. case orderBy(_ field: String, _ value: Bool)
  39. case limitTo(_ value: Int)
  40. case limitToLast(_ value: Int)
  41. /*
  42. Factory methods
  43. */
  44. public static func whereField(_ field: String, isEqualTo value: Any) -> QueryPredicate {
  45. .isEqualTo(field, value)
  46. }
  47. public static func whereField(_ field: String, isIn values: [Any]) -> QueryPredicate {
  48. .isIn(field, values)
  49. }
  50. public static func whereField(_ field: String, isNotIn values: [Any]) -> QueryPredicate {
  51. .isNotIn(field, values)
  52. }
  53. public static func whereField(_ field: String, arrayContains value: Any) -> QueryPredicate {
  54. .arrayContains(field, value)
  55. }
  56. public static func whereField(_ field: String,
  57. arrayContainsAny values: [Any]) -> QueryPredicate {
  58. .arrayContainsAny(field, values)
  59. }
  60. public static func whereField(_ field: String, isLessThan value: Any) -> QueryPredicate {
  61. .isLessThan(field, value)
  62. }
  63. public static func whereField(_ field: String, isGreaterThan value: Any) -> QueryPredicate {
  64. .isGreaterThan(field, value)
  65. }
  66. public static func whereField(_ field: String,
  67. isLessThanOrEqualTo value: Any) -> QueryPredicate {
  68. .isLessThanOrEqualTo(field, value)
  69. }
  70. public static func whereField(_ field: String,
  71. isGreaterThanOrEqualTo value: Any) -> QueryPredicate {
  72. .isGreaterThanOrEqualTo(field, value)
  73. }
  74. public static func order(by field: String, descending value: Bool = false) -> QueryPredicate {
  75. .orderBy(field, value)
  76. }
  77. public static func limit(to value: Int) -> QueryPredicate {
  78. .limitTo(value)
  79. }
  80. public static func limit(toLast value: Int) -> QueryPredicate {
  81. .limitToLast(value)
  82. }
  83. // Alternate naming
  84. public static func `where`(_ name: String, isEqualTo value: Any) -> QueryPredicate {
  85. .isEqualTo(name, value)
  86. }
  87. public static func `where`(_ name: String, isIn values: [Any]) -> QueryPredicate {
  88. .isIn(name, values)
  89. }
  90. public static func `where`(_ name: String, isNotIn values: [Any]) -> QueryPredicate {
  91. .isNotIn(name, values)
  92. }
  93. public static func `where`(field name: String, arrayContains value: Any) -> QueryPredicate {
  94. .arrayContains(name, value)
  95. }
  96. public static func `where`(_ name: String, arrayContainsAny values: [Any]) -> QueryPredicate {
  97. .arrayContainsAny(name, values)
  98. }
  99. public static func `where`(_ name: String, isLessThan value: Any) -> QueryPredicate {
  100. .isLessThan(name, value)
  101. }
  102. public static func `where`(_ name: String, isGreaterThan value: Any) -> QueryPredicate {
  103. .isGreaterThan(name, value)
  104. }
  105. public static func `where`(_ name: String, isLessThanOrEqualTo value: Any) -> QueryPredicate {
  106. .isLessThanOrEqualTo(name, value)
  107. }
  108. public static func `where`(_ name: String,
  109. isGreaterThanOrEqualTo value: Any) -> QueryPredicate {
  110. .isGreaterThanOrEqualTo(name, value)
  111. }
  112. }