Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/data_query/sql_mysql.py

318 lines
8.4 KiB

1 year ago
# coding:utf-8
import sys
import io
import os
import time
import re
import pymongo
import json
web_dir = os.getcwd() + "/web"
if os.path.exists(web_dir):
sys.path.append(web_dir)
os.chdir(web_dir)
import core.mw as mw
1 year ago
def singleton(cls):
_instance = {}
def inner():
if cls not in _instance:
_instance[cls] = cls()
return _instance[cls]
return inner
@singleton
class nosqlMySQL():
__DB_PASS = None
__DB_USER = None
1 year ago
__DB_PORT = 3306
1 year ago
__DB_HOST = '127.0.0.1'
__DB_CONN = None
__DB_ERR = None
1 year ago
__DB_SOCKET = None
1 year ago
__DB_LOCAL = None
def __init__(self):
self.__config = self.get_options(None)
1 year ago
def conn(self):
1 year ago
if self.__DB_HOST in ['127.0.0.1', 'localhost']:
my_path = "{}/mysql".format(mw.getServerDir())
if not os.path.exists(my_path): return False
if not self.__DB_LOCAL:
1 year ago
# print(self.__config)
1 year ago
self.__DB_PORT = int(self.__config['port'])
1 year ago
self.__DB_USER = self.__config['username']
self.__DB_PASS = self.__config['password']
self.__DB_SOCKET = self.__config['socket']
1 year ago
try:
1 year ago
db = mw.getMyORM()
db.setPort(self.__DB_PORT)
db.setPwd(self.__DB_PASS)
db.setUser(self.__DB_USER)
if self.__DB_SOCKET != '':
db.setSocket(self.__DB_SOCKET)
return db
1 year ago
except Exception:
self.__DB_ERR = mw.get_error_info()
return False
1 year ago
def sqliteDb(self,dbname='databases'):
my_root_path = mw.getServerDir() +'/mysql'
name = 'mysql'
conn = mw.M(dbname).dbPos(my_root_path, name)
return conn
1 year ago
# 获取配置项
def get_options(self, get=None):
result = {}
1 year ago
my_cnf_path = "{}/mysql/etc/my.cnf".format(mw.getServerDir())
my_content = mw.readFile(my_cnf_path)
if not my_content: return False
mysql_pass = self.sqliteDb('config').where('id=?', (1,)).getField('mysql_root')
result['password'] = mysql_pass
result['username'] = 'root'
1 year ago
keys = ["bind_ip", "port"]
result['host'] = '127.0.0.1'
rep = 'port\s*=\s*(.*)'
1 year ago
port_re = re.search(rep, my_content)
if port_re:
result['port'] = int(port_re.groups()[0].strip())
else:
result['port'] = 3306
socket_rep = 'socket\s*=\s*(.*)'
socket_re = re.search(socket_rep, my_content)
if socket_re:
result['socket'] = socket_re.groups()[0].strip()
1 year ago
else:
1 year ago
result['socket'] = ''
1 year ago
return result
def set_host(self, host, port, name, username, password, prefix=''):
self.__DB_HOST = host
self.__DB_PORT = int(port)
self.__DB_NAME = name
if self.__DB_NAME: self.__DB_NAME = str(self.__DB_NAME)
self.__DB_USER = str(username)
self._USER = str(username)
self.__DB_PASS = str(password)
self.__DB_PREFIX = prefix
self.__DB_LOCAL = 1
return self
@singleton
class nosqlMySQLCtr():
def __init__(self):
pass
def getInstanceBySid(self, sid = 0):
instance = nosqlMySQL()
return instance
def getDbList(self, args):
sid = args['sid']
1 year ago
my_instance = self.getInstanceBySid(sid).conn()
if my_instance is False:
1 year ago
return mw.returnData(False,'无法链接')
result = {}
1 year ago
db_list = my_instance.query('show databases')
1 year ago
rlist = []
1 year ago
for x in db_list:
if not x['Database'] in ['information_schema', 'mysql', 'performance_schema','sys']:
rlist.append(x['Database'])
1 year ago
result['list'] = rlist
return mw.returnData(True,'ok', result)
1 year ago
def getTableList(self, args):
1 year ago
sid = args['sid']
1 year ago
db = args['db']
1 year ago
1 year ago
my_instance = self.getInstanceBySid(sid).conn()
if my_instance is False:
1 year ago
return mw.returnData(False,'无法链接')
1 year ago
sql = "select * from information_schema.tables where table_schema = '"+db+"'"
table_list = my_instance.query(sql)
# print(table_list)
rlist = []
for x in table_list:
# print(x['TABLE_NAME'])
rlist.append(x['TABLE_NAME'])
1 year ago
result = {}
1 year ago
result['list'] = rlist
1 year ago
return mw.returnData(True,'ok', result)
def getDataList(self, args):
sid = args['sid']
db = args['db']
1 year ago
table = args['table']
9 months ago
if table == '':
page_args = {}
page_args['count'] = 0
page_args['tojs'] = 'mysqlGetDataList'
page_args['p'] = 1
page_args['row'] = 10
rdata = {}
rdata['page'] = mw.getPage(page_args)
rdata['list'] = []
rdata['count'] = 0
return mw.returnData(True,'ok', rdata)
1 year ago
p = 1
size = 10
if 'p' in args:
p = args['p']
if 'size' in args:
size = args['size']
1 year ago
start_index = (p - 1) * size
1 year ago
1 year ago
args_where = {}
1 year ago
where_sql = ''
1 year ago
if 'where' in args:
args_where = args['where']
1 year ago
if 'field' in args_where:
if args_where['field'] == 'id' or args_where['field'].find('id')>-1:
where_sql = ' where '+args_where['field'] + " = '"+args_where['value']+"' "
else:
where_sql = ' where '+args_where['field'] + " like '%"+args_where['value']+"%' "
1 year ago
1 year ago
my_instance = self.getInstanceBySid(sid).conn()
if my_instance is False:
return mw.returnData(False,'无法链接')
1 year ago
1 year ago
my_instance.setDbName(db)
1 year ago
sql = 'select count(*) as num from ' + table + where_sql
# print(sql)
1 year ago
count_result = my_instance.query(sql)
count = count_result[0]['num']
1 year ago
1 year ago
sql = 'select * from ' + table + where_sql + ' limit '+str(start_index)+',10';
1 year ago
# print(sql)
result = my_instance.query(sql)
for i in range(len(result)):
for f in result[i]:
result[i][f] = str(result[i][f])
1 year ago
# print(result)
1 year ago
page_args = {}
page_args['count'] = count
1 year ago
page_args['tojs'] = 'mysqlGetDataList'
1 year ago
page_args['p'] = p
page_args['row'] = size
rdata = {}
rdata['page'] = mw.getPage(page_args)
rdata['list'] = result
rdata['count'] = count
rdata['soso_field'] = ''
if 'field' in args_where:
rdata['soso_field'] = args_where['field']
9 months ago
1 year ago
return mw.returnData(True,'ok', rdata)
1 year ago
1 year ago
def showProcessList(self,args):
sql = 'show processlist';
sid = args['sid']
1 year ago
1 year ago
my_instance = self.getInstanceBySid(sid).conn()
if my_instance is False:
return mw.returnData(False,'无法链接')
1 year ago
1 year ago
result = my_instance.query(sql)
rdata = {}
rdata['list'] = result
return mw.returnData(True,'ok', rdata)
1 year ago
1 year ago
def showStatusList(self,args):
sql = 'show status';
sid = args['sid']
my_instance = self.getInstanceBySid(sid).conn()
if my_instance is False:
return mw.returnData(False,'无法链接')
result = my_instance.query(sql)
rdata = {}
rdata['list'] = result
return mw.returnData(True,'ok', rdata)
def showStatsList(self, args):
sql = "show status like 'Com_%'";
sid = args['sid']
my_instance = self.getInstanceBySid(sid).conn()
if my_instance is False:
return mw.returnData(False,'无法链接')
result = my_instance.query(sql)
rdata = {}
rdata['list'] = result
return mw.returnData(True,'ok', rdata)
1 year ago
# ---------------------------------- run ----------------------------------
# 获取 mysql 列表
def get_db_list(args):
t = nosqlMySQLCtr()
return t.getDbList(args)
# 获取 mysql 列表
1 year ago
def get_table_list(args):
1 year ago
t = nosqlMySQLCtr()
1 year ago
return t.getTableList(args)
1 year ago
def get_data_list(args):
t = nosqlMySQLCtr()
return t.getDataList(args)
1 year ago
def get_proccess_list(args):
t = nosqlMySQLCtr()
return t.showProcessList(args)
1 year ago
def get_status_list(args):
t = nosqlMySQLCtr()
return t.showStatusList(args)
def get_stats_list(args):
t = nosqlMySQLCtr()
return t.showStatsList(args)
1 year ago
# 测试
def test(args):
sid = args['sid']
t = nosqlMySQLCtr()
print(t.get_options())
print("test")
return 'ok'