|
@ -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 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|