2019-12-29 07:53:54 +01:00
|
|
|
//nolint:errcheck
|
2019-12-25 07:24:30 +01:00
|
|
|
package psql
|
|
|
|
|
|
|
|
import (
|
2019-12-29 07:53:54 +01:00
|
|
|
"errors"
|
2019-12-25 07:24:30 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
"github.com/dosco/super-graph/core/internal/qcode"
|
|
|
|
"github.com/dosco/super-graph/core/internal/util"
|
2019-12-25 07:24:30 +01:00
|
|
|
)
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (c *compilerContext) renderUpdate(
|
|
|
|
w io.Writer, qc *qcode.QCode, vars Variables, ti *DBTableInfo) (uint32, error) {
|
2019-12-25 07:24:30 +01:00
|
|
|
|
2019-12-29 07:53:54 +01:00
|
|
|
update, ok := vars[qc.ActionVar]
|
2019-12-25 07:24:30 +01:00
|
|
|
if !ok {
|
2020-02-02 07:43:09 +01:00
|
|
|
return 0, fmt.Errorf("variable '%s' not !defined", qc.ActionVar)
|
|
|
|
}
|
|
|
|
if len(update) == 0 {
|
|
|
|
return 0, fmt.Errorf("variable '%s' is empty", qc.ActionVar)
|
2019-12-25 07:24:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, `WITH "_sg_input" AS (SELECT `)
|
2020-06-05 03:55:52 +02:00
|
|
|
c.md.renderValueExp(c.w, Param{Name: qc.ActionVar, Type: "json"})
|
2020-05-27 01:41:28 +02:00
|
|
|
// io.WriteString(c.w, qc.ActionVar)
|
|
|
|
io.WriteString(c.w, ` :: json AS j)`)
|
2019-12-25 07:24:30 +01:00
|
|
|
|
|
|
|
st := util.NewStack()
|
2019-12-29 07:53:54 +01:00
|
|
|
st.Push(kvitem{_type: itemUpdate, key: ti.Name, val: update, ti: ti})
|
2019-12-25 07:24:30 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
if st.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-12-29 07:53:54 +01:00
|
|
|
if update[0] == '[' && st.Len() > 1 {
|
|
|
|
return 0, errors.New("Nested bulk update not supported")
|
|
|
|
}
|
2019-12-25 07:24:30 +01:00
|
|
|
intf := st.Pop()
|
|
|
|
|
|
|
|
switch item := intf.(type) {
|
|
|
|
case kvitem:
|
|
|
|
if err := c.handleKVItem(st, item); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case renitem:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// if w := qc.Selects[0].Where; w != nil && w.Op == qcode.OpFalse {
|
|
|
|
// io.WriteString(c.w, ` WHERE false`)
|
|
|
|
// }
|
|
|
|
|
|
|
|
switch item._type {
|
|
|
|
case itemUpdate:
|
|
|
|
err = c.renderUpdateStmt(w, qc, item)
|
2019-12-29 07:53:54 +01:00
|
|
|
case itemConnect:
|
|
|
|
err = c.renderConnectStmt(qc, w, item)
|
|
|
|
case itemDisconnect:
|
|
|
|
err = c.renderDisconnectStmt(qc, w, item)
|
2019-12-25 07:24:30 +01:00
|
|
|
case itemUnion:
|
2019-12-29 07:53:54 +01:00
|
|
|
err = c.renderUnionStmt(w, item)
|
2019-12-25 07:24:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
io.WriteString(c.w, ` `)
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compilerContext) renderUpdateStmt(w io.Writer, qc *qcode.QCode, item renitem) error {
|
|
|
|
ti := item.ti
|
|
|
|
jt := item.data
|
2019-12-29 07:53:54 +01:00
|
|
|
sk := nestedUpdateRelColumnsMap(item.kvitem)
|
2019-12-25 07:24:30 +01:00
|
|
|
|
|
|
|
io.WriteString(c.w, `, `)
|
|
|
|
renderCteName(c.w, item.kvitem)
|
|
|
|
io.WriteString(c.w, ` AS (`)
|
|
|
|
|
|
|
|
io.WriteString(w, `UPDATE `)
|
|
|
|
quoted(w, ti.Name)
|
|
|
|
io.WriteString(w, ` SET (`)
|
2020-05-27 01:41:28 +02:00
|
|
|
c.renderInsertUpdateColumns(qc, jt, ti, sk, false)
|
2019-12-29 07:53:54 +01:00
|
|
|
renderNestedUpdateRelColumns(w, item.kvitem, false)
|
2019-12-25 07:24:30 +01:00
|
|
|
|
|
|
|
io.WriteString(w, `) = (SELECT `)
|
2020-05-27 01:41:28 +02:00
|
|
|
c.renderInsertUpdateColumns(qc, jt, ti, sk, true)
|
2019-12-29 07:53:54 +01:00
|
|
|
renderNestedUpdateRelColumns(w, item.kvitem, true)
|
2019-12-25 07:24:30 +01:00
|
|
|
|
2020-04-18 23:42:17 +02:00
|
|
|
io.WriteString(w, ` FROM "_sg_input" i`)
|
2019-12-29 07:53:54 +01:00
|
|
|
renderNestedUpdateRelTables(w, item.kvitem)
|
2020-04-18 23:42:17 +02:00
|
|
|
io.WriteString(w, `) `)
|
2019-12-25 07:24:30 +01:00
|
|
|
|
|
|
|
if item.id != 0 {
|
|
|
|
// Render sql to set id values if child-to-parent
|
|
|
|
// relationship is one-to-one
|
|
|
|
rel := item.relCP
|
2019-12-29 07:53:54 +01:00
|
|
|
|
|
|
|
io.WriteString(w, `FROM `)
|
|
|
|
quoted(w, rel.Right.Table)
|
|
|
|
|
|
|
|
io.WriteString(w, ` WHERE ((`)
|
2019-12-25 07:24:30 +01:00
|
|
|
colWithTable(w, rel.Left.Table, rel.Left.Col)
|
|
|
|
io.WriteString(w, `) = (`)
|
|
|
|
colWithTable(w, rel.Right.Table, rel.Right.Col)
|
|
|
|
io.WriteString(w, `)`)
|
|
|
|
|
|
|
|
if item.relPC.Type == RelOneToMany {
|
|
|
|
if conn, ok := item.data["where"]; ok {
|
|
|
|
io.WriteString(w, ` AND `)
|
2020-01-26 07:10:54 +01:00
|
|
|
renderWhereFromJSON(w, item.kvitem, "where", conn)
|
2019-12-25 07:24:30 +01:00
|
|
|
} else if conn, ok := item.data["_where"]; ok {
|
|
|
|
io.WriteString(w, ` AND `)
|
2020-01-26 07:10:54 +01:00
|
|
|
renderWhereFromJSON(w, item.kvitem, "_where", conn)
|
2019-12-25 07:24:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
io.WriteString(w, `)`)
|
|
|
|
|
|
|
|
} else {
|
2020-04-18 23:42:17 +02:00
|
|
|
if qc.Selects[0].Where != nil {
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(w, `WHERE `)
|
2020-04-18 23:42:17 +02:00
|
|
|
if err := c.renderWhere(&qc.Selects[0], ti); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-25 07:24:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 07:53:54 +01:00
|
|
|
io.WriteString(w, ` RETURNING `)
|
|
|
|
quoted(w, ti.Name)
|
|
|
|
io.WriteString(w, `.*)`)
|
2019-12-25 07:24:30 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-29 07:53:54 +01:00
|
|
|
func nestedUpdateRelColumnsMap(item kvitem) map[string]struct{} {
|
|
|
|
sk := make(map[string]struct{}, len(item.items))
|
|
|
|
|
|
|
|
for _, v := range item.items {
|
|
|
|
if v._ctype > 0 && v.relCP.Type == RelOneToMany {
|
|
|
|
sk[v.relCP.Right.Col] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sk
|
|
|
|
}
|
2019-12-25 07:24:30 +01:00
|
|
|
|
2019-12-29 07:53:54 +01:00
|
|
|
func renderNestedUpdateRelColumns(w io.Writer, item kvitem, values bool) error {
|
|
|
|
// Render child foreign key columns if child-to-parent
|
|
|
|
// relationship is one-to-many
|
2019-12-25 07:24:30 +01:00
|
|
|
for _, v := range item.items {
|
2019-12-29 07:53:54 +01:00
|
|
|
if v._ctype > 0 && v.relCP.Type == RelOneToMany {
|
|
|
|
if values {
|
2020-01-31 06:19:38 +01:00
|
|
|
// if v.relCP.Right.Array {
|
|
|
|
// io.WriteString(w, `array_diff(`)
|
|
|
|
// colWithTable(w, v.relCP.Right.Table, v.relCP.Right.Col)
|
|
|
|
// io.WriteString(w, `, `)
|
|
|
|
// }
|
|
|
|
|
|
|
|
if v._ctype > 0 {
|
|
|
|
io.WriteString(w, `"_x_`)
|
|
|
|
io.WriteString(w, v.relCP.Left.Table)
|
|
|
|
io.WriteString(w, `".`)
|
|
|
|
quoted(w, v.relCP.Left.Col)
|
2020-01-26 07:10:54 +01:00
|
|
|
} else {
|
2020-01-31 06:19:38 +01:00
|
|
|
colWithTable(w, v.relCP.Left.Table, v.relCP.Left.Col)
|
2020-01-26 07:10:54 +01:00
|
|
|
}
|
2020-01-31 06:19:38 +01:00
|
|
|
|
|
|
|
// if v.relCP.Right.Array {
|
|
|
|
// io.WriteString(w, `)`)
|
|
|
|
// }
|
|
|
|
} else {
|
|
|
|
|
|
|
|
quoted(w, v.relCP.Right.Col)
|
|
|
|
|
2019-12-25 07:24:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 07:53:54 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderNestedUpdateRelTables(w io.Writer, item kvitem) error {
|
2020-01-31 06:19:38 +01:00
|
|
|
// Render tables needed to set values if child-to-parent
|
2019-12-29 07:53:54 +01:00
|
|
|
// relationship is one-to-many
|
|
|
|
for _, v := range item.items {
|
|
|
|
if v._ctype > 0 && v.relCP.Type == RelOneToMany {
|
2020-04-19 19:40:14 +02:00
|
|
|
io.WriteString(w, `, "_x_`)
|
2020-01-31 06:19:38 +01:00
|
|
|
io.WriteString(w, v.relCP.Left.Table)
|
2020-04-19 19:40:14 +02:00
|
|
|
io.WriteString(w, `"`)
|
2019-12-29 07:53:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-25 07:24:30 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (c *compilerContext) renderDelete(
|
|
|
|
w io.Writer, qc *qcode.QCode, vars Variables, ti *DBTableInfo) (uint32, error) {
|
|
|
|
|
2019-12-25 07:24:30 +01:00
|
|
|
root := &qc.Selects[0]
|
|
|
|
|
|
|
|
io.WriteString(c.w, `WITH `)
|
|
|
|
quoted(c.w, ti.Name)
|
|
|
|
|
|
|
|
io.WriteString(c.w, ` AS (DELETE FROM `)
|
|
|
|
quoted(c.w, ti.Name)
|
|
|
|
io.WriteString(c.w, ` WHERE `)
|
|
|
|
|
2020-03-22 01:11:04 +01:00
|
|
|
if root.Where == nil {
|
|
|
|
return 0, errors.New("'where' clause missing in delete mutation")
|
|
|
|
}
|
|
|
|
|
2019-12-25 07:24:30 +01:00
|
|
|
if err := c.renderWhere(root, ti); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-12-29 07:53:54 +01:00
|
|
|
io.WriteString(w, ` RETURNING `)
|
|
|
|
quoted(w, ti.Name)
|
2020-02-19 05:52:44 +01:00
|
|
|
io.WriteString(w, `.*) `)
|
2019-12-25 07:24:30 +01:00
|
|
|
return 0, nil
|
|
|
|
}
|