Browse Source

Updated with custom exceptions

bugfixes
harshadyeola 10 years ago
parent
commit
65faa21e62
  1. 2
      ee/cli/plugins/debug.py
  2. 115
      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()

115
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 execute(self, statement, errormsg='', log=True): def connect(self):
"""Get login details from ~/.my.cnf & Execute MySQL query""" """Makes connection with MySQL server"""
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: try:
port = config.get('client', 'port') connection = pymysql.connect(read_default_file='~/.my.cnf')
except configparser.NoOptionError as e: return connection
port = '3306' 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: try:
conn = pymysql.connect(host=host, port=int(port), connection = connections.Connection(db=db_name,
user=user, passwd=passwd) read_default_file='~/.my.cnf')
cur = conn.cursor() return connection
except Exception as e: except DatabaseError as e:
if errormsg: if e.args[1] == '#42000Unknown database \'{0}\''.format(db_name):
Log.debug(self, '{0}' raise DatabaseNotExistsError
.format(e))
Log.error(self, '{0}'
.format(errormsg))
else: else:
Log.debug(self, '{0}' raise MySQLConnectionError
.format(e)) except pymysql.err.InternalError as e:
Log.error(self, 'Unable to connect to database: {0}' Log.debug(self, str(e))
.format(e)) raise MySQLConnectionError
try: def execute(self, statement, errormsg='', log=True):
if log: """Get login details from ~/.my.cnf & Execute MySQL query"""
Log.debug(self, "Executing MySQL statement: {0}" connection = EEMysql.connect(self)
log and Log.debug(self, "Exceuting MySQL Statement : {0}"
.format(statement)) .format(statement))
try:
cursor = connection.cursor()
sql = statement
cursor.execute(sql)
result = cur.execute(statement) # connection is not autocommit by default.
cur.close() # So you must commit to save your changes.
conn.close() connection.commit()
return result except AttributeError as e:
Log.debug(self, str(e))
except Exception as e: raise StatementExcecutionError
cur.close() except Error as e:
conn.close() Log.debug(self, str(e))
Log.debug(self, "{0}".format(e)) raise StatementExcecutionError
if not errormsg: finally:
Log.error(self, 'Unable to execute statement') connection.close()
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