MCPcopy Index your code
hub / github.com/bytebase/bytebase / generateSQLForSingleTable

Function generateSQLForSingleTable

backend/plugin/parser/tidb/backup.go:496–594  ·  view source on GitHub ↗
(ctx context.Context, tCtx base.TransformContext, statementInfoList []statementInfo, databaseName string, tablePrefix string)

Source from the content-addressed store, hash-verified

494}
495
496func generateSQLForSingleTable(ctx context.Context, tCtx base.TransformContext, statementInfoList []statementInfo, databaseName string, tablePrefix string) ([]base.BackupStatement, error) {
497 table := statementInfoList[0].table
498
499 for _, item := range statementInfoList {
500 if !equalTable(table, item.table, tCtx.IsCaseSensitive) {
501 return nil, errors.Errorf("prior backup cannot handle statements on different tables more than %d", maxMixedDMLCount)
502 }
503 // This path UNION ALLs the statements into one backup SELECT. A WITH
504 // (CTE) prefix can't be emitted per union arm (TiDB rejects WITH after
505 // UNION ALL), so reject CTE statements here. CTEs are still supported on
506 // the per-statement mixed-DML path (<= maxMixedDMLCount).
507 if extractCTE(item.fullSQL, nodeStmtLoc(item.node)) != "" {
508 return nil, errors.Errorf("prior backup does not support WITH (CTE) statements when more than %d DML statements target the same table", maxMixedDMLCount)
509 }
510 }
511 generatedColumns, normalColumns, err := classifyColumns(ctx, tCtx.GetDatabaseMetadataFunc, tCtx.ListDatabaseNamesFunc, tCtx.IsCaseSensitive, tCtx.InstanceID, table)
512 if err != nil {
513 return nil, errors.Wrap(err, "failed to classify columns")
514 }
515
516 targetTable := fmt.Sprintf("%s_%s_%s", tablePrefix, table.Table, table.Database)
517 targetTable, _ = common.TruncateString(targetTable, maxTableNameLength)
518 var buf strings.Builder
519 if _, err := fmt.Fprintf(&buf, "CREATE TABLE `%s`.`%s` LIKE `%s`.`%s`;\n", databaseName, targetTable, table.Database, table.Table); err != nil {
520 return nil, errors.Wrap(err, "failed to write create table statement")
521 }
522
523 if _, err := fmt.Fprintf(&buf, "INSERT INTO `%s`.`%s`", databaseName, targetTable); err != nil {
524 return nil, errors.Wrap(err, "failed to write insert into statement")
525 }
526 if len(generatedColumns) > 0 {
527 if _, err := buf.WriteString(" ("); err != nil {
528 return nil, errors.Wrap(err, "failed to write insert into statement")
529 }
530 for i, column := range normalColumns {
531 if i > 0 {
532 if err := buf.WriteByte(','); err != nil {
533 return nil, errors.Wrap(err, "failed to write comma")
534 }
535 }
536 if _, err := fmt.Fprintf(&buf, "`%s`", column); err != nil {
537 return nil, errors.Wrap(err, "failed to write column")
538 }
539 }
540 if _, err := buf.WriteString(")"); err != nil {
541 return nil, errors.Wrap(err, "failed to write insert into statement")
542 }
543 }
544 for i, item := range statementInfoList {
545 if i != 0 {
546 if _, err := buf.WriteString("\n UNION ALL\n"); err != nil {
547 return nil, errors.Wrap(err, "failed to write union all statement")
548 }
549 }
550 tableNameOrAlias := item.table.Table
551 if len(item.table.Alias) > 0 {
552 tableNameOrAlias = item.table.Alias
553 }

Callers 1

generateSQLFunction · 0.85

Calls 8

TruncateStringFunction · 0.92
equalTableFunction · 0.85
ErrorfMethod · 0.80
extractCTEFunction · 0.70
nodeStmtLocFunction · 0.70
classifyColumnsFunction · 0.70
writeSuffixSelectClauseFunction · 0.70
StringMethod · 0.65

Tested by

no test coverage detected