From e1374b06bf96c0a2acd019cbc52b463cab1cafcf Mon Sep 17 00:00:00 2001 From: u Date: Thu, 16 Feb 2023 14:47:24 +0800 Subject: [PATCH] 'fix' --- builder2.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/builder2.go b/builder2.go index 316a4ff..ddeb929 100644 --- a/builder2.go +++ b/builder2.go @@ -2,6 +2,7 @@ package vql import ( "encoding/json" + "errors" "fmt" "strings" @@ -14,6 +15,7 @@ type Builder2 struct { From interface{} `json:"from"` Join interface{} `json:"join"` Where interface{} `json:"where"` + Filter interface{} `json:"filter"` //同where,区别是作为做外层条件 Group interface{} `json:"group"` Having interface{} `json:"having"` Order interface{} `json:"order"` @@ -64,6 +66,8 @@ func (b *Builder2) Sql() (string, error) { return "", err } + filterSql, err := toWhereSql(b.Filter) + sql := fmt.Sprintf("select %s from %s ", selectSql, fromSql) if joinSql != "" { sql += fmt.Sprintf(" %s ", joinSql) @@ -81,6 +85,14 @@ func (b *Builder2) Sql() (string, error) { sql += fmt.Sprintf(" order by %s ", orderSql) } + if filterSql != "" { + as := b.As + if as == "" { + as = "____tmp" + } + sql = fmt.Sprintf("select * from (%s) as %s where %s", sql, as, filterSql) + } + return sql, nil } @@ -150,8 +162,38 @@ func toJoinSql(join interface{}) (string, error) { js = append(js, nj) } return strings.Join(js, " "), nil + } else if joinGjson.IsObject() { + types := joinGjson.Get("type").String() + if types == "" { + types = "left" + } + as := joinGjson.Get("as").String() + if as == "" { + return "", errors.New("join 中必须赋予as(别名)") + } + + objGjson := joinGjson.Get("obj") + obj := "" + if objGjson.IsObject() { + nb := NewBuilder2() + json.Unmarshal([]byte(objGjson.Raw), &nb) + ns, err := nb.Sql() + if err != nil { + return "", err + } + obj = ns + } else { + obj = objGjson.String() + } + + on := joinGjson.Get("on").String() + if on == "" { + return "", errors.New("join 中必须赋予on(条件)") + } + + return fmt.Sprintf("%s join ( %s ) as %s on %s", types, obj, as, on), nil } else { - return "", fmt.Errorf("目前仅支持数组或者字符串") + return joinGjson.String(), nil } // joinGjson := gjson.Parse(jsonEncode(join))