-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathinsert_stmt.go
More file actions
141 lines (120 loc) · 3.58 KB
/
insert_stmt.go
File metadata and controls
141 lines (120 loc) · 3.58 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
package validate
import (
"strings"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
)
const excludedTable = "EXCLUDED"
func InsertStmt(c *catalog.Catalog, fqn *ast.TableName, stmt *ast.InsertStmt) error {
sel, ok := stmt.SelectStmt.(*ast.SelectStmt)
if !ok {
return nil
}
if sel.ValuesLists == nil {
return nil
}
if len(sel.ValuesLists.Items) != 1 {
return nil
}
sublist, ok := sel.ValuesLists.Items[0].(*ast.List)
if !ok {
return nil
}
colsLen := len(stmt.Cols.Items)
valsLen := len(sublist.Items)
switch {
case colsLen > valsLen:
return &sqlerr.Error{
Code: "42601",
Message: "INSERT has more target columns than expressions",
}
case colsLen < valsLen:
return &sqlerr.Error{
Code: "42601",
Message: "INSERT has more expressions than target columns",
}
}
return onConflictClause(c, fqn, stmt)
}
// onConflictClause validates an ON CONFLICT DO UPDATE clause against the target
// table. It checks:
// - ON CONFLICT (col, ...) conflict target columns exist
// - DO UPDATE SET col = ... assignment target columns exist
// - EXCLUDED.col references exist
func onConflictClause(c *catalog.Catalog, fqn *ast.TableName, n *ast.InsertStmt) error {
if fqn == nil || n.OnConflictClause == nil || n.OnConflictClause.Action != ast.OnConflictActionUpdate {
return nil
}
table, err := c.GetTable(fqn)
if err != nil {
return err
}
// Build set of column names for existence checks.
colNames := make(map[string]struct{}, len(table.Columns))
for _, col := range table.Columns {
colNames[col.Name] = struct{}{}
}
// DO UPDATE requires a conflict target: ON CONFLICT (col) or ON CONFLICT ON CONSTRAINT name.
if n.OnConflictClause.Infer == nil {
return &sqlerr.Error{
Code: "42601",
Message: "ON CONFLICT DO UPDATE requires inference specification or constraint name",
}
}
// Validate ON CONFLICT (col, ...) conflict target columns.
if n.OnConflictClause.Infer.IndexElems != nil {
for _, item := range n.OnConflictClause.Infer.IndexElems.Items {
elem, ok := item.(*ast.IndexElem)
if !ok || elem.Name == nil {
continue
}
if _, exists := colNames[*elem.Name]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, *elem.Name)
e.Location = n.OnConflictClause.Infer.Location
return e
}
}
}
// Validate DO UPDATE SET col = ... assignment target columns and EXCLUDED.col references.
if n.OnConflictClause.TargetList == nil {
return nil
}
for _, item := range n.OnConflictClause.TargetList.Items {
target, ok := item.(*ast.ResTarget)
if !ok || target.Name == nil {
continue
}
if _, exists := colNames[*target.Name]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, *target.Name)
e.Location = target.Location
return e
}
if ref, ok := target.Val.(*ast.ColumnRef); ok {
if excludedCol, ok := excludedColumnRef(ref); ok {
if _, exists := colNames[excludedCol]; !exists {
e := sqlerr.ColumnNotFound(excludedTable, excludedCol)
e.Location = ref.Location
return e
}
}
}
}
return nil
}
// excludedColumnRef returns the column name if the ColumnRef is an EXCLUDED.col
// reference, and ok=true. Returns "", false otherwise.
func excludedColumnRef(ref *ast.ColumnRef) (string, bool) {
if ref.Fields == nil || len(ref.Fields.Items) != 2 {
return "", false
}
first, ok := ref.Fields.Items[0].(*ast.String)
if !ok || !strings.EqualFold(first.Str, excludedTable) {
return "", false
}
second, ok := ref.Fields.Items[1].(*ast.String)
if !ok {
return "", false
}
return second.Str, true
}