This commit is contained in:
walkor 2022-12-26 15:22:32 +08:00
parent 4e6cd56577
commit b5ca8b20b1
10 changed files with 24 additions and 56 deletions

View File

@ -123,15 +123,6 @@ class Util
return $table;
}
/**
* 获取表前缀
* @return string
*/
public static function getTablePrefix(): string
{
return config('plugin.admin.database.connections.mysql.prefix', '');
}
/**
* 变量或数组中的元素只能是字母数字下划线组合
* @param $var
@ -378,10 +369,6 @@ class Util
public static function getSchema($table, $section = null)
{
Util::checkTableName($table);
$table_prefix = Util::getTablePrefix();
if (strpos($table, $table_prefix) !== 0) {
$table = "$table_prefix{$table}";
}
$database = config('database.connections')['plugin.admin.mysql']['database'];
$schema_raw = $section !== 'table' ? Util::db()->select("select * from information_schema.COLUMNS where TABLE_SCHEMA = '$database' and table_name = '$table'") : [];
$forms = [];

View File

@ -124,7 +124,7 @@ return [
'password' => '$password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'prefix' => 'wa_',
'prefix' => '',
'strict' => true,
'engine' => null,
],
@ -160,7 +160,7 @@ return [
// 数据库编码默认采用utf8
'charset' => 'utf8mb4',
// 数据库表前缀
'prefix' => 'wa_',
'prefix' => '',
// 断线重连
'break_reconnect' => true,
// 关闭SQL监听日志

View File

@ -67,14 +67,13 @@ class TableController extends Base
$field = 'TABLE_NAME';
}
$order = $order === 'asc' ? 'asc' : 'desc';
$table_prefix = Util::getTablePrefix();
$tables = Util::db()->select("SELECT TABLE_NAME,TABLE_COMMENT,ENGINE,TABLE_ROWS,CREATE_TIME,UPDATE_TIME,TABLE_COLLATION FROM information_schema.`TABLES` WHERE TABLE_SCHEMA='$database' AND TABLE_NAME like '$table_prefix%' order by $field $order");
$tables = Util::db()->select("SELECT TABLE_NAME,TABLE_COMMENT,ENGINE,TABLE_ROWS,CREATE_TIME,UPDATE_TIME,TABLE_COLLATION FROM information_schema.`TABLES` WHERE TABLE_SCHEMA='$database' order by $field $order");
if ($tables) {
$table_names = array_column($tables, 'TABLE_NAME');
$table_rows_count = [];
foreach ($table_names as $table_name) {
$table_rows_count[$table_name] = Util::db()->table($this->getTableName($table_name))->count();
$table_rows_count[$table_name] = Util::db()->table($table_name)->count();
}
foreach ($tables as $key => $table) {
$tables[$key]->TABLE_ROWS = $table_rows_count[$table->TABLE_NAME] ?? $table->TABLE_ROWS;
@ -97,10 +96,6 @@ class TableController extends Base
}
$data = $request->post();
$table_name = Util::filterAlphaNum($data['table']);
$table_prefix = Util::getTablePrefix();
if (strpos($table_name, $table_prefix) !== 0) {
return $this->json(1, "表名必须以{$table_prefix}开头");
}
$table_comment = Util::pdoQuote($data['table_comment']);
$columns = $data['columns'];
$forms = $data['forms'];
@ -146,7 +141,7 @@ class TableController extends Base
}
}
Util::schema()->create($this->getTableName($table_name), function (Blueprint $table) use ($columns) {
Util::schema()->create($table_name, function (Blueprint $table) use ($columns) {
$type_method_map = Util::methodControlMap();
foreach ($columns as $column) {
if (!isset($column['type'])) {
@ -165,7 +160,7 @@ class TableController extends Base
Util::db()->statement("ALTER TABLE `$table_name` COMMENT $table_comment");
// 索引
Util::schema()->table($this->getTableName($table_name), function (Blueprint $table) use ($keys) {
Util::schema()->table($table_name, function (Blueprint $table) use ($keys) {
foreach ($keys as $key) {
$name = $key['name'];
$columns = is_array($key['columns']) ? $key['columns'] : explode(',', $key['columns']);
@ -293,7 +288,7 @@ class TableController extends Base
}
$old_columns = Util::getSchema($table_name, 'columns');
Util::schema()->table($this->getTableName($table_name), function (Blueprint $table) use ($columns, $old_columns, $keys, $table_name) {
Util::schema()->table($table_name, function (Blueprint $table) use ($columns, $old_columns, $keys, $table_name) {
foreach ($columns as $column) {
$field = $column['field'];
// 新字段
@ -320,7 +315,7 @@ class TableController extends Base
}
$old_keys = Util::getSchema($table_name, 'keys');
Util::schema()->table($this->getTableName($table_name), function (Blueprint $table) use ($keys, $old_keys, $table_name) {
Util::schema()->table($table_name, function (Blueprint $table) use ($keys, $old_keys, $table_name) {
foreach ($keys as $key) {
$key_name = $key['name'];
$old_key = $old_keys[$key_name] ?? [];
@ -390,7 +385,7 @@ class TableController extends Base
{
$table_name = $request->input('table');
Util::checkTableName($table_name);
$prefix = Util::getTablePrefix();
$prefix = 'wa_';
$table_basename = strpos($table_name, $prefix) === 0 ? substr($table_name, strlen($prefix)) : $table_name;
$inflector = InflectorFactory::create()->build();
$model_class = $inflector->classify($inflector->singularize($table_basename));
@ -469,7 +464,7 @@ class TableController extends Base
$model_namespace = str_replace('/' , '\\', trim($model_path, '/'));
// 创建model
$this->createModel($model_class, $model_namespace, base_path($model_file), $this->getTableName($table_name));
$this->createModel($model_class, $model_namespace, base_path($model_file), $table_name);
$controller_suffix = $plugin ? config("plugin.$plugin.app.controller_suffix") : config('app.controller_suffix');
$controller_class = $controller_file_name;
@ -1164,7 +1159,7 @@ EOF;
$field = current($allow_column);
}
$order = $order === 'asc' ? 'asc' : 'desc';
$paginator = Util::db()->table($this->getTableName($table));
$paginator = Util::db()->table($table);
foreach ($request->get() as $column => $value) {
if ($value === '') {
continue;
@ -1253,7 +1248,7 @@ EOF;
if (isset($columns['updated_at']) && empty($data['updated_at'])) {
$data['updated_at'] = $datetime;
}
$id = Util::db()->table($this->getTableName($table))->insertGetId($data);
$id = Util::db()->table($table)->insertGetId($data);
return $this->json(0, $id);
}
@ -1320,7 +1315,7 @@ EOF;
if (isset($columns['updated_at']) && empty($data['updated_at'])) {
$data['updated_at'] = $datetime;
}
Util::db()->table($this->getTableName($table))->where($primary_key, $value)->update($data);
Util::db()->table($table)->where($primary_key, $value)->update($data);
return $this->json(0);
}
@ -1343,7 +1338,7 @@ EOF;
}
$primary_key = $primary_keys[0];
$value = (array)$request->post($primary_key);
Util::db()->table($this->getTableName($table))->whereIn($primary_key, $value)->delete();
Util::db()->table($table)->whereIn($primary_key, $value)->delete();
return $this->json(0);
}
@ -1359,15 +1354,15 @@ EOF;
if (!$tables) {
return $this->json(0, 'not found');
}
$prefix = Util::getTablePrefix();
$prefix = 'wa_';
$table_not_allow_drop = ["{$prefix}admins", "{$prefix}users", "{$prefix}options", "{$prefix}roles", "{$prefix}rules", "{$prefix}admin_roles", "{$prefix}uploads"];
if ($found = array_intersect($tables, $table_not_allow_drop)) {
return $this->json(400, implode(',', $found) . '不允许删除');
}
foreach ($tables as $table) {
Util::schema()->drop($this->getTableName($table));
Util::schema()->drop($table);
// 删除schema
Util::db()->table('options')->where('name', "table_form_schema_$table")->delete();
Util::db()->table('wa_options')->where('name', "table_form_schema_$table")->delete();
}
return $this->json(0, 'ok');
}
@ -1598,18 +1593,4 @@ EOF;
}
}
/**
* 获取表名(不带前缀)
* @param string $table_name
* @return false|string
*/
protected function getTableName(string $table_name)
{
$prefix = Util::getTablePrefix();
if (strpos($table_name, $prefix) === 0) {
return substr($table_name, strlen($prefix));
}
return $table_name;
}
}

