Browse Source

'fix'

apijson
u 2 years ago
parent
commit
43fd13a3a1
  1. 20
      apijson/query_context.go
  2. 8
      apijson/query_node.go
  3. 33
      apijson/sqlparser.go

20
apijson/query_context.go

@ -9,18 +9,20 @@ import (
) )
type QueryContext struct { type QueryContext struct {
req map[string]interface{}
code int
nodeTree map[string]*QueryNode
nodePathMap map[string]*QueryNode
err error
explain bool
req map[string]interface{}
code int
nodeTree map[string]*QueryNode
nodePathMap map[string]*QueryNode
Data interface{}
ExtensionValue map[string]string
err error
explain bool
} }
func NewQueryContext(req map[string]interface{}) *QueryContext { func NewQueryContext(req map[string]interface{}) *QueryContext {
return &QueryContext{ return &QueryContext{
req: req, req: req,
code: http.StatusOK,
code: 0,
nodeTree: make(map[string]*QueryNode), nodeTree: make(map[string]*QueryNode),
nodePathMap: make(map[string]*QueryNode), nodePathMap: make(map[string]*QueryNode),
} }
@ -64,7 +66,7 @@ func (c *QueryContext) doQuery() {
if c.err != nil { if c.err != nil {
return return
} }
n.doQueryData()
n.doQueryData(c)
} }
} }
@ -100,7 +102,7 @@ func (c *QueryContext) findResult(value string) interface{} {
c.err = fmt.Errorf("有循环依赖") c.err = fmt.Errorf("有循环依赖")
return nil return nil
} }
node.doQueryData()
node.doQueryData(c)
if c.err != nil { if c.err != nil {
return nil return nil
} }

8
apijson/query_node.go

@ -147,7 +147,7 @@ func (n *QueryNode) Result() interface{} {
return nil return nil
} }
func (n *QueryNode) doQueryData() {
func (n *QueryNode) doQueryData(ctx *QueryContext) {
if n.completed { if n.completed {
return return
} }
@ -164,7 +164,7 @@ func (n *QueryNode) doQueryData() {
} }
} }
if !n.isList { if !n.isList {
n.ResultList, root.err = n.sqlExecutor.Exec()
n.ResultList, root.err = n.sqlExecutor.Exec(ctx)
if len(n.ResultList) > 0 { if len(n.ResultList) > 0 {
n.CurrentData = n.ResultList[0] n.CurrentData = n.ResultList[0]
return return
@ -173,7 +173,7 @@ func (n *QueryNode) doQueryData() {
} }
primary := n.children[n.primaryKey] primary := n.children[n.primaryKey]
primary.sqlExecutor.PageSize(n.page, n.count) primary.sqlExecutor.PageSize(n.page, n.count)
primary.doQueryData()
primary.doQueryData(ctx)
if root.err != nil { if root.err != nil {
return return
} }
@ -186,7 +186,7 @@ func (n *QueryNode) doQueryData() {
if len(n.children) > 0 { if len(n.children) > 0 {
for _, child := range n.children { for _, child := range n.children {
if child != primary { if child != primary {
child.doQueryData()
child.doQueryData(ctx)
n.ResultList[i][child.Key] = child.Result() n.ResultList[i][child.Key] = child.Result()
} }
} }

33
apijson/sqlparser.go

@ -2,6 +2,7 @@ package apijson
import ( import (
"bytes" "bytes"
"encoding/json"
"strconv" "strconv"
"strings" "strings"
) )
@ -58,17 +59,24 @@ func (e *MysqlExecutor) ToSQL() string {
return buf.String() return buf.String()
} }
func jsonEncode(v interface{}) string {
raw, _ := json.Marshal(v)
return string(raw)
}
func (e *MysqlExecutor) ParseCondition(field string, value interface{}) { func (e *MysqlExecutor) ParseCondition(field string, value interface{}) {
if values, ok := value.([]interface{}); ok { if values, ok := value.([]interface{}); ok {
// 数组使用 IN 条件 // 数组使用 IN 条件
condition := field + " in (" condition := field + " in ("
for i, v := range values { for i, v := range values {
if i == 0 { if i == 0 {
condition += "?"
// condition += "?"
condition += jsonEncode(v)
} else { } else {
condition += ",?"
// condition += ",?"
condition += "," + jsonEncode(v)
} }
e.params = append(e.params, v)
// e.params = append(e.params, v)
} }
e.where = append(e.where, condition+")") e.where = append(e.where, condition+")")
} else if valueStr, ok := value.(string); ok { } else if valueStr, ok := value.(string); ok {
@ -80,25 +88,28 @@ func (e *MysqlExecutor) ParseCondition(field string, value interface{}) {
e.columns = strings.Split(valueStr, ",") e.columns = strings.Split(valueStr, ",")
} }
} else { } else {
e.where = append(e.where, field+"=?")
e.params = append(e.params, valueStr)
// e.where = append(e.where, field+"=?")
e.where = append(e.where, field+"="+jsonEncode(valueStr))
// e.params = append(e.params, valueStr)
} }
} else { } else {
e.where = append(e.where, field+"=?")
e.params = append(e.params, value)
// e.where = append(e.where, field+"=?")
e.where = append(e.where, field+"="+jsonEncode(value))
// e.params = append(e.params, value)
} }
} }
func (e *MysqlExecutor) Exec() ([]map[string]interface{}, error) {
func (e *MysqlExecutor) Exec(c *QueryContext) ([]map[string]interface{}, error) {
sql := e.ToSQL() sql := e.ToSQL()
return QueryAll(sql, e.params...)
return QueryAll(c, sql, e.params...)
} }
var QueryAll = func(sql string, args ...interface{}) ([]map[string]interface{}, error) {
var QueryAll = func(c *QueryContext, sql string, args ...interface{}) ([]map[string]interface{}, error) {
return nil, nil return nil, nil
} }
func SetQueryAll(f func(sql string, args ...interface{}) ([]map[string]interface{}, error)) {
func SetQueryAll(f func(c *QueryContext, sql string, args ...interface{}) ([]map[string]interface{}, error)) {
QueryAll = f QueryAll = f
} }

Loading…
Cancel
Save