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

Method expandStmt

internal/compiler/expand.go:81–203  ·  view source on GitHub ↗
(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)

Source from the content-addressed store, hash-verified

79}
80
81func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node) ([]source.Edit, error) {
82 tables, err := c.sourceTables(qc, node)
83 if err != nil {
84 return nil, err
85 }
86
87 var targets *ast.List
88 switch n := node.(type) {
89 case *ast.DeleteStmt:
90 targets = n.ReturningList
91 case *ast.InsertStmt:
92 targets = n.ReturningList
93 case *ast.SelectStmt:
94 targets = n.TargetList
95 case *ast.UpdateStmt:
96 targets = n.ReturningList
97 default:
98 return nil, fmt.Errorf("outputColumns: unsupported node type: %T", n)
99 }
100
101 var edits []source.Edit
102 for _, target := range targets.Items {
103 res, ok := target.(*ast.ResTarget)
104 if !ok {
105 continue
106 }
107 ref, ok := res.Val.(*ast.ColumnRef)
108 if !ok {
109 continue
110 }
111 if !hasStarRef(ref) {
112 continue
113 }
114 var parts, cols []string
115 for _, f := range ref.Fields.Items {
116 switch field := f.(type) {
117 case *ast.String:
118 parts = append(parts, field.Str)
119 case *ast.A_Star:
120 parts = append(parts, "*")
121 default:
122 return nil, fmt.Errorf("unknown field in ColumnRef: %T", f)
123 }
124 }
125 scope := astutils.Join(ref.Fields, ".")
126 counts := map[string]int{}
127 if scope == "" {
128 for _, t := range tables {
129 for _, c := range t.Columns {
130 counts[c.Name] += 1
131 }
132 }
133 }
134 for _, t := range tables {
135 if scope != "" && scope != t.Rel.Name {
136 continue
137 }
138 tableName := c.quoteIdent(t.Rel.Name)

Callers 1

expandMethod · 0.95

Calls 9

sourceTablesMethod · 0.95
quoteIdentMethod · 0.95
quoteMethod · 0.95
JoinFunction · 0.92
hasStarRefFunction · 0.85
OrigMethod · 0.80
ColumnExprMethod · 0.65
FindMethod · 0.45
JoinMethod · 0.45

Tested by

no test coverage detected