|
| 1 | +package compiler |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/sqlc-dev/sqlc/internal/config" |
| 5 | + "github.com/sqlc-dev/sqlc/internal/sql/ast" |
| 6 | +) |
| 7 | + |
| 8 | +// returningTables builds virtual tables for the OLD and NEW aliases that |
| 9 | +// PostgreSQL 18 makes available in the RETURNING clause of INSERT, UPDATE and |
| 10 | +// DELETE statements. Each alias exposes the columns of the statement's target |
| 11 | +// table. For INSERT there is usually no old row and for DELETE there is no |
| 12 | +// new row, so every column reached through those aliases becomes nullable. |
| 13 | +func (c *Compiler) returningTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) { |
| 14 | + if c.conf.Engine != config.EnginePostgreSQL { |
| 15 | + return nil, nil |
| 16 | + } |
| 17 | + |
| 18 | + var rv *ast.RangeVar |
| 19 | + var returning *ast.List |
| 20 | + oldAlias, newAlias := "old", "new" |
| 21 | + var oldNullable, newNullable bool |
| 22 | + switch n := node.(type) { |
| 23 | + case *ast.DeleteStmt: |
| 24 | + rv = firstRangeVar(n.Relations) |
| 25 | + returning = n.ReturningList |
| 26 | + if n.ReturningOldAlias != "" { |
| 27 | + oldAlias = n.ReturningOldAlias |
| 28 | + } |
| 29 | + if n.ReturningNewAlias != "" { |
| 30 | + newAlias = n.ReturningNewAlias |
| 31 | + } |
| 32 | + // A deleted row has no new value |
| 33 | + newNullable = true |
| 34 | + case *ast.InsertStmt: |
| 35 | + rv = n.Relation |
| 36 | + returning = n.ReturningList |
| 37 | + if n.ReturningOldAlias != "" { |
| 38 | + oldAlias = n.ReturningOldAlias |
| 39 | + } |
| 40 | + if n.ReturningNewAlias != "" { |
| 41 | + newAlias = n.ReturningNewAlias |
| 42 | + } |
| 43 | + // An inserted row has no old value, except when an ON CONFLICT |
| 44 | + // clause updates an existing row instead |
| 45 | + oldNullable = true |
| 46 | + case *ast.UpdateStmt: |
| 47 | + rv = firstRangeVar(n.Relations) |
| 48 | + returning = n.ReturningList |
| 49 | + if n.ReturningOldAlias != "" { |
| 50 | + oldAlias = n.ReturningOldAlias |
| 51 | + } |
| 52 | + if n.ReturningNewAlias != "" { |
| 53 | + newAlias = n.ReturningNewAlias |
| 54 | + } |
| 55 | + default: |
| 56 | + return nil, nil |
| 57 | + } |
| 58 | + if rv == nil || returning == nil || len(returning.Items) == 0 { |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + |
| 62 | + fqn, err := ParseTableName(rv) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + build := func(alias string, nullable bool) *Table { |
| 68 | + table, err := qc.GetTable(fqn) |
| 69 | + if err != nil { |
| 70 | + // An unresolvable target table is reported by the regular |
| 71 | + // source table lookup, so ignore the error here |
| 72 | + return nil |
| 73 | + } |
| 74 | + table.Rel = &ast.TableName{Name: alias} |
| 75 | + if nullable { |
| 76 | + for _, col := range table.Columns { |
| 77 | + col.NotNull = false |
| 78 | + } |
| 79 | + } |
| 80 | + return table |
| 81 | + } |
| 82 | + |
| 83 | + var tables []*Table |
| 84 | + if t := build(oldAlias, oldNullable); t != nil { |
| 85 | + tables = append(tables, t) |
| 86 | + } |
| 87 | + if t := build(newAlias, newNullable); t != nil { |
| 88 | + tables = append(tables, t) |
| 89 | + } |
| 90 | + return tables, nil |
| 91 | +} |
| 92 | + |
| 93 | +func firstRangeVar(list *ast.List) *ast.RangeVar { |
| 94 | + if list == nil { |
| 95 | + return nil |
| 96 | + } |
| 97 | + for _, item := range list.Items { |
| 98 | + if rv, ok := item.(*ast.RangeVar); ok && rv != nil { |
| 99 | + return rv |
| 100 | + } |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// returningTableForScope returns the OLD or NEW virtual table named by scope. |
| 106 | +// A source table with the same name shadows the virtual table, matching |
| 107 | +// PostgreSQL, where the implicit aliases are only available when no relation |
| 108 | +// in the query is already known under that name. |
| 109 | +func returningTableForScope(tables, rtables []*Table, scope string) *Table { |
| 110 | + if scope == "" { |
| 111 | + return nil |
| 112 | + } |
| 113 | + for _, t := range tables { |
| 114 | + if t.Rel.Name == scope { |
| 115 | + return nil |
| 116 | + } |
| 117 | + } |
| 118 | + for _, t := range rtables { |
| 119 | + if t.Rel.Name == scope { |
| 120 | + return t |
| 121 | + } |
| 122 | + } |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +// tablesForRef resolves a column reference against the source tables, |
| 127 | +// extended with the OLD or NEW virtual table when the reference is qualified |
| 128 | +// with one of their names. |
| 129 | +func tablesForRef(ref *ast.ColumnRef, tables, rtables []*Table) []*Table { |
| 130 | + parts := stringSlice(ref.Fields) |
| 131 | + if len(parts) != 2 { |
| 132 | + return tables |
| 133 | + } |
| 134 | + if vt := returningTableForScope(tables, rtables, parts[0]); vt != nil { |
| 135 | + return append(append([]*Table{}, tables...), vt) |
| 136 | + } |
| 137 | + return tables |
| 138 | +} |
0 commit comments