国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

294 linhas
10KB

  1. <?php
  2. /**
  3. * SQL命令工具
  4. *
  5. * @version $id:sys_sql_query.php 22:28 2010年7月20日 tianya $
  6. * @package DedeBIZ.Administrator
  7. * @copyright Copyright (c) 2022 DedeBIZ.COM
  8. * @license GNU GPL v2 (https://www.dedebiz.com/license)
  9. * @link https://www.dedebiz.com
  10. */
  11. require(dirname(__FILE__)."/config.php");
  12. if (DEDEBIZ_SAFE_MODE) {
  13. die(DedeAlert("系统已启用安全模式,无法使用当前功能",ALERT_DANGER));
  14. }
  15. CheckPurview('sys_Data');
  16. if (empty($dopost)) $dopost = '';
  17. //查看表结构
  18. if ($dopost == "viewinfo") {
  19. CheckCSRF();
  20. if ($cfg_dbtype == 'sqlite') {
  21. echo "<xmp>";
  22. if (empty($tablename)) {
  23. echo "没有指定表名";
  24. } else {
  25. //获取创建表的SQL语句
  26. $dsql->SetQuery("SELECT sql FROM sqlite_master WHERE type='table' AND name='$tablename'");
  27. $dsql->Execute('me');
  28. $row = $dsql->GetArray('me', SQLITE3_ASSOC);
  29. if ($row) {
  30. $createTableSql = str_replace(" ", "\r\n", $row['sql']);
  31. echo trim($createTableSql)."\n\n";
  32. }
  33. }
  34. echo '</xmp>';
  35. exit();
  36. } else {
  37. if (empty($tablename)) {
  38. echo "没有指定表名";
  39. } else {
  40. $dsql->SetQuery("SHOW CREATE TABLE ".$dsql->dbName.".".$tablename);
  41. $dsql->Execute('me');
  42. $row2 = $dsql->GetArray('me', MYSQL_BOTH);
  43. $ctinfo = $row2[1];
  44. echo "<xmp>".trim($ctinfo)."</xmp>";
  45. }
  46. }
  47. exit();
  48. }
  49. //优化表
  50. else if ($dopost == "opimize") {
  51. CheckCSRF();
  52. if (empty($tablename)) {
  53. echo "没有指定表名";
  54. } else {
  55. if ($cfg_dbtype == 'sqlite') {
  56. $rs = $dsql->ExecuteNoneQuery("VACUUM");
  57. if ($rs) {
  58. echo "执行优化表 {$tablename} 完成<br>";
  59. } else {
  60. echo "执行优化表 {$tablename} 失败,原因是:".$dsql->GetError();
  61. }
  62. } else {
  63. $rs = $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `$tablename`");
  64. if ($rs) echo "执行优化表".$tablename."完成<br>";
  65. else echo "执行优化表".$tablename."失败,原因是:".$dsql->GetError();
  66. }
  67. }
  68. exit();
  69. }
  70. //优化全部表
  71. else if ($dopost == "opimizeAll") {
  72. CheckCSRF();
  73. $dsql->SetQuery("SHOW TABLES");
  74. $dsql->Execute('t');
  75. if ($cfg_dbtype == 'sqlite') {
  76. $rs = $dsql->ExecuteNoneQuery("VACUUM");
  77. if ($rs) {
  78. echo "执行数据库完成<br>";
  79. } else {
  80. echo "执行数据库失败,原因是:".$dsql->GetError();
  81. }
  82. } else {
  83. while ($row = $dsql->GetArray('t', MYSQL_BOTH)) {
  84. $rs = $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `{$row[0]}`");
  85. if ($rs) {
  86. echo "优化表{$row[0]}完成<br>";
  87. } else {
  88. echo "优化表{$row[0]}失败,原因是: ".$dsql->GetError();
  89. }
  90. }
  91. }
  92. exit();
  93. }
  94. //修复表
  95. else if ($dopost == "repair") {
  96. CheckCSRF();
  97. if (empty($tablename)) {
  98. echo "没有指定表名";
  99. } else {
  100. if ($cfg_dbtype =='sqlite') {
  101. //SQLite数据库使用VACUUM尝试修复和优化
  102. $rs = $dsql->ExecuteNoneQuery("VACUUM");
  103. if ($rs) {
  104. echo "对表 {$tablename} 尝试修复和优化完成<br>";
  105. } else {
  106. echo "对表 {$tablename} 尝试修复和优化失败,原因是:".$dsql->GetError();
  107. }
  108. } else {
  109. //非SQLite数据库(如 MySQL)使用REPAIR TABLE语句
  110. $rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `{$tablename}`");
  111. if ($rs) {
  112. echo "修复表 {$tablename} 完成<br>";
  113. } else {
  114. echo "修复表 {$tablename} 失败,原因是:".$dsql->GetError();
  115. }
  116. }
  117. }
  118. exit();
  119. }
  120. //修复全部表
  121. else if ($dopost == "repairAll") {
  122. CheckCSRF();
  123. if ($cfg_dbtype =='sqlite') {
  124. //SQLite 数据库使用VACUUM尝试修复和优化整个数据库
  125. $rs = $dsql->ExecuteNoneQuery("VACUUM");
  126. if ($rs) {
  127. echo "对所有表尝试修复和优化完成<br>";
  128. } else {
  129. echo "对所有表尝试修复和优化失败,原因是:".$dsql->GetError();
  130. }
  131. } else {
  132. $dsql->SetQuery("Show Tables");
  133. $dsql->Execute('t');
  134. while ($row = $dsql->GetArray('t', MYSQL_BOTH)) {
  135. $rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `{$row[0]}`");
  136. if ($rs) {
  137. echo "修复表 {$row[0]} 完成<br>";
  138. } else {
  139. echo "修复表 {$row[0]} 失败,原因是: ".$dsql->GetError();
  140. }
  141. }
  142. }
  143. exit();
  144. }
  145. //执行SQL语句
  146. else if ($dopost == "query") {
  147. CheckCSRF();
  148. $mysqlVersions = explode('.',trim($row[0]));
  149. $mysqlVersion = $mysqlVersions[0].".".$mysqlVersions[1];
  150. $sqlquery = trim(stripslashes($sqlquery));
  151. if (preg_match("#drop(.*)table#i", $sqlquery) || preg_match("#drop(.*)database#", $sqlquery)) {
  152. echo "删除数据表或数据库的语句不允许在这里执行";
  153. exit();
  154. }
  155. if ($mysqlVersion >= 4.1 && preg_match('#CREATE#i', $sqlquery)) {
  156. $sql4tmp = "ENGINE=MyISAM DEFAULT CHARSET=".$$cfg_db_language;
  157. $sqlquery = preg_replace("#TYPE=MyISAM#i", $sql4tmp, $sqlquery);
  158. }
  159. echo '<link rel="stylesheet" href="/static/web/css/bootstrap.min.css">';
  160. //运行查询语句
  161. if (preg_match("#^select #i", $sqlquery)) {
  162. $dsql->SetQuery($sqlquery);
  163. $dsql->Execute();
  164. if ($dsql->GetTotalRow() <= 0) {
  165. echo "运行SQL:{$sqlquery}无返回记录<br>";
  166. } else {
  167. echo "运行SQL:{$sqlquery}共有".$dsql->GetTotalRow()."条记录,最大返回100条";
  168. }
  169. $j = 0;
  170. while ($row = $dsql->GetArray()) {
  171. $j++;
  172. if ($j > 100) {
  173. break;
  174. }
  175. echo "<hr>";
  176. echo "记录:$j";
  177. echo "<hr>";
  178. foreach ($row as $k => $v) {
  179. echo "{$k}:{$v}<br>\r\n";
  180. }
  181. }
  182. exit();
  183. }
  184. if ($querytype == 2) {
  185. //普通的SQL语句
  186. $sqlquery = str_replace("\r", "", $sqlquery);
  187. $sqls = preg_split("#;[ \t]{0,}\n#", $sqlquery);
  188. $nerrCode = '';
  189. $i = 0;
  190. foreach ($sqls as $q) {
  191. $q = trim($q);
  192. if ($q == "") {
  193. continue;
  194. }
  195. $dsql->ExecuteNoneQuery($q);
  196. $errCode = trim($dsql->GetError());
  197. if ($errCode == "") {
  198. $i++;
  199. } else {
  200. $nerrCode .= "执行".$q."出错,错误提示:".$errCode."";
  201. }
  202. }
  203. echo "成功执行{$i}个SQL语句";
  204. echo $nerrCode;
  205. } else {
  206. $dsql->ExecuteNoneQuery($sqlquery);
  207. $nerrCode = trim($dsql->GetError());
  208. echo "成功执行1个SQL语句";
  209. echo $nerrCode;
  210. }
  211. exit();
  212. } else if ($dopost == "docs") {
  213. if ($cfg_dbtype == 'sqlite') {
  214. die("SQLite数据库不支持此功能");
  215. }
  216. CheckCSRF();
  217. $dsql->SetQuery("SHOW TABLES");
  218. $dsql->Execute('t');
  219. $output = '<!DOCTYPE html>
  220. <html>
  221. <head>
  222. <meta charset="UTF-8">
  223. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  224. <link rel="stylesheet" href="/static/web/css/bootstrap.min.css">
  225. <link rel="stylesheet" href="/static/web/css/admin.css">
  226. <title>DedeBIZ数据库文档</title>
  227. <style>.card{transition:transform 0.5s}.card:hover{transform:translateY(-0.25rem)}.card-header{border-top-left-radius:0.5rem!important;border-top-right-radius:0.5rem!important}.table thead th{padding:0.5rem;background:#e9ecef;border-bottom:none}.table tbody td{padding:0.5rem}</style>
  228. </head>
  229. <body>
  230. <div class="container-fluid">
  231. <ol class="breadcrumb">
  232. <li class="breadcrumb-item"><a href="index_body.php">后台面板</a></li>
  233. <li class="breadcrumb-item active"><a href="sys_sql_query.php">SQL命令工具</a></li>
  234. <li class="breadcrumb-item">数据库文档</li>
  235. </ol>';
  236. while ($row = $dsql->GetArray('t', MYSQL_BOTH)) {
  237. $tableName = $row[0];
  238. $output .= '<div class="card shadow-sm mb-3">
  239. <div class="card-header">表名: '.$tableName.'</div>
  240. <div class="card-body">';
  241. //获取表的注释
  242. $dsql->SetQuery("SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$tableName'");
  243. $dsql->Execute('c');
  244. $tableCommentRow = $dsql->GetArray('c', MYSQL_BOTH);
  245. $tableComment = $tableCommentRow['TABLE_COMMENT'];
  246. if (!empty($tableComment)) {
  247. $output .= '<p><strong>表注释:</strong> '.$tableComment.'</p>';
  248. }
  249. //获取表的字段信息
  250. $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'");
  251. $dsql->Execute('col');
  252. $output .= '<table class="table table-bordered">
  253. <thead>
  254. <tr>
  255. <th>字段名</th>
  256. <th>类型</th>
  257. <th>是否可为空</th>
  258. <th>默认值</th>
  259. <th>字段注释</th>
  260. </tr>
  261. </thead>
  262. <tbody>';
  263. while ($colRow = $dsql->GetArray('col', MYSQL_BOTH)) {
  264. $columnName = $colRow['COLUMN_NAME'];
  265. $columnType = $colRow['COLUMN_TYPE'];
  266. $isNullable = $colRow['IS_NULLABLE'];
  267. $columnDefault = $colRow['COLUMN_DEFAULT'];
  268. $columnComment = $colRow['COLUMN_COMMENT'];
  269. $output .= '<tr>
  270. <td>'.$columnName.'</td>
  271. <td>'.$columnType.'</td>
  272. <td>'.$isNullable.'</td>
  273. <td>'.($columnDefault !== null? $columnDefault : '无').'</td>
  274. <td>'.$columnComment. '</td>
  275. </tr>';
  276. }
  277. $output .= '</tbody>
  278. </table>
  279. </div>
  280. </div>';
  281. }
  282. $output .= '<p class="text-center">版权所有 &copy; '.date('Y').' <a href="https://www.dedebiz.com/?from=dbdocs" class="text-success">DedeBIZ</a> 保留所有权利。</p>
  283. </div>
  284. </body>
  285. </html>';
  286. //输出网页文档
  287. header('Content-Type: text/html');
  288. echo $output;
  289. exit();
  290. }
  291. make_hash();
  292. include DedeInclude('templets/sys_sql_query.htm');
  293. ?>