# -*- coding: utf-8 -*-
##
-# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
+# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
# This file is part of openmano
# All Rights Reserved.
#
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
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
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 += "="
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
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
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:
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)
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