diff --git a/ee/cli/plugins/debug.py b/ee/cli/plugins/debug.py index 0e9bab2f..1f8a47ea 100644 --- a/ee/cli/plugins/debug.py +++ b/ee/cli/plugins/debug.py @@ -473,7 +473,7 @@ class EEDebugController(CementBaseController): and (not self.app.pargs.all) and (not self.app.pargs.site_name)): if self.app.pargs.stop or self.app.pargs.start: - print("--start/stop option is deprecated in ee3.0.5") + print("--start/stop option is deprecated since ee3.0.5") self.app.args.print_help() else: self.app.args.print_help() diff --git a/ee/core/mysql.py b/ee/core/mysql.py index 8295c770..cbf9bb01 100644 --- a/ee/core/mysql.py +++ b/ee/core/mysql.py @@ -1,6 +1,6 @@ """EasyEngine MySQL core classes.""" import pymysql -from pymysql import connections, DatabaseError +from pymysql import connections, DatabaseError, Error import configparser from os.path import expanduser import sys @@ -9,60 +9,71 @@ from ee.core.logging import Log from ee.core.variables import EEVariables +class MySQLConnectionError(Exception): + """Custom Exception when MySQL server Not Connected""" + pass + + +class StatementExcecutionError(Exception): + """Custom Exception when any Query Fails to execute""" + pass + + +class DatabaseNotExistsError(Exception): + """Custom Exception when Database not Exist""" + pass + + class EEMysql(): """Method for MySQL connection""" + def connect(self): + """Makes connection with MySQL server""" + try: + connection = pymysql.connect(read_default_file='~/.my.cnf') + return connection + except ValueError as e: + Log.debug(self, str(e)) + raise MySQLConnectionError + except pymysql.err.InternalError as e: + Log.debug(self, str(e)) + raise MySQLConnectionError + + def dbConnection(self, db_name): + try: + connection = connections.Connection(db=db_name, + read_default_file='~/.my.cnf') + return connection + except DatabaseError as e: + if e.args[1] == '#42000Unknown database \'{0}\''.format(db_name): + raise DatabaseNotExistsError + else: + raise MySQLConnectionError + except pymysql.err.InternalError as e: + Log.debug(self, str(e)) + raise MySQLConnectionError + def execute(self, statement, errormsg='', log=True): """Get login details from ~/.my.cnf & Execute MySQL query""" - config = configparser.RawConfigParser() - cnfpath = expanduser("~")+"/.my.cnf" - if [cnfpath] == config.read(cnfpath): - user = config.get('client', 'user') - passwd = config.get('client', 'password') - try: - host = config.get('client', 'host') - except configparser.NoOptionError as e: - host = 'localhost' - - try: - port = config.get('client', 'port') - except configparser.NoOptionError as e: - port = '3306' - - try: - conn = pymysql.connect(host=host, port=int(port), - user=user, passwd=passwd) - cur = conn.cursor() - except Exception as e: - if errormsg: - Log.debug(self, '{0}' - .format(e)) - Log.error(self, '{0}' - .format(errormsg)) - else: - Log.debug(self, '{0}' - .format(e)) - Log.error(self, 'Unable to connect to database: {0}' - .format(e)) - - try: - if log: - Log.debug(self, "Executing MySQL statement: {0}" - .format(statement)) - - result = cur.execute(statement) - cur.close() - conn.close() - return result - - except Exception as e: - cur.close() - conn.close() - Log.debug(self, "{0}".format(e)) - if not errormsg: - Log.error(self, 'Unable to execute statement') - else: - Log.error(self, '{0}'.format(errormsg)) + connection = EEMysql.connect(self) + log and Log.debug(self, "Exceuting MySQL Statement : {0}" + .format(statement)) + try: + cursor = connection.cursor() + sql = statement + cursor.execute(sql) + + # connection is not autocommit by default. + # So you must commit to save your changes. + connection.commit() + except AttributeError as e: + Log.debug(self, str(e)) + raise StatementExcecutionError + except Error as e: + Log.debug(self, str(e)) + raise StatementExcecutionError + finally: + connection.close() def backupAll(self): import subprocess @@ -104,16 +115,10 @@ class EEMysql(): Log.error(self, "Error: process exited with status %s" % e) - def check_db_exists(self, db_name): + def check_db_exist(self, db_name): try: - connection = connections.Connection(db=db_name, - read_default_file='~/.my.cnf') - if connection: + if EEMysql.dbConnection(self, db_name): return True - except DatabaseError as e: - if e.args[1] == '#42000Unknown database \'{0}\''.format(db_name): - return False - else: - raise RuntimeError - except Exception as e: - Log.error(self, "Runtime Exception occured") + except DatabaseNotExistsError as e: + Log.debug(self, str(e)) + return False