-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
insert.go
59 lines (48 loc) · 1.31 KB
/
insert.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
//nolint:errcheck
package psql
import (
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/dosco/graphjin/core/internal/sdata"
)
func (c *compilerContext) renderInsert() {
for _, m := range c.qc.Mutates {
switch {
case m.Type == qcode.MTInsert:
c.renderInsertStmt(m, false)
case m.Type == qcode.MTUpsert:
c.renderInsertStmt(m, true)
case m.Rel.Type == sdata.RelOneToOne && m.Type == qcode.MTConnect:
c.renderOneToOneConnectStmt(m)
}
}
c.w.WriteString(` `)
}
func (c *compilerContext) renderInsertStmt(m qcode.Mutate, embedded bool) {
c.w.WriteString(`, `)
c.renderCteName(m)
c.w.WriteString(` AS (`)
c.renderOneToManyModifiers(m)
c.w.WriteString(`INSERT INTO `)
c.quoted(m.Ti.Name)
c.w.WriteString(` (`)
n := c.renderInsertUpdateColumns(m, false)
c.renderNestedRelColumns(m, false, false, n)
c.w.WriteString(`)`)
c.w.WriteString(` SELECT `)
n = c.renderInsertUpdateColumns(m, true)
c.renderNestedRelColumns(m, true, false, n)
c.w.WriteString(` FROM _sg_input i`)
c.renderNestedRelTables(m, false)
if m.Array {
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`)
if !embedded {
c.w.WriteString(` RETURNING *)`)
}
}