Skip to content

Commit

Permalink
fix unsupported func names in where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
cch123 committed Apr 3, 2020
1 parent 6110605 commit e78d5a1
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions select_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func handleSelectWhere(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Ex
return "", errors.New("elasticsql: error expression cannot be nil here")
}

switch (*expr).(type) {
switch e := (*expr).(type) {
case *sqlparser.AndExpr:
return handleSelectWhereAndExpr(expr, topLevel, parent)

Expand Down Expand Up @@ -336,39 +336,44 @@ func handleSelectWhere(expr *sqlparser.Expr, topLevel bool, parent *sqlparser.Ex
case *sqlparser.NotExpr:
return "", errors.New("elasticsql: not expression currently not supported")
case *sqlparser.FuncExpr:
params := (*expr).(*sqlparser.FuncExpr).Exprs
if len(params) > 3 || len(params) < 2 {
return "", errors.New("elasticsql: the multi_match must have 2 or 3 params, (query, fields and type) or (query, fields)")
}

var typ, query, fields string
for i := 0; i < len(params); i++ {
elem := strings.Replace(sqlparser.String(params[i]), "`", "", -1) // a = b
kv := strings.Split(elem, "=")
if len(kv) != 2 {
return "", errors.New("elasticsql: the param should be query = xxx, field = yyy, type = zzz")
switch e.Name.Lowered() {
case "multi_match":
params := e.Exprs
if len(params) > 3 || len(params) < 2 {
return "", errors.New("elasticsql: the multi_match must have 2 or 3 params, (query, fields and type) or (query, fields)")
}
k, v := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1])
switch k {
case "type":
typ = strings.Replace(v, "'", "", -1)
case "query":
query = strings.Replace(v, "`", "", -1)
query = strings.Replace(query, "'", "", -1)
case "fields":
fieldList := strings.Split(strings.TrimRight(strings.TrimLeft(v, "("), ")"), ",")
for idx, field := range fieldList {
fieldList[idx] = fmt.Sprintf(`"%v"`, strings.TrimSpace(field))

var typ, query, fields string
for i := 0; i < len(params); i++ {
elem := strings.Replace(sqlparser.String(params[i]), "`", "", -1) // a = b
kv := strings.Split(elem, "=")
if len(kv) != 2 {
return "", errors.New("elasticsql: the param should be query = xxx, field = yyy, type = zzz")
}
k, v := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1])
switch k {
case "type":
typ = strings.Replace(v, "'", "", -1)
case "query":
query = strings.Replace(v, "`", "", -1)
query = strings.Replace(query, "'", "", -1)
case "fields":
fieldList := strings.Split(strings.TrimRight(strings.TrimLeft(v, "("), ")"), ",")
for idx, field := range fieldList {
fieldList[idx] = fmt.Sprintf(`"%v"`, strings.TrimSpace(field))
}
fields = strings.Join(fieldList, ",")
default:
return "", errors.New("elaticsql: unknow param for multi_match")
}
fields = strings.Join(fieldList, ",")
default:
return "", errors.New("elaticsql: unknow param for multi_match")
}
if typ == "" {
return fmt.Sprintf(`{"multi_match" : {"query" : "%v", "fields" : [%v]}}`, query, fields), nil
}
return fmt.Sprintf(`{"multi_match" : {"query" : "%v", "type" : "%v", "fields" : [%v]}}`, query, typ, fields), nil
default:
return "", errors.New("elaticsql: function in where not supported" + e.Name.Lowered())
}
if typ == "" {
return fmt.Sprintf(`{"multi_match" : {"query" : "%v", "fields" : [%v]}}`, query, fields), nil
}
return fmt.Sprintf(`{"multi_match" : {"query" : "%v", "type" : "%v", "fields" : [%v]}}`, query, typ, fields), nil
}

return "", errors.New("elaticsql: logically cannot reached here")
Expand Down

0 comments on commit e78d5a1

Please sign in to comment.