func analyzeSelectExprs(exprs sqlparser.SelectExprs, table *schema.Table) (selects []int, err error) { selects = make([]int, 0, len(exprs)) for _, expr := range exprs { switch expr := expr.(type) { case *sqlparser.StarExpr: // Append all columns. for colIndex := range table.Columns { selects = append(selects, colIndex) } case *sqlparser.NonStarExpr: name := sqlparser.GetColName(expr.Expr) if name == "" { // Not a simple column name. return nil, nil } colIndex := table.FindColumn(name) if colIndex == -1 { return nil, fmt.Errorf("column %s not found in table %s", name, table.Name) } selects = append(selects, colIndex) default: panic("unreachable") } } return selects, nil }
func getInsertPKColumns(columns sqlparser.Columns, tableInfo *schema.Table) (pkColumnNumbers []int) { if len(columns) == 0 { return tableInfo.PKColumns } pkIndex := tableInfo.Indexes[0] pkColumnNumbers = make([]int, len(pkIndex.Columns)) for i := range pkColumnNumbers { pkColumnNumbers[i] = -1 } for i, column := range columns { index := pkIndex.FindColumn(sqlparser.GetColName(column.(*sqlparser.NonStarExpr).Expr)) if index == -1 { continue } pkColumnNumbers[index] = i } return pkColumnNumbers }
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 }
func buildIndexPlan(ins *sqlparser.Insert, tablename string, colVindex *ColVindex, plan *Plan) error { pos := -1 for i, column := range ins.Columns { if colVindex.Col == sqlparser.GetColName(column.(*sqlparser.NonStarExpr).Expr) { pos = i break } } if pos == -1 { pos = len(ins.Columns) ins.Columns = append(ins.Columns, &sqlparser.NonStarExpr{Expr: &sqlparser.ColName{Name: []byte(colVindex.Col)}}) ins.Rows.(sqlparser.Values)[0] = append(ins.Rows.(sqlparser.Values)[0].(sqlparser.ValTuple), &sqlparser.NullVal{}) } row := ins.Rows.(sqlparser.Values)[0].(sqlparser.ValTuple) val, err := asInterface(row[pos]) if err != nil { return fmt.Errorf("could not convert val: %s, pos: %d", row[pos], pos) } plan.Values = append(plan.Values.([]interface{}), val) row[pos] = sqlparser.ValArg([]byte(fmt.Sprintf(":_%s", colVindex.Col))) return nil }