(databaseName string, n *ast.DeleteStmt, fullSQL string, offset int, _ *model.DatabaseMetadata)
| 124 | } |
| 125 | |
| 126 | func extractTablesFromDelete(databaseName string, n *ast.DeleteStmt, fullSQL string, offset int, _ *model.DatabaseMetadata) ([]StatementInfo, error) { |
| 127 | cteNames := collectCTENames(fullSQL, n.Loc) |
| 128 | stmtText := extractStatementText(fullSQL, n.Loc) |
| 129 | |
| 130 | // Collect all single table refs from the Tables list. |
| 131 | singleTables := collectSingleTables(databaseName, n.Tables) |
| 132 | |
| 133 | if len(n.Using) == 0 { |
| 134 | // Single-table DELETE: DELETE FROM t WHERE ... |
| 135 | if len(singleTables) == 0 { |
| 136 | return nil, nil |
| 137 | } |
| 138 | for _, ref := range singleTables { |
| 139 | ref.StatementType = StatementTypeDelete |
| 140 | } |
| 141 | first := singleTables[firstKey(singleTables)] |
| 142 | return []StatementInfo{{ |
| 143 | Offset: offset, |
| 144 | Statement: stmtText, |
| 145 | Node: n, |
| 146 | Table: first, |
| 147 | FullSQL: fullSQL, |
| 148 | }}, nil |
| 149 | } |
| 150 | |
| 151 | // Multi-table DELETE: DELETE t1, t2 FROM t1 JOIN t2 ... WHERE ... |
| 152 | // Tables = targets to delete from, Using = referenced table list. |
| 153 | refTables := collectSingleTables(databaseName, n.Using) |
| 154 | |
| 155 | var result []StatementInfo |
| 156 | for _, target := range singleTables { |
| 157 | name := target.Table |
| 158 | if target.Alias != "" { |
| 159 | name = target.Alias |
| 160 | } |
| 161 | if cteNames[name] { |
| 162 | continue |
| 163 | } |
| 164 | ref, ok := refTables[name] |
| 165 | if !ok { |
| 166 | return nil, errors.Errorf("cannot extract reference table: no matched table %q in referenced table list", name) |
| 167 | } |
| 168 | ref.StatementType = StatementTypeDelete |
| 169 | result = append(result, StatementInfo{ |
| 170 | Offset: offset, |
| 171 | Statement: stmtText, |
| 172 | Node: n, |
| 173 | Table: ref, |
| 174 | FullSQL: fullSQL, |
| 175 | }) |
| 176 | } |
| 177 | return result, nil |
| 178 | } |
| 179 | |
| 180 | func extractTablesFromUpdate(databaseName string, n *ast.UpdateStmt, fullSQL string, offset int, dbMetadata *model.DatabaseMetadata) ([]StatementInfo, error) { |
| 181 | cteNames := collectCTENames(fullSQL, n.Loc) |
no test coverage detected