Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions expression/expressions/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ func (p *PatternLike) Eval(ctx context.Context, args map[interface{}]interface{}
if pattern == nil {
return nil, nil
}
spattern, ok := pattern.(string)
if !ok {
return nil, errors.Errorf("non-string pattern in LIKE: %v (Value of type %T)", pattern, pattern)
var spattern string
switch v := pattern.(type) {
case string:
spattern = v
case []byte:
spattern = string(v)
default:
return nil, errors.Errorf("Pattern should be string or []byte in LIKE: %v (Value of type %T)", pattern, pattern)
}

p.patChars, p.patTypes = compilePattern(spattern)
}

Expand Down
16 changes: 16 additions & 0 deletions expression/expressions/like_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,20 @@ func (*testLikeSuite) TestEval(c *C) {
pattern.Expr = mockExpr{isStatic: false, val: nil}
_, err = pattern.Eval(nil, nil)
c.Assert(err, IsNil)

// Testcase for "LIKE BINARY xxx"
pattern = &PatternLike{
Expr: mockExpr{isStatic: true, val: "slien"},
Pattern: mockExpr{isStatic: true, val: []byte("%E%")},
}
v, err := pattern.Eval(nil, nil)
c.Assert(err, IsNil)
c.Assert(v, IsTrue)
pattern = &PatternLike{
Expr: mockExpr{isStatic: true, val: "slin"},
Pattern: mockExpr{isStatic: true, val: []byte("%E%")},
}
v, err = pattern.Eval(nil, nil)
c.Assert(err, IsNil)
c.Assert(v, IsFalse)
}
2 changes: 1 addition & 1 deletion stmt/stmts/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (s *InsertIntoStmt) Exec(ctx context.Context) (_ rset.Recordset, err error)
// On duplicate key Update the duplicate row.
// Evaluate the updated value.
// TODO: report rows affected and last insert id.
toUpdateColumns, err := getUpdateColumns(t, s.OnDuplicate, false)
toUpdateColumns, err := getUpdateColumns(t, s.OnDuplicate, false, nil)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
24 changes: 21 additions & 3 deletions stmt/stmts/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,23 @@ func (s *UpdateStmt) SetText(text string) {
s.Text = text
}

func getUpdateColumns(t table.Table, assignList []expressions.Assignment, isMultipleTable bool) ([]*column.Col, error) {
func getUpdateColumns(t table.Table, assignList []expressions.Assignment, isMultipleTable bool, tblAliasMap map[string]string) ([]*column.Col, error) {
// TODO: We should check the validate if assignList in somewhere else. Maybe in building plan.
// TODO: We should use field.GetFieldIndex to replace this function.
tcols := make([]*column.Col, 0, len(assignList))
tname := t.TableName()
for _, asgn := range assignList {
if isMultipleTable {
if !strings.EqualFold(tname.O, asgn.TableName) {
continue
// Try to compare alias name with t.TableName()
if tblAliasMap == nil {
continue
}
if alias, ok := tblAliasMap[asgn.TableName]; !ok {
continue
} else if !strings.EqualFold(tname.O, alias) {
continue
}
}
}
col := column.FindCol(t.Cols(), asgn.ColName)
Expand Down Expand Up @@ -243,6 +252,15 @@ func (s *UpdateStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
updatedRowKeys := make(map[string]bool)
// For single-table syntax, TableRef may contain multiple tables
isMultipleTable := s.MultipleTable || s.TableRefs.MultipleTable()

// Get table alias map.
fs := p.GetFields()
tblAliasMap := make(map[string]string)
for _, f := range fs {
if f.TableName != f.OrgTableName {
tblAliasMap[f.TableName] = f.OrgTableName
}
}
for {
row, err1 := p.Next(ctx)
if err1 != nil {
Expand Down Expand Up @@ -279,7 +297,7 @@ func (s *UpdateStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
end := start + len(tbl.Cols())
data := rowData[start:end]
start = end
tcols, err2 := getUpdateColumns(tbl, s.List, isMultipleTable)
tcols, err2 := getUpdateColumns(tbl, s.List, isMultipleTable, tblAliasMap)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use GetFieldIndex or others in fields package.

I thin these can do the same thing, @qiuyesuifeng, how do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we have to do some update in field package to support these,
so i think it is better in a new PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can add a TODO here. @shenli

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

if err2 != nil {
return nil, errors.Trace(err2)
}
Expand Down
22 changes: 22 additions & 0 deletions stmt/stmts/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,26 @@ func (s *testStmtSuite) TestMultipleTableUpdate(c *C) {
}
rows.Close()
mustCommit(c, tx)

// JoinTable with alias table name.
r = mustExec(c, testDB, `UPDATE items T0 join month T1 on T0.id=T1.mid SET T0.price=T1.mprice;`)
c.Assert(r, NotNil)
tx = mustBegin(c, testDB)
rows, err = tx.Query("SELECT * FROM items")
c.Assert(err, IsNil)
expectedResult = map[int]string{
11: "month_price_11",
12: "items_price_12",
13: "month_price_13",
}
for rows.Next() {
var (
id int
price string
)
rows.Scan(&id, &price)
c.Assert(price, Equals, expectedResult[id])
}
rows.Close()
mustCommit(c, tx)
}