mdserver-web/class/plugin/orm.py

117 lines
4.1 KiB
Python
Raw Permalink Normal View History

2022-07-13 01:55:55 -04:00
# coding: utf-8
import re
import os
import sys
2022-08-01 13:29:19 -04:00
import pymysql.cursors
2022-07-13 01:55:55 -04:00
class ORM:
__DB_PASS = None
__DB_USER = 'root'
__DB_PORT = 3306
2022-08-13 06:35:30 -04:00
__DB_NAME = ''
2022-07-13 01:55:55 -04:00
__DB_HOST = 'localhost'
__DB_CONN = None
__DB_CUR = None
__DB_ERR = None
__DB_CNF = '/etc/my.cnf'
__DB_SOCKET = '/www/server/mysql/mysql.sock'
2022-07-27 09:59:35 -04:00
__DB_CHARSET = "utf8"
2022-07-27 09:28:24 -04:00
2022-07-13 01:55:55 -04:00
def __Conn(self):
2022-08-01 13:29:19 -04:00
'''连接数据库'''
2022-07-13 01:55:55 -04:00
try:
2022-07-13 03:39:24 -04:00
2022-08-03 03:06:10 -04:00
if os.path.exists(self.__DB_SOCKET):
try:
self.__DB_CONN = pymysql.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS,
2022-08-13 06:35:30 -04:00
database=self.__DB_NAME,
2022-08-03 03:06:10 -04:00
port=int(self.__DB_PORT), charset=self.__DB_CHARSET, connect_timeout=1,
unix_socket=self.__DB_SOCKET, cursorclass=pymysql.cursors.DictCursor)
except Exception as e:
self.__DB_HOST = '127.0.0.1'
self.__DB_CONN = pymysql.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS,
2022-08-13 06:35:30 -04:00
database=self.__DB_NAME,
2022-08-03 03:06:10 -04:00
port=int(self.__DB_PORT), charset=self.__DB_CHARSET, connect_timeout=1,
unix_socket=self.__DB_SOCKET, cursorclass=pymysql.cursors.DictCursor)
else:
try:
self.__DB_CONN = pymysql.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS,
2022-08-13 06:35:30 -04:00
database=self.__DB_NAME,
2022-08-03 03:06:10 -04:00
port=int(self.__DB_PORT), charset=self.__DB_CHARSET, connect_timeout=1,
cursorclass=pymysql.cursors.DictCursor)
except Exception as e:
self.__DB_HOST = '127.0.0.1'
self.__DB_CONN = pymysql.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS,
2022-08-13 06:35:30 -04:00
database=self.__DB_NAME,
2022-08-03 03:06:10 -04:00
port=int(self.__DB_PORT), charset=self.__DB_CHARSET, connect_timeout=1,
cursorclass=pymysql.cursors.DictCursor)
2022-07-13 01:55:55 -04:00
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
2022-07-13 05:21:14 -04:00
def setSocket(self, sock):
self.__DB_SOCKET = sock
2022-07-27 09:28:24 -04:00
def setCharset(self, charset):
self.__DB_CHARSET = charset
2023-03-01 11:49:44 -05:00
def setHost(self, host):
self.__DB_HOST = host
2022-07-13 05:21:14 -04:00
def setPort(self, port):
self.__DB_PORT = port
2022-09-06 05:45:56 -04:00
def setUser(self, user):
self.__DB_USER = user
2022-07-13 01:55:55 -04:00
def setPwd(self, pwd):
self.__DB_PASS = pwd
def getPwd(self):
return self.__DB_PASS
2022-08-13 06:35:30 -04:00
def setDbName(self, name):
self.__DB_NAME = name
2022-07-13 01:55:55 -04:00
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()
2022-08-01 16:11:08 -04:00
# print(result)
2022-07-13 01:55:55 -04:00
# 将元组转换成列表
2022-08-01 16:11:08 -04:00
# data = map(list, result)
2022-07-13 01:55:55 -04:00
self.__Close()
2022-08-01 16:11:08 -04:00
return result
2022-07-13 01:55:55 -04:00
except Exception as ex:
return ex
def __Close(self):
# 关闭连接
self.__DB_CUR.close()
self.__DB_CONN.close()