Browse Source

Updated with custom exceptions

bugfixes
harshadyeola 10 years ago
parent
commit
65faa21e62
  1. 2
      ee/cli/plugins/debug.py
  2. 127
      ee/core/mysql.py

2
ee/cli/plugins/debug.py

@ -473,7 +473,7 @@ class EEDebugController(CementBaseController):
and (not self.app.pargs.all) and (not self.app.pargs.all)
and (not self.app.pargs.site_name)): and (not self.app.pargs.site_name)):
if self.app.pargs.stop or self.app.pargs.start: 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() self.app.args.print_help()
else: else:
self.app.args.print_help() self.app.args.print_help()

127
ee/core/mysql.py

@ -1,6 +1,6 @@
"""EasyEngine MySQL core classes.""" """EasyEngine MySQL core classes."""
import pymysql import pymysql
from pymysql import connections, DatabaseError from pymysql import connections, DatabaseError, Error
import configparser import configparser
from os.path import expanduser from os.path import expanduser
import sys import sys
@ -9,60 +9,71 @@ from ee.core.logging import Log
from ee.core.variables import EEVariables 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(): class EEMysql():
"""Method for MySQL connection""" """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): def execute(self, statement, errormsg='', log=True):
"""Get login details from ~/.my.cnf & Execute MySQL query""" """Get login details from ~/.my.cnf & Execute MySQL query"""
config = configparser.RawConfigParser() connection = EEMysql.connect(self)
cnfpath = expanduser("~")+"/.my.cnf" log and Log.debug(self, "Exceuting MySQL Statement : {0}"
if [cnfpath] == config.read(cnfpath): .format(statement))
user = config.get('client', 'user') try:
passwd = config.get('client', 'password') cursor = connection.cursor()
try: sql = statement
host = config.get('client', 'host') cursor.execute(sql)
except configparser.NoOptionError as e:
host = 'localhost' # connection is not autocommit by default.
# So you must commit to save your changes.
try: connection.commit()
port = config.get('client', 'port') except AttributeError as e:
except configparser.NoOptionError as e: Log.debug(self, str(e))
port = '3306' raise StatementExcecutionError
except Error as e:
try: Log.debug(self, str(e))
conn = pymysql.connect(host=host, port=int(port), raise StatementExcecutionError
user=user, passwd=passwd) finally:
cur = conn.cursor() connection.close()
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))
def backupAll(self): def backupAll(self):
import subprocess import subprocess
@ -104,16 +115,10 @@ class EEMysql():
Log.error(self, "Error: process exited with status %s" Log.error(self, "Error: process exited with status %s"
% e) % e)
def check_db_exists(self, db_name): def check_db_exist(self, db_name):
try: try:
connection = connections.Connection(db=db_name, if EEMysql.dbConnection(self, db_name):
read_default_file='~/.my.cnf')
if connection:
return True return True
except DatabaseError as e: except DatabaseNotExistsError as e:
if e.args[1] == '#42000Unknown database \'{0}\''.format(db_name): Log.debug(self, str(e))
return False return False
else:
raise RuntimeError
except Exception as e:
Log.error(self, "Runtime Exception occured")

Loading…
Cancel
Save