(statementInfoList []statementInfo, targetSchema string, tablePrefix string)
| 73 | } |
| 74 | |
| 75 | func generateSQL(statementInfoList []statementInfo, targetSchema string, tablePrefix string) ([]base.BackupStatement, error) { |
| 76 | groupByTable := make(map[string][]statementInfo) |
| 77 | for _, item := range statementInfoList { |
| 78 | key := fmt.Sprintf("%s.%s", item.table.Schema, item.table.Table) |
| 79 | groupByTable[key] = append(groupByTable[key], item) |
| 80 | } |
| 81 | |
| 82 | // Check if the statement type is the same for all statements on the same table. |
| 83 | for key, list := range groupByTable { |
| 84 | statementType := StatementTypeUnknown |
| 85 | for _, item := range list { |
| 86 | if statementType == StatementTypeUnknown { |
| 87 | statementType = item.table.StatementType |
| 88 | } |
| 89 | if statementType != item.table.StatementType { |
| 90 | return nil, errors.Errorf("The statement type is not the same for all statements on the same table %q", key) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | var result []base.BackupStatement |
| 96 | for key, list := range groupByTable { |
| 97 | backupStatement, err := generateSQLForTable(list, targetSchema, tablePrefix) |
| 98 | if err != nil { |
| 99 | return nil, errors.Wrapf(err, "failed to generate SQL for table %q", key) |
| 100 | } |
| 101 | result = append(result, *backupStatement) |
| 102 | } |
| 103 | |
| 104 | slices.SortFunc(result, func(a, b base.BackupStatement) int { |
| 105 | if a.StartPosition.Line != b.StartPosition.Line { |
| 106 | if a.StartPosition.Line < b.StartPosition.Line { |
| 107 | return -1 |
| 108 | } |
| 109 | return 1 |
| 110 | } |
| 111 | if a.StartPosition.Column != b.StartPosition.Column { |
| 112 | if a.StartPosition.Column < b.StartPosition.Column { |
| 113 | return -1 |
| 114 | } |
| 115 | return 1 |
| 116 | } |
| 117 | if a.SourceTableName < b.SourceTableName { |
| 118 | return -1 |
| 119 | } |
| 120 | if a.SourceTableName > b.SourceTableName { |
| 121 | return 1 |
| 122 | } |
| 123 | return 0 |
| 124 | }) |
| 125 | |
| 126 | return result, nil |
| 127 | } |
| 128 | |
| 129 | func generateSQLForTable(statementInfoList []statementInfo, targetSchema string, tablePrefix string) (*base.BackupStatement, error) { |
| 130 | table := statementInfoList[0].table |
no test coverage detected