DatabaseLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\system;
  8. use plugin\saiadmin\basic\BaseLogic;
  9. use plugin\saiadmin\exception\ApiException;
  10. use think\facade\Db;
  11. /**
  12. * 数据表维护逻辑层
  13. */
  14. class DatabaseLogic extends BaseLogic
  15. {
  16. /**
  17. * 数据列表
  18. * @param $query
  19. * @return mixed
  20. */
  21. public function getList($query): mixed
  22. {
  23. $page = request()->input('page') ? request()->input('page') : 1;
  24. $limit = request()->input('limit') ? request()->input('limit') : 10;
  25. return self::getTableList($query, $page, $limit);
  26. }
  27. /**
  28. * 获取数据库表数据
  29. */
  30. public function getTableList($query, $current_page = 1, $per_page = 10): array
  31. {
  32. if (!empty($query['source'])) {
  33. if (!empty($query['name'])) {
  34. $sql = 'show table status where name=:name ';
  35. $list = Db::connect($query['source'])->query($sql, ['name' => $query['name']]);
  36. } else {
  37. $list = Db::connect($query['source'])->query('show table status');
  38. }
  39. } else {
  40. if (!empty($query['name'])) {
  41. $sql = 'show table status where name=:name ';
  42. $list = Db::query($sql, ['name' => $query['name']]);
  43. } else {
  44. $list = Db::query('show table status');
  45. }
  46. }
  47. $data = [];
  48. foreach ($list as $item) {
  49. $data[] = [
  50. 'name' => $item['Name'],
  51. 'engine' => $item['Engine'],
  52. 'rows' => $item['Rows'],
  53. 'data_free' => $item['Data_free'],
  54. 'data_length' => $item['Data_length'],
  55. 'index_length' => $item['Index_length'],
  56. 'collation' => $item['Collation'],
  57. 'create_time' => $item['Create_time'],
  58. 'update_time' => $item['Update_time'],
  59. 'comment' => $item['Comment'],
  60. ];
  61. }
  62. $total = count($data);
  63. $last_page = ceil($total/$per_page);
  64. $startIndex = ($current_page - 1) * $per_page;
  65. $pageData = array_slice($data, $startIndex, $per_page);
  66. return [
  67. 'data' => $pageData,
  68. 'total' => $total,
  69. 'current_page' => $current_page,
  70. 'per_page' => $per_page,
  71. 'last_page' => $last_page,
  72. ];
  73. }
  74. /**
  75. * 获取列信息
  76. */
  77. public function getColumnList($table, $source): array
  78. {
  79. $columnList = [];
  80. if (preg_match("/^[a-zA-Z0-9_]+$/", $table)) {
  81. if (!empty($source)) {
  82. $list = Db::connect($source)->query('SHOW FULL COLUMNS FROM `'.$table.'`');
  83. } else {
  84. $list = Db::query('SHOW FULL COLUMNS FROM `'.$table.'`');
  85. }
  86. foreach ($list as $column) {
  87. preg_match('/^\w+/', $column['Type'], $matches);
  88. $columnList[] = [
  89. 'column_key' => $column['Key'],
  90. 'column_name'=> $column['Field'],
  91. 'column_type' => $matches[0],
  92. 'column_comment' => trim(preg_replace("/\([^()]*\)/", "", $column['Comment'])),
  93. 'extra' => $column['Extra'],
  94. 'default_value' => $column['Default'],
  95. 'is_nullable' => $column['Null'],
  96. ];
  97. }
  98. }
  99. return $columnList;
  100. }
  101. /**
  102. * 优化表
  103. */
  104. public function optimizeTable($tables)
  105. {
  106. foreach ($tables as $table) {
  107. if (preg_match("/^[a-zA-Z0-9_]+$/", $table)) {
  108. Db::execute('OPTIMIZE TABLE `'. $table. '`');
  109. }
  110. }
  111. }
  112. /**
  113. * 清理表碎片
  114. */
  115. public function fragmentTable($tables)
  116. {
  117. foreach ($tables as $table) {
  118. if (preg_match("/^[a-zA-Z0-9_]+$/", $table)) {
  119. Db::execute('ANALYZE TABLE `'. $table. '`');
  120. }
  121. }
  122. }
  123. /**
  124. * 获取回收站数据
  125. */
  126. public function recycleData($table)
  127. {
  128. if (preg_match("/^[a-zA-Z0-9_]+$/", $table)) {
  129. // 查询表字段
  130. $sql = 'SHOW COLUMNS FROM `'.$table.'` where Field = "delete_time"';
  131. $columns = Db::query($sql);
  132. $isDeleteTime = false;
  133. if (count($columns) > 0) {
  134. $isDeleteTime = true;
  135. }
  136. if (!$isDeleteTime) {
  137. throw new ApiException('当前表不支持回收站功能');
  138. }
  139. // 查询软删除数据
  140. $limit = request()->input('limit') ? request()->input('limit') : 10;
  141. return Db::table($table)->whereNotNull('delete_time')
  142. ->order('delete_time', 'desc')
  143. ->paginate($limit)
  144. ->toArray();
  145. } else {
  146. return [];
  147. }
  148. }
  149. /**
  150. * 删除数据
  151. * @param $table
  152. * @param $ids
  153. * @return bool
  154. */
  155. public function delete($table, $ids)
  156. {
  157. if (preg_match("/^[a-zA-Z0-9_]+$/", $table)) {
  158. $count = Db::table($table)->delete($ids);
  159. return $count > 0;
  160. } else {
  161. return false;
  162. }
  163. }
  164. /**
  165. * 恢复数据
  166. * @param $table
  167. * @param $ids
  168. * @return bool
  169. */
  170. public function recovery($table, $ids)
  171. {
  172. if (preg_match("/^[a-zA-Z0-9_]+$/", $table)) {
  173. $count = Db::table($table)
  174. ->where('id', 'in', $ids)
  175. ->update(['delete_time' => null]);
  176. return $count > 0;
  177. } else {
  178. return false;
  179. }
  180. }
  181. }