(databaseName string, n *ast.DeleteStmt, fullSQL string, isCaseSensitive bool)
| 186 | } |
| 187 | |
| 188 | func extractTablesFromDelete(databaseName string, n *ast.DeleteStmt, fullSQL string, isCaseSensitive bool) ([]statementInfo, error) { |
| 189 | cteNames := collectCTENames(fullSQL, n.Loc) |
| 190 | stmtText := extractStatementText(fullSQL, n.Loc) |
| 191 | |
| 192 | singleTables := collectSingleTables(databaseName, cteNames, n.Tables, isCaseSensitive) |
| 193 | |
| 194 | if len(n.Using) == 0 { |
| 195 | // Single-table DELETE: DELETE FROM t WHERE ... |
| 196 | if len(singleTables) == 0 { |
| 197 | return nil, nil |
| 198 | } |
| 199 | first := singleTables[firstKey(singleTables)] |
| 200 | return []statementInfo{{ |
| 201 | statement: stmtText, |
| 202 | table: first, |
| 203 | node: n, |
| 204 | fullSQL: fullSQL, |
| 205 | }}, nil |
| 206 | } |
| 207 | |
| 208 | // Multi-table DELETE: DELETE t1, t2 FROM t1 JOIN t2 ... WHERE ... |
| 209 | // Tables = targets to delete from; Using = the referenced table list. |
| 210 | refTables := collectSingleTables(databaseName, cteNames, n.Using, isCaseSensitive) |
| 211 | |
| 212 | var result []statementInfo |
| 213 | for _, target := range singleTables { |
| 214 | name := target.Table |
| 215 | if target.Alias != "" { |
| 216 | name = target.Alias |
| 217 | } |
| 218 | ref, ok := refTables[identifierKey(name, isCaseSensitive)] |
| 219 | if !ok { |
| 220 | return nil, errors.Errorf("cannot extract reference table: no matched table %q in referenced table list", name) |
| 221 | } |
| 222 | // Skip only if the target resolves to a CTE reference (you can't delete a |
| 223 | // CTE). A real table aliased with a CTE's name is still a delete target. |
| 224 | if isCTERef(cteNames, ref) { |
| 225 | continue |
| 226 | } |
| 227 | result = append(result, statementInfo{ |
| 228 | statement: stmtText, |
| 229 | table: ref, |
| 230 | node: n, |
| 231 | fullSQL: fullSQL, |
| 232 | }) |
| 233 | } |
| 234 | return result, nil |
| 235 | } |
| 236 | |
| 237 | func extractTablesFromUpdate(databaseName string, n *ast.UpdateStmt, fullSQL string, dbMetadata *model.DatabaseMetadata, isCaseSensitive bool) ([]statementInfo, error) { |
| 238 | cteNames := collectCTENames(fullSQL, n.Loc) |
no test coverage detected