| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Telefonica S.A. |
| 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 |
| 14 | # implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 18 | |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 19 | from base64 import b64decode |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame] | 20 | from copy import deepcopy |
| 21 | from http import HTTPStatus |
| 22 | import logging |
| 23 | from time import sleep, time |
| tierno | 2c9794c | 2020-04-29 10:24:28 +0000 | [diff] [blame] | 24 | from uuid import uuid4 |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 25 | |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame] | 26 | from osm_common.dbbase import DbBase, DbException |
| 27 | from pymongo import errors, MongoClient |
| 28 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 29 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 30 | |
| 31 | # TODO consider use this decorator for database access retries |
| 32 | # @retry_mongocall |
| 33 | # def retry_mongocall(call): |
| 34 | # def _retry_mongocall(*args, **kwargs): |
| 35 | # retry = 1 |
| 36 | # while True: |
| 37 | # try: |
| 38 | # return call(*args, **kwargs) |
| 39 | # except pymongo.AutoReconnect as e: |
| 40 | # if retry == 4: |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 41 | # raise DbException(e) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 42 | # sleep(retry) |
| 43 | # return _retry_mongocall |
| 44 | |
| 45 | |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 46 | def deep_update(to_update, update_with): |
| 47 | """ |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 48 | Similar to deepcopy but recursively with nested dictionaries. 'to_update' dict is updated with a content copy of |
| 49 | 'update_with' dict recursively |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 50 | :param to_update: must be a dictionary to be modified |
| 51 | :param update_with: must be a dictionary. It is not changed |
| 52 | :return: to_update |
| 53 | """ |
| 54 | for key in update_with: |
| 55 | if key in to_update: |
| 56 | if isinstance(to_update[key], dict) and isinstance(update_with[key], dict): |
| 57 | deep_update(to_update[key], update_with[key]) |
| 58 | continue |
| 59 | to_update[key] = deepcopy(update_with[key]) |
| 60 | return to_update |
| 61 | |
| 62 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 63 | class DbMongo(DbBase): |
| 64 | conn_initial_timout = 120 |
| 65 | conn_timout = 10 |
| 66 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 67 | def __init__(self, logger_name="db", lock=False): |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 68 | super().__init__(logger_name, lock) |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 69 | self.client = None |
| 70 | self.db = None |
| tierno | c5297e4 | 2019-12-11 12:32:41 +0000 | [diff] [blame] | 71 | self.database_key = None |
| 72 | self.secret_obtained = False |
| 73 | # ^ This is used to know if database serial has been got. Database is inited by NBI, who generates the serial |
| 74 | # In case it is not ready when connected, it should be got later on before any decrypt operation |
| 75 | |
| 76 | def get_secret_key(self): |
| 77 | if self.secret_obtained: |
| 78 | return |
| 79 | |
| 80 | self.secret_key = None |
| 81 | if self.database_key: |
| 82 | self.set_secret_key(self.database_key) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 83 | version_data = self.get_one( |
| 84 | "admin", {"_id": "version"}, fail_on_empty=False, fail_on_more=True |
| 85 | ) |
| tierno | c5297e4 | 2019-12-11 12:32:41 +0000 | [diff] [blame] | 86 | if version_data and version_data.get("serial"): |
| 87 | self.set_secret_key(b64decode(version_data["serial"])) |
| 88 | self.secret_obtained = True |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 89 | |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 90 | def db_connect(self, config, target_version=None): |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 91 | """ |
| 92 | Connect to database |
| 93 | :param config: Configuration of database |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 94 | :param target_version: if provided it checks if database contains required version, raising exception otherwise. |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 95 | :return: None or raises DbException on error |
| 96 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 97 | try: |
| 98 | if "logger_name" in config: |
| 99 | self.logger = logging.getLogger(config["logger_name"]) |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 100 | master_key = config.get("commonkey") or config.get("masterpassword") |
| 101 | if master_key: |
| tierno | c5297e4 | 2019-12-11 12:32:41 +0000 | [diff] [blame] | 102 | self.database_key = master_key |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 103 | self.set_secret_key(master_key) |
| Juan | c837a78 | 2018-11-16 10:47:46 -0300 | [diff] [blame] | 104 | if config.get("uri"): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 105 | self.client = MongoClient( |
| 106 | config["uri"], replicaSet=config.get("replicaset", None) |
| 107 | ) |
| tierno | cfc5272 | 2018-10-23 11:41:49 +0200 | [diff] [blame] | 108 | # when all modules are ready |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 109 | self.db = self.client[config["name"]] |
| 110 | if "loglevel" in config: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 111 | self.logger.setLevel(getattr(logging, config["loglevel"])) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 112 | # get data to try a connection |
| 113 | now = time() |
| 114 | while True: |
| 115 | try: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 116 | version_data = self.get_one( |
| 117 | "admin", |
| 118 | {"_id": "version"}, |
| 119 | fail_on_empty=False, |
| 120 | fail_on_more=True, |
| 121 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 122 | # check database status is ok |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 123 | if version_data and version_data.get("status") != "ENABLED": |
| 124 | raise DbException( |
| 125 | "Wrong database status '{}'".format( |
| 126 | version_data.get("status") |
| 127 | ), |
| 128 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 129 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 130 | # check version |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 131 | db_version = ( |
| 132 | None if not version_data else version_data.get("version") |
| 133 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 134 | if target_version and target_version != db_version: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 135 | raise DbException( |
| 136 | "Invalid database version {}. Expected {}".format( |
| 137 | db_version, target_version |
| 138 | ) |
| 139 | ) |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 140 | # get serial |
| 141 | if version_data and version_data.get("serial"): |
| tierno | c5297e4 | 2019-12-11 12:32:41 +0000 | [diff] [blame] | 142 | self.secret_obtained = True |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 143 | self.set_secret_key(b64decode(version_data["serial"])) |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 144 | self.logger.info( |
| 145 | "Connected to database {} version {}".format( |
| 146 | config["name"], db_version |
| 147 | ) |
| 148 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 149 | return |
| 150 | except errors.ConnectionFailure as e: |
| 151 | if time() - now >= self.conn_initial_timout: |
| 152 | raise |
| 153 | self.logger.info("Waiting to database up {}".format(e)) |
| 154 | sleep(2) |
| 155 | except errors.PyMongoError as e: |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 156 | raise DbException(e) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 157 | |
| 158 | @staticmethod |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 159 | def _format_filter(q_filter): |
| 160 | """ |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 161 | Translate query string q_filter into mongo database filter |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 162 | :param q_filter: Query string content. Follows SOL005 section 4.3.2 guidelines, with the follow extensions and |
| tierno | af24106 | 2018-08-31 14:53:15 +0200 | [diff] [blame] | 163 | differences: |
| 164 | It accept ".nq" (not equal) in addition to ".neq". |
| 165 | For arrays you can specify index (concrete index must match), nothing (any index may match) or 'ANYINDEX' |
| 166 | (two or more matches applies for the same array element). Examples: |
| 167 | with database register: {A: [{B: 1, C: 2}, {B: 6, C: 9}]} |
| 168 | query 'A.B=6' matches because array A contains one element with B equal to 6 |
| 169 | query 'A.0.B=6' does no match because index 0 of array A contains B with value 1, but not 6 |
| 170 | query 'A.B=6&A.C=2' matches because one element of array matches B=6 and other matchesC=2 |
| 171 | query 'A.ANYINDEX.B=6&A.ANYINDEX.C=2' does not match because it is needed the same element of the |
| 172 | array matching both |
| 173 | |
| 174 | Examples of translations from SOL005 to >> mongo # comment |
| 175 | A=B; A.eq=B >> A: B # must contain key A and equal to B or be a list that contains B |
| 176 | A.cont=B >> A: B |
| 177 | A=B&A=C; A=B,C >> A: {$in: [B, C]} # must contain key A and equal to B or C or be a list that contains |
| 178 | # B or C |
| 179 | A.cont=B&A.cont=C; A.cont=B,C >> A: {$in: [B, C]} |
| 180 | A.ncont=B >> A: {$nin: B} # must not contain key A or if present not equal to B or if a list, |
| 181 | # it must not not contain B |
| 182 | A.ncont=B,C; A.ncont=B&A.ncont=C >> A: {$nin: [B,C]} # must not contain key A or if present not equal |
| 183 | # neither B nor C; or if a list, it must not contain neither B nor C |
| 184 | A.ne=B&A.ne=C; A.ne=B,C >> A: {$nin: [B, C]} |
| 185 | A.gt=B >> A: {$gt: B} # must contain key A and greater than B |
| 186 | A.ne=B; A.neq=B >> A: {$ne: B} # must not contain key A or if present not equal to B, or if |
| 187 | # an array not contain B |
| 188 | A.ANYINDEX.B=C >> A: {$elemMatch: {B=C} |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 189 | :return: database mongo filter |
| 190 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 191 | try: |
| 192 | db_filter = {} |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 193 | if not q_filter: |
| 194 | return db_filter |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 195 | for query_k, query_v in q_filter.items(): |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 196 | dot_index = query_k.rfind(".") |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 197 | if dot_index > 1 and query_k[dot_index + 1 :] in ( |
| 198 | "eq", |
| 199 | "ne", |
| 200 | "gt", |
| 201 | "gte", |
| 202 | "lt", |
| 203 | "lte", |
| 204 | "cont", |
| 205 | "ncont", |
| 206 | "neq", |
| 207 | ): |
| 208 | operator = "$" + query_k[dot_index + 1 :] |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 209 | if operator == "$neq": |
| 210 | operator = "$ne" |
| 211 | k = query_k[:dot_index] |
| 212 | else: |
| 213 | operator = "$eq" |
| 214 | k = query_k |
| 215 | |
| 216 | v = query_v |
| 217 | if isinstance(v, list): |
| 218 | if operator in ("$eq", "$cont"): |
| 219 | operator = "$in" |
| 220 | v = query_v |
| 221 | elif operator in ("$ne", "$ncont"): |
| 222 | operator = "$nin" |
| 223 | v = query_v |
| 224 | else: |
| 225 | v = query_v.join(",") |
| 226 | |
| 227 | if operator in ("$eq", "$cont"): |
| 228 | # v cannot be a comma separated list, because operator would have been changed to $in |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 229 | db_v = v |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 230 | elif operator == "$ncount": |
| 231 | # v cannot be a comma separated list, because operator would have been changed to $nin |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 232 | db_v = {"$ne": v} |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 233 | else: |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 234 | db_v = {operator: v} |
| 235 | |
| tierno | af24106 | 2018-08-31 14:53:15 +0200 | [diff] [blame] | 236 | # process the ANYINDEX word at k. |
| tierno | 6ec13b0 | 2018-05-14 11:24:57 +0200 | [diff] [blame] | 237 | kleft, _, kright = k.rpartition(".ANYINDEX.") |
| 238 | while kleft: |
| 239 | k = kleft |
| 240 | db_v = {"$elemMatch": {kright: db_v}} |
| 241 | kleft, _, kright = k.rpartition(".ANYINDEX.") |
| 242 | |
| 243 | # insert in db_filter |
| 244 | # maybe db_filter[k] exist. e.g. in the query string for values between 5 and 8: "a.gt=5&a.lt=8" |
| 245 | deep_update(db_filter, {k: db_v}) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 246 | |
| 247 | return db_filter |
| 248 | except Exception as e: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 249 | raise DbException( |
| 250 | "Invalid query string filter at {}:{}. Error: {}".format(query_k, v, e), |
| 251 | http_code=HTTPStatus.BAD_REQUEST, |
| 252 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 253 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 254 | def get_list(self, table, q_filter=None): |
| 255 | """ |
| 256 | Obtain a list of entries matching q_filter |
| 257 | :param table: collection or table |
| 258 | :param q_filter: Filter |
| 259 | :return: a list (can be empty) with the found entries. Raises DbException on error |
| 260 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 261 | try: |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 262 | result = [] |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 263 | with self.lock: |
| 264 | collection = self.db[table] |
| 265 | db_filter = self._format_filter(q_filter) |
| 266 | rows = collection.find(db_filter) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 267 | for row in rows: |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 268 | result.append(row) |
| 269 | return result |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 270 | except DbException: |
| 271 | raise |
| 272 | except Exception as e: # TODO refine |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 273 | raise DbException(e) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 274 | |
| delacruzramo | ae049d8 | 2019-09-17 16:05:17 +0200 | [diff] [blame] | 275 | def count(self, table, q_filter=None): |
| 276 | """ |
| 277 | Count the number of entries matching q_filter |
| 278 | :param table: collection or table |
| 279 | :param q_filter: Filter |
| 280 | :return: number of entries found (can be zero) |
| 281 | :raise: DbException on error |
| 282 | """ |
| 283 | try: |
| 284 | with self.lock: |
| 285 | collection = self.db[table] |
| 286 | db_filter = self._format_filter(q_filter) |
| 287 | count = collection.count(db_filter) |
| 288 | return count |
| 289 | except DbException: |
| 290 | raise |
| 291 | except Exception as e: # TODO refine |
| 292 | raise DbException(e) |
| 293 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 294 | def get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True): |
| 295 | """ |
| 296 | Obtain one entry matching q_filter |
| 297 | :param table: collection or table |
| 298 | :param q_filter: Filter |
| 299 | :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case |
| 300 | it raises a DbException |
| 301 | :param fail_on_more: If more than one matches filter it returns one of then unless this flag is set tu True, so |
| 302 | that it raises a DbException |
| 303 | :return: The requested element, or None |
| 304 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 305 | try: |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 306 | db_filter = self._format_filter(q_filter) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 307 | with self.lock: |
| 308 | collection = self.db[table] |
| 309 | if not (fail_on_empty and fail_on_more): |
| 310 | return collection.find_one(db_filter) |
| 311 | rows = collection.find(db_filter) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 312 | if rows.count() == 0: |
| 313 | if fail_on_empty: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 314 | raise DbException( |
| 315 | "Not found any {} with filter='{}'".format( |
| 316 | table[:-1], q_filter |
| 317 | ), |
| 318 | HTTPStatus.NOT_FOUND, |
| 319 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 320 | return None |
| 321 | elif rows.count() > 1: |
| 322 | if fail_on_more: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 323 | raise DbException( |
| 324 | "Found more than one {} with filter='{}'".format( |
| 325 | table[:-1], q_filter |
| 326 | ), |
| 327 | HTTPStatus.CONFLICT, |
| 328 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 329 | return rows[0] |
| 330 | except Exception as e: # TODO refine |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 331 | raise DbException(e) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 332 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 333 | def del_list(self, table, q_filter=None): |
| 334 | """ |
| 335 | Deletes all entries that match q_filter |
| 336 | :param table: collection or table |
| 337 | :param q_filter: Filter |
| 338 | :return: Dict with the number of entries deleted |
| 339 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 340 | try: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 341 | with self.lock: |
| 342 | collection = self.db[table] |
| 343 | rows = collection.delete_many(self._format_filter(q_filter)) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 344 | return {"deleted": rows.deleted_count} |
| 345 | except DbException: |
| 346 | raise |
| 347 | except Exception as e: # TODO refine |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 348 | raise DbException(e) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 349 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 350 | def del_one(self, table, q_filter=None, fail_on_empty=True): |
| 351 | """ |
| 352 | Deletes one entry that matches q_filter |
| 353 | :param table: collection or table |
| 354 | :param q_filter: Filter |
| 355 | :param fail_on_empty: If nothing matches filter it returns '0' deleted unless this flag is set tu True, in |
| 356 | which case it raises a DbException |
| 357 | :return: Dict with the number of entries deleted |
| 358 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 359 | try: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 360 | with self.lock: |
| 361 | collection = self.db[table] |
| 362 | rows = collection.delete_one(self._format_filter(q_filter)) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 363 | if rows.deleted_count == 0: |
| 364 | if fail_on_empty: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 365 | raise DbException( |
| 366 | "Not found any {} with filter='{}'".format( |
| 367 | table[:-1], q_filter |
| 368 | ), |
| 369 | HTTPStatus.NOT_FOUND, |
| 370 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 371 | return None |
| 372 | return {"deleted": rows.deleted_count} |
| 373 | except Exception as e: # TODO refine |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 374 | raise DbException(e) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 375 | |
| 376 | def create(self, table, indata): |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 377 | """ |
| 378 | Add a new entry at database |
| 379 | :param table: collection or table |
| 380 | :param indata: content to be added |
| 381 | :return: database id of the inserted element. Raises a DbException on error |
| 382 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 383 | try: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 384 | with self.lock: |
| 385 | collection = self.db[table] |
| 386 | data = collection.insert_one(indata) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 387 | return data.inserted_id |
| 388 | except Exception as e: # TODO refine |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 389 | raise DbException(e) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 390 | |
| tierno | 2c9794c | 2020-04-29 10:24:28 +0000 | [diff] [blame] | 391 | def create_list(self, table, indata_list): |
| 392 | """ |
| 393 | Add several entries at once |
| 394 | :param table: collection or table |
| 395 | :param indata_list: content list to be added. |
| 396 | :return: the list of inserted '_id's. Exception on error |
| 397 | """ |
| 398 | try: |
| 399 | for item in indata_list: |
| 400 | if item.get("_id") is None: |
| 401 | item["_id"] = str(uuid4()) |
| 402 | with self.lock: |
| 403 | collection = self.db[table] |
| 404 | data = collection.insert_many(indata_list) |
| 405 | return data.inserted_ids |
| 406 | except Exception as e: # TODO refine |
| 407 | raise DbException(e) |
| 408 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 409 | def set_one( |
| 410 | self, |
| 411 | table, |
| 412 | q_filter, |
| 413 | update_dict, |
| 414 | fail_on_empty=True, |
| 415 | unset=None, |
| 416 | pull=None, |
| 417 | push=None, |
| 418 | push_list=None, |
| 419 | pull_list=None, |
| 420 | upsert=False, |
| 421 | ): |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 422 | """ |
| 423 | Modifies an entry at database |
| 424 | :param table: collection or table |
| 425 | :param q_filter: Filter |
| 426 | :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value |
| bravof | 722a320 | 2021-01-15 11:54:45 -0300 | [diff] [blame] | 427 | :param fail_on_empty: If nothing matches filter it returns None unless this flag is set to True, in which case |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 428 | it raises a DbException |
| tierno | d63ea27 | 2018-11-27 12:03:36 +0100 | [diff] [blame] | 429 | :param unset: Plain dictionary with the content to be removed if exist. It is a dot separated keys, value is |
| 430 | ignored. If not exist, it is ignored |
| 431 | :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value |
| 432 | if exist in the array is removed. If not exist, it is ignored |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 433 | :param pull_list: Same as pull but values are arrays where each item is removed from the array |
| tierno | d63ea27 | 2018-11-27 12:03:36 +0100 | [diff] [blame] | 434 | :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value |
| 435 | is appended to the end of the array |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 436 | :param push_list: Same as push but values are arrays where each item is and appended instead of appending the |
| 437 | whole array |
| bravof | 722a320 | 2021-01-15 11:54:45 -0300 | [diff] [blame] | 438 | :param upsert: If this parameter is set to True and no document is found using 'q_filter' it will be created. |
| 439 | By default this is false. |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 440 | :return: Dict with the number of entries modified. None if no matching is found. |
| 441 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 442 | try: |
| tierno | d63ea27 | 2018-11-27 12:03:36 +0100 | [diff] [blame] | 443 | db_oper = {} |
| 444 | if update_dict: |
| 445 | db_oper["$set"] = update_dict |
| 446 | if unset: |
| 447 | db_oper["$unset"] = unset |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 448 | if pull or pull_list: |
| 449 | db_oper["$pull"] = pull or {} |
| 450 | if pull_list: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 451 | db_oper["$pull"].update( |
| 452 | {k: {"$in": v} for k, v in pull_list.items()} |
| 453 | ) |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 454 | if push or push_list: |
| 455 | db_oper["$push"] = push or {} |
| 456 | if push_list: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 457 | db_oper["$push"].update( |
| 458 | {k: {"$each": v} for k, v in push_list.items()} |
| 459 | ) |
| tierno | d63ea27 | 2018-11-27 12:03:36 +0100 | [diff] [blame] | 460 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 461 | with self.lock: |
| 462 | collection = self.db[table] |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 463 | rows = collection.update_one( |
| 464 | self._format_filter(q_filter), db_oper, upsert=upsert |
| 465 | ) |
| tierno | 3054f78 | 2018-04-25 16:59:53 +0200 | [diff] [blame] | 466 | if rows.matched_count == 0: |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 467 | if fail_on_empty: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 468 | raise DbException( |
| 469 | "Not found any {} with filter='{}'".format( |
| 470 | table[:-1], q_filter |
| 471 | ), |
| 472 | HTTPStatus.NOT_FOUND, |
| 473 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 474 | return None |
| tierno | 3054f78 | 2018-04-25 16:59:53 +0200 | [diff] [blame] | 475 | return {"modified": rows.modified_count} |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 476 | except Exception as e: # TODO refine |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 477 | raise DbException(e) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 478 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 479 | def set_list( |
| 480 | self, |
| 481 | table, |
| 482 | q_filter, |
| 483 | update_dict, |
| 484 | unset=None, |
| 485 | pull=None, |
| 486 | push=None, |
| 487 | push_list=None, |
| 488 | pull_list=None, |
| 489 | ): |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 490 | """ |
| 491 | Modifies al matching entries at database |
| 492 | :param table: collection or table |
| 493 | :param q_filter: Filter |
| 494 | :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value |
| delacruzramo | f71fcff | 2020-02-11 11:14:07 +0000 | [diff] [blame] | 495 | :param unset: Plain dictionary with the content to be removed if exist. It is a dot separated keys, value is |
| 496 | ignored. If not exist, it is ignored |
| 497 | :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value |
| 498 | if exist in the array is removed. If not exist, it is ignored |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 499 | :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys, the |
| 500 | single value is appended to the end of the array |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 501 | :param pull_list: Same as pull but values are arrays where each item is removed from the array |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 502 | :param push_list: Same as push but values are arrays where each item is and appended instead of appending the |
| 503 | whole array |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 504 | :return: Dict with the number of entries modified |
| 505 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 506 | try: |
| delacruzramo | f71fcff | 2020-02-11 11:14:07 +0000 | [diff] [blame] | 507 | db_oper = {} |
| 508 | if update_dict: |
| 509 | db_oper["$set"] = update_dict |
| 510 | if unset: |
| 511 | db_oper["$unset"] = unset |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 512 | if pull or pull_list: |
| 513 | db_oper["$pull"] = pull or {} |
| 514 | if pull_list: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 515 | db_oper["$pull"].update( |
| 516 | {k: {"$in": v} for k, v in pull_list.items()} |
| 517 | ) |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 518 | if push or push_list: |
| 519 | db_oper["$push"] = push or {} |
| 520 | if push_list: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 521 | db_oper["$push"].update( |
| 522 | {k: {"$each": v} for k, v in push_list.items()} |
| 523 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 524 | with self.lock: |
| 525 | collection = self.db[table] |
| delacruzramo | f71fcff | 2020-02-11 11:14:07 +0000 | [diff] [blame] | 526 | rows = collection.update_many(self._format_filter(q_filter), db_oper) |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 527 | return {"modified": rows.modified_count} |
| 528 | except Exception as e: # TODO refine |
| 529 | raise DbException(e) |
| 530 | |
| 531 | def replace(self, table, _id, indata, fail_on_empty=True): |
| 532 | """ |
| 533 | Replace the content of an entry |
| 534 | :param table: collection or table |
| 535 | :param _id: internal database id |
| 536 | :param indata: content to replace |
| 537 | :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case |
| 538 | it raises a DbException |
| 539 | :return: Dict with the number of entries replaced |
| 540 | """ |
| 541 | try: |
| 542 | db_filter = {"_id": _id} |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 543 | with self.lock: |
| 544 | collection = self.db[table] |
| 545 | rows = collection.replace_one(db_filter, indata) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 546 | if rows.matched_count == 0: |
| 547 | if fail_on_empty: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 548 | raise DbException( |
| 549 | "Not found any {} with _id='{}'".format(table[:-1], _id), |
| 550 | HTTPStatus.NOT_FOUND, |
| 551 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 552 | return None |
| tierno | 3054f78 | 2018-04-25 16:59:53 +0200 | [diff] [blame] | 553 | return {"replaced": rows.modified_count} |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 554 | except Exception as e: # TODO refine |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 555 | raise DbException(e) |