QueryPredicate.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. import FirebaseFirestore
  17. /// Query predicates that can be used to filter results fetched by `FirestoreQuery`.
  18. ///
  19. /// Construct predicates using one of the following ways:
  20. ///
  21. /// let onlyFavourites: QueryPredicate = .whereField("isFavourite", isEqualTo: true)
  22. /// let onlyFavourites2: QueryPredicate = .isEqualTo("isFavourite", true)
  23. /// let onlyFavourites3: QueryPredicate = .where("isFavourite", isEqualTo: true)
  24. public enum QueryPredicate {
  25. case isEqualTo(_ field: String, _ value: Any)
  26. case isIn(_ field: String, _ values: [Any])
  27. case isNotIn(_ field: String, _ values: [Any])
  28. case arrayContains(_ field: String, _ value: Any)
  29. case arrayContainsAny(_ field: String, _ values: [Any])
  30. case isLessThan(_ field: String, _ value: Any)
  31. case isGreaterThan(_ field: String, _ value: Any)
  32. case isLessThanOrEqualTo(_ field: String, _ value: Any)
  33. case isGreaterThanOrEqualTo(_ field: String, _ value: Any)
  34. case orderBy(_ field: String, _ value: Bool)
  35. case limitTo(_ value: Int)
  36. case limitToLast(_ value: Int)
  37. /*
  38. Factory methods
  39. */
  40. public static func whereField(_ field: String, isEqualTo value: Any) -> QueryPredicate {
  41. .isEqualTo(field, value)
  42. }
  43. public static func whereField(_ field: String, isIn values: [Any]) -> QueryPredicate {
  44. .isIn(field, values)
  45. }
  46. public static func whereField(_ field: String, isNotIn values: [Any]) -> QueryPredicate {
  47. .isNotIn(field, values)
  48. }
  49. public static func whereField(_ field: String, arrayContains value: Any) -> QueryPredicate {
  50. .arrayContains(field, value)
  51. }
  52. public static func whereField(_ field: String,
  53. arrayContainsAny values: [Any]) -> QueryPredicate {
  54. .arrayContainsAny(field, values)
  55. }
  56. public static func whereField(_ field: String, isLessThan value: Any) -> QueryPredicate {
  57. .isLessThan(field, value)
  58. }
  59. public static func whereField(_ field: String, isGreaterThan value: Any) -> QueryPredicate {
  60. .isGreaterThan(field, value)
  61. }
  62. public static func whereField(_ field: String,
  63. isLessThanOrEqualTo value: Any) -> QueryPredicate {
  64. .isLessThanOrEqualTo(field, value)
  65. }
  66. public static func whereField(_ field: String,
  67. isGreaterThanOrEqualTo value: Any) -> QueryPredicate {
  68. .isGreaterThanOrEqualTo(field, value)
  69. }
  70. public static func order(by field: String, descending value: Bool = false) -> QueryPredicate {
  71. .orderBy(field, value)
  72. }
  73. public static func limit(to value: Int) -> QueryPredicate {
  74. .limitTo(value)
  75. }
  76. public static func limit(toLast value: Int) -> QueryPredicate {
  77. .limitToLast(value)
  78. }
  79. // Alternate naming
  80. public static func `where`(_ name: String, isEqualTo value: Any) -> QueryPredicate {
  81. .isEqualTo(name, value)
  82. }
  83. public static func `where`(_ name: String, isIn values: [Any]) -> QueryPredicate {
  84. .isIn(name, values)
  85. }
  86. public static func `where`(_ name: String, isNotIn values: [Any]) -> QueryPredicate {
  87. .isNotIn(name, values)
  88. }
  89. public static func `where`(field name: String, arrayContains value: Any) -> QueryPredicate {
  90. .arrayContains(name, value)
  91. }
  92. public static func `where`(_ name: String, arrayContainsAny values: [Any]) -> QueryPredicate {
  93. .arrayContainsAny(name, values)
  94. }
  95. public static func `where`(_ name: String, isLessThan value: Any) -> QueryPredicate {
  96. .isLessThan(name, value)
  97. }
  98. public static func `where`(_ name: String, isGreaterThan value: Any) -> QueryPredicate {
  99. .isGreaterThan(name, value)
  100. }
  101. public static func `where`(_ name: String, isLessThanOrEqualTo value: Any) -> QueryPredicate {
  102. .isLessThanOrEqualTo(name, value)
  103. }
  104. public static func `where`(_ name: String,
  105. isGreaterThanOrEqualTo value: Any) -> QueryPredicate {
  106. .isGreaterThanOrEqualTo(name, value)
  107. }
  108. }