feat: implement MySQL driver for database management, schema discovery, and CRUD operations
This commit is contained in:
@@ -177,6 +177,67 @@ class SchemaController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function drop(Request $request, $table)
|
||||
{
|
||||
try {
|
||||
$this->initializeDriver($request);
|
||||
$this->databaseService->dropTable($table);
|
||||
return Response::json(['message' => "Table '{$table}' dropped successfully"]);
|
||||
} catch (\Exception $e) {
|
||||
return Response::json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function optimize(Request $request, $table)
|
||||
{
|
||||
try {
|
||||
$this->initializeDriver($request);
|
||||
$this->databaseService->optimizeTable($table);
|
||||
return Response::json(['message' => "Table '{$table}' optimized successfully"]);
|
||||
} catch (\Exception $e) {
|
||||
return Response::json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function bulkAction(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'tables' => 'required|array',
|
||||
'action' => 'required|string|in:truncate,drop,optimize',
|
||||
'database' => 'required|string'
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->initializeDriver($request);
|
||||
$tables = $request->tables;
|
||||
$action = $request->action;
|
||||
$results = [];
|
||||
|
||||
foreach ($tables as $table) {
|
||||
try {
|
||||
switch ($action) {
|
||||
case 'truncate':
|
||||
$this->databaseService->truncateTable($table);
|
||||
break;
|
||||
case 'drop':
|
||||
$this->databaseService->dropTable($table);
|
||||
break;
|
||||
case 'optimize':
|
||||
$this->databaseService->optimizeTable($table);
|
||||
break;
|
||||
}
|
||||
$results[$table] = 'success';
|
||||
} catch (\Exception $e) {
|
||||
$results[$table] = 'error: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
return Response::json(['message' => 'Bulk operation completed', 'results' => $results]);
|
||||
} catch (\Exception $e) {
|
||||
return Response::json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function tablesMetadata(Request $request, $database)
|
||||
{
|
||||
try {
|
||||
|
||||
@@ -265,6 +265,18 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
public function dropTable(string $table): bool
|
||||
{
|
||||
DB::connection($this->connectionName)->statement("DROP TABLE `{$table}`");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function optimizeTable(string $table): bool
|
||||
{
|
||||
DB::connection($this->connectionName)->statement("OPTIMIZE TABLE `{$table}`");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getTablesMetadata(string $database): array
|
||||
{
|
||||
$sql = "
|
||||
|
||||
@@ -139,6 +139,16 @@ class DatabaseService
|
||||
return $this->getDriver()->truncateTable($table);
|
||||
}
|
||||
|
||||
public function dropTable(string $table): bool
|
||||
{
|
||||
return $this->getDriver()->dropTable($table);
|
||||
}
|
||||
|
||||
public function optimizeTable(string $table): bool
|
||||
{
|
||||
return $this->getDriver()->optimizeTable($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get metadata for all tables in a database.
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,9 @@ Route::prefix('schema')->group(function () {
|
||||
Route::get('/metadata/{database}/tables', [SchemaController::class, 'tablesMetadata']);
|
||||
Route::get('/metadata/{database}/{table}', [SchemaController::class, 'tableMetadata']);
|
||||
Route::post('/truncate/{table}', [SchemaController::class, 'truncate']);
|
||||
Route::post('/drop/{table}', [SchemaController::class, 'drop']);
|
||||
Route::post('/optimize/{table}', [SchemaController::class, 'optimize']);
|
||||
Route::post('/bulk-action', [SchemaController::class, 'bulkAction']);
|
||||
Route::get('/{table}', [SchemaController::class, 'schema']);
|
||||
Route::get('/{table}/data', [SchemaController::class, 'data']);
|
||||
Route::post('/execute', [SchemaController::class, 'execute']);
|
||||
|
||||
Reference in New Issue
Block a user