Browse Source

'fix'

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

8
apijson/query_context.go

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

8
apijson/query_node.go

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

33
apijson/sqlparser.go

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

Loading…
Cancel
Save