View File

@ -25,7 +25,7 @@ class Admin extends Base
*
* @var string
*/
protected $table = 'admins';
protected $table = 'wa_admins';
/**
* The primary key associated with the table.

View File

@ -16,7 +16,7 @@ class AdminRole extends Base
*
* @var string
*/
protected $table = 'admin_roles';
protected $table = 'wa_admin_roles';
/**
* The primary key associated with the table.

View File

@ -17,7 +17,7 @@ class Option extends Base
*
* @var string
*/
protected $table = 'options';
protected $table = 'wa_options';
/**
* The primary key associated with the table.

View File

@ -18,7 +18,7 @@ class Role extends Base
*
* @var string
*/
protected $table = 'roles';
protected $table = 'wa_roles';
/**
* The primary key associated with the table.

View File

@ -23,7 +23,7 @@ class Rule extends Base
*
* @var string
*/
protected $table = 'rules';
protected $table = 'wa_rules';
/**
* The primary key associated with the table.

View File

@ -27,7 +27,7 @@ class Upload extends Base
*
* @var string
*/
protected $table = 'uploads';
protected $table = 'wa_uploads';
/**
* The primary key associated with the table.

View File

@ -34,7 +34,7 @@ class User extends Base
*
* @var string
*/
protected $table = 'users';
protected $table = 'wa_users';
/**
* The primary key associated with the table.