Skip to content

Commit

Permalink
add drop all also to single entity delete if table is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
chicco785 committed Sep 22, 2020
1 parent ef77124 commit b80d3da
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions src/translators/sql_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,18 @@ def delete_entity(self, entity_id, entity_type=None, from_date=None,

# First delete entries from table
table_name = self._et2tn(entity_type, fiware_service)

# how many rows in total?
try:
self.cursor.execute("select count(*) from {}".format(table_name))
except Exception as e:
logging.error("{}".format(e))
return 0
# TODO why the result still keeps into account the deleted rows???
# this query was moved up to make it "consistent" also with db that
# may execute delete instantly (this does not seem the case of crate)
count = self.cursor.fetchall()[0][0]

where_clause = self._get_where_clause([entity_id, ],
from_date,
to_date,
Expand All @@ -1021,12 +1033,31 @@ def delete_entity(self, entity_id, entity_type=None, from_date=None,
logging.error("{}".format(e))
return 0

return self.cursor.rowcount
deleted_rows = self.cursor.rowcount

count = count - deleted_rows

if count == 0:
# Drop whole table
self._drop_table(table_name)

return deleted_rows

def delete_entities(self, entity_type, from_date=None, to_date=None,
fiware_service=None, fiware_servicepath=None):
table_name = self._et2tn(entity_type, fiware_service)

# how many rows in total?
try:
self.cursor.execute("select count(*) from {}".format(table_name))
except Exception as e:
logging.error("{}".format(e))
return 0
# TODO why the result still keeps into account the deleted rows???
# this query was moved up to make it "consistent" also with db that
# may execute delete instantly (this does not seem the case of crate)
count = self.cursor.fetchall()[0][0]

# Delete only requested range
entity_id = None
where_clause = self._get_where_clause(entity_id,
Expand All @@ -1042,24 +1073,20 @@ def delete_entities(self, entity_type, from_date=None, to_date=None,

deleted_rows = self.cursor.rowcount

# Drop whole table
try:
self.cursor.execute("select count(*) from {}".format(table_name))
except Exception as e:
logging.error("{}".format(e))
return 0
#TODO why the result still keeps into account the deleted rows???
count = self.cursor.fetchall()[0][0] - deleted_rows
count = count - deleted_rows

if count != 0:
return deleted_rows
if count == 0:
# Drop whole table
self._drop_table(table_name)

return deleted_rows

def _drop_table(self, table_name):
op = "drop table {}".format(table_name)
try:
self.cursor.execute(op)
except Exception as e:
logging.error("{}".format(e))
return 0

# Delete entry from metadata table
op = "delete from {} where table_name = ?".format(METADATA_TABLE_NAME)
Expand All @@ -1076,8 +1103,6 @@ def delete_entities(self, entity_type, from_date=None, to_date=None,
except Exception as e:
logging.error("{}".format(e))

return deleted_rows

def _get_entity_type(self, entity_id, fiware_service):
"""
Find the type of the given entity_id.
Expand Down

0 comments on commit b80d3da

Please sign in to comment.