|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 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 | + |
| 17 | +/* |
| 18 | + * Copyright 2018 the original author or authors. |
| 19 | + * |
| 20 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | + * you may not use this file except in compliance with the License. |
| 22 | + * You may obtain a copy of the License at |
| 23 | + * |
| 24 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | + * |
| 26 | + * Unless required by applicable law or agreed to in writing, software |
| 27 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | + * See the License for the specific language governing permissions and |
| 30 | + * limitations under the License. |
| 31 | + */ |
| 32 | +package org.springframework.data.mongodb.core.query |
| 33 | + |
| 34 | +import org.bson.BsonRegularExpression |
| 35 | +import org.springframework.data.geo.Circle |
| 36 | +import org.springframework.data.geo.Point |
| 37 | +import org.springframework.data.geo.Shape |
| 38 | +import org.springframework.data.mongodb.core.geo.GeoJson |
| 39 | +import org.springframework.data.mongodb.core.schema.JsonSchemaObject |
| 40 | +import java.util.regex.Pattern |
| 41 | +import kotlin.reflect.KProperty |
| 42 | + |
| 43 | +/** |
| 44 | + * Creates a criterion using equality. |
| 45 | + * @author Tjeu Kayim |
| 46 | + * @since 2.2 |
| 47 | + * @see Criteria.isEqualTo |
| 48 | + */ |
| 49 | +infix fun <T> KProperty<T>.isEqualTo(value: T) = Criteria(nestedFieldName(this)).isEqualTo(value) |
| 50 | + |
| 51 | +/** |
| 52 | + * Creates a criterion using the $ne operator. |
| 53 | + * |
| 54 | + * See [MongoDB Query operator: $ne](https://docs.mongodb.com/manual/reference/operator/query/ne/) |
| 55 | + * @author Tjeu Kayim |
| 56 | + * @since 2.2 |
| 57 | + * @see Criteria.ne |
| 58 | + */ |
| 59 | +infix fun <T> KProperty<T>.ne(value: T): Criteria = Criteria(nestedFieldName(this)).ne(value) |
| 60 | + |
| 61 | +/** |
| 62 | + * Creates a criterion using the $lt operator. |
| 63 | + * |
| 64 | + * See [MongoDB Query operator: $lt](https://docs.mongodb.com/manual/reference/operator/query/lt/) |
| 65 | + * @author Tjeu Kayim |
| 66 | + * @since 2.2 |
| 67 | + * @see Criteria.lt |
| 68 | + */ |
| 69 | +infix fun <T> KProperty<T>.lt(value: T): Criteria = Criteria(nestedFieldName(this)).lt(value) |
| 70 | + |
| 71 | +/** |
| 72 | + * Creates a criterion using the $lte operator. |
| 73 | + * |
| 74 | + * See [MongoDB Query operator: $lte](https://docs.mongodb.com/manual/reference/operator/query/lte/) |
| 75 | + * @author Tjeu Kayim |
| 76 | + * @since 2.2 |
| 77 | + * @see Criteria.lte |
| 78 | + */ |
| 79 | +infix fun <T> KProperty<T>.lte(value: T): Criteria = Criteria(nestedFieldName(this)).lte(value) |
| 80 | + |
| 81 | +/** |
| 82 | + * Creates a criterion using the $gt operator. |
| 83 | + * |
| 84 | + * See [MongoDB Query operator: $gt](https://docs.mongodb.com/manual/reference/operator/query/gt/) |
| 85 | + * @author Tjeu Kayim |
| 86 | + * @since 2.2 |
| 87 | + * @see Criteria.gt |
| 88 | + */ |
| 89 | +infix fun <T> KProperty<T>.gt(value: T): Criteria = Criteria(nestedFieldName(this)).gt(value) |
| 90 | + |
| 91 | +/** |
| 92 | + * Creates a criterion using the $gte operator. |
| 93 | + * |
| 94 | + * See [MongoDB Query operator: $gte](https://docs.mongodb.com/manual/reference/operator/query/gte/) |
| 95 | + * @author Tjeu Kayim |
| 96 | + * @since 2.2 |
| 97 | + * @see Criteria.gte |
| 98 | + */ |
| 99 | +infix fun <T> KProperty<T>.gte(value: T): Criteria = Criteria(nestedFieldName(this)).gte(value) |
| 100 | + |
| 101 | +/** |
| 102 | + * Creates a criterion using the $in operator. |
| 103 | + * |
| 104 | + * See [MongoDB Query operator: $in](https://docs.mongodb.com/manual/reference/operator/query/in/) |
| 105 | + * @author Tjeu Kayim |
| 106 | + * @since 2.2 |
| 107 | + * @see Criteria.inValues |
| 108 | + */ |
| 109 | +fun <T> KProperty<T>.inValues(vararg o: Any): Criteria = Criteria(nestedFieldName(this)).`in`(*o) |
| 110 | + |
| 111 | +/** |
| 112 | + * Creates a criterion using the $in operator. |
| 113 | + * |
| 114 | + * See [MongoDB Query operator: $in](https://docs.mongodb.com/manual/reference/operator/query/in/) |
| 115 | + * @author Tjeu Kayim |
| 116 | + * @since 2.2 |
| 117 | + * @see Criteria.inValues |
| 118 | + */ |
| 119 | +infix fun <T> KProperty<T>.inValues(value: Collection<T>): Criteria = Criteria(nestedFieldName(this)).`in`(value) |
| 120 | + |
| 121 | +/** |
| 122 | + * Creates a criterion using the $nin operator. |
| 123 | + * |
| 124 | + * See [MongoDB Query operator: $nin](https://docs.mongodb.com/manual/reference/operator/query/nin/) |
| 125 | + * @author Tjeu Kayim |
| 126 | + * @since 2.2 |
| 127 | + * @see Criteria.nin |
| 128 | + */ |
| 129 | +fun <T> KProperty<T>.nin(vararg o: Any): Criteria = Criteria(nestedFieldName(this)).nin(*o) |
| 130 | + |
| 131 | +/** |
| 132 | + * Creates a criterion using the $nin operator. |
| 133 | + * |
| 134 | + * See [MongoDB Query operator: $nin](https://docs.mongodb.com/manual/reference/operator/query/nin/) |
| 135 | + * @author Tjeu Kayim |
| 136 | + * @since 2.2 |
| 137 | + * @see Criteria.nin |
| 138 | + */ |
| 139 | +infix fun <T> KProperty<T>.nin(value: Collection<T>): Criteria = Criteria(nestedFieldName(this)).nin(value) |
| 140 | + |
| 141 | +/** |
| 142 | + * Creates a criterion using the $mod operator. |
| 143 | + * |
| 144 | + * See [MongoDB Query operator: $mod](https://docs.mongodb.com/manual/reference/operator/query/mod/) |
| 145 | + * @author Tjeu Kayim |
| 146 | + * @since 2.2 |
| 147 | + * @see Criteria.mod |
| 148 | + */ |
| 149 | +fun KProperty<Number>.mod(value: Number, remainder: Number): Criteria = Criteria(nestedFieldName(this)).mod(value, remainder) |
| 150 | + |
| 151 | +/** |
| 152 | + * Creates a criterion using the $all operator. |
| 153 | + * |
| 154 | + * See [MongoDB Query operator: $all](https://docs.mongodb.com/manual/reference/operator/query/all/) |
| 155 | + * @author Tjeu Kayim |
| 156 | + * @since 2.2 |
| 157 | + * @see Criteria.all |
| 158 | + */ |
| 159 | +fun KProperty<*>.all(vararg o: Any): Criteria = Criteria(nestedFieldName(this)).all(*o) |
| 160 | + |
| 161 | +/** |
| 162 | + * Creates a criterion using the $all operator. |
| 163 | + * |
| 164 | + * See [MongoDB Query operator: $all](https://docs.mongodb.com/manual/reference/operator/query/all/) |
| 165 | + * @author Tjeu Kayim |
| 166 | + * @since 2.2 |
| 167 | + * @see Criteria.all |
| 168 | + */ |
| 169 | +infix fun KProperty<*>.all(value: Collection<*>): Criteria = Criteria(nestedFieldName(this)).all(value) |
| 170 | + |
| 171 | +/** |
| 172 | + * Creates a criterion using the $size operator. |
| 173 | + * |
| 174 | + * See [MongoDB Query operator: $size](https://docs.mongodb.com/manual/reference/operator/query/size/) |
| 175 | + * @author Tjeu Kayim |
| 176 | + * @since 2.2 |
| 177 | + * @see Criteria.size |
| 178 | + */ |
| 179 | +infix fun KProperty<*>.size(s: Int): Criteria = Criteria(nestedFieldName(this)).size(s) |
| 180 | + |
| 181 | +/** |
| 182 | + * Creates a criterion using the $exists operator. |
| 183 | + * |
| 184 | + * See [MongoDB Query operator: $exists](https://docs.mongodb.com/manual/reference/operator/query/exists/) |
| 185 | + * @author Tjeu Kayim |
| 186 | + * @since 2.2 |
| 187 | + * @see Criteria.exists |
| 188 | + */ |
| 189 | +infix fun KProperty<*>.exists(b: Boolean): Criteria = Criteria(nestedFieldName(this)).exists(b) |
| 190 | + |
| 191 | +/** |
| 192 | + * Creates a criterion using the $type operator. |
| 193 | + * |
| 194 | + * See [MongoDB Query operator: $type](https://docs.mongodb.com/manual/reference/operator/query/type/) |
| 195 | + * @author Tjeu Kayim |
| 196 | + * @since 2.2 |
| 197 | + * @see Criteria.type |
| 198 | + */ |
| 199 | +infix fun KProperty<*>.type(t: Int): Criteria = Criteria(nestedFieldName(this)).type(t) |
| 200 | + |
| 201 | +/** |
| 202 | + * Creates a criterion using the $type operator. |
| 203 | + * |
| 204 | + * See [MongoDB Query operator: $type](https://docs.mongodb.com/manual/reference/operator/query/type/) |
| 205 | + * @author Tjeu Kayim |
| 206 | + * @since 2.2 |
| 207 | + * @see Criteria.type |
| 208 | + */ |
| 209 | +infix fun KProperty<*>.type(t: Collection<JsonSchemaObject.Type>): Criteria = Criteria(nestedFieldName(this)).type(*t.toTypedArray()) |
| 210 | + |
| 211 | +/** |
| 212 | + * Creates a criterion using the $type operator. |
| 213 | + * |
| 214 | + * See [MongoDB Query operator: $type](https://docs.mongodb.com/manual/reference/operator/query/type/) |
| 215 | + * @author Tjeu Kayim |
| 216 | + * @since 2.2 |
| 217 | + * @see Criteria.type |
| 218 | + */ |
| 219 | +fun KProperty<*>.type(vararg t: JsonSchemaObject.Type): Criteria = Criteria(nestedFieldName(this)).type(*t) |
| 220 | + |
| 221 | +/** |
| 222 | + * Creates a criterion using the $not meta operator which affects the clause directly following |
| 223 | + * |
| 224 | + * See [MongoDB Query operator: $not](https://docs.mongodb.com/manual/reference/operator/query/not/) |
| 225 | + * @author Tjeu Kayim |
| 226 | + * @since 2.2 |
| 227 | + * @see Criteria.not |
| 228 | + */ |
| 229 | +fun KProperty<*>.not(): Criteria = Criteria(nestedFieldName(this)).not() |
| 230 | + |
| 231 | +/** |
| 232 | + * Creates a criterion using a $regex operator. |
| 233 | + * |
| 234 | + * See [MongoDB Query operator: $regex](https://docs.mongodb.com/manual/reference/operator/query/regex/) |
| 235 | + * @author Tjeu Kayim |
| 236 | + * @since 2.2 |
| 237 | + * @see Criteria.regex |
| 238 | + */ |
| 239 | +infix fun KProperty<String?>.regex(re: String): Criteria = Criteria(nestedFieldName(this)).regex(re, null) |
| 240 | + |
| 241 | +/** |
| 242 | + * Creates a criterion using a $regex and $options operator. |
| 243 | + * |
| 244 | + * See [MongoDB Query operator: $regex](https://docs.mongodb.com/manual/reference/operator/query/regex/) |
| 245 | + * @author Tjeu Kayim |
| 246 | + * @since 2.2 |
| 247 | + * @see Criteria.regex |
| 248 | + */ |
| 249 | +fun KProperty<String?>.regex(re: String, options: String?): Criteria = Criteria(nestedFieldName(this)).regex(re, options) |
| 250 | + |
| 251 | +/** |
| 252 | + * Syntactical sugar for [isEqualTo] making obvious that we create a regex predicate. |
| 253 | + * @author Tjeu Kayim |
| 254 | + * @since 2.2 |
| 255 | + * @see Criteria.regex |
| 256 | + */ |
| 257 | +infix fun KProperty<String?>.regex(re: Regex): Criteria = Criteria(nestedFieldName(this)).regex(re.toPattern()) |
| 258 | + |
| 259 | +/** |
| 260 | + * Syntactical sugar for [isEqualTo] making obvious that we create a regex predicate. |
| 261 | + * @author Tjeu Kayim |
| 262 | + * @since 2.2 |
| 263 | + * @see Criteria.regex |
| 264 | + */ |
| 265 | +infix fun KProperty<String?>.regex(re: Pattern): Criteria = Criteria(nestedFieldName(this)).regex(re) |
| 266 | + |
| 267 | +/** |
| 268 | + * Syntactical sugar for [isEqualTo] making obvious that we create a regex predicate. |
| 269 | + * @author Tjeu Kayim |
| 270 | + * @since 2.2 |
| 271 | + * @see Criteria.regex |
| 272 | + */ |
| 273 | +infix fun KProperty<String?>.regex(re: BsonRegularExpression): Criteria = Criteria(nestedFieldName(this)).regex(re) |
| 274 | + |
| 275 | +/** |
| 276 | + * Creates a geospatial criterion using a $geoWithin $centerSphere operation. This is only available for |
| 277 | + * Mongo 2.4 and higher. |
| 278 | + * |
| 279 | + * See [MongoDB Query operator: |
| 280 | + * $geoWithin](https://docs.mongodb.com/manual/reference/operator/query/geoWithin/) |
| 281 | + * |
| 282 | + * See [MongoDB Query operator: |
| 283 | + * $centerSphere](https://docs.mongodb.com/manual/reference/operator/query/centerSphere/) |
| 284 | + * @author Tjeu Kayim |
| 285 | + * @since 2.2 |
| 286 | + * @see Criteria.withinSphere |
| 287 | + */ |
| 288 | +infix fun KProperty<GeoJson<*>>.withinSphere(circle: Circle): Criteria = Criteria(nestedFieldName(this)).withinSphere(circle) |
| 289 | + |
| 290 | +/** |
| 291 | + * Creates a geospatial criterion using a $geoWithin operation. |
| 292 | + * |
| 293 | + * See [MongoDB Query operator: |
| 294 | + * $geoWithin](https://docs.mongodb.com/manual/reference/operator/query/geoWithin/) |
| 295 | + * @author Tjeu Kayim |
| 296 | + * @since 2.2 |
| 297 | + * @see Criteria.within |
| 298 | + */ |
| 299 | +infix fun KProperty<GeoJson<*>>.within(shape: Shape): Criteria = Criteria(nestedFieldName(this)).within(shape) |
| 300 | + |
| 301 | +/** |
| 302 | + * Creates a geospatial criterion using a $near operation. |
| 303 | + * |
| 304 | + * See [MongoDB Query operator: $near](https://docs.mongodb.com/manual/reference/operator/query/near/) |
| 305 | + * @author Tjeu Kayim |
| 306 | + * @since 2.2 |
| 307 | + * @see Criteria.near |
| 308 | + */ |
| 309 | +infix fun KProperty<GeoJson<*>>.near(point: Point): Criteria = Criteria(nestedFieldName(this)).near(point) |
| 310 | + |
| 311 | +/** |
| 312 | + * Creates a geospatial criterion using a $nearSphere operation. This is only available for Mongo 1.7 and |
| 313 | + * higher. |
| 314 | + * |
| 315 | + * See [MongoDB Query operator: |
| 316 | + * $nearSphere](https://docs.mongodb.com/manual/reference/operator/query/nearSphere/) |
| 317 | + * @author Tjeu Kayim |
| 318 | + * @since 2.2 |
| 319 | + * @see Criteria.nearSphere |
| 320 | + */ |
| 321 | +infix fun KProperty<GeoJson<*>>.nearSphere(point: Point): Criteria = Criteria(nestedFieldName(this)).nearSphere(point) |
| 322 | + |
| 323 | +/** |
| 324 | + * Creates criterion using `$geoIntersects` operator which matches intersections of the given `geoJson` |
| 325 | + * structure and the documents one. Requires MongoDB 2.4 or better. |
| 326 | + * @author Tjeu Kayim |
| 327 | + * @since 2.2 |
| 328 | + * @see Criteria.intersects |
| 329 | + */ |
| 330 | +infix fun KProperty<GeoJson<*>>.intersects(geoJson: GeoJson<*>): Criteria = Criteria(nestedFieldName(this)).intersects(geoJson) |
| 331 | + |
| 332 | +/** |
| 333 | + * Creates a geo-spatial criterion using a $maxDistance operation, for use with $near |
| 334 | + * |
| 335 | + * See [MongoDB Query operator: |
| 336 | + * $maxDistance](https://docs.mongodb.com/manual/reference/operator/query/maxDistance/) |
| 337 | + * @author Tjeu Kayim |
| 338 | + * @since 2.2 |
| 339 | + * @see Criteria.maxDistance |
| 340 | + */ |
| 341 | +infix fun KProperty<GeoJson<*>>.maxDistance(d: Double): Criteria = Criteria(nestedFieldName(this)).maxDistance(d) |
| 342 | + |
| 343 | +/** |
| 344 | + * Creates a geospatial criterion using a $minDistance operation, for use with $near or |
| 345 | + * $nearSphere. |
| 346 | + * @author Tjeu Kayim |
| 347 | + * @since 2.2 |
| 348 | + * @see Criteria.minDistance |
| 349 | + */ |
| 350 | +infix fun KProperty<GeoJson<*>>.minDistance(d: Double): Criteria = Criteria(nestedFieldName(this)).minDistance(d) |
| 351 | + |
| 352 | +/** |
| 353 | + * Creates a criterion using the $elemMatch operator |
| 354 | + * |
| 355 | + * See [MongoDB Query operator: |
| 356 | + * $elemMatch](https://docs.mongodb.com/manual/reference/operator/query/elemMatch/) |
| 357 | + * @author Tjeu Kayim |
| 358 | + * @since 2.2 |
| 359 | + * @see Criteria.elemMatch |
| 360 | + */ |
| 361 | +infix fun KProperty<*>.elemMatch(c: Criteria): Criteria = Criteria(nestedFieldName(this)).elemMatch(c) |
| 362 | + |
| 363 | +/** |
| 364 | + * Use [Criteria.BitwiseCriteriaOperators] as gateway to create a criterion using one of the |
| 365 | + * [bitwise operators](https://docs.mongodb.com/manual/reference/operator/query-bitwise/) like |
| 366 | + * `$bitsAllClear`. |
| 367 | + * |
| 368 | + * Example: |
| 369 | + * ``` |
| 370 | + * bits { allClear(123) } |
| 371 | + * ``` |
| 372 | + * @author Tjeu Kayim |
| 373 | + * @since 2.2 |
| 374 | + * @see Criteria.bits |
| 375 | + */ |
| 376 | +infix fun KProperty<*>.bits(bitwiseCriteria: Criteria.BitwiseCriteriaOperators.() -> Criteria) = |
| 377 | + Criteria(nestedFieldName(this)).bits().let(bitwiseCriteria) |
0 commit comments