DataDictionary.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\common\controller\AdminController;
  4. use app\http\IResponse;
  5. use think\Db;
  6. class DataDictionary extends AdminController
  7. {
  8. /**
  9. * 获取表数据
  10. *
  11. * @author 许祖兴 < zuxing.xu@lettered.cn>
  12. * @date 2020/3/16 17:05
  13. *
  14. * @return mixed
  15. */
  16. public function tables()
  17. {
  18. $tables = Db::query('show table status');
  19. $tablename = $this->request->get('tablename');
  20. $engine = $this->request->get('engine');
  21. $searchTables = [];
  22. $searchMode = false;
  23. if ($tablename || $engine) {
  24. $searchMode = true;
  25. }
  26. foreach ($tables as $key => &$table) {
  27. $table = array_change_key_case($table);
  28. $table['id'] = $table['name'];
  29. $table['index_length'] = $table['index_length'] > 1024 ? intval($table['index_length']/1024) .'KB' : $table['index_length'].'B';
  30. $table['data_length'] = $table['data_length'] > 1024 ? intval($table['data_length']/1024) .'KB' : $table['data_length'].'B';
  31. $table['create_time'] = date('Y-m-d', strtotime($table['create_time']));
  32. // 搜索
  33. if ($tablename && !$engine && stripos($table['name'], $tablename) !== false) {
  34. $searchTables[] = $table;
  35. }
  36. // 搜索
  37. if (!$tablename && $engine && stripos($table['engine'], $engine) !== false) {
  38. $searchTables[] = $table;
  39. }
  40. if ($tablename && $engine && stripos($table['engine'], $engine) !== false && stripos($table['name'], $tablename) !== false) {
  41. $searchTables[] = $table;
  42. }
  43. }
  44. return IResponse::success(['total' => count($tables),'list' => $tables]);
  45. }
  46. /**
  47. * 查看表字段
  48. *
  49. * @author 许祖兴 < zuxing.xu@lettered.cn>
  50. * @date 2020/3/18 10:39
  51. *
  52. * @param string $table 表名称
  53. * @return mixed
  54. */
  55. public function view($table)
  56. {
  57. $fields = Db::query('show full columns from ' . $table);
  58. array_walk($fields, function (&$item){
  59. $item = array_change_key_case($item);
  60. });
  61. return IResponse::success(['total' => count($fields), 'list' => $fields]);
  62. }
  63. /**
  64. * 优化表内容
  65. *
  66. * @author 许祖兴 < zuxing.xu@lettered.cn>
  67. * @date 2020/3/21 14:57
  68. *
  69. * @return mixed
  70. */
  71. public function optimize()
  72. {
  73. $tables = $this->request->param('ids');
  74. foreach (str2arr($tables) as $table){
  75. Db::query(sprintf('optimize table %s', $table));
  76. }
  77. return IResponse::success([], '优化成功');
  78. }
  79. /**
  80. * 备份表数据
  81. *
  82. * @author 许祖兴 < zuxing.xu@lettered.cn>
  83. * @date 2020/3/21 15:59
  84. *
  85. * @return mixed
  86. */
  87. public function backup()
  88. {
  89. $to_file_name = date('YmdHis') . ".sql";
  90. //数据库中有哪些表
  91. $tables = Db::query('SHOW TABLES ');
  92. $tablelist = array();
  93. foreach ($tables as $v) {
  94. foreach ($v as $vv) {
  95. $tablelist[] = $vv;
  96. }
  97. }
  98. $info = "-- ----------------------------\r\n";
  99. $info .= "-- 日期:" . date("Y-m-d H:i:s", time()) . "\r\n";
  100. $info .= "-- 仅用于测试和学习,本程序不适合处理超大量数据\r\n";
  101. $info .= "-- ----------------------------\r\n\r\n";
  102. file_put_contents($to_file_name, $info, FILE_APPEND);
  103. //将每个表的表结构导出到文件
  104. foreach ($tablelist as $val) {
  105. $res = Db::query('show create table ' . $val);
  106. foreach ($res as $v) {
  107. $newres = $v['Create Table'];
  108. }
  109. $info = "-- ----------------------------\r\n";
  110. $info .= "-- Table structure for `" . $val . "`\r\n";
  111. $info .= "-- ----------------------------\r\n";
  112. $info .= "DROP TABLE IF EXISTS `" . $val . "`;\r\n";
  113. $sqlStr = $info . $newres . ";\r\n\r\n";
  114. //追加到文件
  115. file_put_contents($to_file_name, $sqlStr, FILE_APPEND);
  116. }
  117. //将每个表的数据导出到文件
  118. foreach ($tablelist as $val) {
  119. $sql = "select * from " . $val;
  120. $res = Db::query('select * from ' . $val);
  121. //如果表中没有数据,则继续下一张表
  122. if (count($res) < 1) continue;
  123. //
  124. $info = "-- ----------------------------\r\n";
  125. $info .= "-- Records for `" . $val . "`\r\n";
  126. $info .= "-- ----------------------------\r\n";
  127. file_put_contents($to_file_name, $info, FILE_APPEND);
  128. //读取数据
  129. foreach ($res as $v) {
  130. $sqlstr = "INSERT INTO `" . $val . "` VALUES (";
  131. foreach ($v as $vv) {
  132. //将数据中的单引号转义,否则还原时会出错
  133. $newvv = str_replace("'", "\'", $vv);
  134. $sqlstr .= "'" . $newvv . "', ";
  135. }
  136. //去掉最后一个逗号和空格
  137. $sqlstr = substr($sqlstr, 0, strlen($sqlstr) - 2);
  138. $sqlstr .= ");\r\n";
  139. file_put_contents($to_file_name, $sqlstr, FILE_APPEND);
  140. }
  141. file_put_contents($to_file_name, "\r\n", FILE_APPEND);
  142. }
  143. return IResponse::success([], '备份成功');
  144. }
  145. }