2019-11-28 07:25:46 +01:00
|
|
|
//nolint:errcheck
|
2019-03-24 14:57:29 +01:00
|
|
|
package psql
|
|
|
|
|
|
|
|
import (
|
2019-06-01 08:03:09 +02:00
|
|
|
"bytes"
|
2019-09-05 06:09:56 +02:00
|
|
|
"encoding/json"
|
2019-03-24 14:57:29 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-05-29 08:23:54 +02:00
|
|
|
"strconv"
|
2019-03-27 07:58:19 +01:00
|
|
|
"strings"
|
2019-03-24 14:57:29 +01:00
|
|
|
|
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-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
const (
|
2019-06-08 02:53:08 +02:00
|
|
|
closeBlock = 500
|
2019-05-13 01:27:26 +02:00
|
|
|
)
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
type Param struct {
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
IsArray bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Metadata struct {
|
2020-06-05 03:55:52 +02:00
|
|
|
skipped uint32
|
|
|
|
params []Param
|
2020-05-27 01:41:28 +02:00
|
|
|
pindex map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
type compilerContext struct {
|
|
|
|
md Metadata
|
|
|
|
w io.Writer
|
|
|
|
s []qcode.Select
|
|
|
|
*Compiler
|
|
|
|
}
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
type Variables map[string]json.RawMessage
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
type Config struct {
|
2020-02-19 05:52:44 +01:00
|
|
|
Schema *DBSchema
|
|
|
|
Vars map[string]string
|
2019-04-08 08:47:59 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
type Compiler struct {
|
2020-02-19 05:52:44 +01:00
|
|
|
schema *DBSchema
|
|
|
|
vars map[string]string
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
func NewCompiler(conf Config) *Compiler {
|
2020-02-10 07:45:37 +01:00
|
|
|
return &Compiler{
|
2020-02-19 05:52:44 +01:00
|
|
|
schema: conf.Schema,
|
|
|
|
vars: conf.Vars,
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (co *Compiler) AddRelationship(child, parent string, rel *DBRel) error {
|
|
|
|
return co.schema.SetRel(child, parent, rel)
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (co *Compiler) IDColumn(table string) (*DBColumn, error) {
|
|
|
|
ti, err := co.schema.GetTable(table)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
2019-12-09 07:48:18 +01:00
|
|
|
return nil, err
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
if ti.PrimaryCol == nil {
|
|
|
|
return nil, fmt.Errorf("no primary key column found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ti.PrimaryCol, nil
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (co *Compiler) CompileEx(qc *qcode.QCode, vars Variables) (Metadata, []byte, error) {
|
2019-06-02 01:48:42 +02:00
|
|
|
w := &bytes.Buffer{}
|
2020-05-27 01:41:28 +02:00
|
|
|
metad, err := co.Compile(w, qc, vars)
|
|
|
|
return metad, w.Bytes(), err
|
2019-06-02 01:48:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (co *Compiler) Compile(w io.Writer, qc *qcode.QCode, vars Variables) (Metadata, error) {
|
2020-06-05 03:55:52 +02:00
|
|
|
return co.CompileWithMetadata(w, qc, vars, Metadata{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *Compiler) CompileWithMetadata(w io.Writer, qc *qcode.QCode, vars Variables, md Metadata) (Metadata, error) {
|
|
|
|
md.skipped = 0
|
|
|
|
|
2020-05-31 23:11:28 +02:00
|
|
|
if qc == nil {
|
2020-06-05 03:55:52 +02:00
|
|
|
return md, fmt.Errorf("qcode is nil")
|
2020-05-31 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
switch qc.Type {
|
|
|
|
case qcode.QTQuery:
|
2020-06-05 03:55:52 +02:00
|
|
|
return co.compileQueryWithMetadata(w, qc, vars, md)
|
2020-05-27 01:41:28 +02:00
|
|
|
|
|
|
|
case qcode.QTInsert,
|
|
|
|
qcode.QTUpdate,
|
|
|
|
qcode.QTDelete,
|
|
|
|
qcode.QTUpsert:
|
|
|
|
return co.compileMutation(w, qc, vars)
|
2019-09-05 06:09:56 +02:00
|
|
|
|
2020-06-05 03:55:52 +02:00
|
|
|
default:
|
|
|
|
return Metadata{}, fmt.Errorf("Unknown operation type %d", qc.Type)
|
|
|
|
}
|
2020-05-27 01:41:28 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (co *Compiler) compileQueryWithMetadata(
|
|
|
|
w io.Writer, qc *qcode.QCode, vars Variables, md Metadata) (Metadata, error) {
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
if len(qc.Selects) == 0 {
|
2020-05-27 01:41:28 +02:00
|
|
|
return md, errors.New("empty query")
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
c := &compilerContext{md, w, qc.Selects, co}
|
2019-12-25 07:24:30 +01:00
|
|
|
st := NewIntStack()
|
2020-02-10 07:45:37 +01:00
|
|
|
i := 0
|
2019-11-19 06:47:55 +01:00
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(c.w, `SELECT jsonb_build_object(`)
|
2020-02-10 07:45:37 +01:00
|
|
|
for _, id := range qc.Roots {
|
2020-05-24 08:24:24 +02:00
|
|
|
if i != 0 {
|
|
|
|
io.WriteString(c.w, `, `)
|
2019-11-19 06:47:55 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 08:24:24 +02:00
|
|
|
root := &qc.Selects[id]
|
2020-01-21 05:38:17 +01:00
|
|
|
|
2020-05-24 08:24:24 +02:00
|
|
|
if root.SkipRender || len(root.Cols) == 0 {
|
|
|
|
squoted(c.w, root.FieldName)
|
2020-02-10 07:45:37 +01:00
|
|
|
io.WriteString(c.w, `, `)
|
2020-05-24 08:24:24 +02:00
|
|
|
io.WriteString(c.w, `NULL`)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
st.Push(root.ID + closeBlock)
|
|
|
|
st.Push(root.ID)
|
|
|
|
c.renderRootSelect(root)
|
2020-01-21 05:38:17 +01:00
|
|
|
}
|
2019-09-20 06:19:11 +02:00
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
i++
|
2020-01-21 05:38:17 +01:00
|
|
|
}
|
2019-11-19 06:47:55 +01:00
|
|
|
|
2020-05-24 08:24:24 +02:00
|
|
|
if st.Len() != 0 {
|
|
|
|
io.WriteString(c.w, `) as "__root" FROM `)
|
|
|
|
} else {
|
|
|
|
io.WriteString(c.w, `) as "__root"`)
|
2020-05-27 01:41:28 +02:00
|
|
|
return c.md, nil
|
2019-09-20 06:19:11 +02:00
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
for {
|
|
|
|
if st.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
id := st.Pop()
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if id < closeBlock {
|
|
|
|
sel := &c.s[id]
|
|
|
|
|
2020-03-18 04:03:41 +01:00
|
|
|
if len(sel.Cols) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-29 07:38:23 +01:00
|
|
|
ti, err := c.schema.GetTable(sel.Name)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
2020-05-27 01:41:28 +02:00
|
|
|
return c.md, err
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
if sel.ParentID == -1 {
|
|
|
|
io.WriteString(c.w, `(`)
|
|
|
|
} else {
|
|
|
|
c.renderLateralJoin(sel)
|
2020-02-21 05:59:37 +01:00
|
|
|
}
|
2020-02-19 05:52:44 +01:00
|
|
|
|
2020-04-20 05:48:49 +02:00
|
|
|
if !ti.IsSingular {
|
2020-02-21 05:59:37 +01:00
|
|
|
c.renderPluralSelect(sel, ti)
|
2019-06-08 02:53:08 +02:00
|
|
|
}
|
2019-11-19 06:47:55 +01:00
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
if err := c.renderSelect(sel, ti, vars); err != nil {
|
|
|
|
return c.md, err
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
for _, cid := range sel.Children {
|
2020-06-05 03:55:52 +02:00
|
|
|
if hasBit(c.md.skipped, uint32(cid)) {
|
2019-05-13 01:27:26 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
child := &c.s[cid]
|
2020-01-21 05:38:17 +01:00
|
|
|
if child.SkipRender {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-06 08:35:08 +02:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
st.Push(child.ID + closeBlock)
|
|
|
|
st.Push(child.ID)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
} else {
|
|
|
|
sel := &c.s[(id - closeBlock)]
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
ti, err := c.schema.GetTable(sel.Name)
|
|
|
|
if err != nil {
|
2020-05-27 01:41:28 +02:00
|
|
|
return c.md, err
|
2020-02-19 05:52:44 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 03:25:50 +02:00
|
|
|
io.WriteString(c.w, `)`)
|
|
|
|
aliasWithID(c.w, "__sr", sel.ID)
|
|
|
|
|
|
|
|
io.WriteString(c.w, `)`)
|
|
|
|
aliasWithID(c.w, "__sj", sel.ID)
|
|
|
|
|
2020-04-20 05:48:49 +02:00
|
|
|
if !ti.IsSingular {
|
2020-02-21 05:59:37 +01:00
|
|
|
io.WriteString(c.w, `)`)
|
2020-04-02 03:25:50 +02:00
|
|
|
aliasWithID(c.w, "__sj", sel.ID)
|
2020-02-21 05:59:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
if sel.ParentID == -1 {
|
2019-11-19 06:47:55 +01:00
|
|
|
if st.Len() != 0 {
|
|
|
|
io.WriteString(c.w, `, `)
|
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
} else {
|
|
|
|
c.renderLateralJoinClose(sel)
|
2019-06-08 02:53:08 +02:00
|
|
|
}
|
2019-11-19 06:47:55 +01:00
|
|
|
|
2019-12-02 16:52:22 +01:00
|
|
|
if len(sel.Args) != 0 {
|
|
|
|
for _, v := range sel.Args {
|
2020-06-09 01:28:22 +02:00
|
|
|
qcode.FreeNode(v)
|
2019-12-02 16:52:22 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
return c.md, nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
func (c *compilerContext) renderPluralSelect(sel *qcode.Select, ti *DBTableInfo) error {
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(c.w, `SELECT coalesce(jsonb_agg("__sj_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, sel.ID)
|
2020-02-19 05:52:44 +01:00
|
|
|
io.WriteString(c.w, `"."json"), '[]') as "json"`)
|
|
|
|
|
|
|
|
if sel.Paging.Type != qcode.PtOffset {
|
|
|
|
n := 0
|
|
|
|
|
|
|
|
// check if primary key already included in order by
|
|
|
|
// query argument
|
|
|
|
for _, ob := range sel.OrderBy {
|
|
|
|
if ob.Col == ti.PrimaryCol.Key {
|
|
|
|
n = 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == 1 {
|
|
|
|
n = len(sel.OrderBy)
|
|
|
|
} else {
|
|
|
|
n = len(sel.OrderBy) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, `, CONCAT_WS(','`)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
io.WriteString(c.w, `, max("__cur_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, int32(i))
|
2020-02-19 05:52:44 +01:00
|
|
|
io.WriteString(c.w, `")`)
|
|
|
|
}
|
|
|
|
io.WriteString(c.w, `) as "cursor"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, ` FROM (`)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 05:59:37 +01:00
|
|
|
func (c *compilerContext) renderRootSelect(sel *qcode.Select) error {
|
2020-02-19 05:52:44 +01:00
|
|
|
io.WriteString(c.w, `'`)
|
|
|
|
io.WriteString(c.w, sel.FieldName)
|
|
|
|
io.WriteString(c.w, `', `)
|
|
|
|
|
2020-04-02 03:25:50 +02:00
|
|
|
io.WriteString(c.w, `"__sj_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, sel.ID)
|
2020-02-21 05:59:37 +01:00
|
|
|
io.WriteString(c.w, `"."json"`)
|
2020-02-19 05:52:44 +01:00
|
|
|
|
2020-02-21 05:59:37 +01:00
|
|
|
if sel.Paging.Type != qcode.PtOffset {
|
|
|
|
io.WriteString(c.w, `, '`)
|
|
|
|
io.WriteString(c.w, sel.FieldName)
|
|
|
|
io.WriteString(c.w, `_cursor', `)
|
2020-02-19 05:52:44 +01:00
|
|
|
|
2020-04-02 03:25:50 +02:00
|
|
|
io.WriteString(c.w, `"__sj_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, sel.ID)
|
2020-02-21 05:59:37 +01:00
|
|
|
io.WriteString(c.w, `"."cursor"`)
|
2020-02-19 05:52:44 +01:00
|
|
|
}
|
2020-02-21 05:59:37 +01:00
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (c *compilerContext) initSelect(sel *qcode.Select, ti *DBTableInfo, vars Variables) ([]*qcode.Column, error) {
|
2019-06-08 02:53:08 +02:00
|
|
|
cols := make([]*qcode.Column, 0, len(sel.Cols))
|
|
|
|
colmap := make(map[string]struct{}, len(sel.Cols))
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
for i := range sel.Cols {
|
|
|
|
colmap[sel.Cols[i].Name] = struct{}{}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-19 09:12:51 +01:00
|
|
|
for i := range sel.OrderBy {
|
|
|
|
colmap[sel.OrderBy[i].Col] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
if sel.Paging.Type != qcode.PtOffset {
|
|
|
|
colmap[ti.PrimaryCol.Key] = struct{}{}
|
2020-02-22 21:58:37 +01:00
|
|
|
addPrimaryKey := true
|
2020-02-19 05:52:44 +01:00
|
|
|
|
|
|
|
for _, ob := range sel.OrderBy {
|
|
|
|
if ob.Col == ti.PrimaryCol.Key {
|
2020-02-22 21:58:37 +01:00
|
|
|
addPrimaryKey = false
|
|
|
|
break
|
2020-02-19 05:52:44 +01:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 21:58:37 +01:00
|
|
|
if addPrimaryKey {
|
|
|
|
ob := &qcode.OrderBy{Col: ti.PrimaryCol.Name, Order: qcode.OrderAsc}
|
2020-02-11 07:11:35 +01:00
|
|
|
|
2020-02-22 21:58:37 +01:00
|
|
|
if sel.Paging.Type == qcode.PtBackward {
|
2020-02-19 05:52:44 +01:00
|
|
|
ob.Order = qcode.OrderDesc
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
2020-02-22 21:58:37 +01:00
|
|
|
sel.OrderBy = append(sel.OrderBy, ob)
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 21:58:37 +01:00
|
|
|
if sel.Paging.Cursor {
|
|
|
|
c.addSeekPredicate(sel)
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
for _, id := range sel.Children {
|
|
|
|
child := &c.s[id]
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-11-29 07:38:23 +01:00
|
|
|
rel, err := c.schema.GetRel(child.Name, ti.Name)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
2020-05-27 01:41:28 +02:00
|
|
|
return nil, err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
switch rel.Type {
|
2019-12-25 07:24:30 +01:00
|
|
|
case RelOneToOne, RelOneToMany:
|
2019-12-09 07:48:18 +01:00
|
|
|
if _, ok := colmap[rel.Right.Col]; !ok {
|
|
|
|
cols = append(cols, &qcode.Column{Table: ti.Name, Name: rel.Right.Col, FieldName: rel.Right.Col})
|
2020-01-28 06:26:53 +01:00
|
|
|
colmap[rel.Right.Col] = struct{}{}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-01-01 07:54:38 +01:00
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
case RelOneToManyThrough:
|
2019-12-09 07:48:18 +01:00
|
|
|
if _, ok := colmap[rel.Left.Col]; !ok {
|
|
|
|
cols = append(cols, &qcode.Column{Table: ti.Name, Name: rel.Left.Col, FieldName: rel.Left.Col})
|
2020-01-28 06:26:53 +01:00
|
|
|
colmap[rel.Left.Col] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
case RelEmbedded:
|
|
|
|
if _, ok := colmap[rel.Left.Col]; !ok {
|
|
|
|
cols = append(cols, &qcode.Column{Table: ti.Name, Name: rel.Left.Col, FieldName: rel.Left.Col})
|
|
|
|
colmap[rel.Left.Col] = struct{}{}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-01-01 07:54:38 +01:00
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
case RelRemote:
|
2019-12-09 07:48:18 +01:00
|
|
|
if _, ok := colmap[rel.Left.Col]; !ok {
|
|
|
|
cols = append(cols, &qcode.Column{Table: ti.Name, Name: rel.Left.Col, FieldName: rel.Right.Col})
|
2020-01-28 06:26:53 +01:00
|
|
|
colmap[rel.Left.Col] = struct{}{}
|
2020-06-05 03:55:52 +02:00
|
|
|
c.md.skipped |= (1 << uint(id))
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2020-05-27 01:41:28 +02:00
|
|
|
return nil, fmt.Errorf("unknown relationship %s", rel)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
return cols, nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 21:58:37 +01:00
|
|
|
// This
|
|
|
|
// (A, B, C) >= (X, Y, Z)
|
|
|
|
//
|
|
|
|
// Becomes
|
|
|
|
// (A > X)
|
|
|
|
// OR ((A = X) AND (B > Y))
|
|
|
|
// OR ((A = X) AND (B = Y) AND (C > Z))
|
|
|
|
// OR ((A = X) AND (B = Y) AND (C = Z))
|
|
|
|
|
|
|
|
func (c *compilerContext) addSeekPredicate(sel *qcode.Select) error {
|
|
|
|
var or, and *qcode.Exp
|
|
|
|
|
|
|
|
obLen := len(sel.OrderBy)
|
|
|
|
|
|
|
|
if obLen > 1 {
|
|
|
|
or = qcode.NewFilter()
|
|
|
|
or.Op = qcode.OpOr
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < obLen; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
and = qcode.NewFilter()
|
|
|
|
and.Op = qcode.OpAnd
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, ob := range sel.OrderBy {
|
|
|
|
f := qcode.NewFilter()
|
|
|
|
f.Col = ob.Col
|
|
|
|
f.Type = qcode.ValRef
|
|
|
|
f.Table = "__cur"
|
|
|
|
f.Val = ob.Col
|
|
|
|
|
|
|
|
if obLen == 1 {
|
|
|
|
qcode.AddFilter(sel, f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case i > 0 && n != i:
|
|
|
|
f.Op = qcode.OpEquals
|
|
|
|
case ob.Order == qcode.OrderDesc:
|
|
|
|
f.Op = qcode.OpLesserThan
|
|
|
|
default:
|
|
|
|
f.Op = qcode.OpGreaterThan
|
|
|
|
}
|
|
|
|
|
|
|
|
if and != nil {
|
|
|
|
and.Children = append(and.Children, f)
|
|
|
|
} else {
|
|
|
|
or.Children = append(or.Children, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == i {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if and != nil {
|
|
|
|
or.Children = append(or.Children, and)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qcode.AddFilter(sel, or)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (c *compilerContext) renderSelect(sel *qcode.Select, ti *DBTableInfo, vars Variables) error {
|
2020-01-28 06:26:53 +01:00
|
|
|
var rel *DBRel
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if sel.ParentID != -1 {
|
|
|
|
parent := c.s[sel.ParentID]
|
|
|
|
|
|
|
|
rel, err = c.schema.GetRel(ti.Name, parent.Name)
|
|
|
|
if err != nil {
|
2020-05-27 01:41:28 +02:00
|
|
|
return err
|
2020-01-28 06:26:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
childCols, err := c.initSelect(sel, ti, vars)
|
2020-01-28 06:26:53 +01:00
|
|
|
if err != nil {
|
2020-05-27 01:41:28 +02:00
|
|
|
return err
|
2020-01-28 06:26:53 +01:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
// SELECT
|
2020-04-10 08:27:43 +02:00
|
|
|
// io.WriteString(c.w, `SELECT jsonb_build_object(`)
|
2020-04-02 03:25:50 +02:00
|
|
|
// if err := c.renderColumns(sel, ti, skipped); err != nil {
|
|
|
|
// return 0, err
|
|
|
|
// }
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(c.w, `SELECT to_jsonb("__sr_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, sel.ID)
|
2020-04-18 23:42:17 +02:00
|
|
|
io.WriteString(c.w, `".*) `)
|
2020-04-10 08:27:43 +02:00
|
|
|
|
|
|
|
if sel.Paging.Type != qcode.PtOffset {
|
|
|
|
for i := range sel.OrderBy {
|
|
|
|
io.WriteString(c.w, `- '__cur_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, int32(i))
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(c.w, `' `)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, `AS "json"`)
|
2020-04-02 03:25:50 +02:00
|
|
|
|
|
|
|
if sel.Paging.Type != qcode.PtOffset {
|
|
|
|
for i := range sel.OrderBy {
|
|
|
|
io.WriteString(c.w, `, "__cur_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, int32(i))
|
2020-04-02 03:25:50 +02:00
|
|
|
io.WriteString(c.w, `"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, `FROM (SELECT `)
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
if err := c.renderColumns(sel, ti); err != nil {
|
|
|
|
return err
|
2020-02-19 05:52:44 +01:00
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if sel.Paging.Type != qcode.PtOffset {
|
|
|
|
for i, ob := range sel.OrderBy {
|
|
|
|
io.WriteString(c.w, `, LAST_VALUE(`)
|
|
|
|
colWithTableID(c.w, ti.Name, sel.ID, ob.Col)
|
|
|
|
io.WriteString(c.w, `) OVER() AS "__cur_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, int32(i))
|
2020-02-19 05:52:44 +01:00
|
|
|
io.WriteString(c.w, `"`)
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
io.WriteString(c.w, ` FROM (`)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-03-31 17:18:33 +02:00
|
|
|
// FROM (SELECT .... )
|
2020-05-27 01:41:28 +02:00
|
|
|
if err = c.renderBaseSelect(sel, ti, rel, childCols); err != nil {
|
|
|
|
return err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
//fmt.Fprintf(w, `) AS "%s_%d"`, c.sel.Name, c.sel.ID)
|
|
|
|
io.WriteString(c.w, `)`)
|
|
|
|
aliasWithID(c.w, ti.Name, sel.ID)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
// END-FROM
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
func (c *compilerContext) renderLateralJoin(sel *qcode.Select) error {
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` LEFT OUTER JOIN LATERAL (`)
|
2019-03-24 14:57:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
func (c *compilerContext) renderLateralJoinClose(sel *qcode.Select) error {
|
2020-04-02 03:25:50 +02:00
|
|
|
// io.WriteString(c.w, `) `)
|
|
|
|
// aliasWithID(c.w, "__sj", sel.ID)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` ON ('true')`)
|
2019-03-24 14:57:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-07 08:37:24 +01:00
|
|
|
func (c *compilerContext) renderJoin(sel *qcode.Select, ti *DBTableInfo) error {
|
2019-06-14 06:32:15 +02:00
|
|
|
parent := &c.s[sel.ParentID]
|
2019-11-29 07:38:23 +01:00
|
|
|
return c.renderJoinByName(ti.Name, parent.Name, parent.ID)
|
2019-11-05 05:44:42 +01:00
|
|
|
}
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
func (c *compilerContext) renderJoinByName(table, parent string, id int32) error {
|
|
|
|
rel, err := c.schema.GetRel(table, parent)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
2019-09-27 08:19:24 +02:00
|
|
|
return err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
// This join is only required for one-to-many relations since
|
|
|
|
// these make use of join tables that need to be pulled in.
|
2019-03-24 14:57:29 +01:00
|
|
|
if rel.Type != RelOneToManyThrough {
|
2019-09-27 08:19:24 +02:00
|
|
|
return err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
pt, err := c.schema.GetTable(parent)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
2019-09-27 08:19:24 +02:00
|
|
|
return err
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, ` LEFT OUTER JOIN "%s" ON (("%s"."%s") = ("%s_%d"."%s"))`,
|
2019-12-09 07:48:18 +01:00
|
|
|
//rel.Through, rel.Through, rel.ColT, c.parent.Name, c.parent.ID, rel.Left.Col)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` LEFT OUTER JOIN "`)
|
|
|
|
io.WriteString(c.w, rel.Through)
|
|
|
|
io.WriteString(c.w, `" ON ((`)
|
2019-06-08 02:53:08 +02:00
|
|
|
colWithTable(c.w, rel.Through, rel.ColT)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `) = (`)
|
2019-12-09 07:48:18 +01:00
|
|
|
colWithTableID(c.w, pt.Name, id, rel.Left.Col)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `))`)
|
2019-09-27 08:19:24 +02:00
|
|
|
|
|
|
|
return nil
|
2019-06-08 02:53:08 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (c *compilerContext) renderColumns(sel *qcode.Select, ti *DBTableInfo) error {
|
2019-10-14 08:51:36 +02:00
|
|
|
i := 0
|
2020-03-14 06:35:42 +01:00
|
|
|
var cn string
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
for _, col := range sel.Cols {
|
2020-04-23 02:51:14 +02:00
|
|
|
if n := funcPrefixLen(c.schema.fm, col.Name); n != 0 {
|
2019-11-28 07:25:46 +01:00
|
|
|
if !sel.Functions {
|
2019-10-27 06:52:48 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-03-14 06:35:42 +01:00
|
|
|
cn = col.Name[n:]
|
2019-10-27 06:52:48 +01:00
|
|
|
} else {
|
2020-03-14 06:35:42 +01:00
|
|
|
cn = col.Name
|
2020-03-16 06:40:47 +01:00
|
|
|
|
|
|
|
if strings.HasSuffix(cn, "_cursor") {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-14 06:35:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(sel.Allowed) != 0 {
|
|
|
|
if _, ok := sel.Allowed[cn]; !ok {
|
|
|
|
continue
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
if i != 0 {
|
2019-06-08 02:53:08 +02:00
|
|
|
io.WriteString(c.w, ", ")
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
colWithTableID(c.w, ti.Name, sel.ID, col.Name)
|
2020-04-02 03:25:50 +02:00
|
|
|
alias(c.w, col.FieldName)
|
2020-02-10 07:45:37 +01:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
i++
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
i += c.renderRemoteRelColumns(sel, ti, i)
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
return c.renderJoinColumns(sel, ti, i)
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
func (c *compilerContext) renderRemoteRelColumns(sel *qcode.Select, ti *DBTableInfo, colsRendered int) int {
|
|
|
|
i := colsRendered
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
for _, id := range sel.Children {
|
|
|
|
child := &c.s[id]
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-11-29 07:38:23 +01:00
|
|
|
rel, err := c.schema.GetRel(child.Name, sel.Name)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil || rel.Type != RelRemote {
|
2019-05-13 01:27:26 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
if i != 0 || len(sel.Cols) != 0 {
|
|
|
|
io.WriteString(c.w, ", ")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
colWithTableID(c.w, ti.Name, sel.ID, rel.Left.Col)
|
2020-04-02 03:25:50 +02:00
|
|
|
alias(c.w, rel.Right.Col)
|
2019-05-13 01:27:26 +02:00
|
|
|
i++
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
return i
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func (c *compilerContext) renderJoinColumns(sel *qcode.Select, ti *DBTableInfo, colsRendered int) error {
|
2020-01-21 05:38:17 +01:00
|
|
|
// columns previously rendered
|
2020-02-10 07:45:37 +01:00
|
|
|
i := colsRendered
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
for _, id := range sel.Children {
|
2020-06-05 03:55:52 +02:00
|
|
|
if hasBit(c.md.skipped, uint32(id)) {
|
2019-05-13 01:27:26 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-10-03 09:08:01 +02:00
|
|
|
childSel := &c.s[id]
|
2020-01-21 05:38:17 +01:00
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
io.WriteString(c.w, ", ")
|
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2020-03-14 06:35:42 +01:00
|
|
|
if childSel.SkipRender {
|
2020-04-02 03:25:50 +02:00
|
|
|
io.WriteString(c.w, `NULL`)
|
|
|
|
alias(c.w, childSel.FieldName)
|
2020-03-14 06:35:42 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-02 03:25:50 +02:00
|
|
|
io.WriteString(c.w, `"__sj_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, childSel.ID)
|
2020-02-10 07:45:37 +01:00
|
|
|
io.WriteString(c.w, `"."json"`)
|
2020-04-02 03:25:50 +02:00
|
|
|
alias(c.w, childSel.FieldName)
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
if childSel.Paging.Type != qcode.PtOffset {
|
2020-04-02 03:25:50 +02:00
|
|
|
io.WriteString(c.w, `, "__sj_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, childSel.ID)
|
2020-04-02 03:25:50 +02:00
|
|
|
io.WriteString(c.w, `"."cursor" AS "`)
|
|
|
|
io.WriteString(c.w, childSel.FieldName)
|
|
|
|
io.WriteString(c.w, `_cursor"`)
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
i++
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-28 06:26:53 +01:00
|
|
|
func (c *compilerContext) renderBaseSelect(sel *qcode.Select, ti *DBTableInfo, rel *DBRel,
|
2020-05-27 01:41:28 +02:00
|
|
|
childCols []*qcode.Column) error {
|
2020-01-28 06:26:53 +01:00
|
|
|
isRoot := (rel == nil)
|
2019-11-25 08:22:33 +01:00
|
|
|
isFil := (sel.Where != nil && sel.Where.Op != qcode.OpNop)
|
2020-02-10 07:45:37 +01:00
|
|
|
hasOrder := len(sel.OrderBy) != 0
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if sel.Paging.Cursor {
|
|
|
|
c.renderCursorCTE(sel)
|
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
io.WriteString(c.w, `SELECT `)
|
2020-01-19 09:12:51 +01:00
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
if len(sel.DistinctOn) != 0 {
|
|
|
|
c.renderDistinctOn(sel, ti)
|
2020-01-19 09:12:51 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
realColsRendered, isAgg, err := c.renderBaseColumns(sel, ti, childCols)
|
2020-02-10 07:45:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` FROM `)
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2020-01-28 06:26:53 +01:00
|
|
|
c.renderFrom(sel, ti, rel)
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2019-04-04 06:53:24 +02:00
|
|
|
if isRoot && isFil {
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` WHERE (`)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err := c.renderWhere(sel, ti); err != nil {
|
2019-04-04 06:53:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `)`)
|
2019-04-01 07:18:14 +02:00
|
|
|
}
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2019-04-04 06:53:24 +02:00
|
|
|
if !isRoot {
|
2019-11-07 08:37:24 +01:00
|
|
|
if err := c.renderJoin(sel, ti); err != nil {
|
2019-09-27 08:19:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-04-04 06:53:24 +02:00
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` WHERE (`)
|
2019-10-03 09:08:01 +02:00
|
|
|
if err := c.renderRelationship(sel, ti); err != nil {
|
2019-09-27 08:19:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-04-01 07:18:14 +02:00
|
|
|
if isFil {
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` AND `)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err := c.renderWhere(sel, ti); err != nil {
|
2019-03-31 17:18:33 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `)`)
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
if isAgg && len(realColsRendered) != 0 {
|
|
|
|
io.WriteString(c.w, ` GROUP BY `)
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
for i, id := range realColsRendered {
|
|
|
|
c.renderComma(i)
|
|
|
|
//fmt.Fprintf(w, `"%s"."%s"`, c.sel.Name, c.sel.Cols[id].Name)
|
|
|
|
colWithTable(c.w, ti.Name, sel.Cols[id].Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasOrder {
|
2020-02-19 05:52:44 +01:00
|
|
|
if err := c.renderOrderBy(sel, ti); err != nil {
|
2020-02-10 07:45:37 +01:00
|
|
|
return err
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
switch {
|
2020-04-20 05:48:49 +02:00
|
|
|
case ti.IsSingular:
|
2019-11-19 06:47:55 +01:00
|
|
|
io.WriteString(c.w, ` LIMIT ('1') :: integer`)
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
case len(sel.Paging.Limit) != 0:
|
|
|
|
//fmt.Fprintf(w, ` LIMIT ('%s') :: integer`, c.sel.Paging.Limit)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` LIMIT ('`)
|
|
|
|
io.WriteString(c.w, sel.Paging.Limit)
|
|
|
|
io.WriteString(c.w, `') :: integer`)
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-11-19 06:47:55 +01:00
|
|
|
case sel.Paging.NoLimit:
|
|
|
|
break
|
2019-10-14 08:51:36 +02:00
|
|
|
|
|
|
|
default:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` LIMIT ('20') :: integer`)
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if len(sel.Paging.Offset) != 0 {
|
|
|
|
//fmt.Fprintf(w, ` OFFSET ('%s') :: integer`, c.sel.Paging.Offset)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` OFFSET ('`)
|
|
|
|
io.WriteString(c.w, sel.Paging.Offset)
|
|
|
|
io.WriteString(c.w, `') :: integer`)
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 06:26:53 +01:00
|
|
|
func (c *compilerContext) renderFrom(sel *qcode.Select, ti *DBTableInfo, rel *DBRel) error {
|
|
|
|
if rel != nil && rel.Type == RelEmbedded {
|
2020-04-10 08:27:43 +02:00
|
|
|
// jsonb_to_recordset('[{"a":1,"b":[1,2,3],"c":"bar"}, {"a":2,"b":[1,2,3],"c":"bar"}]') as x(a int, b text, d text);
|
2020-01-28 06:26:53 +01:00
|
|
|
|
|
|
|
io.WriteString(c.w, `"`)
|
|
|
|
io.WriteString(c.w, rel.Left.Table)
|
|
|
|
io.WriteString(c.w, `", `)
|
|
|
|
|
|
|
|
io.WriteString(c.w, ti.Type)
|
|
|
|
io.WriteString(c.w, `_to_recordset(`)
|
|
|
|
colWithTable(c.w, rel.Left.Table, rel.Right.Col)
|
|
|
|
io.WriteString(c.w, `) AS `)
|
|
|
|
|
|
|
|
io.WriteString(c.w, `"`)
|
|
|
|
io.WriteString(c.w, ti.Name)
|
|
|
|
io.WriteString(c.w, `"`)
|
|
|
|
|
|
|
|
io.WriteString(c.w, `(`)
|
|
|
|
for i, col := range ti.Columns {
|
|
|
|
if i != 0 {
|
|
|
|
io.WriteString(c.w, `, `)
|
|
|
|
}
|
|
|
|
io.WriteString(c.w, col.Name)
|
|
|
|
io.WriteString(c.w, ` `)
|
|
|
|
io.WriteString(c.w, col.Type)
|
|
|
|
}
|
|
|
|
io.WriteString(c.w, `)`)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//fmt.Fprintf(w, ` FROM "%s"`, c.sel.Name)
|
|
|
|
io.WriteString(c.w, `"`)
|
|
|
|
io.WriteString(c.w, ti.Name)
|
|
|
|
io.WriteString(c.w, `"`)
|
|
|
|
}
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if sel.Paging.Cursor {
|
|
|
|
io.WriteString(c.w, `, "__cur"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compilerContext) renderCursorCTE(sel *qcode.Select) error {
|
|
|
|
io.WriteString(c.w, `WITH "__cur" AS (SELECT `)
|
|
|
|
for i, ob := range sel.OrderBy {
|
|
|
|
if i != 0 {
|
|
|
|
io.WriteString(c.w, `, `)
|
|
|
|
}
|
|
|
|
io.WriteString(c.w, `a[`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(c.w, int32(i+1))
|
2020-02-19 05:52:44 +01:00
|
|
|
io.WriteString(c.w, `] as `)
|
|
|
|
quoted(c.w, ob.Col)
|
|
|
|
}
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, ` FROM string_to_array(`)
|
2020-06-05 03:55:52 +02:00
|
|
|
c.md.renderValueExp(c.w, Param{Name: "cursor", Type: "json"})
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, `, ',') as a) `)
|
2020-01-28 06:26:53 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 09:08:01 +02:00
|
|
|
func (c *compilerContext) renderRelationship(sel *qcode.Select, ti *DBTableInfo) error {
|
2019-06-08 02:53:08 +02:00
|
|
|
parent := c.s[sel.ParentID]
|
2019-12-09 07:48:18 +01:00
|
|
|
|
|
|
|
pti, err := c.schema.GetTable(parent.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.renderRelationshipByName(ti.Name, pti.Name, parent.ID)
|
2019-11-05 05:44:42 +01:00
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
func (c *compilerContext) renderRelationshipByName(table, parent string, id int32) error {
|
|
|
|
rel, err := c.schema.GetRel(table, parent)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
2019-09-27 08:19:24 +02:00
|
|
|
return err
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
io.WriteString(c.w, `((`)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
switch rel.Type {
|
2019-12-25 07:24:30 +01:00
|
|
|
case RelOneToOne, RelOneToMany:
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s"."%s") = ("%s_%d"."%s"))`,
|
2019-12-09 07:48:18 +01:00
|
|
|
//c.sel.Name, rel.Left.Col, c.parent.Name, c.parent.ID, rel.Right.Col)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !rel.Left.Array && rel.Right.Array:
|
|
|
|
colWithTable(c.w, table, rel.Left.Col)
|
|
|
|
io.WriteString(c.w, `) = any (`)
|
|
|
|
colWithTableID(c.w, parent, id, rel.Right.Col)
|
|
|
|
|
|
|
|
case rel.Left.Array && !rel.Right.Array:
|
|
|
|
colWithTableID(c.w, parent, id, rel.Right.Col)
|
|
|
|
io.WriteString(c.w, `) = any (`)
|
|
|
|
colWithTable(c.w, table, rel.Left.Col)
|
|
|
|
|
|
|
|
default:
|
|
|
|
colWithTable(c.w, table, rel.Left.Col)
|
|
|
|
io.WriteString(c.w, `) = (`)
|
|
|
|
colWithTableID(c.w, parent, id, rel.Right.Col)
|
2019-11-05 05:44:42 +01:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
case RelOneToManyThrough:
|
2019-11-05 05:44:42 +01:00
|
|
|
// This requires the through table to be joined onto this select
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s"."%s") = ("%s"."%s"))`,
|
2019-12-09 07:48:18 +01:00
|
|
|
//c.sel.Name, rel.Left.Col, rel.Through, rel.Right.Col)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !rel.Left.Array && rel.Right.Array:
|
|
|
|
colWithTable(c.w, table, rel.Left.Col)
|
|
|
|
io.WriteString(c.w, `) = any (`)
|
|
|
|
colWithTable(c.w, rel.Through, rel.Right.Col)
|
|
|
|
|
|
|
|
case rel.Left.Array && !rel.Right.Array:
|
|
|
|
colWithTable(c.w, rel.Through, rel.Right.Col)
|
|
|
|
io.WriteString(c.w, `) = any (`)
|
|
|
|
colWithTable(c.w, table, rel.Left.Col)
|
|
|
|
|
|
|
|
default:
|
|
|
|
colWithTable(c.w, table, rel.Left.Col)
|
|
|
|
io.WriteString(c.w, `) = (`)
|
|
|
|
colWithTable(c.w, rel.Through, rel.Right.Col)
|
|
|
|
}
|
2020-01-28 06:26:53 +01:00
|
|
|
|
|
|
|
case RelEmbedded:
|
|
|
|
colWithTable(c.w, rel.Left.Table, rel.Left.Col)
|
|
|
|
io.WriteString(c.w, `) = (`)
|
|
|
|
colWithTableID(c.w, parent, id, rel.Left.Col)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-01-28 06:26:53 +01:00
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
io.WriteString(c.w, `))`)
|
2019-09-27 08:19:24 +02:00
|
|
|
|
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
func (c *compilerContext) renderWhere(sel *qcode.Select, ti *DBTableInfo) error {
|
2019-06-08 02:53:08 +02:00
|
|
|
if sel.Where != nil {
|
2020-01-13 15:34:15 +01:00
|
|
|
return c.renderExp(sel.Where, ti, false)
|
2019-06-08 02:53:08 +02:00
|
|
|
}
|
2020-01-13 15:34:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compilerContext) renderExp(ex *qcode.Exp, ti *DBTableInfo, skipNested bool) error {
|
|
|
|
st := util.NewStack()
|
|
|
|
st.Push(ex)
|
2019-06-08 02:53:08 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
for {
|
|
|
|
if st.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
intf := st.Pop()
|
|
|
|
|
|
|
|
switch val := intf.(type) {
|
2020-01-08 05:44:19 +01:00
|
|
|
case int32:
|
|
|
|
switch val {
|
|
|
|
case '(':
|
|
|
|
io.WriteString(c.w, `(`)
|
|
|
|
case ')':
|
|
|
|
io.WriteString(c.w, `)`)
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.ExpOp:
|
|
|
|
switch val {
|
|
|
|
case qcode.OpAnd:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` AND `)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpOr:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` OR `)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpNot:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `NOT `)
|
2019-10-26 07:34:29 +02:00
|
|
|
case qcode.OpFalse:
|
|
|
|
io.WriteString(c.w, `false`)
|
2019-03-24 14:57:29 +01:00
|
|
|
default:
|
2019-05-13 01:27:26 +02:00
|
|
|
return fmt.Errorf("11: unexpected value %v (%t)", intf, intf)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-10-03 09:08:01 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
case *qcode.Exp:
|
|
|
|
switch val.Op {
|
2019-10-26 07:34:29 +02:00
|
|
|
case qcode.OpFalse:
|
|
|
|
st.Push(val.Op)
|
|
|
|
|
2019-03-27 07:58:19 +01:00
|
|
|
case qcode.OpAnd, qcode.OpOr:
|
2020-01-08 05:44:19 +01:00
|
|
|
st.Push(')')
|
2019-03-27 07:58:19 +01:00
|
|
|
for i := len(val.Children) - 1; i >= 0; i-- {
|
|
|
|
st.Push(val.Children[i])
|
|
|
|
if i > 0 {
|
|
|
|
st.Push(val.Op)
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 05:44:19 +01:00
|
|
|
st.Push('(')
|
2019-10-03 09:08:01 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpNot:
|
|
|
|
st.Push(val.Children[0])
|
2019-03-27 07:58:19 +01:00
|
|
|
st.Push(qcode.OpNot)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-10-03 09:08:01 +02:00
|
|
|
default:
|
2020-01-13 15:34:15 +01:00
|
|
|
if !skipNested && len(val.NestedCols) != 0 {
|
2019-11-05 05:44:42 +01:00
|
|
|
io.WriteString(c.w, `EXISTS `)
|
2019-10-03 09:08:01 +02:00
|
|
|
|
2020-01-13 15:34:15 +01:00
|
|
|
if err := c.renderNestedWhere(val, ti); err != nil {
|
2019-11-05 05:44:42 +01:00
|
|
|
return err
|
2019-10-03 09:08:01 +02:00
|
|
|
}
|
2019-04-06 08:35:08 +02:00
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
} else {
|
2019-11-29 07:38:23 +01:00
|
|
|
//fmt.Fprintf(w, `(("%s"."%s") `, c.sel.Name, val.Col)
|
2020-01-13 15:34:15 +01:00
|
|
|
if err := c.renderOp(val, ti); err != nil {
|
2019-11-05 05:44:42 +01:00
|
|
|
return err
|
2019-10-03 09:08:01 +02:00
|
|
|
}
|
2019-03-27 07:58:19 +01:00
|
|
|
}
|
2019-10-03 09:08:01 +02:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
//qcode.FreeExp(val)
|
2019-06-15 07:10:53 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
default:
|
2019-05-13 01:27:26 +02:00
|
|
|
return fmt.Errorf("12: unexpected value %v (%t)", intf, intf)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-13 15:34:15 +01:00
|
|
|
func (c *compilerContext) renderNestedWhere(ex *qcode.Exp, ti *DBTableInfo) error {
|
2019-11-05 05:44:42 +01:00
|
|
|
for i := 0; i < len(ex.NestedCols)-1; i++ {
|
|
|
|
cti, err := c.schema.GetTable(ex.NestedCols[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
io.WriteString(c.w, ` AND `)
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, `(SELECT 1 FROM `)
|
|
|
|
io.WriteString(c.w, cti.Name)
|
|
|
|
|
|
|
|
if err := c.renderJoinByName(cti.Name, ti.Name, -1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, ` WHERE `)
|
|
|
|
|
|
|
|
if err := c.renderRelationshipByName(cti.Name, ti.Name, -1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-13 15:34:15 +01:00
|
|
|
io.WriteString(c.w, ` AND (`)
|
|
|
|
|
|
|
|
if err := c.renderExp(ex, cti, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, `)`)
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(ex.NestedCols)-1; i++ {
|
|
|
|
io.WriteString(c.w, `)`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-13 15:34:15 +01:00
|
|
|
func (c *compilerContext) renderOp(ex *qcode.Exp, ti *DBTableInfo) error {
|
2019-11-21 08:14:12 +01:00
|
|
|
var col *DBColumn
|
|
|
|
var ok bool
|
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
if ex.Op == qcode.OpNop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
if len(ex.Col) != 0 {
|
2019-12-09 07:48:18 +01:00
|
|
|
if col, ok = ti.ColMap[ex.Col]; !ok {
|
2019-11-21 08:14:12 +01:00
|
|
|
return fmt.Errorf("no column '%s' found ", ex.Col)
|
|
|
|
}
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
io.WriteString(c.w, `((`)
|
|
|
|
colWithTable(c.w, ti.Name, ex.Col)
|
|
|
|
io.WriteString(c.w, `) `)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ex.Op {
|
|
|
|
case qcode.OpEquals:
|
2020-02-22 21:58:37 +01:00
|
|
|
io.WriteString(c.w, `=`)
|
2019-11-05 05:44:42 +01:00
|
|
|
case qcode.OpNotEquals:
|
2020-02-22 21:58:37 +01:00
|
|
|
io.WriteString(c.w, `!=`)
|
|
|
|
case qcode.OpNotDistinct:
|
|
|
|
io.WriteString(c.w, `IS NOT DISTINCT FROM`)
|
|
|
|
case qcode.OpDistinct:
|
2020-01-08 05:44:19 +01:00
|
|
|
io.WriteString(c.w, `IS DISTINCT FROM`)
|
2019-11-05 05:44:42 +01:00
|
|
|
case qcode.OpGreaterOrEquals:
|
|
|
|
io.WriteString(c.w, `>=`)
|
|
|
|
case qcode.OpLesserOrEquals:
|
|
|
|
io.WriteString(c.w, `<=`)
|
|
|
|
case qcode.OpGreaterThan:
|
|
|
|
io.WriteString(c.w, `>`)
|
|
|
|
case qcode.OpLesserThan:
|
|
|
|
io.WriteString(c.w, `<`)
|
|
|
|
case qcode.OpIn:
|
2020-05-24 23:43:54 +02:00
|
|
|
io.WriteString(c.w, `= ANY`)
|
2019-11-05 05:44:42 +01:00
|
|
|
case qcode.OpNotIn:
|
2020-05-24 23:43:54 +02:00
|
|
|
io.WriteString(c.w, `!= ANY`)
|
2019-11-05 05:44:42 +01:00
|
|
|
case qcode.OpLike:
|
|
|
|
io.WriteString(c.w, `LIKE`)
|
|
|
|
case qcode.OpNotLike:
|
|
|
|
io.WriteString(c.w, `NOT LIKE`)
|
|
|
|
case qcode.OpILike:
|
|
|
|
io.WriteString(c.w, `ILIKE`)
|
|
|
|
case qcode.OpNotILike:
|
|
|
|
io.WriteString(c.w, `NOT ILIKE`)
|
|
|
|
case qcode.OpSimilar:
|
|
|
|
io.WriteString(c.w, `SIMILAR TO`)
|
|
|
|
case qcode.OpNotSimilar:
|
|
|
|
io.WriteString(c.w, `NOT SIMILAR TO`)
|
|
|
|
case qcode.OpContains:
|
|
|
|
io.WriteString(c.w, `@>`)
|
|
|
|
case qcode.OpContainedIn:
|
|
|
|
io.WriteString(c.w, `<@`)
|
|
|
|
case qcode.OpHasKey:
|
|
|
|
io.WriteString(c.w, `?`)
|
|
|
|
case qcode.OpHasKeyAny:
|
|
|
|
io.WriteString(c.w, `?|`)
|
|
|
|
case qcode.OpHasKeyAll:
|
|
|
|
io.WriteString(c.w, `?&`)
|
|
|
|
case qcode.OpIsNull:
|
|
|
|
if strings.EqualFold(ex.Val, "true") {
|
|
|
|
io.WriteString(c.w, `IS NULL)`)
|
|
|
|
} else {
|
|
|
|
io.WriteString(c.w, `IS NOT NULL)`)
|
|
|
|
}
|
|
|
|
return nil
|
2019-12-02 16:52:22 +01:00
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
case qcode.OpEqID:
|
2019-12-09 07:48:18 +01:00
|
|
|
if ti.PrimaryCol == nil {
|
2019-11-05 05:44:42 +01:00
|
|
|
return fmt.Errorf("no primary key column defined for %s", ti.Name)
|
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
col = ti.PrimaryCol
|
2019-11-05 05:44:42 +01:00
|
|
|
//fmt.Fprintf(w, `(("%s") =`, c.ti.PrimaryCol)
|
|
|
|
io.WriteString(c.w, `((`)
|
2019-12-09 07:48:18 +01:00
|
|
|
colWithTable(c.w, ti.Name, ti.PrimaryCol.Name)
|
2019-11-05 05:44:42 +01:00
|
|
|
//io.WriteString(c.w, ti.PrimaryCol)
|
|
|
|
io.WriteString(c.w, `) =`)
|
2019-12-02 16:52:22 +01:00
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
case qcode.OpTsQuery:
|
2019-12-09 07:48:18 +01:00
|
|
|
if ti.PrimaryCol == nil {
|
2019-11-05 05:44:42 +01:00
|
|
|
return fmt.Errorf("no tsv column defined for %s", ti.Name)
|
|
|
|
}
|
2019-12-02 16:52:22 +01:00
|
|
|
//fmt.Fprintf(w, `(("%s") @@ websearch_to_tsquery('%s'))`, c.ti.TSVCol, val.Val)
|
|
|
|
io.WriteString(c.w, `((`)
|
2019-12-09 07:48:18 +01:00
|
|
|
colWithTable(c.w, ti.Name, ti.TSVCol.Name)
|
2019-12-02 16:52:22 +01:00
|
|
|
if c.schema.ver >= 110000 {
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, `) @@ websearch_to_tsquery(`)
|
2019-12-02 16:52:22 +01:00
|
|
|
} else {
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, `) @@ to_tsquery(`)
|
2019-12-02 16:52:22 +01:00
|
|
|
}
|
2020-06-05 03:55:52 +02:00
|
|
|
c.md.renderValueExp(c.w, Param{Name: ex.Val, Type: "string"})
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, `))`)
|
|
|
|
|
2019-11-05 05:44:42 +01:00
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("[Where] unexpected op code %d", ex.Op)
|
|
|
|
}
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
switch {
|
|
|
|
case ex.Type == qcode.ValList:
|
2019-11-05 05:44:42 +01:00
|
|
|
c.renderList(ex)
|
2020-02-19 05:52:44 +01:00
|
|
|
case col == nil:
|
2020-01-13 15:34:15 +01:00
|
|
|
return errors.New("no column found for expression value")
|
2020-02-19 05:52:44 +01:00
|
|
|
default:
|
2019-11-21 08:14:12 +01:00
|
|
|
c.renderVal(ex, c.vars, col)
|
2019-11-05 05:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(c.w, `)`)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 09:08:01 +02:00
|
|
|
func (c *compilerContext) renderOrderBy(sel *qcode.Select, ti *DBTableInfo) error {
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` ORDER BY `)
|
2019-03-24 14:57:29 +01:00
|
|
|
for i := range sel.OrderBy {
|
2019-05-13 01:27:26 +02:00
|
|
|
if i != 0 {
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `, `)
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
ob := sel.OrderBy[i]
|
2020-02-10 07:45:37 +01:00
|
|
|
colWithTable(c.w, ti.Name, ob.Col)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
switch ob.Order {
|
|
|
|
case qcode.OrderAsc:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` ASC`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderDesc:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` DESC`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderAscNullsFirst:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` ASC NULLS FIRST`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderDescNullsFirst:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` DESC NULLLS FIRST`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderAscNullsLast:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` ASC NULLS LAST`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderDescNullsLast:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ` DESC NULLS LAST`)
|
2019-03-24 14:57:29 +01:00
|
|
|
default:
|
2019-05-13 06:05:08 +02:00
|
|
|
return fmt.Errorf("13: unexpected value %v", ob.Order)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 09:08:01 +02:00
|
|
|
func (c *compilerContext) renderDistinctOn(sel *qcode.Select, ti *DBTableInfo) {
|
2019-06-08 02:53:08 +02:00
|
|
|
io.WriteString(c.w, `DISTINCT ON (`)
|
|
|
|
for i := range sel.DistinctOn {
|
2019-05-13 01:27:26 +02:00
|
|
|
if i != 0 {
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `, `)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
colWithTable(c.w, ti.Name, sel.DistinctOn[i])
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `) `)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderList(ex *qcode.Exp) {
|
|
|
|
io.WriteString(c.w, ` (`)
|
2019-03-24 14:57:29 +01:00
|
|
|
for i := range ex.ListVal {
|
2019-05-13 01:27:26 +02:00
|
|
|
if i != 0 {
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `, `)
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
switch ex.ListType {
|
|
|
|
case qcode.ValBool, qcode.ValInt, qcode.ValFloat:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, ex.ListVal[i])
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.ValStr:
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `'`)
|
|
|
|
io.WriteString(c.w, ex.ListVal[i])
|
|
|
|
io.WriteString(c.w, `'`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(c.w, `)`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 08:14:12 +01:00
|
|
|
func (c *compilerContext) renderVal(ex *qcode.Exp, vars map[string]string, col *DBColumn) {
|
2020-02-19 05:52:44 +01:00
|
|
|
io.WriteString(c.w, ` `)
|
2019-10-03 09:08:01 +02:00
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
switch ex.Type {
|
|
|
|
case qcode.ValVar:
|
2020-02-29 06:05:35 +01:00
|
|
|
val, ok := vars[ex.Val]
|
|
|
|
switch {
|
|
|
|
case ok && strings.HasPrefix(val, "sql:"):
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, `(`)
|
2020-06-05 03:55:52 +02:00
|
|
|
c.md.RenderVar(c.w, val[4:])
|
2020-02-29 06:05:35 +01:00
|
|
|
io.WriteString(c.w, `)`)
|
2020-05-27 01:41:28 +02:00
|
|
|
|
2020-02-29 06:05:35 +01:00
|
|
|
case ok:
|
2020-02-19 05:52:44 +01:00
|
|
|
squoted(c.w, val)
|
2020-05-27 01:41:28 +02:00
|
|
|
|
2020-05-24 23:43:54 +02:00
|
|
|
case ex.Op == qcode.OpIn || ex.Op == qcode.OpNotIn:
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, `(ARRAY(SELECT json_array_elements_text(`)
|
2020-06-05 03:55:52 +02:00
|
|
|
c.md.renderValueExp(c.w, Param{Name: ex.Val, Type: col.Type, IsArray: true})
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(c.w, `))`)
|
2020-05-24 23:43:54 +02:00
|
|
|
|
|
|
|
io.WriteString(c.w, ` :: `)
|
|
|
|
io.WriteString(c.w, col.Type)
|
|
|
|
io.WriteString(c.w, `[])`)
|
|
|
|
return
|
|
|
|
|
2020-02-29 06:05:35 +01:00
|
|
|
default:
|
2020-06-05 03:55:52 +02:00
|
|
|
c.md.renderValueExp(c.w, Param{Name: ex.Val, Type: col.Type, IsArray: false})
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-02-19 05:52:44 +01:00
|
|
|
|
|
|
|
case qcode.ValRef:
|
|
|
|
colWithTable(c.w, ex.Table, ex.Col)
|
|
|
|
|
|
|
|
default:
|
|
|
|
squoted(c.w, ex.Val)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
io.WriteString(c.w, ` :: `)
|
2020-02-10 07:45:37 +01:00
|
|
|
io.WriteString(c.w, col.Type)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2020-04-23 02:51:14 +02:00
|
|
|
func funcPrefixLen(fm map[string]*DBFunction, fn string) int {
|
2019-03-31 17:18:33 +02:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(fn, "avg_"):
|
|
|
|
return 4
|
|
|
|
case strings.HasPrefix(fn, "count_"):
|
|
|
|
return 6
|
|
|
|
case strings.HasPrefix(fn, "max_"):
|
|
|
|
return 4
|
|
|
|
case strings.HasPrefix(fn, "min_"):
|
|
|
|
return 4
|
|
|
|
case strings.HasPrefix(fn, "sum_"):
|
|
|
|
return 4
|
|
|
|
case strings.HasPrefix(fn, "stddev_"):
|
|
|
|
return 7
|
|
|
|
case strings.HasPrefix(fn, "stddev_pop_"):
|
|
|
|
return 11
|
|
|
|
case strings.HasPrefix(fn, "stddev_samp_"):
|
|
|
|
return 12
|
|
|
|
case strings.HasPrefix(fn, "variance_"):
|
|
|
|
return 9
|
|
|
|
case strings.HasPrefix(fn, "var_pop_"):
|
|
|
|
return 8
|
|
|
|
case strings.HasPrefix(fn, "var_samp_"):
|
|
|
|
return 9
|
|
|
|
}
|
2020-04-23 02:51:14 +02:00
|
|
|
fnLen := len(fn)
|
|
|
|
|
|
|
|
for k := range fm {
|
|
|
|
kLen := len(k)
|
|
|
|
if kLen < fnLen && k[0] == fn[0] && strings.HasPrefix(fn, k) && fn[kLen] == '_' {
|
|
|
|
return kLen + 1
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 17:18:33 +02:00
|
|
|
return 0
|
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func hasBit(n uint32, pos uint32) bool {
|
2019-05-13 01:27:26 +02:00
|
|
|
val := n & (1 << pos)
|
|
|
|
return (val > 0)
|
|
|
|
}
|
2019-06-01 08:03:09 +02:00
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
func alias(w io.Writer, alias string) {
|
|
|
|
io.WriteString(w, ` AS "`)
|
|
|
|
io.WriteString(w, alias)
|
|
|
|
io.WriteString(w, `"`)
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
func aliasWithID(w io.Writer, alias string, id int32) {
|
|
|
|
io.WriteString(w, ` AS "`)
|
|
|
|
io.WriteString(w, alias)
|
|
|
|
io.WriteString(w, `_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(w, id)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, `"`)
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
func colWithTable(w io.Writer, table, col string) {
|
|
|
|
io.WriteString(w, `"`)
|
|
|
|
io.WriteString(w, table)
|
|
|
|
io.WriteString(w, `"."`)
|
|
|
|
io.WriteString(w, col)
|
|
|
|
io.WriteString(w, `"`)
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
func colWithTableID(w io.Writer, table string, id int32, col string) {
|
|
|
|
io.WriteString(w, `"`)
|
|
|
|
io.WriteString(w, table)
|
2019-12-09 07:48:18 +01:00
|
|
|
if id >= 0 {
|
|
|
|
io.WriteString(w, `_`)
|
2020-05-27 01:41:28 +02:00
|
|
|
int32String(w, id)
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, `"."`)
|
|
|
|
io.WriteString(w, col)
|
|
|
|
io.WriteString(w, `"`)
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
func quoted(w io.Writer, identifier string) {
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, `"`)
|
2020-02-10 07:45:37 +01:00
|
|
|
io.WriteString(w, identifier)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, `"`)
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
func squoted(w io.Writer, identifier string) {
|
|
|
|
io.WriteString(w, `'`)
|
|
|
|
io.WriteString(w, identifier)
|
|
|
|
io.WriteString(w, `'`)
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
func int32String(w io.Writer, val int32) {
|
2020-05-29 08:23:54 +02:00
|
|
|
io.WriteString(w, strconv.FormatInt(int64(val), 10))
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|