AddOn/SQL-ManagmentStudio_Online/inde.php hinzugefügt
This commit is contained in:
parent
e8b64c9e64
commit
3802e79d16
|
|
@ -0,0 +1,392 @@
|
|||
<?php
|
||||
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
|
||||
session_start();
|
||||
|
||||
/**************** Servername\Instance ****************/
|
||||
$ServerName = "<SQL Server HOST>, <SQL Port>";
|
||||
/**************** Cathalog\User ****************/
|
||||
$ConnectionInfo = array( "Database"=>"<Database>",
|
||||
"UID"=>"<Username>",
|
||||
"PWD"=>"<Password>",
|
||||
"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);
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Mini PHPMyAdmin for MSSQL</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; display: flex; }
|
||||
#sidebar { width: 250px; background-color: #f0f0f0; padding: 20px; height: 100vh; }
|
||||
#content { flex-grow: 1; padding: 20px; }
|
||||
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
|
||||
th, td { padding: 8px; border: 1px solid #ddd; }
|
||||
th { background-color: #f2f2f2; }
|
||||
a { text-decoration: none; color: blue; }
|
||||
textarea { width: 100%; height: 150px; margin-top: 10px; }
|
||||
.navbar { background-color: #007bff; padding: 10px; color: white; }
|
||||
.navbar a { color: white; margin-right: 10px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="sidebar">
|
||||
<h2>Tabellen</h2>
|
||||
<ul>
|
||||
<?php foreach ($tables as $table): ?>
|
||||
<li><a href="?table=<?= $table ?>"><?= $table ?></a></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div id="content">
|
||||
<div class="navbar">
|
||||
<a href="?">Home</a>
|
||||
<a href="?sql=1">SQL</a>
|
||||
</div>
|
||||
|
||||
<?php if ($currentTable): ?>
|
||||
<h2>Tabelle: <?= htmlspecialchars($currentTable) ?></h2>
|
||||
<table id="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<?php foreach ($tableColumns as $column): ?>
|
||||
<th><?= htmlspecialchars($column) ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<tr>
|
||||
<th></th> <!-- Platzhalter für die ID-Spalte -->
|
||||
<?php foreach ($tableColumns as $column): ?>
|
||||
<th><input type="text" class="column-filter" placeholder="Filtern..."></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php $rowNumber = 1; ?>
|
||||
<?php foreach ($tableData as $row): ?>
|
||||
<tr>
|
||||
<td><?= $rowNumber++; ?></td>
|
||||
<?php foreach ($tableColumns as $column): ?>
|
||||
<td><?= htmlspecialchars($row[$column] ?? '') ?></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const filters = document.querySelectorAll('.column-filter');
|
||||
const table = document.getElementById('data-table');
|
||||
const rows = Array.from(table.querySelectorAll('tbody tr'));
|
||||
|
||||
filters.forEach((filter, index) => {
|
||||
filter.addEventListener('input', function () {
|
||||
const filterValue = this.value.toLowerCase();
|
||||
|
||||
rows.forEach(row => {
|
||||
const cell = row.cells[index + 1]; // +1 wegen der ID-Spalte
|
||||
if (cell) {
|
||||
const cellText = cell.textContent.toLowerCase();
|
||||
row.style.display = cellText.includes(filterValue) ? '' : 'none';
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (isset($_GET['sql'])): ?>
|
||||
<h2>SQL Query Executor</h2>
|
||||
<form method="post">
|
||||
<textarea name="sql_query" placeholder="Gib dein SQL-Statement hier ein..."></textarea><br>
|
||||
<button type="submit">SQL ausführen</button>
|
||||
</form>
|
||||
|
||||
<?php if ($resultMessage): ?>
|
||||
<h3>Ergebnis:</h3>
|
||||
<pre><?= is_array($resultMessage) ? print_r(json_encode($resultMessage, JSON_PRETTY_PRINT), true) : htmlspecialchars($resultMessage) ?></pre>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue