From 3802e79d16f32434554b09dff4c865ecdae98b11 Mon Sep 17 00:00:00 2001 From: "olaf.braun" Date: Mon, 5 May 2025 19:30:22 +0200 Subject: [PATCH] =?UTF-8?q?AddOn/SQL-ManagmentStudio=5FOnline/inde.php=20h?= =?UTF-8?q?inzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AddOn/SQL-ManagmentStudio_Online/inde.php | 392 ++++++++++++++++++++++ 1 file changed, 392 insertions(+) create mode 100644 AddOn/SQL-ManagmentStudio_Online/inde.php diff --git a/AddOn/SQL-ManagmentStudio_Online/inde.php b/AddOn/SQL-ManagmentStudio_Online/inde.php new file mode 100644 index 00000000..da6719fa --- /dev/null +++ b/AddOn/SQL-ManagmentStudio_Online/inde.php @@ -0,0 +1,392 @@ +, "; +/**************** Cathalog\User ****************/ + $ConnectionInfo = array( "Database"=>"", + "UID"=>"", + "PWD"=>"", + "Encrypt"=>false, + "TrustServerCertificate"=>false, + "CharacterSet" => "UTF-8"); + +/********************************************************/ + + +if(!isset($ServerName)){ http_response_code(403); exit(); } + +$conn = sqlsrv_connect($ServerName, $ConnectionInfo); +if ($conn === false) { + die(print_r(sqlsrv_errors(), true)); +} + +if (isset($_GET['structure'])) { + header('Content-Type: application/json; charset=utf-8'); + if(isset($_GET['format'])){ + if($_GET['format'] == "sql"){ + echo getDatabaseStructureExport($conn, "sql"); + }else{ + echo getDatabaseStructureExport($conn); + } + }else{ + echo getDatabaseStructureExport($conn); + } + + sqlsrv_close($conn); + exit; +} + +if (isset($_GET['export'], $_GET['table'])) { + header('Content-Type: application/json; charset=utf-8'); + if(isset($_GET['format'])){ + if($_GET['format'] == "sql"){ + echo exportTableData($conn, $_GET['table'], "sql"); + }else{ + echo exportTableData($conn, $_GET['table']); + } + }else{ + echo exportTableData($conn, $_GET['table']); + } + + sqlsrv_close($conn); + exit; +} + + + +function getDatabaseStructureExport($conn, $format = 'json') { + $tables = getTables($conn); + $structure = []; + + foreach ($tables as $table) { + $query = " + SELECT + COLUMN_NAME, + DATA_TYPE, + CHARACTER_MAXIMUM_LENGTH, + IS_NULLABLE, + COLUMN_DEFAULT + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_NAME = ?; + "; + + $stmt = sqlsrv_query($conn, $query, [$table]); + if (!$stmt) continue; + + $columns = []; + while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { + $columns[] = [ + 'name' => $row['COLUMN_NAME'], + 'type' => $row['DATA_TYPE'], + 'length' => $row['CHARACTER_MAXIMUM_LENGTH'], + 'nullable' => $row['IS_NULLABLE'], + 'default' => $row['COLUMN_DEFAULT'] + ]; + } + + sqlsrv_free_stmt($stmt); + $structure[$table] = $columns; + } + + // Ausgabe als JSON + if ($format === 'json') { + return json_encode($structure, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + } + + // Ausgabe als SQL CREATE TABLE Statements + if ($format === 'sql') { + $sqlText = "-- Exportierte Tabellenstruktur\n\n"; + foreach ($structure as $table => $columns) { + $sqlText .= "CREATE TABLE [$table] (\n"; + $lines = []; + + foreach ($columns as $col) { + $line = " [{$col['name']}] {$col['type']}"; + if (!is_null($col['length']) && $col['length'] > 0) { + $line .= "({$col['length']})"; + } elseif ($col['length'] == -1) { + $line .= "(MAX)"; + } + $line .= $col['nullable'] === 'NO' ? " NOT NULL" : " NULL"; + if (!is_null($col['default'])) { + $line .= " DEFAULT {$col['default']}"; + } + $lines[] = $line; + } + + $sqlText .= implode(",\n", $lines) . "\n);\n\n"; + } + + return $sqlText; + } + + return null; +} + +function exportTableData($conn, $table, $output_format = 'json') { + $query = "SELECT * FROM [$table]"; + $stmt = sqlsrv_query($conn, $query); + + if (!$stmt) { + return json_encode(['error' => sqlsrv_errors()], JSON_PRETTY_PRINT); + } + + $data = []; + $insertSQL = ''; + + // Spaltennamen holen + $columns = []; + $fieldMeta = sqlsrv_field_metadata($stmt); + foreach ($fieldMeta as $field) { + $columns[] = $field['Name']; + } + + // Zeilen verarbeiten + while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { + // JSON-Darstellung + if ($output_format === 'json' || $output_format === 'file') { + foreach ($row as $key => $value) { + if ($value instanceof DateTime) { + $row[$key] = $value->format('Y-m-d H:i:s'); + } + } + $data[] = $row; + } + + // SQL-Darstellung + if ($output_format === 'sql' || $output_format === 'file') { + $values = array_map(function ($v) { + if (is_null($v)) return "NULL"; + if ($v instanceof DateTime) return "'" . $v->format('Y-m-d H:i:s') . "'"; + return "'" . str_replace("'", "''", $v) . "'"; + }, array_values($row)); + + $insertSQL .= "INSERT INTO [$table] (" . implode(", ", $columns) . ") VALUES (" . implode(", ", $values) . ");\n"; + } + } + + sqlsrv_free_stmt($stmt); + + // Ausgabe als Datei auf dem Server + if ($output_format === 'file') { + $timestamp = date("Ymd_His"); + $dir = __DIR__ . "/db_backup"; + if (!is_dir($dir)) mkdir($dir, 0777, true); + + $jsonFile = "$dir/{$table}_$timestamp.json"; + $sqlFile = "$dir/{$table}_$timestamp.sql"; + + // Beide Formate speichern + file_put_contents($jsonFile, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); + file_put_contents($sqlFile, $insertSQL); + + return json_encode([ + "message" => "Daten gespeichert", + "json_path" => $jsonFile, + "sql_path" => $sqlFile + ], JSON_PRETTY_PRINT); + } + + // Direkt anzeigen + if ($output_format === 'json') { + return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + } + + return $insertSQL; +} + + +function getTables($conn) { + $query = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';"; + $stmt = sqlsrv_query($conn, $query); + $tables = []; + + while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { + $tables[] = $row['TABLE_NAME']; + } + + sqlsrv_free_stmt($stmt); + return $tables; +} + +function getColumns($conn, $table) { + $query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?;"; + $stmt = sqlsrv_query($conn, $query, [$table]); + $columns = []; + + while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { + $columns[] = $row['COLUMN_NAME']; + } + + sqlsrv_free_stmt($stmt); + return $columns; +} + +function getTableData($conn, $table) { + $query = "SELECT * FROM [$table];"; + $stmt = sqlsrv_query($conn, $query); + $rows = []; + + while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { + // $rows[] = $row; + foreach ($row as $key => $value) { + if ($value instanceof DateTime) { + $row[$key] = $value->format('Y-m-d H:i:s'); // Oder jedes gewünschte Format + } + } + $rows[] = $row; + } + + sqlsrv_free_stmt($stmt); + return $rows; +} + +function executeSQL($conn, $sql) { + $stmt = sqlsrv_query($conn, $sql); + + if ($stmt === false) { + return sqlsrv_errors(); // Falls ein Fehler auftritt, gebe die Fehler zurück + } else { + // Überprüfen, ob die Abfrage Ergebnisse liefert (z. B. bei SELECT) + $results = []; + if (strpos(strtoupper($sql), 'SELECT') !== false) { + // Nur bei SELECT-Abfragen die Ergebnisse holen + while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { + $results[] = $row; + } + sqlsrv_free_stmt($stmt); + return $results; // Rückgabe der Ergebnisse der SELECT-Abfrage + } else { + // Bei INSERT, UPDATE, DELETE etc. die Anzahl der betroffenen Zeilen zurückgeben + $affectedRows = sqlsrv_rows_affected($stmt); + sqlsrv_free_stmt($stmt); + return "Anzahl der betroffenen Zeilen: " . $affectedRows; // Gibt die betroffenen Zeilen zurück + } + } +} + + +$tables = getTables($conn); +sort($tables); // Tabellen alphabetisch sortieren +$currentTable = $_GET['table'] ?? null; +$tableData = $currentTable ? getTableData($conn, $currentTable) : []; +$tableColumns = $currentTable ? getColumns($conn, $currentTable) : []; + +$resultMessage = null; +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['sql_query'])) { + $sqlQuery = $_POST['sql_query']; + $resultMessage = executeSQL($conn, $sqlQuery); +} + +sqlsrv_close($conn); +?> + + + + Mini PHPMyAdmin for MSSQL + + + + + + +
+ + + +

Tabelle:

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
#
+ + + + + +

SQL Query Executor

+
+
+ +
+ + +

Ergebnis:

+
+ + + +
+ + +