|
1 | 1 | package validate |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
| 5 | + |
4 | 6 | "github.com/sqlc-dev/sqlc/internal/sql/ast" |
| 7 | + "github.com/sqlc-dev/sqlc/internal/sql/catalog" |
5 | 8 | "github.com/sqlc-dev/sqlc/internal/sql/sqlerr" |
6 | 9 | ) |
7 | 10 |
|
8 | | -func InsertStmt(stmt *ast.InsertStmt) error { |
| 11 | +const excludedTable = "EXCLUDED" |
| 12 | + |
| 13 | +func InsertStmt(c *catalog.Catalog, fqn *ast.TableName, stmt *ast.InsertStmt) error { |
9 | 14 | sel, ok := stmt.SelectStmt.(*ast.SelectStmt) |
10 | 15 | if !ok { |
11 | 16 | return nil |
@@ -35,5 +40,102 @@ func InsertStmt(stmt *ast.InsertStmt) error { |
35 | 40 | Message: "INSERT has more expressions than target columns", |
36 | 41 | } |
37 | 42 | } |
| 43 | + |
| 44 | + return onConflictClause(c, fqn, stmt) |
| 45 | +} |
| 46 | + |
| 47 | +// onConflictClause validates an ON CONFLICT DO UPDATE clause against the target |
| 48 | +// table. It checks: |
| 49 | +// - ON CONFLICT (col, ...) conflict target columns exist |
| 50 | +// - DO UPDATE SET col = ... assignment target columns exist |
| 51 | +// - EXCLUDED.col references exist |
| 52 | +func onConflictClause(c *catalog.Catalog, fqn *ast.TableName, n *ast.InsertStmt) error { |
| 53 | + if fqn == nil || n.OnConflictClause == nil || n.OnConflictClause.Action != ast.OnConflictActionUpdate { |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + table, err := c.GetTable(fqn) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + // Build set of column names for existence checks. |
| 63 | + colNames := make(map[string]struct{}, len(table.Columns)) |
| 64 | + for _, col := range table.Columns { |
| 65 | + colNames[col.Name] = struct{}{} |
| 66 | + } |
| 67 | + |
| 68 | + // DO UPDATE requires a conflict target: ON CONFLICT (col) or ON CONFLICT ON CONSTRAINT name. |
| 69 | + if n.OnConflictClause.Infer == nil { |
| 70 | + return &sqlerr.Error{ |
| 71 | + Code: "42601", |
| 72 | + Message: "ON CONFLICT DO UPDATE requires inference specification or constraint name", |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Validate ON CONFLICT (col, ...) conflict target columns. |
| 77 | + if n.OnConflictClause.Infer.IndexElems != nil { |
| 78 | + for _, item := range n.OnConflictClause.Infer.IndexElems.Items { |
| 79 | + elem, ok := item.(*ast.IndexElem) |
| 80 | + if !ok || elem.Name == nil { |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + if _, exists := colNames[*elem.Name]; !exists { |
| 85 | + e := sqlerr.ColumnNotFound(table.Rel.Name, *elem.Name) |
| 86 | + e.Location = n.OnConflictClause.Infer.Location |
| 87 | + return e |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Validate DO UPDATE SET col = ... assignment target columns and EXCLUDED.col references. |
| 93 | + if n.OnConflictClause.TargetList == nil { |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + for _, item := range n.OnConflictClause.TargetList.Items { |
| 98 | + target, ok := item.(*ast.ResTarget) |
| 99 | + if !ok || target.Name == nil { |
| 100 | + continue |
| 101 | + } |
| 102 | + |
| 103 | + if _, exists := colNames[*target.Name]; !exists { |
| 104 | + e := sqlerr.ColumnNotFound(table.Rel.Name, *target.Name) |
| 105 | + e.Location = target.Location |
| 106 | + return e |
| 107 | + } |
| 108 | + |
| 109 | + if ref, ok := target.Val.(*ast.ColumnRef); ok { |
| 110 | + if excludedCol, ok := excludedColumnRef(ref); ok { |
| 111 | + if _, exists := colNames[excludedCol]; !exists { |
| 112 | + e := sqlerr.ColumnNotFound(excludedTable, excludedCol) |
| 113 | + e.Location = ref.Location |
| 114 | + return e |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
38 | 120 | return nil |
39 | 121 | } |
| 122 | + |
| 123 | +// excludedColumnRef returns the column name if the ColumnRef is an EXCLUDED.col |
| 124 | +// reference, and ok=true. Returns "", false otherwise. |
| 125 | +func excludedColumnRef(ref *ast.ColumnRef) (string, bool) { |
| 126 | + if ref.Fields == nil || len(ref.Fields.Items) != 2 { |
| 127 | + return "", false |
| 128 | + } |
| 129 | + |
| 130 | + first, ok := ref.Fields.Items[0].(*ast.String) |
| 131 | + if !ok || !strings.EqualFold(first.Str, excludedTable) { |
| 132 | + return "", false |
| 133 | + } |
| 134 | + |
| 135 | + second, ok := ref.Fields.Items[1].(*ast.String) |
| 136 | + if !ok { |
| 137 | + return "", false |
| 138 | + } |
| 139 | + |
| 140 | + return second.Str, true |
| 141 | +} |
0 commit comments