feat: implement database management service layer with MySQL driver and API controllers

This commit is contained in:
Ümit Tunç
2026-04-28 20:16:28 +03:00
parent b5282df56f
commit 2e529bb61c
7 changed files with 153 additions and 15 deletions
@@ -78,4 +78,9 @@ interface DatabaseDriverInterface
* Perform batch update on a table.
*/
public function batchUpdate(string $table, array $changes): bool;
/**
* Create a new database.
*/
public function createDatabase(string $name, string $charset = 'utf8mb4', string $collation = 'utf8mb4_unicode_ci'): bool;
}
@@ -266,4 +266,25 @@ class SchemaController extends Controller
return Response::json(['error' => $e->getMessage()], 400);
}
}
public function createDatabase(Request $request)
{
$request->validate([
'name' => 'required|string|max:64',
'charset' => 'nullable|string|max:32',
'collation' => 'nullable|string|max:64',
]);
try {
$this->initializeDriver($request);
$name = $request->get('name');
$charset = $request->get('charset', 'utf8mb4');
$collation = $request->get('collation', 'utf8mb4_unicode_ci');
$this->databaseService->createDatabase($name, $charset, $collation);
return Response::json(['message' => "Database '{$name}' created successfully"]);
} catch (\Exception $e) {
return Response::json(['error' => $e->getMessage()], 400);
}
}
}
@@ -448,4 +448,10 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
fclose($handle);
return $path;
}
public function createDatabase(string $name, string $charset = 'utf8mb4', string $collation = 'utf8mb4_unicode_ci'): bool
{
DB::connection($this->connectionName)->statement("CREATE DATABASE `{$name}` CHARACTER SET {$charset} COLLATE {$collation}");
return true;
}
}
+8
View File
@@ -164,4 +164,12 @@ class DatabaseService
{
return $this->getDriver()->batchUpdate($table, $changes);
}
/**
* Create a new database.
*/
public function createDatabase(string $name, string $charset = 'utf8mb4', string $collation = 'utf8mb4_unicode_ci'): bool
{
return $this->getDriver()->createDatabase($name, $charset, $collation);
}
}