-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathanalyze.go
More file actions
220 lines (201 loc) · 5.52 KB
/
analyze.go
File metadata and controls
220 lines (201 loc) · 5.52 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package compiler
import (
"sort"
analyzer "github.com/sqlc-dev/sqlc/internal/analysis"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/source"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/named"
"github.com/sqlc-dev/sqlc/internal/sql/rewrite"
"github.com/sqlc-dev/sqlc/internal/sql/validate"
)
type analysis struct {
Table *ast.TableName
Columns []*Column
Parameters []Parameter
Named *named.ParamSet
Query string
}
func convertTableName(id *analyzer.Identifier) *ast.TableName {
if id == nil {
return nil
}
return &ast.TableName{
Catalog: id.Catalog,
Schema: id.Schema,
Name: id.Name,
}
}
func convertTypeName(id *analyzer.Identifier) *ast.TypeName {
if id == nil {
return nil
}
return &ast.TypeName{
Catalog: id.Catalog,
Schema: id.Schema,
Name: id.Name,
}
}
func convertColumn(c *analyzer.Column) *Column {
length := int(c.Length)
return &Column{
Name: c.Name,
OriginalName: c.OriginalName,
DataType: c.DataType,
NotNull: c.NotNull,
Unsigned: c.Unsigned,
IsArray: c.IsArray,
ArrayDims: int(c.ArrayDims),
Comment: c.Comment,
Length: &length,
IsNamedParam: c.IsNamedParam,
IsFuncCall: c.IsFuncCall,
Scope: c.Scope,
Table: convertTableName(c.Table),
TableAlias: c.TableAlias,
Type: convertTypeName(c.Type),
EmbedTable: convertTableName(c.EmbedTable),
IsSqlcSlice: c.IsSqlcSlice,
}
}
func combineAnalysis(prev *analysis, a *analyzer.Analysis) *analysis {
var cols []*Column
for _, c := range a.Columns {
cols = append(cols, convertColumn(c))
}
var params []Parameter
for _, p := range a.Params {
params = append(params, Parameter{
Number: int(p.Number),
Column: convertColumn(p.Column),
})
}
// Only update columns if the analyzer returned column info
// An empty slice from the analyzer means it couldn't determine columns,
// so we should keep the catalog-inferred columns
if len(cols) > 0 {
if len(prev.Columns) == len(cols) {
for i := range prev.Columns {
// Only override column types if the analyzer provides a specific type
// (not "any"), since the catalog-based inference may have better info
if cols[i].DataType != "any" {
prev.Columns[i].DataType = cols[i].DataType
prev.Columns[i].IsArray = cols[i].IsArray
prev.Columns[i].ArrayDims = cols[i].ArrayDims
}
}
} else {
embedding := false
for i := range prev.Columns {
if prev.Columns[i].EmbedTable != nil {
embedding = true
}
}
if !embedding {
prev.Columns = cols
}
}
}
if len(prev.Parameters) == len(params) {
for i := range prev.Parameters {
// Only override parameter types if the analyzer provides a specific type
// (not "any"), since the catalog-based inference may have better info
if params[i].Column.DataType != "any" {
prev.Parameters[i].Column.DataType = params[i].Column.DataType
prev.Parameters[i].Column.IsArray = params[i].Column.IsArray
prev.Parameters[i].Column.ArrayDims = params[i].Column.ArrayDims
}
}
} else {
prev.Parameters = params
}
return prev
}
func (c *Compiler) analyzeQuery(raw *ast.RawStmt, query string) (*analysis, error) {
return c._analyzeQuery(raw, query, true)
}
func (c *Compiler) inferQuery(raw *ast.RawStmt, query string) (*analysis, error) {
return c._analyzeQuery(raw, query, false)
}
func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool) (*analysis, error) {
errors := make([]error, 0)
check := func(err error) error {
if failfast {
return err
}
if err != nil {
errors = append(errors, err)
}
return nil
}
numbers, dollar, err := validate.ParamRef(raw)
if err := check(err); err != nil {
return nil, err
}
raw, namedParams, edits := rewrite.NamedParameters(c.conf.Engine, raw, numbers, dollar)
var table *ast.TableName
switch n := raw.Stmt.(type) {
case *ast.InsertStmt:
if err := check(validate.InsertStmt(n)); err != nil {
return nil, err
}
var err error
table, err = ParseTableName(n.Relation)
if err := check(err); err != nil {
return nil, err
}
}
if err := check(validate.FuncCall(c.catalog, c.combo, raw)); err != nil {
return nil, err
}
if err := check(validate.In(c.catalog, raw)); err != nil {
return nil, err
}
rvs := rangeVars(raw.Stmt)
refs, errs := findParameters(raw.Stmt)
if len(errs) > 0 {
if failfast {
return nil, errs[0]
}
errors = append(errors, errs...)
}
refs = uniqueParamRefs(refs, dollar)
if c.conf.Engine == config.EngineMySQL || !dollar {
sort.Slice(refs, func(i, j int) bool { return refs[i].ref.Location < refs[j].ref.Location })
} else {
sort.Slice(refs, func(i, j int) bool { return refs[i].ref.Number < refs[j].ref.Number })
}
raw, embeds := rewrite.Embeds(raw)
qc, err := c.buildQueryCatalog(c.catalog, raw.Stmt, embeds)
if err := check(err); err != nil {
return nil, err
}
params, err := c.resolveCatalogRefs(qc, rvs, refs, namedParams, embeds)
if err := check(err); err != nil {
return nil, err
}
cols, err := c.outputColumns(qc, raw.Stmt)
if err := check(err); err != nil {
return nil, err
}
expandEdits, err := c.expand(qc, raw)
if check(err); err != nil {
return nil, err
}
edits = append(edits, expandEdits...)
expanded, err := source.Mutate(query, edits)
if err != nil {
return nil, err
}
var rerr error
if len(errors) > 0 {
rerr = errors[0]
}
return &analysis{
Table: table,
Columns: cols,
Parameters: params,
Query: expanded,
Named: namedParams,
}, rerr
}