Skip to content

Commit

Permalink
*: Remove duplicate getRows function
Browse files Browse the repository at this point in the history
  • Loading branch information
shenli committed Mar 1, 2016
1 parent 9f81af6 commit 784f135
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 55 deletions.
30 changes: 15 additions & 15 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s *testSessionSuite) TestResultField(c *C) {
mustExecSQL(c, se, `INSERT INTO t VALUES (2);`)
r := mustExecSQL(c, se, `SELECT count(*) from t;`)
c.Assert(r, NotNil)
_, err := getRows(r)
_, err := GetRows(r)
c.Assert(err, IsNil)
fields, err := r.Fields()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -327,7 +327,7 @@ func (s *testSessionSuite) TestRowLock(c *C) {
// Check the result is correct
se3 := newSession(c, store, s.dbName)
r := mustExecSQL(c, se3, "select c2 from t where c1=11")
rows, err := getRows(r)
rows, err := GetRows(r)
fmt.Println(rows)
matches(c, rows, [][]interface{}{{21}})

Expand Down Expand Up @@ -360,7 +360,7 @@ func (s *testSessionSuite) TestSelectForUpdate(c *C) {
mustExecSQL(c, se1, "begin")
rs, err := exec(c, se1, "select * from t where c1=11 for update")
c.Assert(err, IsNil)
_, err = getRows(rs)
_, err = GetRows(rs)

mustExecSQL(c, se2, "begin")
mustExecSQL(c, se2, "update t set c2=211 where c1=11")
Expand All @@ -375,7 +375,7 @@ func (s *testSessionSuite) TestSelectForUpdate(c *C) {
// not conflict
mustExecSQL(c, se1, "begin")
rs, err = exec(c, se1, "select * from t where c1=11 for update")
_, err = getRows(rs)
_, err = GetRows(rs)

mustExecSQL(c, se2, "begin")
mustExecSQL(c, se2, "update t set c2=22 where c1=12")
Expand All @@ -386,7 +386,7 @@ func (s *testSessionSuite) TestSelectForUpdate(c *C) {
// not conflict, auto commit
mustExecSQL(c, se1, "set @@autocommit=1;")
rs, err = exec(c, se1, "select * from t where c1=11 for update")
_, err = getRows(rs)
_, err = GetRows(rs)

mustExecSQL(c, se2, "begin")
mustExecSQL(c, se2, "update t set c2=211 where c1=11")
Expand Down Expand Up @@ -451,7 +451,7 @@ func (s *testSessionSuite) TestIndex(c *C) {
mustExecSQL(c, se, "create table if not exists test_index (c1 int, c double, index(c1), index(c))")
mustExecSQL(c, se, "insert into test_index values (1, 2), (3, null)")
r := mustExecSQL(c, se, "select c1 from test_index where c > 0")
rows, err := getRows(r)
rows, err := GetRows(r)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 1)
match(c, rows[0], 1)
Expand All @@ -465,12 +465,12 @@ func (s *testSessionSuite) TestIndex(c *C) {
insert into t2 values (2);`)

r = mustExecSQL(c, se, "select * from t1 left join t2 on t1.c1 = t2.c2 order by t1.c1")
rows, err = getRows(r)
rows, err = GetRows(r)
c.Assert(err, IsNil)
matches(c, rows, [][]interface{}{{1, nil}, {2, 2}})

r = mustExecSQL(c, se, "select * from t1 left join t2 on t1.c1 = t2.c2 where t2.c2 < 10")
rows, err = getRows(r)
rows, err = GetRows(r)
c.Assert(err, IsNil)
matches(c, rows, [][]interface{}{{2, 2}})
}
Expand Down Expand Up @@ -574,7 +574,7 @@ func (s *testSessionSuite) TestSelect(c *C) {
mustExecSQL(c, se, "insert into t3 values (1), (3)")

r = mustExecSQL(c, se, "select * from t1 left join t2 on t1.c1 = t2.c2 left join t3 on t1.c1 = t3.c3 order by t1.c1, t2.c2, t3.c3")
rows, err := getRows(r)
rows, err := GetRows(r)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 4)
match(c, rows[0], 1, 1, 1, 1)
Expand Down Expand Up @@ -606,7 +606,7 @@ func (s *testSessionSuite) TestSelect(c *C) {
insert into t2 values (2);
insert into t3 values (3);`)
r = mustExecSQL(c, se, "select * from t1 left join t2 on t1.c1 = t2.c2 left join t3 on t1.c1 = t3.c3 order by t1.c1")
rows, err = getRows(r)
rows, err = GetRows(r)
c.Assert(err, IsNil)
matches(c, rows, [][]interface{}{{1, nil, nil}, {2, 2, nil}})

Expand Down Expand Up @@ -640,7 +640,7 @@ func (s *testSessionSuite) TestSubQuery(c *C) {
match(c, row.Data, 1)

r = mustExecSQL(c, se, `select (select count(c1) from t2 where t2.c1 != t1.c2) from t1`)
rows, err := getRows(r)
rows, err := GetRows(r)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 2)
match(c, rows[0], 0)
Expand All @@ -662,7 +662,7 @@ func (s *testSessionSuite) TestShow(c *C) {
mustExecSQL(c, se, "drop table if exists t")
mustExecSQL(c, se, "create table if not exists t (c int)")
r = mustExecSQL(c, se, `show columns from t`)
rows, err := getRows(r)
rows, err := GetRows(r)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 1)
match(c, rows[0], "c", "int(11)", "YES", "", nil, "")
Expand Down Expand Up @@ -878,7 +878,7 @@ func (s *testSessionSuite) TestSet(c *C) {
match(c, row.Data, "a")

r = mustExecSQL(c, se, "select * from t where c = 'a,b'")
rows, err := getRows(r)
rows, err := GetRows(r)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 2)

Expand Down Expand Up @@ -929,7 +929,7 @@ func (s *testSessionSuite) TestWhereLike(c *C) {
mustExecSQL(c, se, "insert into t values ()")

r := mustExecSQL(c, se, "select c from t where c like '%1%'")
rows, err := getRows(r)
rows, err := GetRows(r)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 6)

Expand All @@ -949,7 +949,7 @@ func (s *testSessionSuite) TestDefaultFlenBug(c *C) {
// The data in the second src will be casted as the type of the first src.
// If use flen=0, it will be truncated.
r := mustExecSQL(c, se, "select c from t1 union select c from t2;")
rows, err := getRows(r)
rows, err := GetRows(r)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 2)
c.Assert(rows[1][0], Equals, float64(930))
Expand Down
21 changes: 21 additions & 0 deletions tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ func runStmt(ctx context.Context, s ast.Statement, args ...interface{}) (ast.Rec
return rs, errors.Trace(err)
}

// GetRows gets all the rows from a RecordSet.
func GetRows(rs ast.RecordSet) ([][]interface{}, error) {
if rs == nil {
return nil, nil
}
var rows [][]interface{}
defer rs.Close()
// Negative limit means no limit.
for {
row, err := rs.Next()
if err != nil {
return nil, errors.Trace(err)
}
if row == nil {
break
}
rows = append(rows, row.Data)
}
return rows, nil
}

// RegisterStore registers a kv storage with unique name and its associated Driver.
func RegisterStore(name string, driver kv.Driver) error {
name = strings.ToLower(name)
Expand Down
24 changes: 3 additions & 21 deletions tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package tidb

import (
"database/sql"
"errors"
"flag"
"fmt"
"net/url"
Expand All @@ -25,7 +24,7 @@ import (
"testing"
"time"

jerrors "github.com/juju/errors"
"github.com/juju/errors"
"github.com/ngaut/log"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/ast"
Expand Down Expand Up @@ -221,7 +220,7 @@ func (s *testMainSuite) TestCaseInsensitive(c *C) {
mustExecSQL(c, se, "update T set b = B + 1")
mustExecSQL(c, se, "update T set B = b + 1")
rs = mustExecSQL(c, se, "select b from T")
rows, err := getRows(rs)
rows, err := GetRows(rs)
c.Assert(err, IsNil)
match(c, rows[0], 3)
mustExecSQL(c, se, s.dropDBSQL)
Expand Down Expand Up @@ -429,26 +428,9 @@ func matches(c *C, rows [][]interface{}, expected [][]interface{}) {
}
}

func getRows(rs ast.RecordSet) ([][]interface{}, error) {
var rows [][]interface{}
defer rs.Close()
// Negative limit means no limit.
for {
row, err := rs.Next()
if err != nil {
return nil, jerrors.Trace(err)
}
if row == nil {
break
}
rows = append(rows, row.Data)
}
return rows, nil
}

func mustExecMatch(c *C, se Session, sql string, expected [][]interface{}) {
r := mustExecSQL(c, se, sql)
rows, err := getRows(r)
rows, err := GetRows(r)
c.Assert(err, IsNil)
matches(c, rows, expected)
}
Expand Down
20 changes: 1 addition & 19 deletions util/testkit/testkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"fmt"
"strings"

"github.com/juju/errors"
"github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/ast"
Expand Down Expand Up @@ -101,28 +100,11 @@ func (tk *TestKit) MustQuery(sql string, args ...interface{}) *Result {
rs, err := tk.Exec(sql, args...)
tk.c.Assert(err, check.IsNil, comment)
tk.c.Assert(rs, check.NotNil, comment)
rows, err := getRows(rs)
rows, err := tidb.GetRows(rs)
tk.c.Assert(err, check.IsNil, comment)
return &Result{rows: rows, c: tk.c, comment: comment}
}

func getRows(rs ast.RecordSet) ([][]interface{}, error) {
var rows [][]interface{}
defer rs.Close()
// Negative limit means no limit.
for {
row, err := rs.Next()
if err != nil {
return nil, errors.Trace(err)
}
if row == nil {
break
}
rows = append(rows, row.Data)
}
return rows, nil
}

// RowsWithSep is a convenient function to wrap args to a slice of []interface.
// The arg represents a row, split by sep.
func RowsWithSep(sep string, args ...string) [][]interface{} {
Expand Down

0 comments on commit 784f135

Please sign in to comment.