diff --git a/app/admin/common.php b/app/admin/common.php index fbb4f03b..4ea75df8 100644 --- a/app/admin/common.php +++ b/app/admin/common.php @@ -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')) { function get_table_fields($table, $onlyCleanComment = false): array { diff --git a/app/admin/controller/crud/Crud.php b/app/admin/controller/crud/Crud.php index c1d16fe8..3d01542a 100644 --- a/app/admin/controller/crud/Crud.php +++ b/app/admin/controller/crud/Crud.php @@ -505,7 +505,7 @@ class Crud extends Backend $this->error($e->getMessage()); } - $tableList = get_table_list(); + $tableList = TableManager::getTableList(); $tableExist = array_key_exists(TableManager::tableName($table), $tableList); $controllerExist = file_exists(root_path() . $controllerFile); @@ -518,6 +518,10 @@ class Crud extends Backend $this->success(); } + /** + * 数据表列表 + * @throws Throwable + */ public function databaseList(): void { $tablePrefix = config('database.connections.mysql.prefix'); @@ -535,7 +539,7 @@ class Crud extends Backend ]; $outTables = []; - $tables = get_table_list(); + $tables = TableManager::getTableList(); $pattern = '/^' . $tablePrefix . '/i'; foreach ($tables as $table => $tableComment) { if (!preg_match($pattern, $table)) continue; diff --git a/app/admin/controller/security/DataRecycle.php b/app/admin/controller/security/DataRecycle.php index e4971731..00a39049 100644 --- a/app/admin/controller/security/DataRecycle.php +++ b/app/admin/controller/security/DataRecycle.php @@ -3,6 +3,7 @@ namespace app\admin\controller\security; use Throwable; +use ba\TableManager; use app\common\controller\Backend; use app\admin\model\DataRecycle as DataRecycleModel; @@ -148,6 +149,10 @@ class DataRecycle extends Backend return $outControllers; } + /** + * 数据表列表 + * @throws Throwable + */ protected function getTableList(): array { $tablePrefix = config('database.connections.mysql.prefix'); @@ -163,7 +168,7 @@ class DataRecycle extends Backend ]; $outTables = []; - $tables = get_table_list(); + $tables = TableManager::getTableList(); $pattern = '/^' . $tablePrefix . '/i'; foreach ($tables as $table => $tableComment) { $table = preg_replace($pattern, '', $table); diff --git a/app/admin/controller/security/SensitiveData.php b/app/admin/controller/security/SensitiveData.php index a4774668..9e17eecc 100644 --- a/app/admin/controller/security/SensitiveData.php +++ b/app/admin/controller/security/SensitiveData.php @@ -3,6 +3,7 @@ namespace app\admin\controller\security; use Throwable; +use ba\TableManager; use app\common\controller\Backend; use app\admin\model\SensitiveData as SensitiveDataModel; @@ -202,6 +203,10 @@ class SensitiveData extends Backend return $outControllers; } + /** + * 数据表列表 + * @throws Throwable + */ protected function getTableList(): array { $tablePrefix = config('database.connections.mysql.prefix'); @@ -219,7 +224,7 @@ class SensitiveData extends Backend ]; $outTables = []; - $tables = get_table_list(); + $tables = TableManager::getTableList(); $pattern = '/^' . $tablePrefix . '/i'; foreach ($tables as $table => $tableComment) { $table = preg_replace($pattern, '', $table); diff --git a/extend/ba/TableManager.php b/extend/ba/TableManager.php index 77250b6b..3804ef99 100644 --- a/extend/ba/TableManager.php +++ b/extend/ba/TableManager.php @@ -60,6 +60,26 @@ class TableManager 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; + } + /** * 系统是否存在多个数据库连接配置 */