2019-09-05 06:09:56 +02:00
|
|
|
package psql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2019-09-06 06:34:23 +02:00
|
|
|
"fmt"
|
2019-09-05 06:09:56 +02:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/dosco/super-graph/jsn"
|
|
|
|
"github.com/dosco/super-graph/qcode"
|
|
|
|
)
|
|
|
|
|
2019-10-03 09:08:01 +02:00
|
|
|
var zeroPaging = qcode.Paging{}
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
func (co *Compiler) compileMutation(qc *qcode.QCode, w *bytes.Buffer, vars Variables) (uint32, error) {
|
|
|
|
if len(qc.Selects) == 0 {
|
|
|
|
return 0, errors.New("empty query")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &compilerContext{w, qc.Selects, co}
|
|
|
|
root := &qc.Selects[0]
|
|
|
|
|
2019-09-06 06:34:23 +02:00
|
|
|
switch root.Action {
|
|
|
|
case qcode.ActionInsert:
|
|
|
|
if _, err := c.renderInsert(qc, w, vars); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case qcode.ActionUpdate:
|
|
|
|
if _, err := c.renderUpdate(qc, w, vars); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-09-06 07:17:45 +02:00
|
|
|
case qcode.ActionDelete:
|
|
|
|
if _, err := c.renderDelete(qc, w, vars); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:34:23 +02:00
|
|
|
default:
|
|
|
|
return 0, errors.New("valid mutations are 'insert' and 'update'")
|
2019-09-05 06:09:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 09:08:01 +02:00
|
|
|
root.Paging = zeroPaging
|
|
|
|
root.DistinctOn = root.DistinctOn[:]
|
|
|
|
root.OrderBy = root.OrderBy[:]
|
|
|
|
root.Where = nil
|
|
|
|
root.Args = nil
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
return c.compileQuery(qc, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compilerContext) renderInsert(qc *qcode.QCode, w *bytes.Buffer, vars Variables) (uint32, error) {
|
|
|
|
root := &qc.Selects[0]
|
|
|
|
|
2019-09-06 06:34:23 +02:00
|
|
|
insert, ok := vars[root.ActionVar]
|
2019-09-05 06:09:56 +02:00
|
|
|
if !ok {
|
2019-09-06 06:34:23 +02:00
|
|
|
return 0, fmt.Errorf("Variable '%s' not defined", root.ActionVar)
|
|
|
|
}
|
|
|
|
|
|
|
|
ti, err := c.schema.GetTable(root.Table)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2019-09-05 06:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jt, array, err := jsn.Tree(insert)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-09-06 07:17:45 +02:00
|
|
|
c.w.WriteString(`WITH `)
|
2019-09-20 06:19:11 +02:00
|
|
|
quoted(c.w, ti.Name)
|
2019-09-06 07:17:45 +02:00
|
|
|
|
2019-09-20 06:19:11 +02:00
|
|
|
c.w.WriteString(` AS (WITH "input" AS (SELECT {{`)
|
|
|
|
c.w.WriteString(root.ActionVar)
|
|
|
|
c.w.WriteString(`}}::json AS j) INSERT INTO `)
|
|
|
|
c.w.WriteString(ti.Name)
|
2019-09-06 06:34:23 +02:00
|
|
|
io.WriteString(c.w, ` (`)
|
2019-09-06 07:17:45 +02:00
|
|
|
c.renderInsertUpdateColumns(qc, w, jt, ti)
|
2019-09-06 06:34:23 +02:00
|
|
|
io.WriteString(c.w, `)`)
|
2019-09-05 06:09:56 +02:00
|
|
|
|
|
|
|
c.w.WriteString(` SELECT `)
|
2019-09-06 07:17:45 +02:00
|
|
|
c.renderInsertUpdateColumns(qc, w, jt, ti)
|
2019-09-05 06:09:56 +02:00
|
|
|
c.w.WriteString(` FROM input i, `)
|
|
|
|
|
|
|
|
if array {
|
|
|
|
c.w.WriteString(`json_populate_recordset`)
|
|
|
|
} else {
|
|
|
|
c.w.WriteString(`json_populate_record`)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.w.WriteString(`(NULL::`)
|
2019-09-20 06:19:11 +02:00
|
|
|
c.w.WriteString(ti.Name)
|
2019-09-06 07:17:45 +02:00
|
|
|
c.w.WriteString(`, i.j) t RETURNING *) `)
|
2019-09-05 06:09:56 +02:00
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 07:17:45 +02:00
|
|
|
func (c *compilerContext) renderInsertUpdateColumns(qc *qcode.QCode, w *bytes.Buffer,
|
2019-09-06 06:34:23 +02:00
|
|
|
jt map[string]interface{}, ti *DBTableInfo) (uint32, error) {
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, cn := range ti.ColumnNames {
|
|
|
|
if _, ok := jt[cn]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i != 0 {
|
|
|
|
io.WriteString(c.w, `, `)
|
|
|
|
}
|
|
|
|
c.w.WriteString(cn)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compilerContext) renderUpdate(qc *qcode.QCode, w *bytes.Buffer, vars Variables) (uint32, error) {
|
|
|
|
root := &qc.Selects[0]
|
|
|
|
|
|
|
|
update, ok := vars[root.ActionVar]
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("Variable '%s' not defined", root.ActionVar)
|
|
|
|
}
|
2019-09-05 06:09:56 +02:00
|
|
|
|
2019-09-06 06:34:23 +02:00
|
|
|
ti, err := c.schema.GetTable(root.Table)
|
2019-09-05 06:09:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:34:23 +02:00
|
|
|
jt, array, err := jsn.Tree(update)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-09-06 07:17:45 +02:00
|
|
|
c.w.WriteString(`WITH `)
|
2019-09-20 06:19:11 +02:00
|
|
|
quoted(c.w, ti.Name)
|
2019-09-06 07:17:45 +02:00
|
|
|
|
2019-09-20 06:19:11 +02:00
|
|
|
c.w.WriteString(` AS (WITH "input" AS (SELECT {{`)
|
|
|
|
c.w.WriteString(root.ActionVar)
|
|
|
|
c.w.WriteString(`}}::json AS j) UPDATE `)
|
|
|
|
c.w.WriteString(ti.Name)
|
2019-09-06 06:34:23 +02:00
|
|
|
io.WriteString(c.w, ` SET (`)
|
2019-09-06 07:17:45 +02:00
|
|
|
c.renderInsertUpdateColumns(qc, w, jt, ti)
|
2019-09-06 06:34:23 +02:00
|
|
|
|
|
|
|
c.w.WriteString(`) = (SELECT `)
|
2019-09-06 07:17:45 +02:00
|
|
|
c.renderInsertUpdateColumns(qc, w, jt, ti)
|
2019-09-06 06:34:23 +02:00
|
|
|
c.w.WriteString(` FROM input i, `)
|
|
|
|
|
|
|
|
if array {
|
|
|
|
c.w.WriteString(`json_populate_recordset`)
|
|
|
|
} else {
|
|
|
|
c.w.WriteString(`json_populate_record`)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.w.WriteString(`(NULL::`)
|
2019-09-20 06:19:11 +02:00
|
|
|
c.w.WriteString(ti.Name)
|
2019-09-06 06:34:23 +02:00
|
|
|
c.w.WriteString(`, i.j) t)`)
|
|
|
|
|
|
|
|
io.WriteString(c.w, ` WHERE `)
|
|
|
|
|
|
|
|
if err := c.renderWhere(root, ti); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-09-06 07:17:45 +02:00
|
|
|
io.WriteString(c.w, ` RETURNING *) `)
|
2019-09-06 06:34:23 +02:00
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 07:17:45 +02:00
|
|
|
func (c *compilerContext) renderDelete(qc *qcode.QCode, w *bytes.Buffer, vars Variables) (uint32, error) {
|
|
|
|
root := &qc.Selects[0]
|
2019-09-06 06:34:23 +02:00
|
|
|
|
2019-09-06 07:17:45 +02:00
|
|
|
ti, err := c.schema.GetTable(root.Table)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2019-09-05 06:09:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 16:24:11 +02:00
|
|
|
c.w.WriteString(`WITH `)
|
|
|
|
quoted(c.w, ti.Name)
|
|
|
|
|
|
|
|
c.w.WriteString(` AS (DELETE FROM `)
|
2019-09-20 06:19:11 +02:00
|
|
|
c.w.WriteString(ti.Name)
|
2019-09-06 07:17:45 +02:00
|
|
|
io.WriteString(c.w, ` WHERE `)
|
|
|
|
|
|
|
|
if err := c.renderWhere(root, ti); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, ` RETURNING *) `)
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
return 0, nil
|
|
|
|
}
|
2019-09-20 06:19:11 +02:00
|
|
|
|
|
|
|
func quoted(w *bytes.Buffer, identifier string) {
|
|
|
|
w.WriteString(`"`)
|
|
|
|
w.WriteString(identifier)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|