From 6f544d9eb37a3cf9bc9da2277313104086b580fe Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Mon, 17 Oct 2022 10:47:08 -0400
Subject: [PATCH] Add ability to hide database connection output

---
 py_dbcn/connectors/core/core.py       | 15 ++++++++++++---
 py_dbcn/connectors/mysql/core.py      | 17 ++++++-----------
 py_dbcn/connectors/postgresql/core.py | 17 ++++++-----------
 3 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/py_dbcn/connectors/core/core.py b/py_dbcn/connectors/core/core.py
index 5eef81e..c728008 100644
--- a/py_dbcn/connectors/core/core.py
+++ b/py_dbcn/connectors/core/core.py
@@ -31,7 +31,11 @@ class AbstractDbConnector(ABC):
     and then be gradually moved to specific connectors as needed.)
     """
     @abstractmethod
-    def __init__(self, db_host, db_port, db_user, db_pass, db_name, *args, debug=False, **kwargs):
+    def __init__(
+        self,
+        db_host, db_port, db_user, db_pass, db_name, *args,
+        display_connection_output=True, debug=False, **kwargs,
+    ):
         logger.debug('Generating (core) Connector class.')
         db_port = int(db_port)
 
@@ -47,6 +51,7 @@ class AbstractDbConnector(ABC):
         # Initialize config.
         self._config = Config()
         # Values for connecting.
+        self._config.display_connection_output = display_connection_output
         self._config.db_host = db_host
         self._config.db_port = db_port
         self._config.db_user = db_user
@@ -100,13 +105,17 @@ class AbstractDbConnector(ABC):
         raise NotImplementedError('Please override the connection.create_connection() function.')
 
     def close_connection(self):
-        """Attempts to close database connection, if open."""
+        """Attempts to close database connection, if open.
+
+        :param display_output: Bool indicating if output should display.
+        """
         try:
             self._connection.close()
         except:
             pass
 
-        logger.info('Closed {0} database connection.'.format(self.db_type))
+        if self._config.display_connection_output:
+            logger.info('Closed {0} database connection.'.format(self._config.db_type))
 
     def _get_related_database_class(self):
         """
diff --git a/py_dbcn/connectors/mysql/core.py b/py_dbcn/connectors/mysql/core.py
index 9dd896c..7de7894 100644
--- a/py_dbcn/connectors/mysql/core.py
+++ b/py_dbcn/connectors/mysql/core.py
@@ -45,7 +45,10 @@ class MysqlDbConnector(AbstractDbConnector):
         self.create_connection()
 
     def create_connection(self, db_name=None):
-        """Attempts to create database connection, using config values."""
+        """Attempts to create database connection, using config values.
+
+        :param db_name: Name of database to connect to.
+        """
         if db_name is None or str(db_name).strip() == '':
             # Empty value provided. Fallback to config value.
             db_name = self._config.db_name
@@ -61,16 +64,8 @@ class MysqlDbConnector(AbstractDbConnector):
             db=db_name,
         )
 
-        logger.info('Created MySQL database connection.')
-
-    def close_connection(self):
-        """Attempts to close database connection, if open."""
-        try:
-            self._connection.close()
-        except:
-            pass
-
-        logger.info('Closed MySQL database connection.')
+        if self._config.display_connection_output:
+            logger.info('Created MySQL database connection.')
 
     def _get_related_database_class(self):
         """
diff --git a/py_dbcn/connectors/postgresql/core.py b/py_dbcn/connectors/postgresql/core.py
index ffa29bb..385c7d4 100644
--- a/py_dbcn/connectors/postgresql/core.py
+++ b/py_dbcn/connectors/postgresql/core.py
@@ -45,7 +45,10 @@ class PostgresqlDbConnector(AbstractDbConnector):
         self.create_connection()
 
     def create_connection(self, db_name=None):
-        """Attempts to create database connection, using config values."""
+        """Attempts to create database connection, using config values.
+
+        :param db_name: Name of database to connect to.
+        """
         if db_name is None or str(db_name).strip() == '':
             # Empty value provided. Fallback to config value.
             db_name = self._config.db_name
@@ -66,16 +69,8 @@ class PostgresqlDbConnector(AbstractDbConnector):
         # https://stackoverflow.com/a/68112827
         self._connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
 
-        logger.info('Created PostgreSQL database connection.')
-
-    def close_connection(self):
-        """Attempts to close database connection, if open."""
-        try:
-            self._connection.close()
-        except:
-            pass
-
-        logger.info('Closed PostgreSQL database connection.')
+        if self._config.display_connection_output:
+            logger.info('Created PostgreSQL database connection.')
 
     def _get_related_database_class(self):
         """
-- 
GitLab