refactor:获取数据表的公共函数改为表管理类的方法并支持多数据库连接

This commit is contained in:
妙码生花 2024-02-26 20:45:12 +08:00
parent 34e5b974f9
commit 8a3347da09
5 changed files with 38 additions and 17 deletions

View File

@ -11,19 +11,6 @@ if (!function_exists('get_controller_list')) {
} }
} }
if (!function_exists('get_table_list')) {
function get_table_list(): array
{
$tableList = [];
$database = config('database.connections.mysql.database');
$tables = Db::query("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema = ? ", [$database]);
foreach ($tables as $row) {
$tableList[$row['TABLE_NAME']] = $row['TABLE_NAME'] . ($row['TABLE_COMMENT'] ? ' - ' . $row['TABLE_COMMENT'] : '');
}
return $tableList;
}
}
if (!function_exists('get_table_fields')) { if (!function_exists('get_table_fields')) {
function get_table_fields($table, $onlyCleanComment = false): array function get_table_fields($table, $onlyCleanComment = false): array
{ {

View File

@ -505,7 +505,7 @@ class Crud extends Backend
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
$tableList = get_table_list(); $tableList = TableManager::getTableList();
$tableExist = array_key_exists(TableManager::tableName($table), $tableList); $tableExist = array_key_exists(TableManager::tableName($table), $tableList);
$controllerExist = file_exists(root_path() . $controllerFile); $controllerExist = file_exists(root_path() . $controllerFile);
@ -518,6 +518,10 @@ class Crud extends Backend
$this->success(); $this->success();
} }
/**
* 数据表列表
* @throws Throwable
*/
public function databaseList(): void public function databaseList(): void
{ {
$tablePrefix = config('database.connections.mysql.prefix'); $tablePrefix = config('database.connections.mysql.prefix');
@ -535,7 +539,7 @@ class Crud extends Backend
]; ];
$outTables = []; $outTables = [];
$tables = get_table_list(); $tables = TableManager::getTableList();
$pattern = '/^' . $tablePrefix . '/i'; $pattern = '/^' . $tablePrefix . '/i';
foreach ($tables as $table => $tableComment) { foreach ($tables as $table => $tableComment) {
if (!preg_match($pattern, $table)) continue; if (!preg_match($pattern, $table)) continue;

View File

@ -3,6 +3,7 @@
namespace app\admin\controller\security; namespace app\admin\controller\security;
use Throwable; use Throwable;
use ba\TableManager;
use app\common\controller\Backend; use app\common\controller\Backend;
use app\admin\model\DataRecycle as DataRecycleModel; use app\admin\model\DataRecycle as DataRecycleModel;
@ -148,6 +149,10 @@ class DataRecycle extends Backend
return $outControllers; return $outControllers;
} }
/**
* 数据表列表
* @throws Throwable
*/
protected function getTableList(): array protected function getTableList(): array
{ {
$tablePrefix = config('database.connections.mysql.prefix'); $tablePrefix = config('database.connections.mysql.prefix');
@ -163,7 +168,7 @@ class DataRecycle extends Backend
]; ];
$outTables = []; $outTables = [];
$tables = get_table_list(); $tables = TableManager::getTableList();
$pattern = '/^' . $tablePrefix . '/i'; $pattern = '/^' . $tablePrefix . '/i';
foreach ($tables as $table => $tableComment) { foreach ($tables as $table => $tableComment) {
$table = preg_replace($pattern, '', $table); $table = preg_replace($pattern, '', $table);

View File

@ -3,6 +3,7 @@
namespace app\admin\controller\security; namespace app\admin\controller\security;
use Throwable; use Throwable;
use ba\TableManager;
use app\common\controller\Backend; use app\common\controller\Backend;
use app\admin\model\SensitiveData as SensitiveDataModel; use app\admin\model\SensitiveData as SensitiveDataModel;
@ -202,6 +203,10 @@ class SensitiveData extends Backend
return $outControllers; return $outControllers;
} }
/**
* 数据表列表
* @throws Throwable
*/
protected function getTableList(): array protected function getTableList(): array
{ {
$tablePrefix = config('database.connections.mysql.prefix'); $tablePrefix = config('database.connections.mysql.prefix');
@ -219,7 +224,7 @@ class SensitiveData extends Backend
]; ];
$outTables = []; $outTables = [];
$tables = get_table_list(); $tables = TableManager::getTableList();
$pattern = '/^' . $tablePrefix . '/i'; $pattern = '/^' . $tablePrefix . '/i';
foreach ($tables as $table => $tableComment) { foreach ($tables as $table => $tableComment) {
$table = preg_replace($pattern, '', $table); $table = preg_replace($pattern, '', $table);

View File

@ -60,6 +60,26 @@ class TableManager
return ($fullName ? $connection['prefix'] : '') . (preg_replace($pattern, '', $table)); return ($fullName ? $connection['prefix'] : '') . (preg_replace($pattern, '', $table));
} }
/**
* 数据表列表
* @param ?string $connection 连接配置标识
* @throws Exception
*/
public static function getTableList(?string $connection = null): array
{
$tableList = [];
$connection = self::getConnection($connection);
$connection = config("database.connections.$connection");
if (!is_array($connection)) {
throw new Exception('Database connection configuration error');
}
$tables = Db::query("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema = ? ", [$connection['database']]);
foreach ($tables as $row) {
$tableList[$row['TABLE_NAME']] = $row['TABLE_NAME'] . ($row['TABLE_COMMENT'] ? ' - ' . $row['TABLE_COMMENT'] : '');
}
return $tableList;
}
/** /**
* 系统是否存在多个数据库连接配置 * 系统是否存在多个数据库连接配置
*/ */