2023-01-12 12:42:16 +00:00
|
|
|
|
package model
|
2022-08-27 13:21:05 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2023-01-21 11:31:23 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-02-23 03:43:02 +00:00
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2022-08-31 03:44:58 +00:00
|
|
|
|
"strconv"
|
2022-08-27 13:21:05 +00:00
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-23 09:26:20 +00:00
|
|
|
|
func (w SqlBuilder) parseWhereField(ss []string, s *strings.Builder) {
|
2022-09-16 09:59:17 +00:00
|
|
|
|
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
2023-02-23 03:43:02 +00:00
|
|
|
|
x := slice.Map(strings.Split(ss[0], "."), func(t string) string {
|
|
|
|
|
return str.Join("`", t, "`")
|
|
|
|
|
})
|
|
|
|
|
s.WriteString(strings.Join(x, "."))
|
2022-09-16 09:59:17 +00:00
|
|
|
|
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
|
|
|
|
s.WriteString("`")
|
|
|
|
|
s.WriteString(ss[0])
|
|
|
|
|
s.WriteString("`")
|
|
|
|
|
} else {
|
|
|
|
|
s.WriteString(ss[0])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-04 09:42:11 +00:00
|
|
|
|
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in *[][]any) (t bool) {
|
2023-03-09 14:36:41 +00:00
|
|
|
|
if slice.IsContained([]string{"in", "not in"}, strings.ToLower(ss[1])) && len(*in) > 0 {
|
2022-09-16 14:32:05 +00:00
|
|
|
|
s.WriteString(" (")
|
2023-02-23 03:43:02 +00:00
|
|
|
|
x := strings.TrimRight(strings.Repeat("?,", len((*in)[*c])), ",")
|
|
|
|
|
s.WriteString(x)
|
|
|
|
|
*args = append(*args, (*in)[*c]...)
|
2022-09-16 14:32:05 +00:00
|
|
|
|
s.WriteString(")")
|
|
|
|
|
*c++
|
|
|
|
|
t = true
|
|
|
|
|
}
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 08:46:05 +00:00
|
|
|
|
func (w SqlBuilder) parseType(ss []string, args *[]any) error {
|
2022-09-16 14:32:05 +00:00
|
|
|
|
if len(ss) == 4 && ss[3] == "int" {
|
2022-10-11 08:46:05 +00:00
|
|
|
|
i, err := strconv.ParseInt(ss[2], 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-09-16 14:32:05 +00:00
|
|
|
|
*args = append(*args, i)
|
|
|
|
|
} else if len(ss) == 4 && ss[3] == "float" {
|
2022-10-11 08:46:05 +00:00
|
|
|
|
i, err := strconv.ParseFloat(ss[2], 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-09-16 14:32:05 +00:00
|
|
|
|
*args = append(*args, i)
|
|
|
|
|
} else {
|
|
|
|
|
*args = append(*args, ss[2])
|
|
|
|
|
}
|
2022-10-11 08:46:05 +00:00
|
|
|
|
return nil
|
2022-09-16 14:32:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-06 04:04:41 +00:00
|
|
|
|
// ParseWhere 解析为where条件,支持3种风格,具体用法参照query_test中的 Find 的测试方法
|
|
|
|
|
//
|
2023-02-25 15:10:42 +00:00
|
|
|
|
// 1. 1个为一组 {{"field operator value"}} 为纯字符串条件,不对参数做处理
|
2022-11-06 04:04:41 +00:00
|
|
|
|
//
|
2023-02-25 15:10:42 +00:00
|
|
|
|
// 2. 2个为一组 {{"field1","value1"},{"field2","value2"}} => where field1='value1' and field2='value2'
|
|
|
|
|
//
|
|
|
|
|
// 3. 3个或4个为一组 {{"field","operator","value"[,"int|float"]}} => where field operator 'string'|int|float
|
2022-11-06 04:04:41 +00:00
|
|
|
|
//
|
|
|
|
|
// {{"a",">","1","int"}} => where 'a'> 1
|
|
|
|
|
//
|
|
|
|
|
// {{ "a",">","1"}} => where 'a'>'1'
|
|
|
|
|
//
|
|
|
|
|
// 另外如果是操作符为in的话为 {{"field","in",""}} => where field in (?,..) in的条件传给 in参数
|
|
|
|
|
//
|
2023-02-25 15:10:42 +00:00
|
|
|
|
// 4. 5的倍数为一组{{"and|or","field","operator","value","int|float"}}会忽然掉第一组的and|or
|
2022-11-06 04:04:41 +00:00
|
|
|
|
//
|
|
|
|
|
// {{"and","field","=","value1","","and","field","=","value2",""}} => where (field = 'value1' and field = 'value2')
|
|
|
|
|
//
|
|
|
|
|
// {{"and","field","=","num1","int","or","field","=","num2","int"}} => where (field = num1 or field = num2')
|
2022-11-04 09:42:11 +00:00
|
|
|
|
func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
2022-08-27 13:21:05 +00:00
|
|
|
|
var s strings.Builder
|
2022-09-20 14:37:31 +00:00
|
|
|
|
args := make([]any, 0, len(w))
|
2022-08-31 03:44:58 +00:00
|
|
|
|
c := 0
|
2022-08-27 13:21:05 +00:00
|
|
|
|
for _, ss := range w {
|
2023-02-23 03:43:02 +00:00
|
|
|
|
switch len(ss) {
|
|
|
|
|
case 1:
|
|
|
|
|
s.WriteString(ss[0])
|
|
|
|
|
s.WriteString(" and ")
|
|
|
|
|
case 2:
|
2023-02-23 09:26:20 +00:00
|
|
|
|
w.parseWhereField(ss, &s)
|
2022-09-16 06:46:51 +00:00
|
|
|
|
s.WriteString("=? and ")
|
2022-08-27 13:21:05 +00:00
|
|
|
|
args = append(args, ss[1])
|
2023-02-23 03:43:02 +00:00
|
|
|
|
case 3, 4:
|
2023-02-23 09:26:20 +00:00
|
|
|
|
w.parseWhereField(ss, &s)
|
2022-08-27 13:21:05 +00:00
|
|
|
|
s.WriteString(ss[1])
|
2022-09-16 14:32:05 +00:00
|
|
|
|
if w.parseIn(ss, &s, &c, &args, in) {
|
2022-08-31 03:44:58 +00:00
|
|
|
|
s.WriteString(" and ")
|
|
|
|
|
continue
|
|
|
|
|
}
|
2022-08-28 13:27:41 +00:00
|
|
|
|
s.WriteString(" ? and ")
|
2022-10-11 08:46:05 +00:00
|
|
|
|
err := w.parseType(ss, &args)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", nil, err
|
|
|
|
|
}
|
2023-02-23 03:43:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(ss) >= 5 && len(ss)%5 == 0 {
|
2022-09-16 14:32:05 +00:00
|
|
|
|
j := len(ss) / 5
|
|
|
|
|
for i := 0; i < j; i++ {
|
|
|
|
|
start := i * 5
|
|
|
|
|
end := start + 5
|
2022-09-16 15:03:55 +00:00
|
|
|
|
st := s.String()
|
|
|
|
|
if strings.Contains(st, "and ") && ss[start] == "or" {
|
|
|
|
|
st = strings.TrimRight(st, "and ")
|
|
|
|
|
s.Reset()
|
|
|
|
|
s.WriteString(st)
|
2023-02-22 12:59:26 +00:00
|
|
|
|
s.WriteString(" ")
|
|
|
|
|
s.WriteString(ss[start])
|
|
|
|
|
s.WriteString(" ")
|
2022-09-16 15:03:55 +00:00
|
|
|
|
}
|
|
|
|
|
if i == 0 {
|
|
|
|
|
s.WriteString("( ")
|
|
|
|
|
}
|
2023-02-23 09:26:20 +00:00
|
|
|
|
w.parseWhereField(ss[start+1:end], &s)
|
2022-09-16 15:54:32 +00:00
|
|
|
|
s.WriteString(ss[start+2])
|
2022-09-16 15:03:55 +00:00
|
|
|
|
if w.parseIn(ss[start+1:end], &s, &c, &args, in) {
|
|
|
|
|
s.WriteString(" and ")
|
|
|
|
|
continue
|
2022-09-16 14:32:05 +00:00
|
|
|
|
}
|
2022-09-16 15:03:55 +00:00
|
|
|
|
s.WriteString(" ? and ")
|
2022-10-11 08:46:05 +00:00
|
|
|
|
err := w.parseType(ss[start+1:end], &args)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", nil, err
|
|
|
|
|
}
|
2022-08-31 03:44:58 +00:00
|
|
|
|
}
|
2022-09-16 15:54:32 +00:00
|
|
|
|
st := s.String()
|
|
|
|
|
st = strings.TrimRight(st, "and ")
|
|
|
|
|
s.Reset()
|
|
|
|
|
s.WriteString(st)
|
|
|
|
|
s.WriteString(") and ")
|
2022-08-27 13:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-28 13:59:42 +00:00
|
|
|
|
ss := strings.TrimRight(s.String(), "and ")
|
|
|
|
|
if ss != "" {
|
|
|
|
|
s.Reset()
|
|
|
|
|
s.WriteString(" where ")
|
|
|
|
|
s.WriteString(ss)
|
|
|
|
|
ss = s.String()
|
|
|
|
|
}
|
2022-11-04 09:42:11 +00:00
|
|
|
|
if len(*in) > c {
|
|
|
|
|
*in = (*in)[c:]
|
|
|
|
|
}
|
2022-10-11 08:46:05 +00:00
|
|
|
|
return ss, args, nil
|
2022-08-27 13:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 13:27:41 +00:00
|
|
|
|
func (w SqlBuilder) parseOrderBy() string {
|
|
|
|
|
s := strings.Builder{}
|
|
|
|
|
for _, ss := range w {
|
2023-03-09 14:36:41 +00:00
|
|
|
|
if len(ss) == 2 && ss[0] != "" && slice.IsContained([]string{"asc", "desc"}, strings.ToLower(ss[1])) {
|
2022-09-15 14:35:32 +00:00
|
|
|
|
s.WriteString(" ")
|
2022-08-28 13:27:41 +00:00
|
|
|
|
s.WriteString(ss[0])
|
2022-09-15 14:35:32 +00:00
|
|
|
|
s.WriteString(" ")
|
2022-08-28 13:27:41 +00:00
|
|
|
|
s.WriteString(ss[1])
|
|
|
|
|
s.WriteString(",")
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-28 13:59:42 +00:00
|
|
|
|
ss := strings.TrimRight(s.String(), ",")
|
|
|
|
|
if ss != "" {
|
|
|
|
|
s.Reset()
|
|
|
|
|
s.WriteString(" order by ")
|
|
|
|
|
s.WriteString(ss)
|
|
|
|
|
ss = s.String()
|
|
|
|
|
}
|
|
|
|
|
return ss
|
2022-08-28 13:27:41 +00:00
|
|
|
|
}
|
2022-08-29 04:10:10 +00:00
|
|
|
|
func (w SqlBuilder) parseJoin() string {
|
|
|
|
|
s := strings.Builder{}
|
|
|
|
|
for _, ss := range w {
|
|
|
|
|
l := len(ss)
|
|
|
|
|
for j := 0; j < l; j++ {
|
|
|
|
|
s.WriteString(" ")
|
|
|
|
|
if (l == 4 && j == 3) || (l == 3 && j == 2) {
|
|
|
|
|
s.WriteString("on ")
|
|
|
|
|
}
|
|
|
|
|
s.WriteString(ss[j])
|
|
|
|
|
s.WriteString(" ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return s.String()
|
|
|
|
|
}
|