Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
remove information_schema.columns usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu committed Jul 9, 2021
1 parent 09d257d commit 7dac228
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
6 changes: 3 additions & 3 deletions v4/export/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
20 changes: 8 additions & 12 deletions v4/export/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,32 +667,28 @@ 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)
}
defer rows.Close()
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
continue
}
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
}
Expand Down
55 changes: 29 additions & 26 deletions v4/export/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -238,31 +238,34 @@ 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, "*")
c.Assert(err, IsNil)
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`")
c.Assert(err, IsNil)
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`")
Expand Down

0 comments on commit 7dac228

Please sign in to comment.