QualifyColumns returns a Rule that prefixes every unqualified column reference in a SELECT statement's column list and WHERE clause with tableName. Only identifiers that have no existing table qualifier and whose name is not the wildcard "*" are modified. This is useful when merging standalone colu
(tableName string)
| 117 | // |
| 118 | // Returns ErrUnsupportedStatement for non-SELECT statements. |
| 119 | func QualifyColumns(tableName string) Rule { |
| 120 | return RuleFunc(func(stmt ast.Statement) error { |
| 121 | sel, err := getSelect(stmt, "QualifyColumns") |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | for i, col := range sel.Columns { |
| 126 | sel.Columns[i] = qualifyExpr(col, tableName) |
| 127 | } |
| 128 | if sel.Where != nil { |
| 129 | sel.Where = qualifyExpr(sel.Where, tableName) |
| 130 | } |
| 131 | return nil |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | func replaceTableInFrom(from []ast.TableReference, old, new string) { |
| 136 | for i := range from { |