MCPcopy
hub / github.com/sqldef/sqldef / normalizeViewDefinition

Function normalizeViewDefinition

schema/normalize.go:1400–1444  ·  view source on GitHub ↗

normalizeViewDefinition normalizes a view definition AST for comparison. This function removes database-specific formatting differences that don't affect the logical meaning. If tableLookup is provided, SELECT * expressions are expanded to explicit column names (PostgreSQL expands * when storing vie

(stmt parser.SelectStatement, mode GeneratorMode, tableLookup TableLookupFunc)

Source from the content-addressed store, hash-verified

1398// If tableLookup is provided, SELECT * expressions are expanded to explicit column names
1399// (PostgreSQL expands * when storing view definitions).
1400func normalizeViewDefinition(stmt parser.SelectStatement, mode GeneratorMode, tableLookup TableLookupFunc) parser.SelectStatement {
1401 if stmt == nil {
1402 return nil
1403 }
1404
1405 switch s := stmt.(type) {
1406 case *parser.Select:
1407 selectExprs := s.SelectExprs
1408 // Expand SELECT * if we have table lookup capability
1409 if tableLookup != nil && hasStarExpr(selectExprs) {
1410 if tableName := extractTableNameFromFrom(s.From); !tableName.IsEmpty() {
1411 if table := tableLookup(tableName); table != nil {
1412 selectExprs = expandStarExpr(selectExprs, table)
1413 }
1414 }
1415 }
1416 return &parser.Select{
1417 Cache: s.Cache,
1418 Comments: nil, // Remove comments for view comparison - they don't affect semantic meaning
1419 Distinct: s.Distinct,
1420 Hints: s.Hints,
1421 SelectExprs: normalizeSelectExprs(selectExprs, mode),
1422 From: normalizeTableExprs(s.From, mode),
1423 Where: normalizeWhere(s.Where, mode),
1424 GroupBy: normalizeGroupBy(s.GroupBy, mode),
1425 Having: normalizeWhere(s.Having, mode),
1426 OrderBy: normalizeOrderBy(s.OrderBy, mode),
1427 Limit: s.Limit,
1428 Lock: s.Lock,
1429 With: normalizeWith(s.With, mode),
1430 }
1431 case *parser.Union:
1432 return &parser.Union{
1433 Type: s.Type,
1434 Left: normalizeViewDefinition(s.Left, mode, tableLookup),
1435 Right: normalizeViewDefinition(s.Right, mode, tableLookup),
1436 OrderBy: normalizeOrderBy(s.OrderBy, mode),
1437 Limit: s.Limit,
1438 Lock: s.Lock,
1439 With: normalizeWith(s.With, mode),
1440 }
1441 default:
1442 return stmt
1443 }
1444}
1445
1446// hasStarExpr checks if there's a StarExpr in the SELECT list.
1447func hasStarExpr(exprs parser.SelectExprs) bool {

Callers 4

normalizeExprFunction · 0.85
normalizeWithFunction · 0.85

Calls 10

hasStarExprFunction · 0.85
extractTableNameFromFromFunction · 0.85
expandStarExprFunction · 0.85
normalizeSelectExprsFunction · 0.85
normalizeTableExprsFunction · 0.85
normalizeWhereFunction · 0.85
normalizeGroupByFunction · 0.85
normalizeOrderByFunction · 0.85
normalizeWithFunction · 0.85
IsEmptyMethod · 0.45

Tested by 1