Skip to content

Commit

Permalink
execute: set TableAsName and ColumnAsName for prepared statement (#5776)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored and XuHuaiyu committed Feb 2, 2018
1 parent 16e7e31 commit 2f8b1d2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ func (e *PrepareExec) DoPrepare() {
}
if result, ok := stmt.(ast.ResultSetNode); ok {
e.Fields = result.GetResultFields()
for _, fields := range e.Fields {
if fields.TableAsName.O == "" {
fields.TableAsName = fields.Table.Name
}
if fields.ColumnAsName.O == "" {
fields.ColumnAsName = fields.Column.Name
}
}
}

// The parameter markers are appended in visiting order, which may not
Expand Down
31 changes: 31 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,37 @@ func (s *testSuite) TestPrepared(c *C) {
_, err = tk.Exec("execute stmt")
c.Assert(err, NotNil)

_, _, fields, err := tk.Se.PrepareStmt("select a from prepare3")
c.Assert(err, IsNil)
c.Assert(fields[0].DBName.L, Equals, "test")
c.Assert(fields[0].TableAsName.L, Equals, "prepare3")
c.Assert(fields[0].ColumnAsName.L, Equals, "a")

_, _, fields, err = tk.Se.PrepareStmt("select a from prepare3 where ?")
c.Assert(err, IsNil)
c.Assert(fields[0].DBName.L, Equals, "test")
c.Assert(fields[0].TableAsName.L, Equals, "prepare3")
c.Assert(fields[0].ColumnAsName.L, Equals, "a")

_, _, fields, err = tk.Se.PrepareStmt("select (1,1) in (select 1,1)")
c.Assert(err, IsNil)
c.Assert(fields[0].DBName.L, Equals, "")
c.Assert(fields[0].TableAsName.L, Equals, "")
c.Assert(fields[0].ColumnAsName.L, Equals, "(1,1) in (select 1,1)")

_, _, fields, err = tk.Se.PrepareStmt("select * from prepare3 as t1 join prepare3 as t2")
c.Assert(err, IsNil)
c.Assert(fields[0].DBName.L, Equals, "test")
c.Assert(fields[0].TableAsName.L, Equals, "t1")
c.Assert(fields[0].ColumnAsName.L, Equals, "a")
c.Assert(fields[1].DBName.L, Equals, "test")
c.Assert(fields[1].TableAsName.L, Equals, "t2")
c.Assert(fields[1].ColumnAsName.L, Equals, "a")

_, _, fields, err = tk.Se.PrepareStmt("update prepare3 set a = ?")
c.Assert(err, IsNil)
c.Assert(len(fields), Equals, 0)

// Coverage.
exec := &executor.ExecuteExec{}
exec.Next()
Expand Down

0 comments on commit 2f8b1d2

Please sign in to comment.