@@ -6,54 +6,54 @@ import (
66 "strings"
77)
88
9- // 删除多余的空白和标点符号
9+ // Remove extra whitespace and punctuation
1010func normalizeSQL (sql string ) string {
11- // 转换为小写
11+ // Convert to lowercase
1212 sql = strings .ToLower (sql )
1313
14- // 去除多余空白
14+ // Remove extra whitespace
1515 sql = regexp .MustCompile (`\s+` ).ReplaceAllString (sql , " " )
1616
17- // 去除括号内的空白
17+ // Remove whitespace inside parentheses
1818 sql = regexp .MustCompile (`\(\s+` ).ReplaceAllString (sql , "(" )
1919 sql = regexp .MustCompile (`\s+\)` ).ReplaceAllString (sql , ")" )
2020
21- // 去除逗号周围的空白
21+ // Remove whitespace around commas
2222 sql = regexp .MustCompile (`\s*,\s*` ).ReplaceAllString (sql , "," )
2323
24- // 去除行尾分号
24+ // Remove trailing semicolon
2525 sql = strings .TrimSuffix (sql , ";" )
2626
2727 return strings .TrimSpace (sql )
2828}
2929
30- // 提取 SELECT 语句的关键组件
30+ // Extract key components of a SELECT statement
3131func extractSelectComponents (sql string ) map [string ]string {
3232 components := make (map [string ]string )
3333
34- // 使用正则表达式提取各个部分
34+ // Use regular expressions to extract parts
3535 selectRegex := regexp .MustCompile (`select\s+(.*?)\s+from\s+(.*?)(?:\s+where\s+(.*?))?(?:\s+order\s+by\s+(.*?))?(?:\s+limit\s+(.*?))?$` )
3636 matches := selectRegex .FindStringSubmatch (sql )
3737
3838 if len (matches ) > 1 {
39- // 提取列
39+ // Extract columns
4040 columns := extractAndSortColumns (matches [1 ])
4141 components ["columns" ] = strings .Join (columns , "," )
4242
43- // 提取表
43+ // Extract table
4444 components ["from" ] = normalizeSQL (matches [2 ])
4545
46- // 提取 WHERE 条件(如果存在)
46+ // Extract WHERE clause (if exists)
4747 if len (matches ) > 3 && matches [3 ] != "" {
4848 components ["where" ] = normalizeWhereClause (matches [3 ])
4949 }
5050
51- // 提取 ORDER BY(如果存在)
51+ // Extract ORDER BY (if exists)
5252 if len (matches ) > 4 && matches [4 ] != "" {
5353 components ["order_by" ] = normalizeOrderBy (matches [4 ])
5454 }
5555
56- // 提取 LIMIT(如果存在)
56+ // Extract LIMIT (if exists)
5757 if len (matches ) > 5 && matches [5 ] != "" {
5858 components ["limit" ] = matches [5 ]
5959 }
@@ -62,22 +62,22 @@ func extractSelectComponents(sql string) map[string]string {
6262 return components
6363}
6464
65- // 提取并排序列名
65+ // Extract and sort column names
6666func extractAndSortColumns (columnsStr string ) []string {
67- // 处理 * 情况
67+ // Handle * case
6868 if strings .TrimSpace (columnsStr ) == "*" {
6969 return []string {"*" }
7070 }
7171
72- // 分割列名
72+ // Split column names
7373 columns := strings .Split (columnsStr , "," )
7474
75- // 去除别名和空白
75+ // Remove aliases and whitespace
7676 cleanColumns := make ([]string , 0 )
7777 for _ , col := range columns {
7878 col = strings .TrimSpace (col )
7979
80- // 去除可能的别名
80+ // Remove possible aliases
8181 if strings .Contains (col , " as " ) {
8282 col = strings .Split (col , " as " )[0 ]
8383 } else if strings .Contains (col , " " ) {
@@ -87,75 +87,75 @@ func extractAndSortColumns(columnsStr string) []string {
8787 cleanColumns = append (cleanColumns , col )
8888 }
8989
90- // 排序
90+ // Sort
9191 sort .Strings (cleanColumns )
9292 return cleanColumns
9393}
9494
95- // 规范化 WHERE 子句
95+ // Normalize WHERE clause
9696func normalizeWhereClause (whereClause string ) string {
97- // 去除多余空白
97+ // Remove extra whitespace
9898 whereClause = normalizeSQL (whereClause )
9999
100- // 分割条件
100+ // Split conditions
101101 conditions := strings .Split (whereClause , " and " )
102102
103- // 排序条件
103+ // Sort conditions
104104 sort .Strings (conditions )
105105
106106 return strings .Join (conditions , " and " )
107107}
108108
109- // 规范化 ORDER BY 子句
109+ // Normalize ORDER BY clause
110110func normalizeOrderBy (orderBy string ) string {
111- // 去除多余空白
111+ // Remove extra whitespace
112112 orderBy = normalizeSQL (orderBy )
113113
114- // 分割排序条件
114+ // Split sorting conditions
115115 sorts := strings .Split (orderBy , "," )
116116
117- // 排序并去除 ASC/DESC
117+ // Sort and remove ASC/DESC
118118 cleanSorts := make ([]string , 0 )
119119 for _ , sort := range sorts {
120- // 去除 ASC/DESC
120+ // Remove ASC/DESC
121121 sort = strings .TrimSuffix (sort , " asc" )
122122 sort = strings .TrimSuffix (sort , " desc" )
123123 cleanSorts = append (cleanSorts , strings .TrimSpace (sort ))
124124 }
125125
126- // 排序
126+ // Sort
127127 sort .Strings (cleanSorts )
128128 return strings .Join (cleanSorts , "," )
129129}
130130
131- // 比较两个 SQL 语句是否语义相等
131+ // Compare if two SQL statements are semantically equal
132132func CompareSQL (sql1 , sql2 string ) bool {
133- // 标准化 SQL 语句
133+ // Normalize SQL statements
134134 normSql1 := normalizeSQL (sql1 )
135135 normSql2 := normalizeSQL (sql2 )
136136
137- // 如果完全相同,直接返回 true
137+ // If exactly the same, return true
138138 if normSql1 == normSql2 {
139139 return true
140140 }
141141
142- // 判断 SQL 类型
142+ // Determine SQL type
143143 switch {
144144 case strings .HasPrefix (normSql1 , "select" ) && strings .HasPrefix (normSql2 , "select" ):
145145 return compareSelectStatements (normSql1 , normSql2 )
146- // 可以根据需要添加其他类型的 SQL 语句比较
146+ // Add other types of SQL statement comparisons as needed
147147 default :
148148 return false
149149 }
150150}
151151
152- // 比较 SELECT 语句
152+ // Compare SELECT statements
153153func compareSelectStatements (sql1 , sql2 string ) bool {
154- // 提取组件
154+ // Extract components
155155 comp1 := extractSelectComponents (sql1 )
156156 comp2 := extractSelectComponents (sql2 )
157157
158- // 比较关键组件
158+ // Compare key components
159159 return comp1 ["columns" ] == comp2 ["columns" ] &&
160160 comp1 ["from" ] == comp2 ["from" ] &&
161161 comp1 ["where" ] == comp2 ["where" ] &&
0 commit comments