Files
nl-admin-api/app/Http/Controllers/Api/DatabaseController.php

309 lines
8.0 KiB
PHP
Raw Normal View History

2026-01-23 17:13:23 +08:00
<?php
namespace App\Http\Controllers\Api;
use App\BaseApp\BaseController;
use App\Service\DatabaseService;
use Exception;
use Illuminate\Http\JsonResponse;
class DatabaseController extends BaseController
{
public function __construct()
{
parent::__construct();
$this->service = DatabaseService::getInstance();
}
/**
* 获取表列表
* @Method GET
* @return JsonResponse
*/
public function listTables(): JsonResponse
{
return jok($this->service->listTables());
}
/**
* 获取表详细信息
* @Method GET
* @return JsonResponse
* @throws Exception
*/
public function getTableInfo(): JsonResponse
{
$tableName = request()->get('table_name');
if (!$tableName) {
return jerr('表名不能为空');
}
return jok($this->service->getTableInfo($tableName));
}
/**
* 获取表数据(分页)
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function getTableData(): JsonResponse
{
$tableName = request()->post('table_name');
$page = request()->post('page', 1);
$pageSize = request()->post('page_size', 20);
$where = request()->post('where', []);
if (!$tableName) {
return jerr('表名不能为空');
}
return jok($this->service->getTableData($tableName, $page, $pageSize, $where));
}
/**
* 更新表数据
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function updateTableData(): JsonResponse
{
$tableName = request()->post('table_name');
$data = request()->post('data', []);
$where = request()->post('where', []);
if (!$tableName) {
return jerr('表名不能为空');
}
if (empty($data)) {
return jerr('更新数据不能为空');
}
if (empty($where)) {
return jerr('WHERE条件不能为空');
}
$result = $this->service->updateTableData($tableName, $data, $where);
return jok($result, '更新成功');
}
/**
* 删除表数据
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function deleteTableData(): JsonResponse
{
$tableName = request()->post('table_name');
$where = request()->post('where', []);
if (!$tableName) {
return jerr('表名不能为空');
}
if (empty($where)) {
return jerr('WHERE条件不能为空');
}
$result = $this->service->deleteTableData($tableName, $where);
return jok($result, '删除成功');
}
/**
* 批量删除表数据
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function batchDeleteTableData(): JsonResponse
{
$tableName = request()->post('table_name');
$where = request()->post('where', []);
if (!$tableName) {
return jerr('表名不能为空');
}
if (empty($where)) {
return jerr('WHERE条件不能为空');
}
$count = $this->service->batchDeleteTableData($tableName, $where);
return jok(['count' => $count], "成功删除 {$count} 条记录");
}
/**
* 插入表数据
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function insertTableData(): JsonResponse
{
$tableName = request()->post('table_name');
$data = request()->post('data', []);
if (!$tableName) {
return jerr('表名不能为空');
}
if (empty($data)) {
return jerr('插入数据不能为空');
}
$id = $this->service->insertTableData($tableName, $data);
return jok(['id' => $id], '插入成功');
}
/**
* 更新表注释
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function updateTableComment(): JsonResponse
{
$tableName = request()->post('table_name');
$comment = request()->post('comment', '');
if (!$tableName) {
return jerr('表名不能为空');
}
$this->service->updateTableComment($tableName, $comment);
return jok(true, '更新成功');
}
/**
* 更新字段注释
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function updateColumnComment(): JsonResponse
{
$tableName = request()->post('table_name');
$columnName = request()->post('column_name');
$comment = request()->post('comment', '');
if (!$tableName) {
return jerr('表名不能为空');
}
if (!$columnName) {
return jerr('字段名不能为空');
}
$this->service->updateColumnComment($tableName, $columnName, $comment);
return jok(true, '更新成功');
}
/**
* 添加索引
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function addIndex(): JsonResponse
{
$tableName = request()->post('table_name');
$indexName = request()->post('index_name');
$columns = request()->post('columns', []);
$unique = request()->post('unique', false);
$type = request()->post('type', 'BTREE');
if (!$tableName) {
return jerr('表名不能为空');
}
if (!$indexName) {
return jerr('索引名不能为空');
}
if (empty($columns)) {
return jerr('索引列不能为空');
}
$this->service->addIndex($tableName, $indexName, $columns, $unique, $type);
return jok(true, '添加成功');
}
/**
* 删除索引
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function dropIndex(): JsonResponse
{
$tableName = request()->post('table_name');
$indexName = request()->post('index_name');
if (!$tableName) {
return jerr('表名不能为空');
}
if (!$indexName) {
return jerr('索引名不能为空');
}
$this->service->dropIndex($tableName, $indexName);
return jok(true, '删除成功');
}
/**
* 修改表结构
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function updateTableStructure(): JsonResponse
{
$tableName = request()->post('table_name');
$action = request()->post('action'); // add|modify|drop
$columnInfo = request()->post('column_info', []);
if (!$tableName) {
return jerr('表名不能为空');
}
if (!in_array($action, ['add', 'modify', 'drop'])) {
return jerr('操作类型不正确');
}
$this->service->updateTableStructure($tableName, $action, $columnInfo);
return jok(true, '操作成功');
}
/**
* 执行SQL查询仅SELECT
* @Method POST
* @return JsonResponse
* @throws Exception
*/
public function executeSqlQuery(): JsonResponse
{
$sql = request()->post('sql');
if (empty($sql)) {
return jerr('SQL语句不能为空');
}
$results = $this->service->executeSelectQuery($sql);
return jok($results, '查询成功');
}
/**
* 获取变更记录列表
* @Method GET
* @return JsonResponse
* @throws Exception
*/
public function getChangeLog(): JsonResponse
{
$page = request()->get('page', 1);
$pageSize = request()->get('page_size', 20);
$filters = [
'table_name' => request()->get('table_name'),
'operation_type' => request()->get('operation_type'),
'start_time' => request()->get('start_time'),
'end_time' => request()->get('end_time'),
];
$result = $this->service->getChangeLog((int)$page, (int)$pageSize, array_filter($filters));
return jok($result, '获取成功');
}
}