|
|
@@ -17,14 +17,32 @@ if (empty($dopost)) $dopost = ''; |
|
|
|
//查看表结构 |
|
|
|
if ($dopost == "viewinfo") { |
|
|
|
CheckCSRF(); |
|
|
|
if (empty($tablename)) { |
|
|
|
echo "没有指定表名"; |
|
|
|
if ($cfg_dbtype == 'sqlite') { |
|
|
|
echo "<xmp>"; |
|
|
|
if (empty($tablename)) { |
|
|
|
echo "没有指定表名"; |
|
|
|
} else { |
|
|
|
// 获取创建表的 SQL 语句 |
|
|
|
$dsql->SetQuery("SELECT sql FROM sqlite_master WHERE type='table' AND name='$tablename'"); |
|
|
|
$dsql->Execute('me'); |
|
|
|
$row = $dsql->GetArray('me', SQLITE3_ASSOC); |
|
|
|
if ($row) { |
|
|
|
$createTableSql = str_replace(" ", "\r\n", $row['sql']); |
|
|
|
echo trim($createTableSql) . "\n\n"; |
|
|
|
} |
|
|
|
} |
|
|
|
echo '</xmp>'; |
|
|
|
exit(); |
|
|
|
} else { |
|
|
|
$dsql->SetQuery("SHOW CREATE TABLE ".$dsql->dbName.".".$tablename); |
|
|
|
$dsql->Execute('me'); |
|
|
|
$row2 = $dsql->GetArray('me', MYSQL_BOTH); |
|
|
|
$ctinfo = $row2[1]; |
|
|
|
echo "<xmp>".trim($ctinfo)."</xmp>"; |
|
|
|
if (empty($tablename)) { |
|
|
|
echo "没有指定表名"; |
|
|
|
} else { |
|
|
|
$dsql->SetQuery("SHOW CREATE TABLE ".$dsql->dbName.".".$tablename); |
|
|
|
$dsql->Execute('me'); |
|
|
|
$row2 = $dsql->GetArray('me', MYSQL_BOTH); |
|
|
|
$ctinfo = $row2[1]; |
|
|
|
echo "<xmp>".trim($ctinfo)."</xmp>"; |
|
|
|
} |
|
|
|
} |
|
|
|
exit(); |
|
|
|
} |
|
|
@@ -34,9 +52,18 @@ else if ($dopost == "opimize") { |
|
|
|
if (empty($tablename)) { |
|
|
|
echo "没有指定表名"; |
|
|
|
} else { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `$tablename`"); |
|
|
|
if ($rs) echo "执行优化表".$tablename."完成<br>"; |
|
|
|
else echo "执行优化表".$tablename."失败,原因是:".$dsql->GetError(); |
|
|
|
if ($cfg_dbtype == 'sqlite') { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("VACUUM"); |
|
|
|
if ($rs) { |
|
|
|
echo "执行优化表 {$tablename} 完成<br>"; |
|
|
|
} else { |
|
|
|
echo "执行优化表 {$tablename} 失败,原因是:" . $dsql->GetError(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `$tablename`"); |
|
|
|
if ($rs) echo "执行优化表".$tablename."完成<br>"; |
|
|
|
else echo "执行优化表".$tablename."失败,原因是:".$dsql->GetError(); |
|
|
|
} |
|
|
|
} |
|
|
|
exit(); |
|
|
|
} |
|
|
@@ -45,39 +72,72 @@ else if ($dopost == "opimizeAll") { |
|
|
|
CheckCSRF(); |
|
|
|
$dsql->SetQuery("SHOW TABLES"); |
|
|
|
$dsql->Execute('t'); |
|
|
|
while ($row = $dsql->GetArray('t', MYSQL_BOTH)) { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `{$row[0]}`"); |
|
|
|
if ($cfg_dbtype == 'sqlite') { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("VACUUM"); |
|
|
|
if ($rs) { |
|
|
|
echo "优化表{$row[0]}完成<br>"; |
|
|
|
echo "执行数据库完成<br>"; |
|
|
|
} else { |
|
|
|
echo "优化表{$row[0]}失败,原因是: ".$dsql->GetError(); |
|
|
|
echo "执行数据库失败,原因是:" . $dsql->GetError(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
while ($row = $dsql->GetArray('t', MYSQL_BOTH)) { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `{$row[0]}`"); |
|
|
|
if ($rs) { |
|
|
|
echo "优化表{$row[0]}完成<br>"; |
|
|
|
} else { |
|
|
|
echo "优化表{$row[0]}失败,原因是: ".$dsql->GetError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
exit(); |
|
|
|
} |
|
|
|
//修复表 |
|
|
|
// 修复表 |
|
|
|
else if ($dopost == "repair") { |
|
|
|
CheckCSRF(); |
|
|
|
if (empty($tablename)) { |
|
|
|
echo "没有指定表名"; |
|
|
|
} else { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `$tablename`"); |
|
|
|
if ($rs) echo "修复表".$tablename."完成<br>"; |
|
|
|
else echo "修复表".$tablename."失败,原因是:".$dsql->GetError(); |
|
|
|
if ($cfg_dbtype =='sqlite') { |
|
|
|
// SQLite 数据库使用 VACUUM 尝试修复和优化 |
|
|
|
$rs = $dsql->ExecuteNoneQuery("VACUUM"); |
|
|
|
if ($rs) { |
|
|
|
echo "对表 {$tablename} 尝试修复和优化完成<br>"; |
|
|
|
} else { |
|
|
|
echo "对表 {$tablename} 尝试修复和优化失败,原因是:" . $dsql->GetError(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 非 SQLite 数据库(如 MySQL)使用 REPAIR TABLE 语句 |
|
|
|
$rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `{$tablename}`"); |
|
|
|
if ($rs) { |
|
|
|
echo "修复表 {$tablename} 完成<br>"; |
|
|
|
} else { |
|
|
|
echo "修复表 {$tablename} 失败,原因是:" . $dsql->GetError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
exit(); |
|
|
|
} |
|
|
|
//修复全部表 |
|
|
|
// 修复全部表 |
|
|
|
else if ($dopost == "repairAll") { |
|
|
|
CheckCSRF(); |
|
|
|
$dsql->SetQuery("Show Tables"); |
|
|
|
$dsql->Execute('t'); |
|
|
|
while ($row = $dsql->GetArray('t', MYSQL_BOTH)) { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `{$row[0]}`"); |
|
|
|
if ($cfg_dbtype =='sqlite') { |
|
|
|
// SQLite 数据库使用 VACUUM 尝试修复和优化整个数据库 |
|
|
|
$rs = $dsql->ExecuteNoneQuery("VACUUM"); |
|
|
|
if ($rs) { |
|
|
|
echo "修复表{$row[0]}完成<br>"; |
|
|
|
echo "对所有表尝试修复和优化完成<br>"; |
|
|
|
} else { |
|
|
|
echo "修复表{$row[0]}失败,原因是: ".$dsql->GetError(); |
|
|
|
echo "对所有表尝试修复和优化失败,原因是:" . $dsql->GetError(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
$dsql->SetQuery("Show Tables"); |
|
|
|
$dsql->Execute('t'); |
|
|
|
while ($row = $dsql->GetArray('t', MYSQL_BOTH)) { |
|
|
|
$rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `{$row[0]}`"); |
|
|
|
if ($rs) { |
|
|
|
echo "修复表 {$row[0]} 完成<br>"; |
|
|
|
} else { |
|
|
|
echo "修复表 {$row[0]} 失败,原因是: " . $dsql->GetError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
exit(); |
|
|
@@ -149,6 +209,121 @@ else if ($dopost == "query") { |
|
|
|
echo $nerrCode; |
|
|
|
} |
|
|
|
exit(); |
|
|
|
} else if ($dopost == "docs") { |
|
|
|
if ($cfg_dbtype == 'sqlite') { |
|
|
|
die("SQLite 数据库不支持此功能"); |
|
|
|
} |
|
|
|
CheckCSRF(); |
|
|
|
$dsql->SetQuery("SHOW TABLES"); |
|
|
|
$dsql->Execute('t'); |
|
|
|
$output = '<!DOCTYPE html> |
|
|
|
<html lang="en"> |
|
|
|
<head> |
|
|
|
<meta charset="UTF-8"> |
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
|
<link rel="stylesheet" href="/static/web/css/bootstrap.min.css"> |
|
|
|
<link rel="stylesheet" href="/static/web/css/admin.css"> |
|
|
|
<title>DedeBIZ数据库文档</title> |
|
|
|
<style> |
|
|
|
body { |
|
|
|
background-color: #f4f6f9; |
|
|
|
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; |
|
|
|
} |
|
|
|
.card { |
|
|
|
border: none; |
|
|
|
border-radius: 10px; |
|
|
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); |
|
|
|
transition: transform 0.2s ease-in-out; |
|
|
|
} |
|
|
|
.card:hover { |
|
|
|
transform: translateY(-5px); |
|
|
|
} |
|
|
|
.card-header { |
|
|
|
background-color: #1eb867; |
|
|
|
color: white; |
|
|
|
border-top-left-radius: 10px; |
|
|
|
border-top-right-radius: 10px; |
|
|
|
} |
|
|
|
.table thead th { |
|
|
|
background-color: #e9ecef; |
|
|
|
padding: 10px; |
|
|
|
} |
|
|
|
.table tbody td { |
|
|
|
padding: 10px; |
|
|
|
} |
|
|
|
</style> |
|
|
|
</head> |
|
|
|
<body> |
|
|
|
<div class="container-fluid"> |
|
|
|
<ol class="breadcrumb"> |
|
|
|
<li class="breadcrumb-item"><a href="index_body.php">后台面板</a></li> |
|
|
|
<li class="breadcrumb-item active"><a href="sys_sql_query.php">SQL命令工具</a></li> |
|
|
|
<li class="breadcrumb-item">数据库文档</li> |
|
|
|
</ol>'; |
|
|
|
|
|
|
|
while ($row = $dsql->GetArray('t', MYSQL_BOTH)) { |
|
|
|
$tableName = $row[0]; |
|
|
|
$output .= '<div class="card mb-4"> |
|
|
|
<div class="card-header"> |
|
|
|
<span>表名: ' . $tableName . '</span> |
|
|
|
</div> |
|
|
|
<div class="card-body">'; |
|
|
|
// 获取表的注释 |
|
|
|
$dsql->SetQuery("SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$tableName'"); |
|
|
|
$dsql->Execute('c'); |
|
|
|
$tableCommentRow = $dsql->GetArray('c', MYSQL_BOTH); |
|
|
|
$tableComment = $tableCommentRow['TABLE_COMMENT']; |
|
|
|
if (!empty($tableComment)) { |
|
|
|
$output .= '<p><strong>表注释:</strong> ' . $tableComment . '</p>'; |
|
|
|
} |
|
|
|
|
|
|
|
// 获取表的字段信息 |
|
|
|
$dsql->SetQuery("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$tableName'"); |
|
|
|
$dsql->Execute('col'); |
|
|
|
$output .= '<table class="table table-striped table-bordered"> |
|
|
|
<thead> |
|
|
|
<tr> |
|
|
|
<th>字段名</th> |
|
|
|
<th>类型</th> |
|
|
|
<th>是否可为空</th> |
|
|
|
<th>默认值</th> |
|
|
|
<th>字段注释</th> |
|
|
|
</tr> |
|
|
|
</thead> |
|
|
|
<tbody>'; |
|
|
|
while ($colRow = $dsql->GetArray('col', MYSQL_BOTH)) { |
|
|
|
$columnName = $colRow['COLUMN_NAME']; |
|
|
|
$columnType = $colRow['COLUMN_TYPE']; |
|
|
|
$isNullable = $colRow['IS_NULLABLE']; |
|
|
|
$columnDefault = $colRow['COLUMN_DEFAULT']; |
|
|
|
$columnComment = $colRow['COLUMN_COMMENT']; |
|
|
|
|
|
|
|
$output .= '<tr> |
|
|
|
<td>' . $columnName . '</td> |
|
|
|
<td>' . $columnType . '</td> |
|
|
|
<td>' . $isNullable . '</td> |
|
|
|
<td>' . ($columnDefault!== null? $columnDefault : '无') . '</td> |
|
|
|
<td>' . $columnComment. '</td> |
|
|
|
</tr>'; |
|
|
|
} |
|
|
|
$output .= '</tbody> |
|
|
|
</table> |
|
|
|
|
|
|
|
</div> |
|
|
|
</div>'; |
|
|
|
} |
|
|
|
|
|
|
|
$output .= '</div> |
|
|
|
<footer class="bg-light text-center py-3"> |
|
|
|
<p class="mb-0">版权所有 © ' . date('Y') . ' <a href="https://www.dedebiz.com/?from=dbdocs" class="text-success">DedeBIZ</a> 保留所有权利。</p> |
|
|
|
</footer> |
|
|
|
</body> |
|
|
|
</html>'; |
|
|
|
|
|
|
|
// 输出 HTML 文档 |
|
|
|
header('Content-Type: text/html'); |
|
|
|
echo $output; |
|
|
|
exit(); |
|
|
|
} |
|
|
|
make_hash(); |
|
|
|
include DedeInclude('templets/sys_sql_query.htm'); |