|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Helpers; |
| 6 | + |
| 7 | +class SqlHelper |
| 8 | +{ |
| 9 | + private static array $keywords = [ |
| 10 | + 'SELECT', 'FROM', 'WHERE', 'INSERT', 'INTO', 'VALUES', |
| 11 | + 'UPDATE', 'SET', 'DELETE', 'CREATE', 'DROP', 'ALTER', |
| 12 | + 'TABLE', 'INDEX', 'JOIN', 'LEFT', 'RIGHT', 'INNER', 'OUTER', |
| 13 | + 'ON', 'AND', 'OR', 'NOT', 'NULL', 'IS', 'LIKE', 'IN', |
| 14 | + 'BETWEEN', 'ORDER BY', 'GROUP BY', 'HAVING', 'LIMIT', 'OFFSET', |
| 15 | + 'UNION', 'DISTINCT', 'AS', 'BY', 'DESC', 'ASC', 'WITH', |
| 16 | + 'REPLACE', 'TRUNCATE', 'SHOW', 'DESCRIBE', 'EXPLAIN', |
| 17 | + 'BEGIN', 'COMMIT', 'ROLLBACK', 'START TRANSACTION', |
| 18 | + ]; |
| 19 | + |
| 20 | + /** |
| 21 | + * SQL语法高亮 |
| 22 | + */ |
| 23 | + public static function highlightSql(string $sql): string |
| 24 | + { |
| 25 | + $highlighted = $sql; |
| 26 | + |
| 27 | + // 高亮关键字 |
| 28 | + foreach (self::$keywords as $keyword) { |
| 29 | + $pattern = '/\b(' . $keyword . ')\b/i'; |
| 30 | + $replacement = "\033[1;35m$1\033[0m"; // 紫色加粗 |
| 31 | + $highlighted = preg_replace($pattern, $replacement, $highlighted); |
| 32 | + } |
| 33 | + |
| 34 | + // 高亮字符串 |
| 35 | + $highlighted = preg_replace( |
| 36 | + '/\'([^\']*)\'/', |
| 37 | + "\033[32m'$1'\033[0m", // 绿色 |
| 38 | + $highlighted |
| 39 | + ); |
| 40 | + |
| 41 | + // 高亮数字 |
| 42 | + $highlighted = preg_replace( |
| 43 | + '/\b(\d+)\b/', |
| 44 | + "\033[33m$1\033[0m", // 黄色 |
| 45 | + $highlighted |
| 46 | + ); |
| 47 | + |
| 48 | + return $highlighted; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * 格式化SQL(简单版) |
| 53 | + */ |
| 54 | + public static function formatSql(string $sql): string |
| 55 | + { |
| 56 | + $sql = Query::normalizeSql($sql); |
| 57 | + |
| 58 | + // 在关键字后添加换行 |
| 59 | + $keywords = ['SELECT', 'FROM', 'WHERE', 'INSERT', 'INTO', 'VALUES', |
| 60 | + 'UPDATE', 'SET', 'DELETE', 'LEFT JOIN', 'RIGHT JOIN', |
| 61 | + 'INNER JOIN', 'ORDER BY', 'GROUP BY', 'HAVING', 'LIMIT']; |
| 62 | + |
| 63 | + foreach (array_reverse($keywords) as $keyword) { |
| 64 | + $pattern = '/\b(' . $keyword . ')\b/i'; |
| 65 | + $sql = preg_replace($pattern, "\n$1", $sql); |
| 66 | + } |
| 67 | + |
| 68 | + // 移除开头的换行 |
| 69 | + return trim($sql); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 获取SQL类型 |
| 74 | + */ |
| 75 | + public static function getSqlType(string $sql): string |
| 76 | + { |
| 77 | + $sql = strtoupper(trim($sql)); |
| 78 | + |
| 79 | + if (str_starts_with($sql, 'SELECT')) { |
| 80 | + return 'SELECT'; |
| 81 | + } elseif (str_starts_with($sql, 'INSERT')) { |
| 82 | + return 'INSERT'; |
| 83 | + } elseif (str_starts_with($sql, 'UPDATE')) { |
| 84 | + return 'UPDATE'; |
| 85 | + } elseif (str_starts_with($sql, 'DELETE')) { |
| 86 | + return 'DELETE'; |
| 87 | + } elseif (str_starts_with($sql, 'CREATE')) { |
| 88 | + return 'CREATE'; |
| 89 | + } elseif (str_starts_with($sql, 'DROP')) { |
| 90 | + return 'DROP'; |
| 91 | + } elseif (str_starts_with($sql, 'ALTER')) { |
| 92 | + return 'ALTER'; |
| 93 | + } elseif (str_starts_with($sql, 'SHOW')) { |
| 94 | + return 'SHOW'; |
| 95 | + } elseif (str_starts_with($sql, 'DESCRIBE') || str_starts_with($sql, 'DESC')) { |
| 96 | + return 'DESCRIBE'; |
| 97 | + } elseif (str_starts_with($sql, 'BEGIN') || str_starts_with($sql, 'START TRANSACTION')) { |
| 98 | + return 'BEGIN'; |
| 99 | + } elseif (str_starts_with($sql, 'COMMIT')) { |
| 100 | + return 'COMMIT'; |
| 101 | + } elseif (str_starts_with($sql, 'ROLLBACK')) { |
| 102 | + return 'ROLLBACK'; |
| 103 | + } |
| 104 | + |
| 105 | + return 'OTHER'; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * 提取表名 |
| 110 | + */ |
| 111 | + public static function extractTableNames(string $sql): array |
| 112 | + { |
| 113 | + $tables = []; |
| 114 | + $sql = strtoupper($sql); |
| 115 | + |
| 116 | + // FROM 子句 |
| 117 | + if (preg_match_all('/FROM\s+(\w+)/', $sql, $matches)) { |
| 118 | + $tables = array_merge($tables, $matches[1]); |
| 119 | + } |
| 120 | + |
| 121 | + // JOIN 子句 |
| 122 | + if (preg_match_all('/(?:JOIN)\s+(\w+)/', $sql, $matches)) { |
| 123 | + $tables = array_merge($tables, $matches[1]); |
| 124 | + } |
| 125 | + |
| 126 | + // INSERT INTO |
| 127 | + if (preg_match('/INSERT\s+INTO\s+(\w+)/', $sql, $matches)) { |
| 128 | + $tables[] = $matches[1]; |
| 129 | + } |
| 130 | + |
| 131 | + // UPDATE |
| 132 | + if (preg_match('/UPDATE\s+(\w+)/', $sql, $matches)) { |
| 133 | + $tables[] = $matches[1]; |
| 134 | + } |
| 135 | + |
| 136 | + // DELETE FROM |
| 137 | + if (preg_match('/DELETE\s+FROM\s+(\w+)/', $sql, $matches)) { |
| 138 | + $tables[] = $matches[1]; |
| 139 | + } |
| 140 | + |
| 141 | + return array_unique($tables); |
| 142 | + } |
| 143 | +} |
0 commit comments