MCPcopy
hub / github.com/sqldef/sqldef / normalizeSelectExpr

Function normalizeSelectExpr

schema/normalize.go:1178–1205  ·  view source on GitHub ↗

normalizeSelectExpr normalizes a single SELECT expression for views

(expr parser.SelectExpr, mode GeneratorMode)

Source from the content-addressed store, hash-verified

1176
1177// normalizeSelectExpr normalizes a single SELECT expression for views
1178func normalizeSelectExpr(expr parser.SelectExpr, mode GeneratorMode) parser.SelectExpr {
1179 switch e := expr.(type) {
1180 case *parser.AliasedExpr:
1181 as := e.As
1182 // For PostgreSQL, strip automatic aliases like ?column?
1183 if mode == GeneratorModePostgres && as.Name == "?column?" {
1184 as = parser.NewIdent("", false)
1185 }
1186 // For MySQL, strip redundant aliases where the alias matches the column name
1187 // MySQL adds "column_name as column_name" which is redundant
1188 if mode == GeneratorModeMysql && !as.IsEmpty() {
1189 if colName, ok := e.Expr.(*parser.ColName); ok {
1190 if strings.EqualFold(colName.Name.Name, as.Name) {
1191 // The alias is the same as the column name, strip it
1192 as = parser.NewIdent("", false)
1193 }
1194 }
1195 }
1196 return &parser.AliasedExpr{
1197 Expr: normalizeExpr(e.Expr, mode),
1198 As: as,
1199 }
1200 case *parser.StarExpr:
1201 return e
1202 default:
1203 return expr
1204 }
1205}
1206
1207// normalizeTableExprs normalizes FROM clause table expressions
1208func normalizeTableExprs(exprs parser.TableExprs, mode GeneratorMode) parser.TableExprs {

Callers 3

normalizeExprFunction · 0.85
normalizeSelectExprsFunction · 0.85

Calls 3

NewIdentFunction · 0.92
normalizeExprFunction · 0.85
IsEmptyMethod · 0.45

Tested by

no test coverage detected