|
|
@@ -11,10 +11,23 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ clauseTableRowValueKindTime int = iota + 1
|
|
|
+ clauseTableRowValueKindBool
|
|
|
+ clauseTableRowValueKindString
|
|
|
+ clauseTableRowValueKindUint64
|
|
|
+ clauseTableRowValueKindFloat64
|
|
|
+)
|
|
|
+
|
|
|
+type clauseTableRowValue struct {
|
|
|
+ kind int
|
|
|
+ value any
|
|
|
+}
|
|
|
+
|
|
|
type insertClause struct {
|
|
|
table string
|
|
|
keyColumns []string
|
|
|
- tableRows map[string]any
|
|
|
+ tableRows map[string]clauseTableRowValue
|
|
|
}
|
|
|
|
|
|
type deleteClause struct {
|
|
|
@@ -98,7 +111,7 @@ func insertWalk(sql string) (*insertClause, error) {
|
|
|
clause.table = tableFmtCtx.String()
|
|
|
|
|
|
// 解析rows
|
|
|
- values := make([]any, 0)
|
|
|
+ values := make([]clauseTableRowValue, 0)
|
|
|
|
|
|
rowsStatement, err := parser.Parse(realNode.Rows.String())
|
|
|
if err != nil {
|
|
|
@@ -113,31 +126,43 @@ func insertWalk(sql string) (*insertClause, error) {
|
|
|
switch rowsNode := node.(type) {
|
|
|
case *tree.FuncExpr:
|
|
|
// 函数类型
|
|
|
- value, err := evaluateFuncExpr(rowsNode, nil, &funcStringParams)
|
|
|
+ value, kind, err := evaluateFuncExpr(rowsNode, nil, &funcStringParams)
|
|
|
if err != nil {
|
|
|
rowsWalkFuncErr = err
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
- values = append(values, value)
|
|
|
+ values = append(values, clauseTableRowValue{
|
|
|
+ kind: kind,
|
|
|
+ value: value,
|
|
|
+ })
|
|
|
case *tree.DBool:
|
|
|
// 布尔类型
|
|
|
stringValue := rowsNode.String()
|
|
|
if !canSkipValueInThisContext(&funcStringParams, stringValue) {
|
|
|
if stringValue == "false" {
|
|
|
- values = append(values, false)
|
|
|
+ values = append(values, clauseTableRowValue{
|
|
|
+ kind: clauseTableRowValueKindBool,
|
|
|
+ value: false,
|
|
|
+ })
|
|
|
} else if stringValue == "true" {
|
|
|
- values = append(values, true)
|
|
|
+ values = append(values, clauseTableRowValue{
|
|
|
+ kind: clauseTableRowValueKindBool,
|
|
|
+ value: true,
|
|
|
+ })
|
|
|
} else {
|
|
|
rowsWalkFuncErr = errors.New("不支持的bool值")
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
|
- case *tree.UnresolvedName:
|
|
|
- // 字符串类型或者函数参数类型,这里通过比较字符串value,排除了函数参数类型
|
|
|
+ case *tree.StrVal:
|
|
|
+ // 字符串类型或者函数参数是字符串的类型,这里通过比较字符串value,排除了函数参数类型
|
|
|
stringValue := rowsNode.String()
|
|
|
if !canSkipValueInThisContext(&funcStringParams, stringValue) {
|
|
|
- values = append(values, stringValue)
|
|
|
+ values = append(values, clauseTableRowValue{
|
|
|
+ kind: clauseTableRowValueKindString,
|
|
|
+ value: rowsNode.RawString(),
|
|
|
+ })
|
|
|
}
|
|
|
case *tree.NumVal:
|
|
|
// 数值类型,可以是整形或浮点型
|
|
|
@@ -151,7 +176,10 @@ func insertWalk(sql string) (*insertClause, error) {
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
- values = append(values, valueUint64)
|
|
|
+ values = append(values, clauseTableRowValue{
|
|
|
+ kind: clauseTableRowValueKindUint64,
|
|
|
+ value: valueUint64,
|
|
|
+ })
|
|
|
} else if numKind == constant.Float {
|
|
|
valueFloat64, err := strconv.ParseFloat(rowsNode.String(), 64)
|
|
|
if err != nil {
|
|
|
@@ -159,12 +187,18 @@ func insertWalk(sql string) (*insertClause, error) {
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
- values = append(values, valueFloat64)
|
|
|
+ values = append(values, clauseTableRowValue{
|
|
|
+ kind: clauseTableRowValueKindFloat64,
|
|
|
+ value: valueFloat64,
|
|
|
+ })
|
|
|
} else {
|
|
|
rowsWalkFuncErr = errors.New("不支持的数值类型")
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
|
+ case *tree.UnresolvedName:
|
|
|
+ rowsWalkFuncErr = errors.New("存在无法解析的名字,是否应该使用单引号")
|
|
|
+ return true
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
@@ -185,7 +219,7 @@ func insertWalk(sql string) (*insertClause, error) {
|
|
|
hasIDColumn := false
|
|
|
keyColumns := make([]string, 0)
|
|
|
allColumns := make([]string, 0)
|
|
|
- tableRows := make(map[string]any)
|
|
|
+ tableRows := make(map[string]clauseTableRowValue)
|
|
|
|
|
|
for i, column := range realNode.Columns.ToStrings() {
|
|
|
if column == "id" {
|
|
|
@@ -240,10 +274,10 @@ func selectWalk(sql string) (*insertClause, error) {
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
-func evaluateFuncExpr(expr *tree.FuncExpr, funcName *string, stringParams *[]string) (any, error) {
|
|
|
+func evaluateFuncExpr(expr *tree.FuncExpr, funcName *string, stringParams *[]string) (any, int, error) {
|
|
|
if strings.HasPrefix(expr.String(), "parse_time") {
|
|
|
if expr.Exprs == nil || len(expr.Exprs) != 2 {
|
|
|
- return nil, errors.New("parse_time(time_str, time_format)")
|
|
|
+ return nil, 0, errors.New("parse_time(time_str, time_format)")
|
|
|
}
|
|
|
|
|
|
timeStr := expr.Exprs[0].String()
|
|
|
@@ -257,9 +291,14 @@ func evaluateFuncExpr(expr *tree.FuncExpr, funcName *string, stringParams *[]str
|
|
|
*stringParams = append(*stringParams, timeStr, timeFormat)
|
|
|
}
|
|
|
|
|
|
- return time.ParseInLocation(timeFormat, timeStr, time.Local)
|
|
|
+ parsedTime, err := time.ParseInLocation(timeFormat, timeStr, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ return nil, 0, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return parsedTime, clauseTableRowValueKindTime, nil
|
|
|
} else {
|
|
|
- return nil, errors.New("不支持的函数")
|
|
|
+ return nil, 0, errors.New("不支持的函数")
|
|
|
}
|
|
|
}
|
|
|
|