1
- /**
2
- * @Author: Adam wu
3
- * @Description:
4
- * @File: convert.go
5
- * @Version: 1.0.0
6
- * @Date: 2025/3/21
7
- */
1
+ // Copyright The Karpor Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
8
14
9
15
package meilisearch
10
16
11
17
import (
12
18
"fmt"
19
+ "strings"
20
+ "time"
21
+
13
22
"github.com/KusionStack/karpor/pkg/infra/persistence/meilisearch"
14
23
"github.com/xwb1989/sqlparser"
15
24
"k8s.io/apimachinery/pkg/util/sets"
16
- "strings"
17
- "time"
18
25
)
19
26
20
27
var DefaultFilter = []string {"deleted = false" }
@@ -46,26 +53,26 @@ func ConvertWithDefaultFilter(sql string, defaultFilter []string) (*meilisearch.
46
53
return nil , "" , fmt .Errorf ("only single table queries are supported" )
47
54
}
48
55
49
- // 应用默认过滤器
56
+ // apply the default filter
50
57
sel = applyDefaultFilter (sel , defaultFilter )
51
58
52
- // 获取表名
59
+ // obtain the table name
53
60
tableName := strings .ReplaceAll (sqlparser .String (sel .From ), "`" , "" )
54
61
55
- // 构建搜索请求
62
+ // build a search request
56
63
req := & meilisearch.SearchRequest {
57
- Limit : 1000 , //by default
64
+ Limit : 1000 , // by default
58
65
Offset : 0 ,
59
66
}
60
67
61
- // 处理WHERE条件
68
+ // handle the where condition
62
69
filter , err := buildFilter (sel .Where )
63
70
if err != nil {
64
71
return nil , "" , err
65
72
}
66
73
req .Filter = filter
67
74
68
- // 处理LIMIT /OFFSET
75
+ // deal with LIMIT /OFFSET
69
76
if sel .Limit != nil {
70
77
if offset , ok := sel .Limit .Offset .(* sqlparser.SQLVal ); ok {
71
78
req .Offset = parseInt (string (offset .Val ))
@@ -75,7 +82,7 @@ func ConvertWithDefaultFilter(sql string, defaultFilter []string) (*meilisearch.
75
82
}
76
83
}
77
84
78
- // 处理ORDER BY
85
+ // deal with ORDER BY
79
86
if len (sel .OrderBy ) > 0 {
80
87
sort := make ([]string , 0 , len (sel .OrderBy ))
81
88
for _ , order := range sel .OrderBy {
@@ -118,7 +125,7 @@ func buildFilterRecursive(expr sqlparser.Expr) (interface{}, error) {
118
125
return fmt .Sprintf ("(%s) OR (%s)" , left , right ), nil
119
126
120
127
case * sqlparser.ComparisonExpr :
121
- return buildComparisonFilter (e )
128
+ return buildComparisonFilter (e ), nil
122
129
123
130
case * sqlparser.ParenExpr :
124
131
return buildFilterRecursive (e .Expr )
@@ -131,10 +138,10 @@ func buildFilterRecursive(expr sqlparser.Expr) (interface{}, error) {
131
138
}
132
139
}
133
140
134
- func buildComparisonFilter (expr * sqlparser.ComparisonExpr ) ( string , error ) {
141
+ func buildComparisonFilter (expr * sqlparser.ComparisonExpr ) string {
135
142
field := strings .Trim (sqlparser .String (expr .Left ), "`" )
136
143
op , value := extractOperatorAndValue (field , expr .Operator , expr .Right )
137
- return fmt .Sprintf ("%s %s %s" , field , op , value ), nil
144
+ return fmt .Sprintf ("%s %s %s" , field , op , value )
138
145
}
139
146
140
147
func buildRangeFilter (expr * sqlparser.RangeCond ) (string , error ) {
@@ -180,7 +187,6 @@ func extractLikeOperatorByValue(op, val string) string {
180
187
181
188
// trimLikeValue trim string like '%abc%' to 'abc'
182
189
func trimLikeValue (val string ) string {
183
-
184
190
bs := []byte (val )
185
191
newBytes := make ([]byte , 0 , len (bs ))
186
192
for i := 0 ; i < len (bs ); i ++ {
@@ -196,7 +202,7 @@ func extractOperatorAndValue(field, op string, expr sqlparser.Expr) (string, str
196
202
op = strings .ToLower (op )
197
203
val := extractValue (expr )
198
204
switch op {
199
- //case sqlparser.EqualStr, sqlparser.GreaterEqualStr, sqlparser.GreaterThanStr, sqlparser.LessThanStr, sqlparser.LessEqualStr, sqlparser.NotEqualStr, sqlparser.InStr, sqlparser.NotInStr:
205
+ // case sqlparser.EqualStr, sqlparser.GreaterEqualStr, sqlparser.GreaterThanStr, sqlparser.LessThanStr, sqlparser.LessEqualStr, sqlparser.NotEqualStr, sqlparser.InStr, sqlparser.NotInStr:
200
206
// return strings.ToUpper(op), val
201
207
case sqlparser .LikeStr , sqlparser .NotLikeStr :
202
208
return extractLikeOperatorByValue (op , val ), trimLikeValue (val )
@@ -263,13 +269,12 @@ func getFilterFields(where *sqlparser.Where) sets.Set[string] {
263
269
}
264
270
265
271
_ = sqlparser .Walk (func (node sqlparser.SQLNode ) (bool , error ) {
266
- switch n := node .(type ) {
267
- case * sqlparser.ColName :
272
+ if n , ok := node .(* sqlparser.ColName ); ok {
268
273
fields .Insert (n .Name .String ())
269
274
}
275
+
270
276
return true , nil
271
277
}, where )
272
-
273
278
return fields
274
279
}
275
280
0 commit comments