mirror of https://github.com/tp4a/teleport
temp.
parent
1d665769ee
commit
ba44d6c951
File diff suppressed because it is too large
Load Diff
|
@ -61,7 +61,7 @@ class TPDatabase:
|
||||||
|
|
||||||
# 看看数据库中是否存在指定的数据表(如果不存在,可能是一个空数据库文件),则可能是一个新安装的系统
|
# 看看数据库中是否存在指定的数据表(如果不存在,可能是一个空数据库文件),则可能是一个新安装的系统
|
||||||
# ret = self.query('SELECT COUNT(*) FROM `sqlite_master` WHERE `type`="table" AND `name`="{}account";'.format(self._table_prefix))
|
# ret = self.query('SELECT COUNT(*) FROM `sqlite_master` WHERE `type`="table" AND `name`="{}account";'.format(self._table_prefix))
|
||||||
ret = self.is_table_exists('group')
|
ret = self.is_table_exists('{}group'.format(self._table_prefix))
|
||||||
if ret is None or not ret:
|
if ret is None or not ret:
|
||||||
# if ret is None or ret[0][0] == 0:
|
# if ret is None or ret[0][0] == 0:
|
||||||
log.w('database need create.\n')
|
log.w('database need create.\n')
|
||||||
|
@ -81,41 +81,19 @@ class TPDatabase:
|
||||||
self.need_upgrade = True
|
self.need_upgrade = True
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
# DO TEST
|
||||||
|
self.alter_table('ts_account', [['account_id', 'id'], ['account_type', 'type']])
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# def init_sqlite(self, db_file):
|
|
||||||
# self._table_prefix = 'ts_'
|
|
||||||
# self._conn_pool = TPSqlitePool(db_file)
|
|
||||||
#
|
|
||||||
# if not os.path.exists(db_file):
|
|
||||||
# log.w('database need create.\n')
|
|
||||||
# self.need_create = True
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# # 看看数据库中是否存在指定的数据表(如果不存在,可能是一个空数据库文件),则可能是一个新安装的系统
|
|
||||||
# # ret = self.query('SELECT COUNT(*) FROM `sqlite_master` WHERE `type`="table" AND `name`="{}account";'.format(self._table_prefix))
|
|
||||||
# ret = self.is_table_exists('group')
|
|
||||||
# if ret is None or not ret:
|
|
||||||
# # if ret is None or ret[0][0] == 0:
|
|
||||||
# log.w('database need create.\n')
|
|
||||||
# self.need_create = True
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# # 尝试从配置表中读取当前数据库版本号(如果不存在,说明是比较旧的版本了)
|
|
||||||
# ret = self.query('SELECT `value` FROM {}config WHERE `name`="db_ver";'.format(self._table_prefix))
|
|
||||||
# if ret is None or 0 == len(ret) or ret[0][0] < TELEPORT_DATABASE_VERSION:
|
|
||||||
# log.w('database need upgrade.\n')
|
|
||||||
# self.need_upgrade = True
|
|
||||||
|
|
||||||
def is_table_exists(self, table_name):
|
def is_table_exists(self, table_name):
|
||||||
"""
|
"""
|
||||||
判断指定的表是否存在
|
判断指定的表是否存在
|
||||||
@param table_name: string
|
@param table_name: string
|
||||||
@return: None or Boolean
|
@return: None or Boolean
|
||||||
"""
|
"""
|
||||||
# return self._conn_pool.is_table_exists(table_name)
|
|
||||||
if self.db_source['type'] == self.DB_TYPE_SQLITE:
|
if self.db_source['type'] == self.DB_TYPE_SQLITE:
|
||||||
ret = self.query('SELECT COUNT(*) FROM `sqlite_master` WHERE `type`="table" AND `name`="{}{}";'.format(self._table_prefix, table_name))
|
ret = self.query('SELECT COUNT(*) FROM `sqlite_master` WHERE `type`="table" AND `name`="{}";'.format(table_name))
|
||||||
if ret is None:
|
if ret is None:
|
||||||
return None
|
return None
|
||||||
if len(ret) == 0:
|
if len(ret) == 0:
|
||||||
|
@ -126,6 +104,7 @@ class TPDatabase:
|
||||||
elif self.db_source['type'] == self.DB_TYPE_MYSQL:
|
elif self.db_source['type'] == self.DB_TYPE_MYSQL:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
log.e('Unknown database type.\n')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def query(self, sql):
|
def query(self, sql):
|
||||||
|
@ -149,18 +128,66 @@ class TPDatabase:
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def alter_table(self, table_names, field_names=None):
|
||||||
|
"""
|
||||||
|
修改表名称及字段名称
|
||||||
|
table_name: 如果是string,则指定要操作的表,如果是list,则第一个元素是要操作的表,第二个元素是此表改名的目标名称
|
||||||
|
fields_names: 如果为None,则不修改字段名,否则应该是一个list,其中每个元素是包含两个str的list,表示将此list第一个指定的字段改名为第二个指定的名称
|
||||||
|
@return: None or Boolean
|
||||||
|
"""
|
||||||
|
if self.db_source['type'] == self.DB_TYPE_SQLITE:
|
||||||
|
if not isinstance(table_names, list) and field_names is None:
|
||||||
|
log.w('nothing to do.\n')
|
||||||
|
return False
|
||||||
|
|
||||||
|
if isinstance(table_names, str):
|
||||||
|
old_table_name = table_names
|
||||||
|
new_table_name = table_names
|
||||||
|
elif isinstance(table_names, list) and len(table_names) == 2:
|
||||||
|
old_table_name = table_names[0]
|
||||||
|
new_table_name = table_names[1]
|
||||||
|
else:
|
||||||
|
log.w('invalid param.\n')
|
||||||
|
return False
|
||||||
|
|
||||||
|
if isinstance(field_names, list):
|
||||||
|
for i in field_names:
|
||||||
|
if not isinstance(i, list) or 2 != len(i):
|
||||||
|
log.w('invalid param.\n')
|
||||||
|
return False
|
||||||
|
|
||||||
|
if field_names is None:
|
||||||
|
# 仅数据表改名
|
||||||
|
return self.exec('ALTER TABLE `{}` RENAME TO `{}`;'.format(old_table_name, new_table_name))
|
||||||
|
else:
|
||||||
|
# sqlite不支持字段改名,所以需要通过临时表中转一下
|
||||||
|
|
||||||
|
# 先获取数据表的字段名列表
|
||||||
|
ret = self.query('SELECT * FROM `sqlite_master` WHERE `type`="table" AND `name`="{}";'.format(old_table_name))
|
||||||
|
log.w('-----\n')
|
||||||
|
log.w(ret)
|
||||||
|
log.w('\n')
|
||||||
|
|
||||||
|
# 先将数据表改名,成为一个临时表
|
||||||
|
# tmp_table_name = '{}_sqlite_tmp'.format(old_table_name)
|
||||||
|
# ret = self.exec('ALTER TABLE `{}` RENAME TO `{}`;'.format(old_table_name, tmp_table_name))
|
||||||
|
# if ret is None or not ret:
|
||||||
|
# return ret
|
||||||
|
|
||||||
|
pass
|
||||||
|
elif self.db_source['type'] == self.DB_TYPE_MYSQL:
|
||||||
|
log.e('mysql not supported yet.\n')
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
log.e('Unknown database type.\n')
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class TPDatabasePool:
|
class TPDatabasePool:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._locker = threading.RLock()
|
self._locker = threading.RLock()
|
||||||
self._connections = dict()
|
self._connections = dict()
|
||||||
|
|
||||||
# def is_table_exists(self, table_name):
|
|
||||||
# _conn = self._get_connect()
|
|
||||||
# if _conn is None:
|
|
||||||
# return None
|
|
||||||
# return self._is_table_exists(_conn, table_name)
|
|
||||||
|
|
||||||
def query(self, sql):
|
def query(self, sql):
|
||||||
_conn = self._get_connect()
|
_conn = self._get_connect()
|
||||||
if _conn is None:
|
if _conn is None:
|
||||||
|
@ -187,9 +214,6 @@ class TPDatabasePool:
|
||||||
def _do_connect(self):
|
def _do_connect(self):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# def _is_table_exists(self, conn, table_name):
|
|
||||||
# return None
|
|
||||||
|
|
||||||
def _do_query(self, conn, sql):
|
def _do_query(self, conn, sql):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -209,14 +233,6 @@ class TPSqlitePool(TPDatabasePool):
|
||||||
log.e('[sqlite] can not connect, does the database file correct?')
|
log.e('[sqlite] can not connect, does the database file correct?')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# def _is_table_exists(self, conn, table_name):
|
|
||||||
# ret = self._do_query(conn, 'SELECT COUNT(*) FROM `sqlite_master` WHERE `type`="table" AND `name`="{}{}";'.format(self._table_prefix, table_name))
|
|
||||||
# # ret = self.query('SELECT COUNT(*) FROM `sqlite_master` WHERE `type`="table" AND `name`="{}{}";'.format(self._table_prefix, table_name))
|
|
||||||
# if ret is None or ret[0][0] == 0:
|
|
||||||
# return False
|
|
||||||
# else:
|
|
||||||
# return True
|
|
||||||
|
|
||||||
def _do_query(self, conn, sql):
|
def _do_query(self, conn, sql):
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
/*! ywl_log v1.0.1, (c)2015 eomsoft.net */
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
var ywl_log_analysis = {
|
|
||||||
43:log_process_get_process_by_name_43,
|
|
||||||
61:log_analysis_file_list_61
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
function log_analysis_file_list_61(data) {
|
|
||||||
|
|
||||||
var message = data;
|
|
||||||
var _obj = JSON.parse(data);
|
|
||||||
var _file_path = _obj['path'];
|
|
||||||
if (_file_path != undefined){
|
|
||||||
message = _file_path;
|
|
||||||
}
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
function log_process_get_process_by_name_43(data) {
|
|
||||||
|
|
||||||
var message = data;
|
|
||||||
var _obj = JSON.parse(data);
|
|
||||||
var _process_name = _obj['Name'];
|
|
||||||
if (_process_name != undefined){
|
|
||||||
message = _process_name;
|
|
||||||
}
|
|
||||||
return message;
|
|
||||||
}
|
|
Loading…
Reference in New Issue