-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathreserved.go
More file actions
186 lines (178 loc) · 3.49 KB
/
reserved.go
File metadata and controls
186 lines (178 loc) · 3.49 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
package postgresql
import (
"fmt"
"strings"
)
// hasMixedCase returns true if the string has any uppercase letters
// (identifiers with mixed case need quoting in PostgreSQL)
func hasMixedCase(s string) bool {
for _, r := range s {
if r >= 'A' && r <= 'Z' {
return true
}
}
return false
}
// QuoteIdent returns a quoted identifier if it needs quoting.
// This implements the format.Formatter interface.
func (p *Parser) QuoteIdent(s string) string {
if p.IsReservedKeyword(s) || hasMixedCase(s) {
return `"` + s + `"`
}
return s
}
// TypeName returns the SQL type name for the given namespace and name.
// This implements the format.Formatter interface.
func (p *Parser) TypeName(ns, name string) string {
if ns == "pg_catalog" {
switch name {
case "int4":
return "integer"
case "int8":
return "bigint"
case "int2":
return "smallint"
case "float4":
return "real"
case "float8":
return "double precision"
case "bool":
return "boolean"
case "bpchar":
return "character"
case "timestamptz":
return "timestamp with time zone"
case "timetz":
return "time with time zone"
default:
return name
}
}
if ns != "" {
return ns + "." + name
}
return name
}
// Param returns the parameter placeholder for the given number.
// PostgreSQL uses $1, $2, etc.
func (p *Parser) Param(n int) string {
return fmt.Sprintf("$%d", n)
}
// NamedParam returns the named parameter placeholder for the given name.
// PostgreSQL/sqlc uses @name syntax.
func (p *Parser) NamedParam(name string) string {
return "@" + name
}
// Cast returns a type cast expression.
// PostgreSQL uses expr::type syntax.
func (p *Parser) Cast(arg, typeName string) string {
return arg + "::" + typeName
}
// https://www.postgresql.org/docs/current/sql-keywords-appendix.html
func (p *Parser) IsReservedKeyword(s string) bool {
switch strings.ToLower(s) {
case "all":
case "analyse":
case "analyze":
case "and":
case "any":
case "array":
case "as":
case "asc":
case "asymmetric":
case "authorization":
case "binary":
case "both":
case "case":
case "cast":
case "check":
case "collate":
case "collation":
case "column":
case "concurrently":
case "constraint":
case "create":
case "cross":
case "current_catalog":
case "current_date":
case "current_role":
case "current_schema":
case "current_time":
case "current_timestamp":
case "current_user":
case "default":
case "deferrable":
case "desc":
case "distinct":
case "do":
case "else":
case "end":
case "except":
case "false":
case "fetch":
case "for":
case "foreign":
case "freeze":
case "from":
case "full":
case "grant":
case "group":
case "having":
case "ilike":
case "in":
case "initially":
case "inner":
case "intersect":
case "into":
case "is":
case "isnull":
case "join":
case "lateral":
case "leading":
case "left":
case "like":
case "limit":
case "localtime":
case "localtimestamp":
case "natural":
case "not":
case "notnull":
case "null":
case "offset":
case "on":
case "only":
case "or":
case "order":
case "outer":
case "overlaps":
case "placing":
case "primary":
case "references":
case "returning":
case "right":
case "select":
case "session_user":
case "similar":
case "some":
case "symmetric":
case "table":
case "tablesample":
case "then":
case "to":
case "trailing":
case "true":
case "union":
case "unique":
case "user":
case "using":
case "variadic":
case "verbose":
case "when":
case "where":
case "window":
case "with":
default:
return false
}
return true
}