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)
| 1398 | // If tableLookup is provided, SELECT * expressions are expanded to explicit column names |
| 1399 | // (PostgreSQL expands * when storing view definitions). |
| 1400 | func 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. |
| 1447 | func hasStarExpr(exprs parser.SelectExprs) bool { |