Repository navigation
Expand file tree
/
Copy pathutils.go
More file actions
144 lines (132 loc) · 4.14 KB
/
Copy pathutils.go
File metadata and controls
144 lines (132 loc) · 4.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
package sqlite
import (
"strings"
meyer "github.com/sqlc-dev/meyer/ast"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
)
// identifier folds a name the way SQLite resolves one: an unquoted name is
// case-insensitive and normalizes to lower case, while a quoted name keeps
// the spelling that appeared between the quotes.
func identifier(n *meyer.Ident) string {
if n == nil {
return ""
}
if n.Quote == meyer.QuoteNone {
return strings.ToLower(n.Name)
}
return n.Name
}
// NewIdentifier returns the folded name of id as an ast.String, the shape
// sqlc uses for the parts of a ColumnRef or a function name.
func NewIdentifier(id *meyer.Ident) *ast.String {
return &ast.String{Str: identifier(id)}
}
func parseTableName(n *meyer.QualifiedName) *ast.TableName {
if n == nil {
return &ast.TableName{}
}
return &ast.TableName{
Schema: identifier(n.Schema),
Name: identifier(n.Name),
}
}
// parseRangeVar builds the relation reference sqlc expects for a table named
// in a FROM, INSERT, UPDATE or DELETE clause.
func parseRangeVar(n *meyer.QualifiedName, alias *meyer.Ident) *ast.RangeVar {
rv := &ast.RangeVar{}
if n == nil {
return rv
}
rv.Location = n.Pos()
name := identifier(n.Name)
rv.Relname = &name
if n.Schema != nil {
schema := identifier(n.Schema)
rv.Schemaname = &schema
}
if alias != nil {
aliasName := identifier(alias)
rv.Alias = &ast.Alias{Aliasname: &aliasName}
}
return rv
}
// typeName renders a type the way sqlc's type resolution expects to see it:
// the tokens joined without separators, matching what the ANTLR parser's
// GetText produced. "UNSIGNED BIG INT" becomes "UNSIGNEDBIGINT" and
// "DECIMAL(10, 5)" becomes "DECIMAL(10,5)".
func typeName(n *meyer.TypeName) string {
if n == nil {
return ""
}
var sb strings.Builder
sb.WriteString(strings.ReplaceAll(n.Name, " ", ""))
if len(n.Args) > 0 {
sb.WriteString("(")
sb.WriteString(strings.Join(n.Args, ","))
sb.WriteString(")")
}
return sb.String()
}
// typeSpelling returns the type as the author wrote it — identifier tokens
// space-joined, arguments as written — for the formatter to print back.
// typeName folds the spaces out for catalog matching.
func typeSpelling(n *meyer.TypeName) string {
if n == nil {
return ""
}
var sb strings.Builder
sb.WriteString(n.Name)
if len(n.Args) > 0 {
sb.WriteString("(")
sb.WriteString(strings.Join(n.Args, ","))
sb.WriteString(")")
}
return sb.String()
}
// representableColumn reports whether a column definition round-trips
// through the fields ast.ColumnDef models: a plain NOT NULL, a plain
// PRIMARY KEY, or nothing. Anything else — a named constraint, a conflict
// clause, ASC/DESC or AUTOINCREMENT, DEFAULT, UNIQUE, CHECK, COLLATE,
// REFERENCES, GENERATED — is parsed away, so a statement printing such a
// column would lose it. NOT NULL next to PRIMARY KEY is also beyond the
// node: SQLite's PRIMARY KEY does not imply NOT NULL, and the printed form
// carries only one of the pair.
func representableColumn(def *meyer.ColumnDef) bool {
var notNull, primary bool
for _, con := range def.Constraints {
switch {
case con.Name != nil:
return false
case con.Kind == meyer.ColumnNotNull && con.OnConflict == meyer.ConflictDefault:
notNull = true
case con.Kind == meyer.ColumnPrimaryKey && con.OnConflict == meyer.ConflictDefault &&
con.Order == meyer.SortDefault && !con.AutoIncrement:
primary = true
default:
return false
}
}
return !(notNull && primary)
}
// hasPrimaryKeyConstraint reports whether a column definition carries a
// PRIMARY KEY constraint.
func hasPrimaryKeyConstraint(constraints []*meyer.ColumnConstraint) bool {
for _, constraint := range constraints {
if constraint.Kind == meyer.ColumnPrimaryKey {
return true
}
}
return false
}
// hasNotNullConstraint reports whether a column definition guarantees a
// value. A PRIMARY KEY implies NOT NULL for the purposes of code
// generation, matching the behavior of the previous parser.
func hasNotNullConstraint(constraints []*meyer.ColumnConstraint) bool {
for _, constraint := range constraints {
switch constraint.Kind {
case meyer.ColumnNotNull, meyer.ColumnPrimaryKey:
return true
}
}
return false
}