feat: implement dynamic database connection service with MySQL driver and API schema endpoints

This commit is contained in:
Ümit Tunç
2026-04-24 07:21:59 +03:00
parent 1a75c32469
commit ce67df1067
12 changed files with 417 additions and 78 deletions
@@ -14,6 +14,16 @@ interface DatabaseDriverInterface
*/
public function query(string $sql, array $bindings = []): array;
/**
* Get the table schema.
*/
public function getTableSchema(string $table): array;
/**
* Get table data.
*/
public function getTableData(string $table, int $limit = 100, int $offset = 0): array;
/**
* Get the underlying connection instance.
*/
@@ -60,4 +60,16 @@ class SchemaController extends Controller
return Response::json(['error' => $e->getMessage()], 400);
}
}
public function data(Request $request, $table)
{
try {
$this->initializeDriver($request);
$limit = $request->get('limit', 100);
$offset = $request->get('offset', 0);
return Response::json($this->databaseService->getTableData($table, $limit, $offset));
} catch (\Exception $e) {
return Response::json(['error' => $e->getMessage()], 400);
}
}
}
@@ -64,6 +64,11 @@ class MySqlDriver implements DatabaseDriverInterface, SchemaDiscoveryInterface
return $this->query("DESCRIBE `{$table}`");
}
public function getTableData(string $table, int $limit = 100, int $offset = 0): array
{
return $this->query("SELECT * FROM `{$table}` LIMIT ? OFFSET ?", [$limit, $offset]);
}
public function getForeignKeys(string $table): array
{
$sql = "
+8
View File
@@ -74,4 +74,12 @@ class DatabaseService
}
throw new \Exception("Driver does not support schema discovery.");
}
/**
* Get table data.
*/
public function getTableData(string $table, int $limit = 100, int $offset = 0): array
{
return $this->getDriver()->getTableData($table, $limit, $offset);
}
}
+1
View File
@@ -12,4 +12,5 @@ Route::prefix('schema')->group(function () {
Route::get('/databases', [SchemaController::class, 'databases']);
Route::get('/tables/{database}', [SchemaController::class, 'tables']);
Route::get('/{table}', [SchemaController::class, 'schema']);
Route::get('/{table}/data', [SchemaController::class, 'data']);
});