forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_catalog.go
More file actions
156 lines (149 loc) · 3.57 KB
/
query_catalog.go
File metadata and controls
156 lines (149 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package compiler
import (
"fmt"
"math/rand"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
"github.com/sqlc-dev/sqlc/internal/sql/rewrite"
)
type QueryCatalog struct {
catalog *catalog.Catalog
ctes map[string]*Table
fromClauses map[string]*Table
embeds rewrite.EmbedSet
}
func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embeds rewrite.EmbedSet) (*QueryCatalog, error) {
var with *ast.WithClause
var from *ast.List
switch n := node.(type) {
case *ast.DeleteStmt:
with = n.WithClause
case *ast.InsertStmt:
with = n.WithClause
case *ast.UpdateStmt:
with = n.WithClause
from = n.FromClause
case *ast.SelectStmt:
with = n.WithClause
from = n.FromClause
default:
with = nil
from = nil
}
qc := &QueryCatalog{
catalog: c,
ctes: map[string]*Table{},
fromClauses: map[string]*Table{},
embeds: embeds,
}
if with != nil {
for _, item := range with.Ctes.Items {
if cte, ok := item.(*ast.CommonTableExpr); ok {
cols, err := comp.outputColumns(qc, cte.Ctequery)
if err != nil {
return nil, err
}
var names []string
if cte.Aliascolnames != nil {
for _, item := range cte.Aliascolnames.Items {
if val, ok := item.(*ast.String); ok {
names = append(names, val.Str)
} else {
names = append(names, "")
}
}
}
rel := &ast.TableName{Name: *cte.Ctename}
for i := range cols {
cols[i].Table = rel
if len(names) > i {
cols[i].Name = names[i]
}
}
qc.ctes[*cte.Ctename] = &Table{
Rel: rel,
Columns: cols,
}
}
}
}
if from != nil {
for _, item := range from.Items {
if rs, ok := item.(*ast.RangeSubselect); ok {
cols, err := comp.outputColumns(qc, rs.Subquery)
if err != nil {
return nil, err
}
var names []string
if rs.Alias != nil && rs.Alias.Colnames != nil {
for _, item := range rs.Alias.Colnames.Items {
if val, ok := item.(*ast.String); ok {
names = append(names, val.Str)
} else {
names = append(names, "")
}
}
}
rel := &ast.TableName{}
if rs.Alias != nil && rs.Alias.Aliasname != nil {
rel.Name = *rs.Alias.Aliasname
} else {
rel.Name = fmt.Sprintf("unaliased_table_%d", rand.Int63())
}
for i := range cols {
cols[i].Table = rel
if len(names) > i {
cols[i].Name = names[i]
}
}
qc.fromClauses[rel.Name] = &Table{
Rel: rel,
Columns: cols,
}
}
}
}
return qc, nil
}
func ConvertColumn(rel *ast.TableName, c *catalog.Column) *Column {
return &Column{
Table: rel,
Name: c.Name,
DataType: dataType(&c.Type),
NotNull: c.IsNotNull,
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
ArrayDims: c.ArrayDims,
Type: &c.Type,
Length: c.Length,
}
}
func (qc QueryCatalog) GetTable(rel *ast.TableName) (*Table, error) {
cte, exists := qc.ctes[rel.Name]
if exists {
return &Table{Rel: rel, Columns: cte.Columns}, nil
}
src, err := qc.catalog.GetTable(rel)
if err != nil {
return nil, err
}
var cols []*Column
for _, c := range src.Columns {
cols = append(cols, ConvertColumn(rel, c))
}
return &Table{Rel: rel, Columns: cols}, nil
}
func (qc QueryCatalog) GetFunc(rel *ast.FuncName) (*Function, error) {
funcs, err := qc.catalog.ListFuncsByName(rel)
if err != nil {
return nil, err
}
if len(funcs) == 0 {
return nil, fmt.Errorf("function not found: %s", rel.Name)
}
return &Function{
Rel: rel,
Outs: funcs[0].OutArgs(),
ReturnType: funcs[0].ReturnType,
}, nil
}