(ctx base.TransformContext, statementInfoList []statementInfo, targetDatabase string, tablePrefix string)
| 66 | } |
| 67 | |
| 68 | func generateSQL(ctx base.TransformContext, statementInfoList []statementInfo, targetDatabase string, tablePrefix string) ([]base.BackupStatement, error) { |
| 69 | groupByTable := make(map[string][]statementInfo) |
| 70 | for _, item := range statementInfoList { |
| 71 | key := fmt.Sprintf("%s.%s", item.table.Schema, item.table.Table) |
| 72 | groupByTable[key] = append(groupByTable[key], item) |
| 73 | } |
| 74 | |
| 75 | // Check if the statement type is the same for all statements in the group. |
| 76 | for key, list := range groupByTable { |
| 77 | statementType := StatementTypeUnknown |
| 78 | for _, item := range list { |
| 79 | if statementType == StatementTypeUnknown { |
| 80 | statementType = item.table.StatementType |
| 81 | } |
| 82 | if statementType != item.table.StatementType { |
| 83 | return nil, errors.Errorf("prior backup cannot handle statements with different types on the same table: %s", key) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | var result []base.BackupStatement |
| 89 | for key, list := range groupByTable { |
| 90 | backupStatement, err := generateSQLForTable(ctx, list, targetDatabase, tablePrefix) |
| 91 | if err != nil { |
| 92 | return nil, errors.Wrapf(err, "failed to generate SQL for table: %s", key) |
| 93 | } |
| 94 | result = append(result, *backupStatement) |
| 95 | } |
| 96 | |
| 97 | slices.SortFunc(result, func(i, j base.BackupStatement) int { |
| 98 | if i.StartPosition.Line != j.StartPosition.Line { |
| 99 | if i.StartPosition.Line < j.StartPosition.Line { |
| 100 | return -1 |
| 101 | } |
| 102 | return 1 |
| 103 | } |
| 104 | if i.StartPosition.Column != j.StartPosition.Column { |
| 105 | if i.StartPosition.Column < j.StartPosition.Column { |
| 106 | return -1 |
| 107 | } |
| 108 | return 1 |
| 109 | } |
| 110 | if i.SourceTableName < j.SourceTableName { |
| 111 | return -1 |
| 112 | } |
| 113 | if i.SourceTableName > j.SourceTableName { |
| 114 | return 1 |
| 115 | } |
| 116 | return 0 |
| 117 | }) |
| 118 | |
| 119 | return result, nil |
| 120 | } |
| 121 | |
| 122 | func generateSQLForTable(ctx base.TransformContext, statementInfoList []statementInfo, targetDatabase string, tablePrefix string) (*base.BackupStatement, error) { |
| 123 | table := statementInfoList[0].table |
no test coverage detected