+ def _update(
+ self,
+ db_item,
+ update_dict,
+ unset=None,
+ pull=None,
+ push=None,
+ push_list=None,
+ pull_list=None,
+ ):
+ """
+ Modifies an entry at database
+ :param db_item: entry of the table to update
+ :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value
+ :param unset: Plain dictionary with the content to be removed if exist. It is a dot separated keys, value is
+ ignored. If not exist, it is ignored
+ :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value
+ if exist in the array is removed. If not exist, it is ignored
+ :param pull_list: Same as pull but values are arrays where each item is removed from the array
+ :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value
+ is appended to the end of the array
+ :param push_list: Same as push but values are arrays where each item is and appended instead of appending the
+ whole array
+ :return: True if database has been changed, False if not; Exception on error
+ """
+
+ def _iterate_keys(k, db_nested, populate=True):
+ k_list = k.split(".")
+ k_item_prev = k_list[0]
+ populated = False
+ if k_item_prev not in db_nested and populate:
+ populated = True
+ db_nested[k_item_prev] = None
+ for k_item in k_list[1:]:
+ if isinstance(db_nested[k_item_prev], dict):
+ if k_item not in db_nested[k_item_prev]:
+ if not populate:
+ raise DbException(
+ "Cannot set '{}', not existing '{}'".format(k, k_item)
+ )
+ populated = True
+ db_nested[k_item_prev][k_item] = None
+ elif isinstance(db_nested[k_item_prev], list) and k_item.isdigit():
+ # extend list with Nones if index greater than list
+ k_item = int(k_item)
+ if k_item >= len(db_nested[k_item_prev]):
+ if not populate:
+ raise DbException(
+ "Cannot set '{}', index too large '{}'".format(
+ k, k_item
+ )
+ )
+ populated = True
+ db_nested[k_item_prev] += [None] * (
+ k_item - len(db_nested[k_item_prev]) + 1
+ )
+ elif db_nested[k_item_prev] is None:
+ if not populate:
+ raise DbException(
+ "Cannot set '{}', not existing '{}'".format(k, k_item)
+ )
+ populated = True
+ db_nested[k_item_prev] = {k_item: None}
+ else: # number, string, boolean, ... or list but with not integer key
+ raise DbException(
+ "Cannot set '{}' on existing '{}={}'".format(
+ k, k_item_prev, db_nested[k_item_prev]
+ )
+ )
+ db_nested = db_nested[k_item_prev]
+ k_item_prev = k_item
+ return db_nested, k_item_prev, populated
+
+ updated = False
+ try:
+ if update_dict:
+ for dot_k, v in update_dict.items():
+ dict_to_update, key_to_update, _ = _iterate_keys(dot_k, db_item)
+ dict_to_update[key_to_update] = v
+ updated = True
+ if unset:
+ for dot_k in unset:
+ try:
+ dict_to_update, key_to_update, _ = _iterate_keys(
+ dot_k, db_item, populate=False
+ )
+ del dict_to_update[key_to_update]
+ updated = True
+ except Exception as unset_error:
+ self.logger.error(f"{unset_error} occured while updating DB.")
+ if pull:
+ for dot_k, v in pull.items():
+ try:
+ dict_to_update, key_to_update, _ = _iterate_keys(
+ dot_k, db_item, populate=False
+ )
+ except Exception as pull_error:
+ self.logger.error(f"{pull_error} occured while updating DB.")
+ continue
+
+ if key_to_update not in dict_to_update:
+ continue
+ if not isinstance(dict_to_update[key_to_update], list):
+ raise DbException(
+ "Cannot pull '{}'. Target is not a list".format(dot_k)
+ )
+ while v in dict_to_update[key_to_update]:
+ dict_to_update[key_to_update].remove(v)
+ updated = True
+ if pull_list:
+ for dot_k, v in pull_list.items():
+ if not isinstance(v, list):
+ raise DbException(
+ "Invalid content at pull_list, '{}' must be an array".format(
+ dot_k
+ ),
+ http_code=HTTPStatus.BAD_REQUEST,
+ )
+ try:
+ dict_to_update, key_to_update, _ = _iterate_keys(
+ dot_k, db_item, populate=False
+ )
+ except Exception as iterate_error:
+ self.logger.error(
+ f"{iterate_error} occured while iterating keys in db update."
+ )
+ continue
+
+ if key_to_update not in dict_to_update:
+ continue
+ if not isinstance(dict_to_update[key_to_update], list):
+ raise DbException(
+ "Cannot pull_list '{}'. Target is not a list".format(dot_k)
+ )
+ for single_v in v:
+ while single_v in dict_to_update[key_to_update]:
+ dict_to_update[key_to_update].remove(single_v)
+ updated = True
+ if push:
+ for dot_k, v in push.items():
+ dict_to_update, key_to_update, populated = _iterate_keys(
+ dot_k, db_item
+ )
+ if (
+ isinstance(dict_to_update, dict)
+ and key_to_update not in dict_to_update
+ ):
+ dict_to_update[key_to_update] = [v]
+ updated = True
+ elif populated and dict_to_update[key_to_update] is None:
+ dict_to_update[key_to_update] = [v]
+ updated = True
+ elif not isinstance(dict_to_update[key_to_update], list):
+ raise DbException(
+ "Cannot push '{}'. Target is not a list".format(dot_k)
+ )
+ else:
+ dict_to_update[key_to_update].append(v)
+ updated = True
+ if push_list:
+ for dot_k, v in push_list.items():
+ if not isinstance(v, list):
+ raise DbException(
+ "Invalid content at push_list, '{}' must be an array".format(
+ dot_k
+ ),
+ http_code=HTTPStatus.BAD_REQUEST,
+ )
+ dict_to_update, key_to_update, populated = _iterate_keys(
+ dot_k, db_item
+ )
+ if (
+ isinstance(dict_to_update, dict)
+ and key_to_update not in dict_to_update
+ ):
+ dict_to_update[key_to_update] = v.copy()
+ updated = True
+ elif populated and dict_to_update[key_to_update] is None:
+ dict_to_update[key_to_update] = v.copy()
+ updated = True
+ elif not isinstance(dict_to_update[key_to_update], list):
+ raise DbException(
+ "Cannot push '{}'. Target is not a list".format(dot_k),
+ http_code=HTTPStatus.CONFLICT,
+ )
+ else:
+ dict_to_update[key_to_update] += v
+ updated = True
+
+ return updated
+ except DbException:
+ raise
+ except Exception as e: # TODO refine
+ raise DbException(str(e))
+
+ def set_one(
+ self,
+ table,
+ q_filter,
+ update_dict,
+ fail_on_empty=True,
+ unset=None,
+ pull=None,
+ push=None,
+ push_list=None,
+ pull_list=None,
+ ):
+ """
+ Modifies an entry at database
+ :param table: collection or table
+ :param q_filter: Filter
+ :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value
+ :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case
+ it raises a DbException
+ :param unset: Plain dictionary with the content to be removed if exist. It is a dot separated keys, value is
+ ignored. If not exist, it is ignored
+ :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value
+ if exist in the array is removed. If not exist, it is ignored
+ :param pull_list: Same as pull but values are arrays where each item is removed from the array
+ :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value
+ is appended to the end of the array
+ :param push_list: Same as push but values are arrays where each item is and appended instead of appending the
+ whole array
+ :return: Dict with the number of entries modified. None if no matching is found.
+ """
+ with self.lock:
+ for i, db_item in self._find(table, self._format_filter(q_filter)):
+ updated = self._update(
+ db_item,
+ update_dict,
+ unset=unset,
+ pull=pull,
+ push=push,
+ push_list=push_list,
+ pull_list=pull_list,
+ )
+ return {"updated": 1 if updated else 0}
+ else:
+ if fail_on_empty:
+ raise DbException(
+ "Not found entry with _id='{}'".format(q_filter),
+ HTTPStatus.NOT_FOUND,
+ )
+ return None
+
+ def set_list(
+ self,
+ table,
+ q_filter,
+ update_dict,
+ unset=None,
+ pull=None,
+ push=None,
+ push_list=None,
+ pull_list=None,
+ ):
+ """Modifies al matching entries at database. Same as push. Do not fail if nothing matches"""
+ with self.lock:
+ updated = 0
+ found = 0
+ for _, db_item in self._find(table, self._format_filter(q_filter)):
+ found += 1
+ if self._update(
+ db_item,
+ update_dict,
+ unset=unset,
+ pull=pull,
+ push=push,
+ push_list=push_list,
+ pull_list=pull_list,
+ ):
+ updated += 1
+ # if not found and fail_on_empty:
+ # raise DbException("Not found entry with '{}'".format(q_filter), HTTPStatus.NOT_FOUND)
+ return {"updated": updated} if found else None
+