-
Notifications
You must be signed in to change notification settings - Fork 864
/
Copy pathquery_catalog.go
139 lines (130 loc) · 3.16 KB
/
query_catalog.go
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
package compiler
import (
"fmt"
"strings"
"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
embeds rewrite.EmbedSet
}
func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embeds rewrite.EmbedSet) (*QueryCatalog, error) {
var with *ast.WithClause
switch n := node.(type) {
case *ast.DeleteStmt:
with = n.WithClause
case *ast.InsertStmt:
with = n.WithClause
case *ast.UpdateStmt:
with = n.WithClause
case *ast.SelectStmt:
with = n.WithClause
default:
with = nil
}
qc := &QueryCatalog{catalog: c, ctes: 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,
}
}
}
}
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 RevertConvertColumn(c *Column) *catalog.Column {
out := &catalog.Column{
Name: c.Name,
IsNotNull: c.NotNull,
IsUnsigned: c.Unsigned,
IsArray: c.IsArray,
ArrayDims: c.ArrayDims,
Length: c.Length,
}
if c.Type != nil {
out.Type = *c.Type
}
dataTypes := strings.Split(c.DataType, ".")
if len(dataTypes) == 1 {
out.Type.Name = dataTypes[0]
} else if len(dataTypes) == 2 {
out.Type.Schema = dataTypes[0]
out.Type.Name = dataTypes[1]
}
return out
}
func RevertConvertColumns(columns []*Column) (out []*catalog.Column) {
for i := range columns {
out = append(out, RevertConvertColumn(columns[i]))
}
return
}
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
}