@@ -291,7 +291,7 @@ func handleSelectWhere(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Ex
291
291
return "" , errors .New ("elasticsql: error expression cannot be nil here" )
292
292
}
293
293
294
- switch (* expr ).(type ) {
294
+ switch e := (* expr ).(type ) {
295
295
case * sqlparser.AndExpr :
296
296
return handleSelectWhereAndExpr (expr , topLevel , parent )
297
297
@@ -336,39 +336,44 @@ func handleSelectWhere(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Ex
336
336
case * sqlparser.NotExpr :
337
337
return "" , errors .New ("elasticsql: not expression currently not supported" )
338
338
case * sqlparser.FuncExpr :
339
- params := (* expr ).(* sqlparser.FuncExpr ).Exprs
340
- if len (params ) > 3 || len (params ) < 2 {
341
- return "" , errors .New ("elasticsql: the multi_match must have 2 or 3 params, (query, fields and type) or (query, fields)" )
342
- }
343
-
344
- var typ , query , fields string
345
- for i := 0 ; i < len (params ); i ++ {
346
- elem := strings .Replace (sqlparser .String (params [i ]), "`" , "" , - 1 ) // a = b
347
- kv := strings .Split (elem , "=" )
348
- if len (kv ) != 2 {
349
- return "" , errors .New ("elasticsql: the param should be query = xxx, field = yyy, type = zzz" )
339
+ switch e .Name .Lowered () {
340
+ case "multi_match" :
341
+ params := e .Exprs
342
+ if len (params ) > 3 || len (params ) < 2 {
343
+ return "" , errors .New ("elasticsql: the multi_match must have 2 or 3 params, (query, fields and type) or (query, fields)" )
350
344
}
351
- k , v := strings .TrimSpace (kv [0 ]), strings .TrimSpace (kv [1 ])
352
- switch k {
353
- case "type" :
354
- typ = strings .Replace (v , "'" , "" , - 1 )
355
- case "query" :
356
- query = strings .Replace (v , "`" , "" , - 1 )
357
- query = strings .Replace (query , "'" , "" , - 1 )
358
- case "fields" :
359
- fieldList := strings .Split (strings .TrimRight (strings .TrimLeft (v , "(" ), ")" ), "," )
360
- for idx , field := range fieldList {
361
- fieldList [idx ] = fmt .Sprintf (`"%v"` , strings .TrimSpace (field ))
345
+
346
+ var typ , query , fields string
347
+ for i := 0 ; i < len (params ); i ++ {
348
+ elem := strings .Replace (sqlparser .String (params [i ]), "`" , "" , - 1 ) // a = b
349
+ kv := strings .Split (elem , "=" )
350
+ if len (kv ) != 2 {
351
+ return "" , errors .New ("elasticsql: the param should be query = xxx, field = yyy, type = zzz" )
352
+ }
353
+ k , v := strings .TrimSpace (kv [0 ]), strings .TrimSpace (kv [1 ])
354
+ switch k {
355
+ case "type" :
356
+ typ = strings .Replace (v , "'" , "" , - 1 )
357
+ case "query" :
358
+ query = strings .Replace (v , "`" , "" , - 1 )
359
+ query = strings .Replace (query , "'" , "" , - 1 )
360
+ case "fields" :
361
+ fieldList := strings .Split (strings .TrimRight (strings .TrimLeft (v , "(" ), ")" ), "," )
362
+ for idx , field := range fieldList {
363
+ fieldList [idx ] = fmt .Sprintf (`"%v"` , strings .TrimSpace (field ))
364
+ }
365
+ fields = strings .Join (fieldList , "," )
366
+ default :
367
+ return "" , errors .New ("elaticsql: unknow param for multi_match" )
362
368
}
363
- fields = strings .Join (fieldList , "," )
364
- default :
365
- return "" , errors .New ("elaticsql: unknow param for multi_match" )
366
369
}
370
+ if typ == "" {
371
+ return fmt .Sprintf (`{"multi_match" : {"query" : "%v", "fields" : [%v]}}` , query , fields ), nil
372
+ }
373
+ return fmt .Sprintf (`{"multi_match" : {"query" : "%v", "type" : "%v", "fields" : [%v]}}` , query , typ , fields ), nil
374
+ default :
375
+ return "" , errors .New ("elaticsql: function in where not supported" + e .Name .Lowered ())
367
376
}
368
- if typ == "" {
369
- return fmt .Sprintf (`{"multi_match" : {"query" : "%v", "fields" : [%v]}}` , query , fields ), nil
370
- }
371
- return fmt .Sprintf (`{"multi_match" : {"query" : "%v", "type" : "%v", "fields" : [%v]}}` , query , typ , fields ), nil
372
377
}
373
378
374
379
return "" , errors .New ("elaticsql: logically cannot reached here" )
0 commit comments