databaseService = $databaseService; } protected function initializeDriver(Request $request) { // In a real app, these would come from encrypted session or token $config = $request->only(['host', 'username', 'password', 'database', 'port']); $driver = new MySqlDriver(); if (!$driver->connect($config)) { throw new \Exception("Could not connect to database."); } $this->databaseService->setDriver($driver); } public function databases(Request $request) { try { $this->initializeDriver($request); return Response::json($this->databaseService->getDatabases()); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function tables(Request $request, $database) { try { $request->merge(['database' => $database]); $this->initializeDriver($request); return Response::json($this->databaseService->getTables()); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function schema(Request $request, $table) { try { $this->initializeDriver($request); return Response::json($this->databaseService->getTableSchema($table)); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function data(Request $request, $table) { try { $this->initializeDriver($request); $skip = $request->get('skip', 0); $take = $request->get('take', 100); $data = $this->databaseService->getTableData($table, $take, $skip); $response = [ 'data' => $data, ]; if ($request->get('requireTotalCount') === 'true') { $response['totalCount'] = $this->databaseService->getTableCount($table); } return Response::json($response); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function execute(Request $request) { try { $this->initializeDriver($request); $sql = $request->get('query'); if (empty($sql)) { return Response::json(['error' => 'Query is empty'], 400); } $results = $this->databaseService->executeQuery($sql); return Response::json([ 'data' => $results, 'count' => count($results) ]); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function export(Request $request) { try { $this->initializeDriver($request); $config = $request->only(['host', 'username', 'password', 'database', 'port', 'table']); $filePath = $this->databaseService->export($config); return Response::download($filePath)->deleteFileAfterSend(true); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function import(Request $request) { try { if (!$request->hasFile('file')) { return Response::json(['error' => 'No file uploaded'], 400); } $this->initializeDriver($request); $config = $request->only(['host', 'username', 'password', 'database', 'port']); $file = $request->file('file'); $tempPath = $file->storeAs('temp', $file->getClientOriginalName()); $fullPath = storage_path('app/' . $tempPath); $this->databaseService->import($config, $fullPath); if (file_exists($fullPath)) { unlink($fullPath); } return Response::json(['message' => 'Database imported successfully']); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function metadata(Request $request, $database) { try { $request->merge(['database' => $database]); $this->initializeDriver($request); return Response::json($this->databaseService->getDatabaseMetadata($database)); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function tableMetadata(Request $request, $database, $table) { try { $request->merge(['database' => $database]); $this->initializeDriver($request); return Response::json($this->databaseService->getTableMetadata($database, $table)); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function truncate(Request $request, $table) { try { $this->initializeDriver($request); $this->databaseService->truncateTable($table); return Response::json(['message' => "Table '{$table}' truncated successfully"]); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } public function tablesMetadata(Request $request, $database) { try { $request->merge(['database' => $database]); $this->initializeDriver($request); return Response::json($this->databaseService->getTablesMetadata($database)); } catch (\Exception $e) { return Response::json(['error' => $e->getMessage()], 400); } } }