MCPcopy
hub / github.com/sqlc-dev/sqlc / outputColumns

Method outputColumns

internal/compiler/output_columns.go:54–408  ·  view source on GitHub ↗

Compute the output columns for a statement. Return an error if column references are ambiguous Return an error if column references don't exist

(qc *QueryCatalog, node ast.Node)

Source from the content-addressed store, hash-verified

52// Return an error if column references are ambiguous
53// Return an error if column references don't exist
54func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
55 tables, err := c.sourceTables(qc, node)
56 if err != nil {
57 return nil, err
58 }
59
60 targets := &ast.List{}
61 switch n := node.(type) {
62 case *ast.DeleteStmt:
63 targets = n.ReturningList
64 case *ast.InsertStmt:
65 targets = n.ReturningList
66 case *ast.SelectStmt:
67 targets = n.TargetList
68 isUnion := len(targets.Items) == 0 && n.Larg != nil
69
70 if n.GroupClause != nil {
71 for _, item := range n.GroupClause.Items {
72 if err := findColumnForNode(item, tables, targets); err != nil {
73 return nil, err
74 }
75 }
76 }
77 validateOrderBy := true
78 if c.conf.StrictOrderBy != nil {
79 validateOrderBy = *c.conf.StrictOrderBy
80 }
81 if !isUnion && validateOrderBy {
82 if n.SortClause != nil {
83 for _, item := range n.SortClause.Items {
84 sb, ok := item.(*ast.SortBy)
85 if !ok {
86 continue
87 }
88 if err := findColumnForNode(sb.Node, tables, targets); err != nil {
89 return nil, fmt.Errorf("%v: if you want to skip this validation, set 'strict_order_by' to false", err)
90 }
91 }
92 }
93 if n.WindowClause != nil {
94 for _, item := range n.WindowClause.Items {
95 sb, ok := item.(*ast.List)
96 if !ok {
97 continue
98 }
99 for _, single := range sb.Items {
100 caseExpr, ok := single.(*ast.CaseExpr)
101 if !ok {
102 continue
103 }
104 if err := findColumnForNode(caseExpr.Xpr, tables, targets); err != nil {
105 return nil, fmt.Errorf("%v: if you want to skip this validation, set 'strict_order_by' to false", err)
106 }
107 }
108 }
109 }
110 }
111

Callers 4

OutputColumnsMethod · 0.95
sourceTablesMethod · 0.95
_analyzeQueryMethod · 0.95
buildQueryCatalogMethod · 0.95

Calls 12

sourceTablesMethod · 0.95
JoinFunction · 0.92
IsComparisonOperatorFunction · 0.92
IsMathematicalOperatorFunction · 0.92
findColumnForNodeFunction · 0.85
toColumnFunction · 0.85
outputColumnRefsFunction · 0.85
hasStarRefFunction · 0.85
dataTypeFunction · 0.85
isTableRequiredFunction · 0.85
ResolveFuncCallMethod · 0.80
FindMethod · 0.45

Tested by

no test coverage detected