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