From 7dac228759502c8ec79263968457e75f61bae381 Mon Sep 17 00:00:00 2001 From: Chunzhu Li Date: Fri, 9 Jul 2021 19:38:20 +0800 Subject: [PATCH] remove information_schema.columns usage --- v4/export/dump_test.go | 6 ++--- v4/export/sql.go | 20 ++++++--------- v4/export/sql_test.go | 55 ++++++++++++++++++++++-------------------- 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/v4/export/dump_test.go b/v4/export/dump_test.go index d6db5e87..f166513d 100644 --- a/v4/export/dump_test.go +++ b/v4/export/dump_test.go @@ -72,9 +72,9 @@ func (s *testSQLSuite) TestDumpTableMeta(c *C) { for serverType := ServerTypeUnknown; serverType < ServerTypeAll; serverType++ { conf.ServerInfo.ServerType = ServerType(serverType) hasImplicitRowID := false - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(database, table). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("id", "")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, "")) if serverType == ServerTypeTiDB { mock.ExpectExec("SELECT _tidb_rowid from"). WillReturnResult(sqlmock.NewResult(0, 0)) diff --git a/v4/export/sql.go b/v4/export/sql.go index 0dad0d8e..b67e7c8c 100644 --- a/v4/export/sql.go +++ b/v4/export/sql.go @@ -667,8 +667,8 @@ func createConnWithConsistency(ctx context.Context, db *sql.DB) (*sql.Conn, erro // buildSelectField returns the selecting fields' string(joined by comma(`,`)), // and the number of writable fields. func buildSelectField(db *sql.Conn, dbName, tableName string, completeInsert bool) (string, int, error) { // revive:disable-line:flag-parameter - query := `SELECT COLUMN_NAME,EXTRA FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=? AND TABLE_NAME=? ORDER BY ORDINAL_POSITION;` - rows, err := db.QueryContext(context.Background(), query, dbName, tableName) + query := fmt.Sprintf("SHOW COLUMNS FROM `%s`.`%s`", escapeString(dbName), escapeString(tableName)) + rows, err := db.QueryContext(context.Background(), query) if err != nil { return "", 0, errors.Annotatef(err, "sql: %s", query) } @@ -676,13 +676,12 @@ func buildSelectField(db *sql.Conn, dbName, tableName string, completeInsert boo availableFields := make([]string, 0) hasGenerateColumn := false - var fieldName string - var extra string - for rows.Next() { - err = rows.Scan(&fieldName, &extra) - if err != nil { - return "", 0, errors.Annotatef(err, "sql: %s", query) - } + results, err := GetSpecifiedColumnValuesAndClose(rows, "FIELD", "EXTRA") + if err != nil { + return "", 0, errors.Annotatef(err, "sql: %s", query) + } + for _, oneRow := range results { + fieldName, extra := oneRow[0], oneRow[1] switch extra { case "STORED GENERATED", "VIRTUAL GENERATED": hasGenerateColumn = true @@ -690,9 +689,6 @@ func buildSelectField(db *sql.Conn, dbName, tableName string, completeInsert boo } availableFields = append(availableFields, wrapBackTicks(escapeString(fieldName))) } - if err = rows.Err(); err != nil { - return "", 0, errors.Annotatef(err, "sql: %s", query) - } if completeInsert || hasGenerateColumn { return strings.Join(availableFields, ","), len(availableFields), nil } diff --git a/v4/export/sql_test.go b/v4/export/sql_test.go index f5b2b085..d60bfafc 100644 --- a/v4/export/sql_test.go +++ b/v4/export/sql_test.go @@ -75,9 +75,9 @@ func (s *testSQLSuite) TestBuildSelectAllQuery(c *C) { orderByClause, err := buildOrderByClause(mockConf, conn, "test", "t", true) c.Assert(err, IsNil) - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg()). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("id", "")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, "")) selectedField, _, err := buildSelectField(conn, "test", "t", false) c.Assert(err, IsNil) @@ -91,9 +91,9 @@ func (s *testSQLSuite) TestBuildSelectAllQuery(c *C) { orderByClause, err = buildOrderByClause(mockConf, conn, "test", "t", false) c.Assert(err, IsNil) - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg()). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("id", "")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, "")) selectedField, _, err = buildSelectField(conn, "test", "t", false) c.Assert(err, IsNil) @@ -114,9 +114,9 @@ func (s *testSQLSuite) TestBuildSelectAllQuery(c *C) { orderByClause, err := buildOrderByClause(mockConf, conn, "test", "t", false) c.Assert(err, IsNil, cmt) - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg()). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("id", "")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, "")) selectedField, _, err = buildSelectField(conn, "test", "t", false) c.Assert(err, IsNil) @@ -138,9 +138,9 @@ func (s *testSQLSuite) TestBuildSelectAllQuery(c *C) { orderByClause, err := buildOrderByClause(mockConf, conn, "test", "t", false) c.Assert(err, IsNil, cmt) - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg()). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("id", "")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, "")) selectedField, _, err = buildSelectField(conn, "test", "t", false) c.Assert(err, IsNil) @@ -157,9 +157,9 @@ func (s *testSQLSuite) TestBuildSelectAllQuery(c *C) { mockConf.ServerInfo.ServerType = ServerType(tp) cmt := Commentf("current server type: ", tp) - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg()). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("id", "")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, "")) selectedField, _, err := buildSelectField(conn, "test", "t", false) c.Assert(err, IsNil) @@ -238,9 +238,9 @@ func (s *testSQLSuite) TestBuildSelectField(c *C) { c.Assert(err, IsNil) // generate columns not found - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg()). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("id", "")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, "")) selectedField, _, err := buildSelectField(conn, "test", "t", false) c.Assert(selectedField, Equals, "*") @@ -248,10 +248,11 @@ func (s *testSQLSuite) TestBuildSelectField(c *C) { c.Assert(mock.ExpectationsWereMet(), IsNil) // user assigns completeInsert - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg()). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}).AddRow("id", ""). - AddRow("name", "").AddRow("quo`te", "")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, ""). + AddRow("name", "varchar(12)", "NO", "", nil, ""). + AddRow("quo`te", "varchar(12)", "NO", "UNI", nil, "")) selectedField, _, err = buildSelectField(conn, "test", "t", true) c.Assert(selectedField, Equals, "`id`,`name`,`quo``te`") @@ -259,10 +260,12 @@ func (s *testSQLSuite) TestBuildSelectField(c *C) { c.Assert(mock.ExpectationsWereMet(), IsNil) // found generate columns, rest columns is `id`,`name` - mock.ExpectQuery("SELECT COLUMN_NAME"). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg()). - WillReturnRows(sqlmock.NewRows([]string{"column_name", "extra"}). - AddRow("id", "").AddRow("name", "").AddRow("quo`te", "").AddRow("generated", "VIRTUAL GENERATED")) + mock.ExpectQuery("SHOW COLUMNS FROM"). + WillReturnRows(sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}). + AddRow("id", "int(11)", "NO", "PRI", nil, ""). + AddRow("name", "varchar(12)", "NO", "", nil, ""). + AddRow("quo`te", "varchar(12)", "NO", "UNI", nil, ""). + AddRow("generated", "varchar(12)", "NO", "", nil, "VIRTUAL GENERATED")) selectedField, _, err = buildSelectField(conn, "test", "t", false) c.Assert(selectedField, Equals, "`id`,`name`,`quo``te`")