Repository navigation
Expand file tree
/
Copy pathparameters.go
More file actions
190 lines (177 loc) · 4.51 KB
/
Copy pathparameters.go
File metadata and controls
190 lines (177 loc) · 4.51 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
package rewrite
import (
"fmt"
"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/source"
"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/astutils"
"github.com/kyleconroy/sqlc/internal/sql/named"
)
// Given an AST node, return the string representation of names
func flatten(root ast.Node) (string, bool) {
sw := &stringWalker{}
astutils.Walk(sw, root)
return sw.String, sw.IsConst
}
type stringWalker struct {
String string
IsConst bool
}
func (s *stringWalker) Visit(node ast.Node) astutils.Visitor {
if _, ok := node.(*ast.A_Const); ok {
s.IsConst = true
}
if n, ok := node.(*ast.String); ok {
s.String += n.Str
}
return s
}
func isNamedParamSignCast(node ast.Node) bool {
expr, ok := node.(*ast.A_Expr)
if !ok {
return false
}
_, cast := expr.Rexpr.(*ast.TypeCast)
return astutils.Join(expr.Name, ".") == "@" && cast
}
func NamedParameters(engine config.Engine, raw *ast.RawStmt, numbs map[int]bool, dollar bool) (*ast.RawStmt, map[int]string, []source.Edit) {
foundFunc := astutils.Search(raw, named.IsParamFunc)
foundSign := astutils.Search(raw, named.IsParamSign)
if len(foundFunc.Items)+len(foundSign.Items) == 0 {
return raw, map[int]string{}, nil
}
hasNamedParameterSupport := engine != config.EngineMySQL
args := map[string][]int{}
argn := 0
var edits []source.Edit
node := astutils.Apply(raw, func(cr *astutils.Cursor) bool {
node := cr.Node()
switch {
case named.IsParamFunc(node):
fun := node.(*ast.FuncCall)
param, isConst := flatten(fun.Args)
if nums, ok := args[param]; ok && hasNamedParameterSupport {
cr.Replace(&ast.ParamRef{
Number: nums[0],
Location: fun.Location,
})
} else {
argn++
for numbs[argn] {
argn++
}
if _, found := args[param]; !found {
args[param] = []int{argn}
} else {
args[param] = append(args[param], argn)
}
cr.Replace(&ast.ParamRef{
Number: argn,
Location: fun.Location,
})
}
// TODO: This code assumes that sqlc.arg(name) is on a single line
var old, replace string
if isConst {
old = fmt.Sprintf("sqlc.arg('%s')", param)
} else {
old = fmt.Sprintf("sqlc.arg(%s)", param)
}
if engine == config.EngineMySQL || !dollar {
replace = "?"
} else {
replace = fmt.Sprintf("$%d", args[param][0])
}
edits = append(edits, source.Edit{
Location: fun.Location - raw.StmtLocation,
Old: old,
New: replace,
})
return false
case isNamedParamSignCast(node):
expr := node.(*ast.A_Expr)
cast := expr.Rexpr.(*ast.TypeCast)
param, _ := flatten(cast.Arg)
if nums, ok := args[param]; ok {
cast.Arg = &ast.ParamRef{
Number: nums[0],
Location: expr.Location,
}
cr.Replace(cast)
} else {
argn++
for numbs[argn] {
argn++
}
if _, found := args[param]; !found {
args[param] = []int{argn}
} else {
args[param] = append(args[param], argn)
}
cast.Arg = &ast.ParamRef{
Number: argn,
Location: expr.Location,
}
cr.Replace(cast)
}
// TODO: This code assumes that @foo::bool is on a single line
var replace string
if engine == config.EngineMySQL || !dollar {
replace = "?"
} else {
replace = fmt.Sprintf("$%d", args[param][0])
}
edits = append(edits, source.Edit{
Location: expr.Location - raw.StmtLocation,
Old: fmt.Sprintf("@%s", param),
New: replace,
})
return false
case named.IsParamSign(node):
expr := node.(*ast.A_Expr)
param, _ := flatten(expr.Rexpr)
if nums, ok := args[param]; ok {
cr.Replace(&ast.ParamRef{
Number: nums[0],
Location: expr.Location,
})
} else {
argn++
for numbs[argn] {
argn++
}
if _, found := args[param]; !found {
args[param] = []int{argn}
} else {
args[param] = append(args[param], argn)
}
cr.Replace(&ast.ParamRef{
Number: argn,
Location: expr.Location,
})
}
// TODO: This code assumes that @foo is on a single line
var replace string
if engine == config.EngineMySQL || !dollar {
replace = "?"
} else {
replace = fmt.Sprintf("$%d", args[param][0])
}
edits = append(edits, source.Edit{
Location: expr.Location - raw.StmtLocation,
Old: fmt.Sprintf("@%s", param),
New: replace,
})
return false
default:
return true
}
}, nil)
named := map[int]string{}
for k, vs := range args {
for _, v := range vs {
named[v] = k
}
}
return node.(*ast.RawStmt), named, edits
}