diff --git a/plugins/mysql-yum/class/orm.py b/plugins/mysql-yum/class/orm.py new file mode 100755 index 000000000..2c615c743 --- /dev/null +++ b/plugins/mysql-yum/class/orm.py @@ -0,0 +1,93 @@ +# coding: utf-8 + +import re +import os +import sys + + +from mysql import connector + +sys.path.append(os.getcwd() + "/class/core") +import mw + +# if mw.isAppleSystem(): +# cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\'' +# info = mw.execShell(cmd) +# p = "/usr/local/lib/" + info[0].strip() + "/site-packages" +# sys.path.append(p) + + +class ORM: + __DB_PASS = None + __DB_USER = 'root' + __DB_PORT = 3306 + __DB_HOST = 'localhost' + __DB_CONN = None + __DB_CUR = None + __DB_ERR = None + __DB_CNF = '/etc/my.cnf' + + def __Conn(self): + '''连接MYSQL数据库''' + try: + socket = '/www/server/mariadb/mysql.sock' + try: + myconf = mw.readFile(self.__DB_CNF) + rep = "port\s*=\s*([0-9]+)" + self.__DB_PORT = int(re.search(rep, myconf).groups()[0]) + except: + self.__DB_PORT = 3306 + + try: + self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) + except Exception as e: + self.__DB_HOST = '127.0.0.1' + self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) + + self.__DB_CUR = self.__DB_CONN.cursor() + return True + except Exception as e: + self.__DB_ERR = e + return False + + def setDbConf(self, conf): + self.__DB_CNF = conf + + def setPwd(self, pwd): + self.__DB_PASS = pwd + + def getPwd(self): + return self.__DB_PASS + + def execute(self, sql): + # 执行SQL语句返回受影响行 + if not self.__Conn(): + return self.__DB_ERR + try: + result = self.__DB_CUR.execute(sql) + self.__DB_CONN.commit() + self.__Close() + return result + except Exception as ex: + return ex + + def query(self, sql): + # 执行SQL语句返回数据集 + if not self.__Conn(): + return self.__DB_ERR + try: + self.__DB_CUR.execute(sql) + result = self.__DB_CUR.fetchall() + # 将元组转换成列表 + # data = list(map(list, result)) + self.__Close() + return result + except Exception as ex: + return ex + + def __Close(self): + # 关闭连接 + self.__DB_CUR.close() + self.__DB_CONN.close() diff --git a/plugins/mysql-yum/index.py b/plugins/mysql-yum/index.py index cbcbae301..148e2fc20 100755 --- a/plugins/mysql-yum/index.py +++ b/plugins/mysql-yum/index.py @@ -125,11 +125,13 @@ def pSqliteDb(dbname='databases'): def pMysqlDb(): - db = mysqlDb.mysqlDb() + sys.path.append(getPluginDir() + "/class") + import orm + + db = orm.ORM() db.__DB_CNF = getConf() db.setDbConf(getConf()) - db.setPwd(pSqliteDb('config').where( - 'id=?', (1,)).getField('mysql_root')) + db.setPwd(pSqliteDb('config').where('id=?', (1,)).getField('mysql_root')) return db