feat: implement database schema discovery and management services with frontend integration
This commit is contained in:
@@ -48,4 +48,34 @@ interface DatabaseDriverInterface
|
||||
* Get database metadata (charset, collation, size, etc.)
|
||||
*/
|
||||
public function getDatabaseMetadata(string $database): array;
|
||||
|
||||
/**
|
||||
* Get table metadata.
|
||||
*/
|
||||
public function getTableMetadata(string $database, string $table): array;
|
||||
|
||||
/**
|
||||
* Get metadata for all tables in a database.
|
||||
*/
|
||||
public function getTablesMetadata(string $database): array;
|
||||
|
||||
/**
|
||||
* Truncate a table.
|
||||
*/
|
||||
public function truncateTable(string $table): bool;
|
||||
|
||||
/**
|
||||
* Drop a table.
|
||||
*/
|
||||
public function dropTable(string $table): bool;
|
||||
|
||||
/**
|
||||
* Optimize a table.
|
||||
*/
|
||||
public function optimizeTable(string $table): bool;
|
||||
|
||||
/**
|
||||
* Perform batch update on a table.
|
||||
*/
|
||||
public function batchUpdate(string $table, array $changes): bool;
|
||||
}
|
||||
|
||||
@@ -248,4 +248,19 @@ class SchemaController extends Controller
|
||||
return Response::json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function batchUpdate(Request $request, $table)
|
||||
{
|
||||
$request->validate([
|
||||
'changes' => 'required|array',
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->initializeDriver($request);
|
||||
$this->databaseService->batchUpdate($table, $request->changes);
|
||||
return Response::json(['message' => 'Batch update successful']);
|
||||
} catch (\Exception $e) {
|
||||
return Response::json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,4 +302,41 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
|
||||
|
||||
return $this->query($sql, [$database]);
|
||||
}
|
||||
|
||||
public function batchUpdate(string $table, array $changes): bool
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
|
||||
// Find primary key
|
||||
$schema = $this->getTableSchema($table);
|
||||
$primaryKey = 'id'; // default
|
||||
foreach ($schema as $col) {
|
||||
if (($col->Key ?? '') === 'PRI') {
|
||||
$primaryKey = $col->Field;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$connection->beginTransaction();
|
||||
try {
|
||||
foreach ($changes as $change) {
|
||||
if (!isset($change[$primaryKey])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = $change[$primaryKey];
|
||||
$updateData = $change;
|
||||
unset($updateData[$primaryKey]);
|
||||
|
||||
if (empty($updateData)) continue;
|
||||
|
||||
$connection->table($table)->where($primaryKey, $id)->update($updateData);
|
||||
}
|
||||
$connection->commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$connection->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,4 +156,12 @@ class DatabaseService
|
||||
{
|
||||
return $this->getDriver()->getTablesMetadata($database);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform batch update on a table.
|
||||
*/
|
||||
public function batchUpdate(string $table, array $changes): bool
|
||||
{
|
||||
return $this->getDriver()->batchUpdate($table, $changes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ Route::prefix('schema')->group(function () {
|
||||
Route::post('/bulk-action', [SchemaController::class, 'bulkAction']);
|
||||
Route::get('/{table}', [SchemaController::class, 'schema']);
|
||||
Route::get('/{table}/data', [SchemaController::class, 'data']);
|
||||
Route::post('/{table}/batch-update', [SchemaController::class, 'batchUpdate']);
|
||||
Route::post('/execute', [SchemaController::class, 'execute']);
|
||||
Route::post('/export', [SchemaController::class, 'export']);
|
||||
Route::post('/import', [SchemaController::class, 'import']);
|
||||
|
||||
Reference in New Issue
Block a user