(databaseName string, n *ast.UpdateStmt, fullSQL string, offset int, dbMetadata *model.DatabaseMetadata)
| 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) |
| 182 | stmtText := extractStatementText(fullSQL, n.Loc) |
| 183 | singleTables := collectSingleTables(databaseName, n.Tables) |
| 184 | |
| 185 | // Determine which tables are updated via SET clause column prefixes. |
| 186 | updatedTables := make(map[string]bool) |
| 187 | var unqualifiedColumns []string |
| 188 | for _, assign := range n.SetList { |
| 189 | if assign.Column != nil { |
| 190 | table := assign.Column.Table |
| 191 | if !cteNames[table] { |
| 192 | updatedTables[table] = true |
| 193 | if table == "" { |
| 194 | unqualifiedColumns = append(unqualifiedColumns, assign.Column.Column) |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Resolve unqualified columns using metadata. |
| 201 | if updatedTables[""] { |
| 202 | delete(updatedTables, "") |
| 203 | resolved := resolveUnqualifiedColumns(unqualifiedColumns, singleTables, dbMetadata) |
| 204 | for t := range resolved { |
| 205 | updatedTables[t] = true |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Single-table UPDATE (only one table, no explicit qualification needed). |
| 210 | if len(updatedTables) == 0 && len(singleTables) == 1 { |
| 211 | for _, ref := range singleTables { |
| 212 | ref.StatementType = StatementTypeUpdate |
| 213 | return []StatementInfo{{ |
| 214 | Offset: offset, |
| 215 | Statement: stmtText, |
| 216 | Node: n, |
| 217 | Table: ref, |
| 218 | FullSQL: fullSQL, |
| 219 | }}, nil |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | var result []StatementInfo |
| 224 | for table := range updatedTables { |
| 225 | ref, ok := singleTables[table] |
| 226 | if !ok { |
| 227 | return nil, errors.Errorf("cannot extract reference table: no matched updated table %q in referenced table list", table) |
| 228 | } |
| 229 | ref.StatementType = StatementTypeUpdate |
| 230 | result = append(result, StatementInfo{ |
| 231 | Offset: offset, |
| 232 | Statement: stmtText, |
| 233 | Node: n, |
| 234 | Table: ref, |
| 235 | FullSQL: fullSQL, |
| 236 | }) |
| 237 | } |
no test coverage detected