X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_ro%2Fdb_base.py;h=3b12f7457b31a837136ffb14bfc417c3016843c5;hb=ecc683920f5bd9065bae7b07e10953477fd0a3e8;hp=5bdd02ad4d5495e8cb45428342f70f0573309a21;hpb=3fcfdb7674436861d6ab0740972573293b9a355f;p=osm%2FRO.git diff --git a/osm_ro/db_base.py b/osm_ro/db_base.py index 5bdd02ad..3b12f745 100644 --- a/osm_ro/db_base.py +++ b/osm_ro/db_base.py @@ -195,6 +195,7 @@ class db_base(): highest_version_int, highest_version = row[0:2] return highest_version_int, highest_version except (mdb.Error, AttributeError) as e: + self.logger.error("Exception '{}' with command '{}'".format(e, cmd)) #self.logger.error("get_db_version DB Exception %d: %s. Command %s",e.args[0], e.args[1], cmd) self._format_error(e, tries) tries -= 1 @@ -297,7 +298,7 @@ class db_base(): If a dict it will generate 'key1="value1" AND key2="value2" AND ...'. If value is None, it will produce 'key is null' If value is a list or tuple, it will produce 'key="value[0]" OR key="value[1]" OR ...' - keys can be suffixed by >,<,<>,>=,<= so that this is used to compare key and value instead of "=" + keys can be suffixed by >,<,<>,>=,<=,' LIKE ' so that this is used to compare key and value instead of "=" The special keys "OR", "AND" with a dict value is used to create a nested WHERE If a list, each item will be a dictionary that will be concatenated with OR by default :param data: dict or list of dicts @@ -314,7 +315,7 @@ class db_base(): cmd.append("(" + self.__create_where(v, use_or=False) + ")") continue - if k.endswith(">") or k.endswith("<") or k.endswith("="): + if k.endswith(">") or k.endswith("<") or k.endswith("=") or k.endswith(" LIKE "): pass else: k += "=" @@ -498,24 +499,25 @@ class db_base(): self._format_error(e, tries) tries -= 1 + def _delete_row_by_id_internal(self, table, uuid): + cmd = "DELETE FROM {} WHERE uuid = '{}'".format(table, uuid) + self.logger.debug(cmd) + self.cur.execute(cmd) + deleted = self.cur.rowcount + # delete uuid + self.cur = self.con.cursor() + cmd = "DELETE FROM uuids WHERE root_uuid = '{}'".format(uuid) + self.logger.debug(cmd) + self.cur.execute(cmd) + return deleted + def delete_row_by_id(self, table, uuid): tries = 2 while tries: try: with self.con: - #delete host self.cur = self.con.cursor() - cmd = "DELETE FROM {} WHERE uuid = '{}'".format(table, uuid) - self.logger.debug(cmd) - self.cur.execute(cmd) - deleted = self.cur.rowcount - if deleted: - #delete uuid - self.cur = self.con.cursor() - cmd = "DELETE FROM uuids WHERE root_uuid = '{}'".format(uuid) - self.logger.debug(cmd) - self.cur.execute(cmd) - return deleted + return self._delete_row_by_id_internal(table, uuid) except (mdb.Error, AttributeError) as e: self._format_error(e, tries, "delete", "dependencies") tries -= 1 @@ -617,6 +619,7 @@ class db_base(): rows = self.cur.fetchall() return rows except (mdb.Error, AttributeError) as e: + self.logger.error("Exception '{}' with command '{}'".format(e, cmd)) self._format_error(e, tries) tries -= 1 @@ -652,10 +655,10 @@ class db_base(): self.logger.debug(cmd) self.cur.execute(cmd) number = self.cur.rowcount - if number==0: - return -HTTP_Not_Found, "No %s found with %s '%s'" %(error_item_text, what, uuid_name) - elif number>1 and not allow_serveral: - return -HTTP_Bad_Request, "More than one %s found with %s '%s'" %(error_item_text, what, uuid_name) + if number == 0: + raise db_base_Exception("No {} found with {} '{}'".format(error_item_text, what, uuid_name), http_code=HTTP_Not_Found) + elif number > 1 and not allow_serveral: + raise db_base_Exception("More than one {} found with {} '{}'".format(error_item_text, what, uuid_name), http_code=HTTP_Conflict) if allow_serveral: rows = self.cur.fetchall() else: @@ -667,7 +670,8 @@ class db_base(): def get_uuid(self, uuid): '''check in the database if this uuid is already present''' - for retry_ in range(0,2): + tries = 2 + while tries: try: with self.con: self.cur = self.con.cursor(mdb.cursors.DictCursor) @@ -675,9 +679,8 @@ class db_base(): rows = self.cur.fetchall() return self.cur.rowcount, rows except (mdb.Error, AttributeError) as e: - print "nfvo_db.get_uuid DB Exception %d: %s" % (e.args[0], e.args[1]) - r,c = self._format_error(e) - if r!=-HTTP_Request_Timeout or retry_==1: return r,c + self._format_error(e, tries) + tries -= 1 def get_uuid_from_name(self, table, name): '''Searchs in table the name and returns the uuid