|
| 1 | +/* |
| 2 | + * Copyright 2018-2024 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 | + * https://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 | +package org.springframework.data.mongodb.core.query |
| 17 | + |
| 18 | +import org.springframework.data.mapping.toDotPath |
| 19 | +import org.springframework.data.mongodb.core.query.Update.Position |
| 20 | +import kotlin.reflect.KProperty |
| 21 | + |
| 22 | +/** |
| 23 | + * Static factory method to create an Update using the provided key |
| 24 | + * |
| 25 | + * @author Pawel Matysek |
| 26 | + * @see Update.update |
| 27 | + */ |
| 28 | +fun <T> update(key: KProperty<T>, value: T?) = |
| 29 | + Update.update(key.toDotPath(), value) |
| 30 | + |
| 31 | +/** |
| 32 | + * Update using the {@literal $set} update modifier |
| 33 | + * |
| 34 | + * @author Pawel Matysek |
| 35 | + * @see Update.set |
| 36 | + */ |
| 37 | +fun <T> Update.set(key: KProperty<T>, value: T?) = |
| 38 | + set(key.toDotPath(), value) |
| 39 | + |
| 40 | +/** |
| 41 | + * Update using the {@literal $setOnInsert} update modifier |
| 42 | + * |
| 43 | + * @author Pawel Matysek |
| 44 | + * @see Update.setOnInsert |
| 45 | + */ |
| 46 | +fun <T> Update.setOnInsert(key: KProperty<T>, value: T?) = |
| 47 | + setOnInsert(key.toDotPath(), value) |
| 48 | + |
| 49 | +/** |
| 50 | + * Update using the {@literal $unset} update modifier |
| 51 | + * |
| 52 | + * @author Pawel Matysek |
| 53 | + * @see Update.unset |
| 54 | + */ |
| 55 | +fun <T> Update.unset(key: KProperty<T>) = |
| 56 | + unset(key.toDotPath()) |
| 57 | + |
| 58 | +/** |
| 59 | + * Update using the {@literal $inc} update modifier |
| 60 | + * |
| 61 | + * @author Pawel Matysek |
| 62 | + * @see Update.inc |
| 63 | + */ |
| 64 | +fun <T> Update.inc(key: KProperty<T>, inc: Number) = |
| 65 | + inc(key.toDotPath(), inc) |
| 66 | + |
| 67 | +fun <T> Update.inc(key: KProperty<T>) = |
| 68 | + inc(key.toDotPath()) |
| 69 | + |
| 70 | +/** |
| 71 | + * Update using the {@literal $push} update modifier |
| 72 | + * |
| 73 | + * @author Pawel Matysek |
| 74 | + * @see Update.push |
| 75 | + */ |
| 76 | +fun <T> Update.push(key: KProperty<Collection<T>>, value: T?) = |
| 77 | + push(key.toDotPath(), value) |
| 78 | + |
| 79 | +/** |
| 80 | + * Update using {@code $push} modifier. <br/> |
| 81 | + * Allows creation of {@code $push} command for single or multiple (using {@code $each}) values as well as using |
| 82 | + * |
| 83 | + * {@code $position}. |
| 84 | + * @author Pawel Matysek |
| 85 | + * @see Update.push |
| 86 | + */ |
| 87 | +fun <T> Update.push(key: KProperty<T>) = |
| 88 | + push(key.toDotPath()) |
| 89 | + |
| 90 | +/** |
| 91 | + * Update using {@code $addToSet} modifier. <br/> |
| 92 | + * Allows creation of {@code $push} command for single or multiple (using {@code $each}) values * {@code $position}. |
| 93 | + * |
| 94 | + * @author Pawel Matysek |
| 95 | + * @see Update.addToSet |
| 96 | + */ |
| 97 | +fun <T> Update.addToSet(key: KProperty<T>) = |
| 98 | + addToSet(key.toDotPath()) |
| 99 | + |
| 100 | +/** |
| 101 | + * Update using the {@literal $addToSet} update modifier |
| 102 | + * |
| 103 | + * @author Pawel Matysek |
| 104 | + * @see Update.addToSet |
| 105 | + */ |
| 106 | +fun <T> Update.addToSet(key: KProperty<Collection<T>>, value: T?) = |
| 107 | + addToSet(key.toDotPath(), value) |
| 108 | + |
| 109 | +/** |
| 110 | + * Update using the {@literal $pop} update modifier |
| 111 | + * |
| 112 | + * @author Pawel Matysek |
| 113 | + * @see Update.pop |
| 114 | + */ |
| 115 | +fun <T> Update.pop(key: KProperty<T>, pos: Position) = |
| 116 | + pop(key.toDotPath(), pos) |
| 117 | + |
| 118 | +/** |
| 119 | + * Update using the {@literal $pull} update modifier |
| 120 | + * |
| 121 | + * @author Pawel Matysek |
| 122 | + * @see Update.pull |
| 123 | + */ |
| 124 | +fun <T> Update.pull(key: KProperty<T>, value: Any) = |
| 125 | + pull(key.toDotPath(), value) |
| 126 | + |
| 127 | +/** |
| 128 | + * Update using the {@literal $pullAll} update modifier |
| 129 | + * |
| 130 | + * @author Pawel Matysek |
| 131 | + * @see Update.pullAll |
| 132 | + */ |
| 133 | +fun <T> Update.pullAll(key: KProperty<Collection<T>>, values: Array<T>) = |
| 134 | + pullAll(key.toDotPath(), values) |
| 135 | + |
| 136 | +/** |
| 137 | + * Update given key to current date using {@literal $currentDate : { $type : "timestamp" }} modifier. |
| 138 | + * |
| 139 | + * @author Pawel Matysek |
| 140 | + * @see Update.currentDate |
| 141 | + */ |
| 142 | +fun <T> Update.currentDate(key: KProperty<T>) = |
| 143 | + currentDate(key.toDotPath()) |
| 144 | + |
| 145 | +/** |
| 146 | + * Update given key to current date using {@literal $currentDate} modifier. |
| 147 | + * |
| 148 | + * @author Pawel Matysek |
| 149 | + * @see Update.currentTimestamp |
| 150 | + */ |
| 151 | +fun <T> Update.currentTimestamp(key: KProperty<T>) = |
| 152 | + currentTimestamp(key.toDotPath()) |
| 153 | + |
| 154 | +/** |
| 155 | + * Multiply the value of given key by the given number. |
| 156 | + * |
| 157 | + * @author Pawel Matysek |
| 158 | + * @see Update.multiply |
| 159 | + */ |
| 160 | +fun <T> Update.multiply(key: KProperty<T>, multiplier: Number) = |
| 161 | + multiply(key.toDotPath(), multiplier) |
| 162 | + |
| 163 | +/** |
| 164 | + * Update given key to the {@code value} if the {@code value} is greater than the current value of the field. |
| 165 | + * |
| 166 | + * @author Pawel Matysek |
| 167 | + * @see Update.max |
| 168 | + */ |
| 169 | +fun <T : Any> Update.max(key: KProperty<T>, value: T) = |
| 170 | + max(key.toDotPath(), value) |
| 171 | + |
| 172 | +/** |
| 173 | + * Update given key to the {@code value} if the {@code value} is less than the current value of the field. |
| 174 | + * |
| 175 | + * @author Pawel Matysek |
| 176 | + * @see Update.min |
| 177 | + */ |
| 178 | +fun <T : Any> Update.min(key: KProperty<T>, value: T) = |
| 179 | + min(key.toDotPath(), value) |
| 180 | + |
| 181 | +/** |
| 182 | + * The operator supports bitwise {@code and}, bitwise {@code or}, and bitwise {@code xor} operations. |
| 183 | + * |
| 184 | + * @author Pawel Matysek |
| 185 | + * @see Update.bitwise |
| 186 | + */ |
| 187 | +fun <T> Update.bitwise(key: KProperty<T>) = |
| 188 | + bitwise(key.toDotPath()) |
| 189 | + |
| 190 | +/** |
| 191 | + * Filter elements in an array that match the given criteria for update. {@code expression} is used directly with the |
| 192 | + * driver without further type or field mapping. |
| 193 | + * |
| 194 | + * @author Pawel Matysek |
| 195 | + * @see Update.filterArray |
| 196 | + */ |
| 197 | +fun <T> Update.filterArray(identifier: KProperty<T>, expression: Any) = |
| 198 | + filterArray(identifier.toDotPath(), expression) |
| 199 | + |
| 200 | +/** |
| 201 | + * Determine if a given {@code key} will be touched on execution. |
| 202 | + * |
| 203 | + * @author Pawel Matysek |
| 204 | + * @see Update.modifies |
| 205 | + */ |
| 206 | +fun <T> Update.modifies(key: KProperty<T>) = |
| 207 | + modifies(key.toDotPath()) |
| 208 | + |
0 commit comments