feat: table show pagination #25

This commit is contained in:
Chance 2023-03-31 20:28:06 +08:00
parent 095c5cd81c
commit f1257d99b6
No known key found for this signature in database
GPG Key ID: 3FA81A4CBF3B40B2

View File

@ -58,6 +58,9 @@ class TableController extends Base
*/ */
public function show(Request $request): Response public function show(Request $request): Response
{ {
$limit = (int)$request->get('limit', 10);
$page = (int)$request->get('page', 1);
$offset = ($page - 1) * $limit;
$database = config('database.connections')['plugin.admin.mysql']['database']; $database = config('database.connections')['plugin.admin.mysql']['database'];
$field = $request->get('field', 'TABLE_NAME'); $field = $request->get('field', 'TABLE_NAME');
$field = Util::filterAlphaNum($field); $field = Util::filterAlphaNum($field);
@ -67,7 +70,8 @@ class TableController extends Base
$field = 'TABLE_NAME'; $field = 'TABLE_NAME';
} }
$order = $order === 'asc' ? 'asc' : 'desc'; $order = $order === 'asc' ? 'asc' : 'desc';
$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"); $total = Util::db()->select("SELECT count(*)total FROM information_schema.`TABLES` WHERE TABLE_SCHEMA='$database'")[0]->total ?? 0;
$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 limit $offset,$limit");
if ($tables) { if ($tables) {
$table_names = array_column($tables, 'TABLE_NAME'); $table_names = array_column($tables, 'TABLE_NAME');
@ -80,7 +84,7 @@ class TableController extends Base
} }
} }
return $this->json(0, 'ok', $tables); return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $tables]);
} }
/** /**