Repository navigation
Expand file tree
/
Copy patha_expr.go
More file actions
178 lines (167 loc) · 5.14 KB
/
Copy patha_expr.go
File metadata and controls
178 lines (167 loc) · 5.14 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
package ast
import (
"strings"
"github.com/sqlc-dev/sqlc/internal/sql/format"
)
type A_Expr struct {
Tag NodeTag[A_Expr] `json:"tag"`
Kind A_Expr_Kind `json:"kind"`
// Name is the operator, as a list of String nodes: PostgreSQL's
// operator space is open (user-defined and schema-qualified operators),
// so the shared tree keeps pg_query's shape rather than an enum. Each
// engine's converter writes the operator as its parser saw it — for an
// engine that accepts more than one spelling of the same operator
// (SQLite's != for <>, == for =), that is the author's spelling, which
// is how the printer preserves it.
Name *List `json:"name,omitempty"`
Lexpr Node `json:"lexpr,omitempty"`
Rexpr Node `json:"rexpr,omitempty"`
Location int `json:"location"`
}
func (n *A_Expr) Pos() int {
return n.Location
}
// isNamedParam reports whether this A_Expr represents a named parameter,
// returning its sigil and name. Engines encode a named parameter as a
// prefix pseudo-operator carrying the sigil the author wrote (@name for
// most engines; SQLite also spells :name and $name). The spellings mean
// different things to sqlc, so the printer emits the one it was given.
func (n *A_Expr) isNamedParam() (sigil, name string, ok bool) {
if n.Name == nil || len(n.Name.Items) != 1 {
return "", "", false
}
s, sok := n.Name.Items[0].(*String)
if !sok || (s.Str != "@" && s.Str != ":" && s.Str != "$") {
return "", "", false
}
if set(n.Lexpr) || !set(n.Rexpr) {
return "", "", false
}
if nameStr, sok := n.Rexpr.(*String); sok {
return s.Str, nameStr.Str, true
}
// Before the compiler rewrites named parameters, @name parses as the @
// operator applied to a bare column reference.
if col, sok := n.Rexpr.(*ColumnRef); sok && col.Fields != nil && len(col.Fields.Items) == 1 {
if f, sok := col.Fields.Items[0].(*String); sok {
return s.Str, f.Str, true
}
}
return "", "", false
}
// gluePrefix reports that this prefix expression must print with no space
// between the operator and its operand. `@name::type` is a sqlc named
// parameter with a cast: the cast binds tighter than the operator, so the
// parse is @ applied to a TypeCast and isNamedParam does not see it — but
// `@ name::type` would break the parameter apart for sqlc's scanner. Glue
// the sigils onto any operand that starts with an identifier, which also
// keeps a genuine prefix operator like pg's absolute-value @ meaning the
// same thing.
func (n *A_Expr) gluePrefix() bool {
if set(n.Lexpr) || n.Name == nil || len(n.Name.Items) != 1 {
return false
}
s, ok := n.Name.Items[0].(*String)
if !ok || (s.Str != "@" && s.Str != ":" && s.Str != "$") {
return false
}
operand := n.Rexpr
if tc, ok := operand.(*TypeCast); ok {
operand = tc.Arg
}
switch operand.(type) {
case *ColumnRef, *FuncCall:
return true
}
return false
}
// negated returns true when the expression's operator carries the negated
// spelling of a pattern-match operator (e.g. "!~~" for NOT LIKE).
func (n *A_Expr) negated() bool {
if n.Name == nil || len(n.Name.Items) != 1 {
return false
}
s, ok := n.Name.Items[0].(*String)
return ok && strings.HasPrefix(s.Str, "!")
}
func (n *A_Expr) Format(buf *TrackedBuffer, d format.Dialect) {
if n == nil {
return
}
// Check for named parameter first (works regardless of Kind)
if sigil, name, ok := n.isNamedParam(); ok {
buf.WriteString(sigil)
buf.WriteString(name)
return
}
switch n.Kind {
case A_Expr_Kind_IN:
buf.astFormat(n.Lexpr, d)
buf.WriteString(" IN (")
buf.astFormat(n.Rexpr, d)
buf.WriteString(")")
case A_Expr_Kind_LIKE:
buf.astFormat(n.Lexpr, d)
if n.negated() {
buf.WriteString(" NOT LIKE ")
} else {
buf.WriteString(" LIKE ")
}
buf.astFormat(n.Rexpr, d)
case A_Expr_Kind_ILIKE:
buf.astFormat(n.Lexpr, d)
if n.negated() {
buf.WriteString(" NOT ILIKE ")
} else {
buf.WriteString(" ILIKE ")
}
buf.astFormat(n.Rexpr, d)
case A_Expr_Kind_SIMILAR:
buf.astFormat(n.Lexpr, d)
buf.WriteString(" SIMILAR TO ")
buf.astFormat(n.Rexpr, d)
case A_Expr_Kind_BETWEEN:
buf.astFormat(n.Lexpr, d)
buf.WriteString(" BETWEEN ")
if l, ok := n.Rexpr.(*List); ok && len(l.Items) == 2 {
buf.astFormat(l.Items[0], d)
buf.WriteString(" AND ")
buf.astFormat(l.Items[1], d)
}
case A_Expr_Kind_NOT_BETWEEN:
buf.astFormat(n.Lexpr, d)
buf.WriteString(" NOT BETWEEN ")
if l, ok := n.Rexpr.(*List); ok && len(l.Items) == 2 {
buf.astFormat(l.Items[0], d)
buf.WriteString(" AND ")
buf.astFormat(l.Items[1], d)
}
case A_Expr_Kind_DISTINCT:
buf.astFormat(n.Lexpr, d)
buf.WriteString(" IS DISTINCT FROM ")
buf.astFormat(n.Rexpr, d)
case A_Expr_Kind_NOT_DISTINCT:
buf.astFormat(n.Lexpr, d)
buf.WriteString(" IS NOT DISTINCT FROM ")
buf.astFormat(n.Rexpr, d)
case A_Expr_Kind_NULLIF:
buf.WriteString("NULLIF(")
buf.astFormat(n.Lexpr, d)
buf.WriteString(", ")
buf.astFormat(n.Rexpr, d)
buf.WriteString(")")
default:
// Standard operator (including A_Expr_Kind_OP)
if set(n.Lexpr) {
buf.astFormat(n.Lexpr, d)
buf.WriteString(" ")
}
buf.astFormat(n.Name, d)
if set(n.Rexpr) {
if !n.gluePrefix() {
buf.WriteString(" ")
}
buf.astFormat(n.Rexpr, d)
}
}
}