(ctx context.Context, tCtx base.TransformContext, statementInfoList []StatementInfo, databaseName string, tablePrefix string)
| 473 | } |
| 474 | |
| 475 | func generateSQL(ctx context.Context, tCtx base.TransformContext, statementInfoList []StatementInfo, databaseName string, tablePrefix string) ([]base.BackupStatement, error) { |
| 476 | groupByTable := make(map[string][]StatementInfo) |
| 477 | for _, item := range statementInfoList { |
| 478 | key := fmt.Sprintf("%s.%s", item.Table.Database, item.Table.Table) |
| 479 | groupByTable[key] = append(groupByTable[key], item) |
| 480 | } |
| 481 | |
| 482 | for key, list := range groupByTable { |
| 483 | stmtType := StatementTypeUnknown |
| 484 | for _, item := range list { |
| 485 | if stmtType == StatementTypeUnknown { |
| 486 | stmtType = item.Table.StatementType |
| 487 | } |
| 488 | if stmtType != item.Table.StatementType { |
| 489 | return nil, errors.Errorf("prior backup cannot handle mixed DML statements on the same table %s", key) |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | var result []base.BackupStatement |
| 495 | for key, list := range groupByTable { |
| 496 | backupStatement, err := generateSQLForTable(ctx, tCtx, list, databaseName, tablePrefix) |
| 497 | if err != nil { |
| 498 | return nil, errors.Wrapf(err, "failed to generate SQL for table %s", key) |
| 499 | } |
| 500 | result = append(result, *backupStatement) |
| 501 | } |
| 502 | |
| 503 | slices.SortFunc(result, func(i, j base.BackupStatement) int { |
| 504 | if i.StartPosition.Line != j.StartPosition.Line { |
| 505 | if i.StartPosition.Line < j.StartPosition.Line { |
| 506 | return -1 |
| 507 | } |
| 508 | return 1 |
| 509 | } |
| 510 | if i.StartPosition.Column != j.StartPosition.Column { |
| 511 | if i.StartPosition.Column < j.StartPosition.Column { |
| 512 | return -1 |
| 513 | } |
| 514 | return 1 |
| 515 | } |
| 516 | if i.SourceTableName < j.SourceTableName { |
| 517 | return -1 |
| 518 | } |
| 519 | if i.SourceTableName > j.SourceTableName { |
| 520 | return 1 |
| 521 | } |
| 522 | return 0 |
| 523 | }) |
| 524 | |
| 525 | return result, nil |
| 526 | } |
| 527 | |
| 528 | func generateSQLForTable(ctx context.Context, tCtx base.TransformContext, statementInfoList []StatementInfo, databaseName string, tablePrefix string) (*base.BackupStatement, error) { |
| 529 | table := statementInfoList[0].Table |
no test coverage detected