AddOn/SQL-ManagmentStudio_Online/index.php aktualisiert
Add CSV Download, JSON Download Excle Download on the Select Query
This commit is contained in:
parent
17eeb47d88
commit
e70798fe05
|
|
@ -385,10 +385,24 @@ function executeSQL($conn, $sql, $format = "json", $admin_pass = null, $realPass
|
||||||
$results = [];
|
$results = [];
|
||||||
|
|
||||||
if ($stmtType === "SELECT") {
|
if ($stmtType === "SELECT") {
|
||||||
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $results[] = $row; }
|
||||||
$results[] = $row;
|
|
||||||
|
// === JSON Export (Direkter Download) ===
|
||||||
|
if ($format === "json_download") {
|
||||||
|
// JSON-String erzeugen
|
||||||
|
$jsonData = json_encode($results, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
|
// Header für JSON-Download
|
||||||
|
header("Content-Type: application/json; charset=utf-8");
|
||||||
|
header("Content-Disposition: attachment; filename=" . $exporttableName . ".json");
|
||||||
|
header("Pragma: no-cache");
|
||||||
|
header("Expires: 0");
|
||||||
|
|
||||||
|
echo $jsonData;
|
||||||
|
sqlsrv_free_stmt($stmt);
|
||||||
|
exit; // wichtig: Skript beenden
|
||||||
}
|
}
|
||||||
|
|
||||||
// === BACKUP_SQL ===
|
// === BACKUP_SQL ===
|
||||||
if ($format === "backup_sql") {
|
if ($format === "backup_sql") {
|
||||||
$meta = sqlsrv_field_metadata($stmt);
|
$meta = sqlsrv_field_metadata($stmt);
|
||||||
|
|
@ -446,7 +460,76 @@ function executeSQL($conn, $sql, $format = "json", $admin_pass = null, $realPass
|
||||||
sqlsrv_free_stmt($stmt);
|
sqlsrv_free_stmt($stmt);
|
||||||
return $create . $inserts;
|
return $create . $inserts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === BACKUP_SQL Export (Direkter Download) ===
|
||||||
|
if ($format === "backup_sql_download") {
|
||||||
|
$meta = sqlsrv_field_metadata($stmt);
|
||||||
|
if ($meta === false) {
|
||||||
|
return "Fehler beim Lesen der Feld-Metadaten.";
|
||||||
|
}
|
||||||
|
|
||||||
|
$tableName = "ExportTable";
|
||||||
|
$create = "CREATE TABLE [$tableName] (\n";
|
||||||
|
$cols = [];
|
||||||
|
|
||||||
|
foreach ($meta as $field) {
|
||||||
|
$colName = $field['Name'];
|
||||||
|
switch ($field['Type']) {
|
||||||
|
case SQLSRV_SQLTYPE_INT:
|
||||||
|
case SQLSRV_SQLTYPE_BIGINT:
|
||||||
|
case SQLSRV_SQLTYPE_SMALLINT:
|
||||||
|
$colType = "INT";
|
||||||
|
break;
|
||||||
|
case SQLSRV_SQLTYPE_BIT:
|
||||||
|
$colType = "BIT";
|
||||||
|
break;
|
||||||
|
case SQLSRV_SQLTYPE_FLOAT:
|
||||||
|
case SQLSRV_SQLTYPE_REAL:
|
||||||
|
$colType = "FLOAT";
|
||||||
|
break;
|
||||||
|
case SQLSRV_SQLTYPE_DATETIME:
|
||||||
|
case SQLSRV_SQLTYPE_DATETIME2:
|
||||||
|
case SQLSRV_SQLTYPE_DATE:
|
||||||
|
$colType = "DATETIME";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$colType = "NVARCHAR(MAX)";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$cols[] = " [$colName] $colType";
|
||||||
|
}
|
||||||
|
$create .= implode(",\n", $cols) . "\n);\n\n";
|
||||||
|
|
||||||
|
$inserts = "";
|
||||||
|
foreach ($results as $row) {
|
||||||
|
$values = [];
|
||||||
|
foreach ($row as $val) {
|
||||||
|
if ($val === null) {
|
||||||
|
$values[] = "NULL";
|
||||||
|
} elseif (is_numeric($val)) {
|
||||||
|
$values[] = $val;
|
||||||
|
} elseif ($val instanceof DateTime) {
|
||||||
|
$values[] = "'" . $val->format("Y-m-d H:i:s") . "'";
|
||||||
|
} else {
|
||||||
|
$values[] = "'" . str_replace("'", "''", $val) . "'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$inserts .= "INSERT INTO [$tableName] VALUES (" . implode(", ", $values) . ");\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
$sqlDump = $create . $inserts;
|
||||||
|
|
||||||
|
// Header für SQL-Download
|
||||||
|
header("Content-Type: application/sql; charset=utf-8");
|
||||||
|
header("Content-Disposition: attachment; filename=" . $exporttableName . ".sql");
|
||||||
|
header("Pragma: no-cache");
|
||||||
|
header("Expires: 0");
|
||||||
|
|
||||||
|
echo $sqlDump;
|
||||||
|
sqlsrv_free_stmt($stmt);
|
||||||
|
exit; // wichtig: beenden
|
||||||
|
}
|
||||||
|
|
||||||
// === CSV ===
|
// === CSV ===
|
||||||
if ($format === "csv") {
|
if ($format === "csv") {
|
||||||
if (empty($results)) {
|
if (empty($results)) {
|
||||||
|
|
@ -466,7 +549,79 @@ function executeSQL($conn, $sql, $format = "json", $admin_pass = null, $realPass
|
||||||
sqlsrv_free_stmt($stmt);
|
sqlsrv_free_stmt($stmt);
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === EXCEL Export / EXCEL-HTML (Direkter Download) ===
|
||||||
|
if ($format === "excel_download") {
|
||||||
|
if (empty($results)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
$headers = array_keys($results[0]);
|
||||||
|
|
||||||
|
$output = "<table border='1'><tr>";
|
||||||
|
foreach ($headers as $header) {
|
||||||
|
$output .= "<th>" . htmlspecialchars($header) . "</th>";
|
||||||
|
}
|
||||||
|
$output .= "</tr>";
|
||||||
|
|
||||||
|
foreach ($results as $row) {
|
||||||
|
$output .= "<tr>";
|
||||||
|
foreach ($headers as $header) {
|
||||||
|
$val = $row[$header];
|
||||||
|
if ($val instanceof DateTime) {
|
||||||
|
$val = $val->format("Y-m-d H:i:s");
|
||||||
|
}
|
||||||
|
$output .= "<td>" . htmlspecialchars((string)$val) . "</td>";
|
||||||
|
}
|
||||||
|
$output .= "</tr>";
|
||||||
|
}
|
||||||
|
$output .= "</table>";
|
||||||
|
|
||||||
|
// Header für Excel-Download
|
||||||
|
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
|
||||||
|
header("Content-Disposition: attachment; filename=" . $exporttableName . ".xls");
|
||||||
|
header("Pragma: no-cache");
|
||||||
|
header("Expires: 0");
|
||||||
|
|
||||||
|
echo $output;
|
||||||
|
sqlsrv_free_stmt($stmt);
|
||||||
|
exit; // Wichtig: Script nach Download beenden
|
||||||
|
}
|
||||||
|
|
||||||
|
// === CSV Export (Direkter Download) ===
|
||||||
|
if ($format === "csv_download") {
|
||||||
|
if (empty($results)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// CSV erzeugen
|
||||||
|
$headers = array_keys($results[0]);
|
||||||
|
$output = implode(";", $headers) . "\n";
|
||||||
|
|
||||||
|
foreach ($results as $row) {
|
||||||
|
$escaped = array_map(function($val) {
|
||||||
|
if ($val === null) return "";
|
||||||
|
if ($val instanceof DateTime) {
|
||||||
|
$val = $val->format("Y-m-d H:i:s");
|
||||||
|
}
|
||||||
|
$val = str_replace('"', '""', $val); // Doppelte Anführungszeichen escapen
|
||||||
|
return '"' . $val . '"';
|
||||||
|
}, $row);
|
||||||
|
|
||||||
|
$output .= implode(";", $escaped) . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header für CSV-Download
|
||||||
|
header("Content-Type: text/csv; charset=utf-8");
|
||||||
|
header("Content-Disposition: attachment; filename=" . $exporttableName . ".csv");
|
||||||
|
header("Pragma: no-cache");
|
||||||
|
header("Expires: 0");
|
||||||
|
|
||||||
|
echo $output;
|
||||||
|
sqlsrv_free_stmt($stmt);
|
||||||
|
exit; // Wichtig: Skript nach Download beenden
|
||||||
|
}
|
||||||
|
|
||||||
// === Default (Array) ===
|
// === Default (Array) ===
|
||||||
sqlsrv_free_stmt($stmt);
|
sqlsrv_free_stmt($stmt);
|
||||||
return $results;
|
return $results;
|
||||||
|
|
@ -730,12 +885,16 @@ sqlsrv_close($conn);
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<textarea name="sql_query" placeholder="Gib dein SQL-Statement hier ein..."><?php if (isset($_POST['sql_query'])){ echo $_POST['sql_query']; } ?></textarea><br>
|
<textarea name="sql_query" placeholder="Gib dein SQL-Statement hier ein..."><?php if (isset($_POST['sql_query'])){ echo $_POST['sql_query']; } ?></textarea><br>
|
||||||
<select name="format">
|
<select name="format">
|
||||||
<option value="json">JSON </option>
|
<option value="json">JSON Anzeige</option>
|
||||||
<option value="csv">csv</option>
|
<option value="json_download">JSON Download</option>
|
||||||
<option value="backup_sql">Backup Table and Data</option>
|
<option value="csv">CSV Anzeige</option>
|
||||||
|
<option value="csv_download">CSV Download</option>
|
||||||
|
<option value="excel_download">Excle Download</option>
|
||||||
|
<option value="backup_sql">SQL Anzeige</option>
|
||||||
|
<option value="backup_sql_download">SQL Download</option>
|
||||||
</select>
|
</select>
|
||||||
<input type="password" name="admin_pass" placeholder="Admin Password"/>
|
<input type="password" name="admin_pass" placeholder="Admin Password"/>
|
||||||
<input type="text" name="exporttabelName" placeholder="Export Tablename" />
|
<input type="text" name="exporttabelName" placeholder="Exportname" value="<?php if(isset($_POST['exporttabelName'])){ echo $_POST['exporttabelName']; } ?>" />
|
||||||
<button type="submit">SQL ausführen</button>
|
<button type="submit">SQL ausführen</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue