From bb4eeda688c4db4c38aab10126a12cc7db6f5622 Mon Sep 17 00:00:00 2001 From: Soldy <4786022+Soldy@users.noreply.github.com> Date: Fri, 30 Aug 2024 20:33:43 +0100 Subject: [PATCH] clean command Signed-off-by: Soldy <4786022+Soldy@users.noreply.github.com> --- src/openPanthera/containers.py | 9 ++++ src/openPanthera/mariadblib.py | 93 ++++++++++++++++++++++++++++++---- src/openPanthera/schema.py | 1 + 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/src/openPanthera/containers.py b/src/openPanthera/containers.py index 684c292..aae89ba 100644 --- a/src/openPanthera/containers.py +++ b/src/openPanthera/containers.py @@ -48,11 +48,19 @@ short_commands = { 'b' : 'build', + 'c' : 'clean', 'd' : 'directory', 'm' : 'migrate', 's' : 'show' } +short_clean_commands = { + 'f' : 'function', + 'p' : 'procedure', + 't' : 'table', + 'v' : 'view' +} + short_directory_commands = { 'i' : 'init', 'c' : 'check', @@ -75,6 +83,7 @@ short_specific_commands = { 'directory' : short_directory_commands, + 'clean' : short_clean_commands, 'build' : short_types, 'migrate' : short_migrate_commands, 'show' : short_show_commands diff --git a/src/openPanthera/mariadblib.py b/src/openPanthera/mariadblib.py index a0f6636..d492c7e 100644 --- a/src/openPanthera/mariadblib.py +++ b/src/openPanthera/mariadblib.py @@ -96,7 +96,14 @@ def show(self, name:str): 'function' : self._showFunctions } shows[name]() - + def clean(self, name:str): + cleans = { + 'function' : self._deleteAllFunctions, + 'procedure' : self._deleteAllProcedures, + 'table' : self._deleteAllTables, + 'view' : self._deleteAllViews + } + cleans[name]() def initMigrationTable(self): self._cur.execute(_table_query) @@ -147,7 +154,8 @@ def _cleanBuildScript(self, type_:str, file_name_:str): ) self._conn.commit() - def _showProcedures(self): + def _listProcedures(self): + lista = [] self._cur.execute( "SHOW PROCEDURE STATUS WHERE db = ?", [ @@ -167,10 +175,26 @@ def _showProcedures(self): collation_connection, coll ) in self._cur: + lista.append(Name) + return lista + def _showProcedures(self): + for ( + Name + ) in self._listProcedures(): self._p(f"{Name}") - - - def _showFunctions(self): + def _deleteProcedure(self, procedure:str): + self._cur.execute( + f"DROP PROCEDURE `{procedure}`;" + ) + self._p(f"Delete procedure {procedure}") + self._conn.commit() + def _deleteAllProcedures(self): + for ( + Name + ) in self._listProcedures(): + self._deleteProcedure(Name) + def _listFunctions(self): + lista = [] self._cur.execute( "SHOW FUNCTION STATUS WHERE db = ?", [ @@ -190,9 +214,26 @@ def _showFunctions(self): collation_connection, coll ) in self._cur: + lista.append(Name) + return lista + def _showFunctions(self): + for ( + Name + ) in self._listFunctions(): self._p(f"{Name}") - - def _showViews(self): + def _deleteFunction(self, function:str): + self._cur.execute( + f"DROP FUNCTION `{function}`;" + ) + self._p(f"Delete function {function}") + self._conn.commit() + def _deleteAllFunctions(self): + for ( + Name + ) in self._listFunctions(): + self._deleteFunction(Name) + def _listViews(self): + lista = [] self._cur.execute( "SHOW FULL TABLES WHERE Table_Type = 'VIEW'" ) @@ -200,9 +241,26 @@ def _showViews(self): Name, Type ) in self._cur: + lista.append(Name) + return lista + def _showViews(self): + for ( + Name + ) in self._listViews(): self._p(f"{Name}") - - def _showTables(self): + def _deleteView(self, table:str): + self._cur.execute( + f"DROP VIEW IF EXISTS `{table}`;", + ) + self._p(f"Delete table {table}") + self._conn.commit() + def _deleteAllViews(self): + for ( + Name + ) in self._listViews(): + self._deleteView(Name) + def _listTables(self): + lista = [] self._cur.execute( "SHOW FULL TABLES WHERE Table_Type = 'BASE TABLE'" ) @@ -211,5 +269,22 @@ def _showTables(self): Type ) in self._cur: if Name != "panthera_migration": + lista.append(Name) + return lista + def _showTables(self): + for ( + Name + ) in self._listTables(): self._p(f"{Name}") + def _deleteTable(self, table:str): + self._cur.execute( + f"DROP TABLES IF EXISTS `{table}`;", + ) + self._p(f"Delete table {table}") + self._conn.commit() + def _deleteAllTables(self): + for ( + Name + ) in self._listTables(): + self._deleteTable(Name) diff --git a/src/openPanthera/schema.py b/src/openPanthera/schema.py index daecd9a..771c3ca 100644 --- a/src/openPanthera/schema.py +++ b/src/openPanthera/schema.py @@ -30,6 +30,7 @@ def __init__(self, schema:str, ui): 'migrate' : self._migrate.resolv, 'init' : self._init, 'build' : self._mariadb.build, + 'clean' : self._mariadb.clean, 'directory': self._directory.resolv, 'show' : self._mariadb.show }