-
Notifications
You must be signed in to change notification settings - Fork 298
refactor(conn): replace dbutil.GetTables with conn.GetTables for cons… #12481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -483,7 +483,7 @@ func FetchAllDoTables(ctx context.Context, db *BaseDB, bw *filter.Filter) (map[s | |||||||
| schemaToTables := make(map[string][]string) | ||||||||
| for _, ftSchema := range ftSchemas { | ||||||||
| schema := ftSchema.Schema | ||||||||
| tables, err := dbutil.GetTables(ctx, db.DB, schema) | ||||||||
| tables, err := GetTables(ctx, db.DB, schema) | ||||||||
| if err != nil { | ||||||||
| return nil, terror.DBErrorAdapt(err, db.Scope, terror.ErrDBDriverError) | ||||||||
| } | ||||||||
|
|
@@ -555,3 +555,47 @@ func FetchTargetDoTables( | |||||||
|
|
||||||||
| return tableMapper, extendedColumnPerTable, nil | ||||||||
| } | ||||||||
|
|
||||||||
| // GetTables returns all table names in the schema. | ||||||||
| // It supports both standard MySQL (2 columns) and Ali RDS (4 columns). | ||||||||
| func GetTables(ctx context.Context, db *sql.DB, schema string) ([]string, error) { | ||||||||
| query := fmt.Sprintf("SHOW FULL TABLES IN `%s`", strings.ReplaceAll(schema, "`", "``")) | ||||||||
| rows, err := db.QueryContext(ctx, query) | ||||||||
| if err != nil { | ||||||||
| return nil, err | ||||||||
| } | ||||||||
| defer rows.Close() | ||||||||
|
|
||||||||
| cols, err := rows.Columns() | ||||||||
| if err != nil { | ||||||||
| return nil, err | ||||||||
| } | ||||||||
|
|
||||||||
| if len(cols) < 2 { | ||||||||
| return nil, errors.New("SHOW FULL TABLES returned less than 2 columns") | ||||||||
| } | ||||||||
|
|
||||||||
| var tableName, tableType string | ||||||||
| scanArgs := make([]interface{}, len(cols)) | ||||||||
| scanArgs[0] = &tableName | ||||||||
| scanArgs[1] = &tableType | ||||||||
| for i := 2; i < len(cols); i++ { | ||||||||
| var dummy interface{} | ||||||||
| scanArgs[i] = &dummy | ||||||||
|
Comment on lines
+583
to
+584
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop for creating dummy scan arguments can be made more efficient and idiomatic. Instead of declaring a new
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| var tables []string | ||||||||
| for rows.Next() { | ||||||||
| err = rows.Scan(scanArgs...) | ||||||||
| if err != nil { | ||||||||
| return nil, err | ||||||||
| } | ||||||||
| if tableType == "BASE TABLE" { | ||||||||
| tables = append(tables, tableName) | ||||||||
| } | ||||||||
| } | ||||||||
| if err := rows.Err(); err != nil { | ||||||||
| return nil, err | ||||||||
| } | ||||||||
| return tables, nil | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with the error handling in this package, it's better to return a
terrorerror instead of a raw error created witherrors.New. You can useterror.ErrDBUnExpect.Generatefor this.