ListAllDatabasesTables lists all the databases and tables from the database listTableByInfoSchema list tables by table information_schema in MySQL listTableByShowTableStatus has better performance than listTableByInfoSchema listTableByShowFullTables is used in mysql8 version [8.0.3,8.0.23), more det
(tctx *tcontext.Context, db *sql.Conn, databaseNames []string, listType listTableType, tableTypes ...TableType)
| 365 | // listTableByShowTableStatus has better performance than listTableByInfoSchema |
| 366 | // listTableByShowFullTables is used in mysql8 version [8.0.3,8.0.23), more details can be found in the comments of func matchMysqlBugversion |
| 367 | func ListAllDatabasesTables(tctx *tcontext.Context, db *sql.Conn, databaseNames []string, |
| 368 | listType listTableType, tableTypes ...TableType) (DatabaseTables, error) { // revive:disable-line:flag-parameter |
| 369 | dbTables := DatabaseTables{} |
| 370 | var ( |
| 371 | table, tableTypeStr string |
| 372 | tableType TableType |
| 373 | avgRowLength uint64 |
| 374 | err error |
| 375 | ) |
| 376 | |
| 377 | tableTypeConditions := make([]string, len(tableTypes)) |
| 378 | for i, tableType := range tableTypes { |
| 379 | tableTypeConditions[i] = fmt.Sprintf("TABLE_TYPE='%s'", tableType) |
| 380 | } |
| 381 | switch listType { |
| 382 | case listTableByInfoSchema: |
| 383 | for _, schema := range databaseNames { |
| 384 | query := fmt.Sprintf("SELECT TABLE_NAME,TABLE_TYPE,AVG_ROW_LENGTH FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=? AND (%s)", strings.Join(tableTypeConditions, " OR ")) |
| 385 | dbTables[schema] = make([]*TableInfo, 0) |
| 386 | if err = simpleQueryWithArgs(tctx, db, func(rows *sql.Rows) error { |
| 387 | var ( |
| 388 | sqlAvgRowLength sql.NullInt64 |
| 389 | err2 error |
| 390 | ) |
| 391 | if err2 = rows.Scan(&table, &tableTypeStr, &sqlAvgRowLength); err != nil { |
| 392 | return errors.Trace(err2) |
| 393 | } |
| 394 | tableType, err2 = ParseTableType(tableTypeStr) |
| 395 | if err2 != nil { |
| 396 | return errors.Trace(err2) |
| 397 | } |
| 398 | |
| 399 | if sqlAvgRowLength.Valid { |
| 400 | avgRowLength = uint64(sqlAvgRowLength.Int64) |
| 401 | } else { |
| 402 | avgRowLength = 0 |
| 403 | } |
| 404 | dbTables[schema] = append(dbTables[schema], &TableInfo{table, avgRowLength, tableType}) |
| 405 | return nil |
| 406 | }, query, schema); err != nil { |
| 407 | return nil, errors.Annotatef(err, "sql: %s", query) |
| 408 | } |
| 409 | } |
| 410 | case listTableByShowFullTables: |
| 411 | for _, schema := range databaseNames { |
| 412 | dbTables[schema] = make([]*TableInfo, 0) |
| 413 | query := fmt.Sprintf("SHOW FULL TABLES FROM `%s` WHERE %s", |
| 414 | escapeString(schema), strings.Join(tableTypeConditions, " OR ")) |
| 415 | if err = simpleQueryWithArgs(tctx, db, func(rows *sql.Rows) error { |
| 416 | var err2 error |
| 417 | if err2 = rows.Scan(&table, &tableTypeStr); err != nil { |
| 418 | return errors.Trace(err2) |
| 419 | } |
| 420 | tableType, err2 = ParseTableType(tableTypeStr) |
| 421 | if err2 != nil { |
| 422 | return errors.Trace(err2) |
| 423 | } |
| 424 | avgRowLength = 0 // can't get avgRowLength from the result of `show full tables` so hardcode to 0 here |