-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathelasticsql.go
156 lines (142 loc) · 4.19 KB
/
elasticsql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package elasticsql
import (
"errors"
"fmt"
"github.com/xwb1989/sqlparser"
)
// ElasticSQL ..
type ElasticSQL struct {
}
// NewElasticSQL ...
func NewElasticSQL() *ElasticSQL {
return new(ElasticSQL)
}
// SQLConvert sql convert to elasticsearch dsl
func (esql *ElasticSQL) SQLConvert(sql string) (table string, dsl string, err error) {
stmt, err := sqlparser.Parse(sql)
if err != nil {
return ``, ``, errors.New(`ElasticSQL: ` + err.Error())
}
switch v := stmt.(type) {
case *sqlparser.Select:
return handleParseSelect(v)
default:
return ``, ``, errors.New(`ElasticSQL: Support SQL where parsing only`)
}
}
// handleParseSelect parse sql select
func handleParseSelect(selectStmt *sqlparser.Select) (table string, dsl string, err error) {
// 获取from
if table, err = getFromTable(selectStmt); err != nil {
return ``, ``, err
}
from, size := getFromAndSize(selectStmt)
// 解析where
querydsl := ``
if selectStmt.Where != nil {
if querydsl, err = handleSelectWhere(&selectStmt.Where.Expr, true); err != nil {
return ``, ``, err
}
}
if querydsl == `` {
querydsl = `{"bool" : {"must": [{"match_all" : {}}]}}`
}
colArr, aggsdsl, err := handleSelectGroupBy(selectStmt, size)
if err != nil {
return ``, ``, err
}
var orderByArr []string
if aggsdsl != nil {
from, size = "0", "0"
} else {
// Handle order by
// when executating aggregations, order by is useless
for _, orderByExpr := range selectStmt.OrderBy {
orderByStr := fmt.Sprintf(`{"%v": "%v"}`, sqlparser.String(orderByExpr.Expr), orderByExpr.Direction)
orderByArr = append(orderByArr, orderByStr)
}
}
return table, buildDSL(querydsl, from, size, string(aggsdsl), orderByArr, colArr), nil
}
// extract func expressions from select exprs
func handleSelectFuncExpr(sqlSelect sqlparser.SelectExprs) ([]*sqlparser.FuncExpr, []string, error) {
var colArr []string
var funcArr []*sqlparser.FuncExpr
for _, selectVal := range sqlSelect {
if _, ok := selectVal.(*sqlparser.StarExpr); ok {
colArr = append(colArr, sqlparser.String(selectVal))
}
expr, ok := selectVal.(*sqlparser.AliasedExpr)
if !ok {
continue // no need to handle, star expression * just skip is ok
}
switch exprV := expr.Expr.(type) {
case *sqlparser.ColName:
colArr = append(colArr, exprV.Name.String())
case *sqlparser.FuncExpr:
funcArr = append(funcArr, exprV)
default:
// ignore
continue
}
}
if len(colArr) == 1 && colArr[0] == `*` {
colArr = []string{}
}
return funcArr, colArr, nil
}
// getSqlFrom
// 如果From 含有特殊 "-","*"等用"``" 引起来
func getFromTable(selectStmt *sqlparser.Select) (string, error) {
// 处理
if len(selectStmt.From) != 1 {
return ``, errors.New("ElasticSQL: multiple SQL from currently not supported")
}
return sqlparser.String(selectStmt.From), nil
}
// getFromAndSize ...
// get limit and offset
func getFromAndSize(selectStmt *sqlparser.Select) (from, size string) {
from, size = "0", "10"
if selectStmt.Limit == nil {
return
}
if selectStmt.Limit.Offset != nil {
from = sqlparser.String(selectStmt.Limit.Offset)
}
size = sqlparser.String(selectStmt.Limit.Rowcount)
return
}
// handleSelectWhere ....
// 解析sql where
func handleSelectWhere(expr *sqlparser.Expr, topLevel bool) (string, error) {
if expr == nil {
return "", errors.New("ElasticSQL: SQL where exprssion can not be empty")
}
switch exprVal := (*expr).(type) {
case *sqlparser.AndExpr: // where and
resultStr, err := handleAndExpr(exprVal)
if err != nil {
return ``, err
}
return fmt.Sprintf(`{"bool" : {"must" : [%v]}}`, resultStr), nil
case *sqlparser.OrExpr: // wehew or
resultStr, err := handleOrExpr(exprVal)
if err != nil {
return ``, err
}
return fmt.Sprintf(`{"bool" : {"should" : [%v]}}`, resultStr), nil
case *sqlparser.ComparisonExpr:
return handleComparisonExpr(exprVal, topLevel)
case *sqlparser.RangeCond:
return handleRangeCond(exprVal, topLevel)
case *sqlparser.ParenExpr:
return handleSelectWhere(&exprVal.Expr, topLevel)
case *sqlparser.IsExpr:
return handleIsExpr(exprVal)
case *sqlparser.NotExpr:
return ``, errors.New("ElasticSQL: not expression currently not supported")
default:
return ``, errors.New("ElasticSQL: Such expression currently not supported")
}
}