-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
update.go
89 lines (73 loc) · 2.08 KB
/
update.go
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
//nolint:errcheck
package psql
import (
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/dosco/graphjin/core/internal/sdata"
)
func (c *compilerContext) renderUpdate() {
i := 0
for _, m := range c.qc.Mutates {
switch {
case m.Type == qcode.MTUpdate:
i = c.renderComma(i)
c.renderUpdateStmt(m)
case m.Rel.Type == sdata.RelOneToOne && m.Type == qcode.MTConnect:
i = c.renderComma(i)
c.renderOneToOneConnectStmt(m)
case m.Rel.Type == sdata.RelOneToOne && m.Type == qcode.MTDisconnect:
i = c.renderComma(i)
c.renderOneToOneDisconnectStmt(m)
}
}
c.w.WriteString(` `)
}
func (c *compilerContext) renderUpdateStmt(m qcode.Mutate) {
sel := c.qc.Selects[0]
c.renderCteName(m)
c.w.WriteString(` AS (`)
c.renderOneToManyModifiers(m)
c.w.WriteString(`UPDATE `)
c.quoted(m.Ti.Name)
c.w.WriteString(` SET (`)
n := c.renderInsertUpdateColumns(m, false)
c.renderNestedRelColumns(m, false, false, n)
c.w.WriteString(`) = (`)
c.renderValues(m, true)
c.w.WriteString(`)`)
// inner select ended
if m.ParentID == -1 {
c.w.WriteString(` WHERE `)
c.renderExp(m.Ti, sel.Where.Exp, false)
} else {
// Render sql to set id values if child-to-parent
// relationship is one-to-one
rel := m.Rel
c.w.WriteString(` FROM _sg_input i`)
// c.quoted(rel.Right.Col.Table)
// c.w.WriteString(` _x_`)
// c.w.WriteString(rel.Right.Col.Table)
c.renderNestedRelTables(m, true)
if m.IsArray {
c.w.WriteString(`, json_populate_recordset`)
} else {
c.w.WriteString(`, json_populate_record`)
}
c.w.WriteString(`(NULL::"`)
c.w.WriteString(m.Ti.Name)
joinPath(c.w, `", i.j`, m.Path)
c.w.WriteString(`) t`)
c.w.WriteString(` WHERE ((`)
c.colWithTable(rel.Left.Col.Table, rel.Left.Col.Name)
c.w.WriteString(`) = (`)
c.colWithTable(("_x_" + rel.Right.Col.Table), rel.Right.Col.Name)
c.w.WriteString(`)`)
if m.Rel.Type == sdata.RelOneToOne {
c.w.WriteString(` AND `)
c.renderExpPath(m.Ti, m.Where.Exp, false, append(m.Path, "where"))
}
c.w.WriteString(`)`)
}
c.w.WriteString(` RETURNING `)
c.quoted(m.Ti.Name)
c.w.WriteString(`.*)`)
}