func getInsertPKValues(pkColumnNumbers []int, rowList sqlparser.Values, tableInfo *schema.Table) (pkValues []interface{}, err error) { pkValues = make([]interface{}, len(pkColumnNumbers)) for index, columnNumber := range pkColumnNumbers { if columnNumber == -1 { pkValues[index] = tableInfo.GetPKColumn(index).Default continue } values := make([]interface{}, len(rowList)) for j := 0; j < len(rowList); j++ { if _, ok := rowList[j].(*sqlparser.Subquery); ok { return nil, errors.New("row subquery not supported for inserts") } row := rowList[j].(sqlparser.ValTuple) if columnNumber >= len(row) { return nil, errors.New("column count doesn't match value count") } node := row[columnNumber] if !sqlparser.IsValue(node) { log.Warningf("insert is too complex %v", node) return nil, nil } var err error values[j], err = sqlparser.AsInterface(node) if err != nil { return nil, err } } if len(values) == 1 { pkValues[index] = values[0] } else { pkValues[index] = values } } return pkValues, nil }
func analyzeBoolean(node sqlparser.BoolExpr) (conditions []sqlparser.BoolExpr) { switch node := node.(type) { case *sqlparser.AndExpr: left := analyzeBoolean(node.Left) right := analyzeBoolean(node.Right) if left == nil || right == nil { return nil } if sqlparser.HasINClause(left) && sqlparser.HasINClause(right) { return nil } return append(left, right...) case *sqlparser.ParenBoolExpr: return analyzeBoolean(node.Expr) case *sqlparser.ComparisonExpr: switch { case sqlparser.StringIn( node.Operator, sqlparser.AST_EQ, sqlparser.AST_LT, sqlparser.AST_GT, sqlparser.AST_LE, sqlparser.AST_GE, sqlparser.AST_NSE, sqlparser.AST_LIKE): if sqlparser.IsColName(node.Left) && sqlparser.IsValue(node.Right) { return []sqlparser.BoolExpr{node} } case node.Operator == sqlparser.AST_IN: if sqlparser.IsColName(node.Left) && sqlparser.IsSimpleTuple(node.Right) { return []sqlparser.BoolExpr{node} } } case *sqlparser.RangeCond: if node.Operator != sqlparser.AST_BETWEEN { return nil } if sqlparser.IsColName(node.Left) && sqlparser.IsValue(node.From) && sqlparser.IsValue(node.To) { return []sqlparser.BoolExpr{node} } } return nil }
func getMatch(node sqlparser.BoolExpr, col string) (planID PlanID, values interface{}) { switch node := node.(type) { case *sqlparser.AndExpr: if planID, values = getMatch(node.Left, col); planID != SelectScatter { return planID, values } if planID, values = getMatch(node.Right, col); planID != SelectScatter { return planID, values } case *sqlparser.ParenBoolExpr: return getMatch(node.Expr, col) case *sqlparser.ComparisonExpr: switch node.Operator { case "=": if !nameMatch(node.Left, col) { return SelectScatter, nil } if !sqlparser.IsValue(node.Right) { return SelectScatter, nil } val, err := asInterface(node.Right) if err != nil { return SelectScatter, nil } return SelectEqual, val case "in": if !nameMatch(node.Left, col) { return SelectScatter, nil } if !sqlparser.IsSimpleTuple(node.Right) { return SelectScatter, nil } val, err := asInterface(node.Right) if err != nil { return SelectScatter, nil } node.Right = sqlparser.ListArg("::_vals") return SelectIN, val } } return SelectScatter, nil }
func analyzeUpdateExpressions(exprs sqlparser.UpdateExprs, pkIndex *schema.Index) (pkValues []interface{}, err error) { for _, expr := range exprs { index := pkIndex.FindColumn(sqlparser.GetColName(expr.Name)) if index == -1 { continue } if !sqlparser.IsValue(expr.Expr) { log.Warningf("expression is too complex %v", expr) return nil, TooComplex } if pkValues == nil { pkValues = make([]interface{}, len(pkIndex.Columns)) } var err error pkValues[index], err = sqlparser.AsInterface(expr.Expr) if err != nil { return nil, err } } return pkValues, nil }