RemoveColumn returns a Rule that removes the first column in the SELECT list that matches name. Matching is case-insensitive and checks both identifier names and expression aliases. Returns an error if no column matching name is found, or ErrUnsupportedStatement for non-SELECT statements.
(name string)
| 55 | // Returns an error if no column matching name is found, or ErrUnsupportedStatement |
| 56 | // for non-SELECT statements. |
| 57 | func RemoveColumn(name string) Rule { |
| 58 | return RuleFunc(func(stmt ast.Statement) error { |
| 59 | sel, err := getSelect(stmt, "RemoveColumn") |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | filtered := make([]ast.Expression, 0, len(sel.Columns)) |
| 64 | found := false |
| 65 | for _, col := range sel.Columns { |
| 66 | if columnMatches(col, name) { |
| 67 | found = true |
| 68 | } else { |
| 69 | filtered = append(filtered, col) |
| 70 | } |
| 71 | } |
| 72 | if !found { |
| 73 | return fmt.Errorf("column %q not found", name) |
| 74 | } |
| 75 | sel.Columns = filtered |
| 76 | return nil |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | // ReplaceColumn returns a Rule that replaces every column in the SELECT list that |
| 81 | // matches oldName with a bare *ast.Identifier for newName. Matching is |