AddTableAlias returns a Rule that assigns an alias to a specific table in a SELECT, UPDATE, or DELETE statement. For SELECT statements the alias is applied to the matching entry in the FROM clause; for UPDATE and DELETE it sets the statement-level alias field. Parameters: - tableName: The table to
(tableName, alias string)
| 79 | // |
| 80 | // Returns ErrUnsupportedStatement for INSERT or DDL statements. |
| 81 | func AddTableAlias(tableName, alias string) Rule { |
| 82 | return RuleFunc(func(stmt ast.Statement) error { |
| 83 | switch s := stmt.(type) { |
| 84 | case *ast.SelectStatement: |
| 85 | for i := range s.From { |
| 86 | if strings.EqualFold(s.From[i].Name, tableName) { |
| 87 | s.From[i].Alias = alias |
| 88 | } |
| 89 | } |
| 90 | return nil |
| 91 | case *ast.UpdateStatement: |
| 92 | if strings.EqualFold(s.TableName, tableName) { |
| 93 | s.Alias = alias |
| 94 | } |
| 95 | return nil |
| 96 | case *ast.DeleteStatement: |
| 97 | if strings.EqualFold(s.TableName, tableName) { |
| 98 | s.Alias = alias |
| 99 | } |
| 100 | return nil |
| 101 | default: |
| 102 | return &ErrUnsupportedStatement{Transform: "AddTableAlias", Got: stmtTypeName(stmt)} |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | // QualifyColumns returns a Rule that prefixes every unqualified column reference |
| 108 | // in a SELECT statement's column list and WHERE clause with tableName. Only |