MCPcopy Create free account
hub / github.com/bytebase/bytebase / getTableStmt

Function getTableStmt

backend/plugin/db/starrocks/dump.go:416–461  ·  view source on GitHub ↗

getTableStmt gets the create statement of a table.

(db *sql.DB, dbName, tblName, tblType string)

Source from the content-addressed store, hash-verified

414
415// getTableStmt gets the create statement of a table.
416func getTableStmt(db *sql.DB, dbName, tblName, tblType string) (string, error) {
417 switch tblType {
418 case baseTableType:
419 query := fmt.Sprintf("SHOW CREATE TABLE `%s`.`%s`;", dbName, tblName)
420 var stmt, unused string
421 if err := db.QueryRow(query).Scan(&unused, &stmt); err != nil {
422 if err == sql.ErrNoRows {
423 return "", common.FormatDBErrorEmptyRowWithQuery(query)
424 }
425 return "", err
426 }
427 // Remove trailing semicolon if present, as the format string will add one
428 stmt = strings.TrimSuffix(stmt, ";")
429 return fmt.Sprintf(tableStmtFmt, tblName, stmt), nil
430 case viewTableType:
431 // This differs from mysqldump as it includes.
432 query := fmt.Sprintf("SHOW CREATE VIEW `%s`.`%s`;", dbName, tblName)
433 var createStmt, unused string
434 if err := db.QueryRow(query).Scan(&unused, &createStmt, &unused, &unused); err != nil {
435 if err == sql.ErrNoRows {
436 return "", common.FormatDBErrorEmptyRowWithQuery(query)
437 }
438 return "", err
439 }
440 // Remove trailing semicolon if present, as the format string will add one
441 createStmt = strings.TrimSuffix(createStmt, ";")
442 return fmt.Sprintf(viewStmtFmt, tblName, createStmt), nil
443 case materializedViewType:
444 // For Doris materialized views, we use SHOW CREATE MATERIALIZED VIEW.
445 // This command returns 2 columns: materialized view name and create statement.
446 query := fmt.Sprintf("SHOW CREATE MATERIALIZED VIEW `%s`.`%s`;", dbName, tblName)
447 var createStmt, unused string
448 if err := db.QueryRow(query).Scan(&unused, &createStmt); err != nil {
449 if err == sql.ErrNoRows {
450 return "", common.FormatDBErrorEmptyRowWithQuery(query)
451 }
452 return "", err
453 }
454 // Remove trailing semicolon if present, as the format string will add one
455 createStmt = strings.TrimSuffix(createStmt, ";")
456 return fmt.Sprintf(materializedViewStmtFmt, tblName, createStmt), nil
457 default:
458 slog.Debug("skip unsupported table type", slog.String("dbName", dbName), slog.String("tableName", tblName), slog.String("tableType", tblType))
459 return "", nil
460 }
461}
462
463// getViewColumns gets the create statement of a table.
464func getViewColumns(db *sql.DB, dbName, tblName string) ([]string, error) {

Callers 1

getTablesFunction · 0.85

Calls 4

ScanMethod · 0.80
DebugMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected