2019-03-24 14:57:29 +01:00
|
|
|
package psql
|
|
|
|
|
|
|
|
import (
|
2019-06-01 08:03:09 +02:00
|
|
|
"bytes"
|
2019-03-24 14:57:29 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-06-01 08:03:09 +02:00
|
|
|
"math"
|
2019-03-27 07:58:19 +01:00
|
|
|
"strings"
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
"github.com/dosco/super-graph/qcode"
|
|
|
|
"github.com/dosco/super-graph/util"
|
|
|
|
)
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
const (
|
2019-06-08 02:53:08 +02:00
|
|
|
empty = ""
|
|
|
|
closeBlock = 500
|
2019-05-13 01:27:26 +02:00
|
|
|
)
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
type Config struct {
|
2019-04-09 03:24:29 +02:00
|
|
|
Schema *DBSchema
|
|
|
|
Vars map[string]string
|
|
|
|
TableMap map[string]string
|
2019-04-08 08:47:59 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
type Compiler struct {
|
|
|
|
schema *DBSchema
|
2019-04-08 08:47:59 +02:00
|
|
|
vars map[string]string
|
2019-04-09 03:24:29 +02:00
|
|
|
tmap map[string]string
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
func NewCompiler(conf Config) *Compiler {
|
2019-04-09 03:24:29 +02:00
|
|
|
return &Compiler{conf.Schema, conf.Vars, conf.TableMap}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 04:32:12 +02:00
|
|
|
func (c *Compiler) AddRelationship(key uint64, val *DBRel) {
|
2019-05-13 01:27:26 +02:00
|
|
|
c.schema.RelMap[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) IDColumn(table string) string {
|
|
|
|
t, ok := c.schema.Tables[table]
|
|
|
|
if !ok {
|
|
|
|
return empty
|
|
|
|
}
|
|
|
|
return t.PrimaryCol
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
type compilerContext struct {
|
|
|
|
w *bytes.Buffer
|
|
|
|
s []qcode.Select
|
|
|
|
*Compiler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *Compiler) CompileEx(qc *qcode.QCode) (uint32, []byte, error) {
|
2019-06-02 01:48:42 +02:00
|
|
|
w := &bytes.Buffer{}
|
2019-06-08 02:53:08 +02:00
|
|
|
skipped, err := co.Compile(qc, w)
|
2019-06-02 01:48:42 +02:00
|
|
|
return skipped, w.Bytes(), err
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (co *Compiler) Compile(qc *qcode.QCode, w *bytes.Buffer) (uint32, error) {
|
2019-05-13 01:27:26 +02:00
|
|
|
if len(qc.Query.Selects) == 0 {
|
2019-06-02 01:48:42 +02:00
|
|
|
return 0, errors.New("empty query")
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
c := &compilerContext{w, qc.Query.Selects, co}
|
|
|
|
root := &qc.Query.Selects[0]
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
st := NewStack()
|
|
|
|
st.Push(root.ID + closeBlock)
|
|
|
|
st.Push(root.ID)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `SELECT json_object_agg('%s', %s) FROM (`,
|
|
|
|
//root.FieldName, root.Table)
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`SELECT json_object_agg('`)
|
|
|
|
c.w.WriteString(root.FieldName)
|
|
|
|
c.w.WriteString(`', `)
|
|
|
|
c.w.WriteString(root.Table)
|
|
|
|
c.w.WriteString(`) FROM (`)
|
2019-05-13 01:27:26 +02:00
|
|
|
|
|
|
|
var ignored uint32
|
2019-06-08 02:53:08 +02:00
|
|
|
var err error
|
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]
|
|
|
|
|
|
|
|
if sel.ID != 0 {
|
|
|
|
if err = c.renderJoin(sel); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skipped, err := c.renderSelect(sel)
|
2019-05-13 01:27:26 +02:00
|
|
|
if err != nil {
|
2019-06-02 01:48:42 +02:00
|
|
|
return 0, err
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
ignored |= skipped
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
for _, cid := range sel.Children {
|
|
|
|
if hasBit(skipped, uint32(cid)) {
|
2019-05-13 01:27:26 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
child := &c.s[cid]
|
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)]
|
|
|
|
err = c.renderSelectClose(sel)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if sel.ID != 0 {
|
|
|
|
if err = c.renderJoinClose(sel); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`)`)
|
|
|
|
alias(c.w, `done_1337`)
|
|
|
|
c.w.WriteString(`;`)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-02 01:48:42 +02:00
|
|
|
return ignored, nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) getTable(sel *qcode.Select) (
|
|
|
|
*DBTableInfo, error) {
|
2019-04-09 03:24:29 +02:00
|
|
|
if tn, ok := c.tmap[sel.Table]; ok {
|
|
|
|
return c.schema.GetTable(tn)
|
|
|
|
}
|
2019-06-04 16:54:51 +02:00
|
|
|
|
2019-04-09 03:24:29 +02:00
|
|
|
return c.schema.GetTable(sel.Table)
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) processChildren(sel *qcode.Select) (uint32, []*qcode.Column) {
|
2019-05-13 01:27:26 +02:00
|
|
|
var skipped uint32
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-06-08 02:53:08 +02:00
|
|
|
rel, ok := c.schema.RelMap[child.RelID]
|
2019-03-24 14:57:29 +01:00
|
|
|
if !ok {
|
2019-05-13 01:27:26 +02:00
|
|
|
skipped |= (1 << uint(id))
|
2019-03-24 14:57:29 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
switch rel.Type {
|
|
|
|
case RelOneToMany:
|
|
|
|
fallthrough
|
|
|
|
case RelBelongTo:
|
2019-03-24 14:57:29 +01:00
|
|
|
if _, ok := colmap[rel.Col2]; !ok {
|
2019-06-08 02:53:08 +02:00
|
|
|
cols = append(cols, &qcode.Column{sel.Table, rel.Col2, rel.Col2})
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
case RelOneToManyThrough:
|
2019-03-24 14:57:29 +01:00
|
|
|
if _, ok := colmap[rel.Col1]; !ok {
|
2019-06-08 02:53:08 +02:00
|
|
|
cols = append(cols, &qcode.Column{sel.Table, rel.Col1, rel.Col1})
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
case RelRemote:
|
|
|
|
if _, ok := colmap[rel.Col1]; !ok {
|
2019-06-08 02:53:08 +02:00
|
|
|
cols = append(cols, &qcode.Column{sel.Table, rel.Col1, rel.Col2})
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
skipped |= (1 << uint(id))
|
|
|
|
|
|
|
|
default:
|
|
|
|
skipped |= (1 << uint(id))
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
return skipped, cols
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderSelect(sel *qcode.Select) (uint32, error) {
|
|
|
|
skipped, childCols := c.processChildren(sel)
|
|
|
|
hasOrder := len(sel.OrderBy) != 0
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
// SELECT
|
2019-06-08 02:53:08 +02:00
|
|
|
if sel.AsList {
|
|
|
|
//fmt.Fprintf(w, `SELECT coalesce(json_agg("%s"`, c.sel.Table)
|
|
|
|
c.w.WriteString(`SELECT coalesce(json_agg("`)
|
|
|
|
c.w.WriteString(sel.Table)
|
|
|
|
c.w.WriteString(`"`)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
if hasOrder {
|
2019-06-08 02:53:08 +02:00
|
|
|
err := c.renderOrderBy(sel)
|
2019-03-24 14:57:29 +01:00
|
|
|
if err != nil {
|
2019-05-13 01:27:26 +02:00
|
|
|
return skipped, err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `), '[]') AS "%s" FROM (`, c.sel.Table)
|
|
|
|
c.w.WriteString(`), '[]')`)
|
|
|
|
alias(c.w, sel.Table)
|
|
|
|
c.w.WriteString(` FROM (`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ROW-TO-JSON
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`SELECT `)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if len(sel.DistinctOn) != 0 {
|
|
|
|
c.renderDistinctOn(sel)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`row_to_json((`)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `SELECT "sel_%d" FROM (SELECT `, c.sel.ID)
|
|
|
|
c.w.WriteString(`SELECT "sel_`)
|
|
|
|
int2string(c.w, sel.ID)
|
|
|
|
c.w.WriteString(`" FROM (SELECT `)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
// Combined column names
|
2019-06-08 02:53:08 +02:00
|
|
|
c.renderColumns(sel)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
c.renderRemoteRelColumns(sel)
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
err := c.renderJoinedColumns(sel, skipped)
|
2019-03-24 14:57:29 +01:00
|
|
|
if err != nil {
|
2019-05-13 01:27:26 +02:00
|
|
|
return skipped, err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `) AS "sel_%d"`, c.sel.ID)
|
|
|
|
c.w.WriteString(`)`)
|
|
|
|
aliasWithID(c.w, "sel", sel.ID)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `)) AS "%s"`, c.sel.Table)
|
|
|
|
c.w.WriteString(`))`)
|
|
|
|
alias(c.w, sel.Table)
|
2019-03-24 14:57:29 +01:00
|
|
|
// END-ROW-TO-JSON
|
|
|
|
|
|
|
|
if hasOrder {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.renderOrderByColumns(sel)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
// END-SELECT
|
|
|
|
|
2019-03-31 17:18:33 +02:00
|
|
|
// FROM (SELECT .... )
|
2019-06-08 02:53:08 +02:00
|
|
|
err = c.renderBaseSelect(sel, childCols, skipped)
|
2019-03-31 17:18:33 +02:00
|
|
|
if err != nil {
|
2019-05-13 01:27:26 +02:00
|
|
|
return skipped, err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
// END-FROM
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
return skipped, nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderSelectClose(sel *qcode.Select) error {
|
|
|
|
hasOrder := len(sel.OrderBy) != 0
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
if hasOrder {
|
2019-06-08 02:53:08 +02:00
|
|
|
err := c.renderOrderBy(sel)
|
2019-03-24 14:57:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if len(sel.Paging.Limit) != 0 {
|
|
|
|
//fmt.Fprintf(w, ` LIMIT ('%s') :: integer`, c.sel.Paging.Limit)
|
|
|
|
c.w.WriteString(` LIMIT ('`)
|
|
|
|
c.w.WriteString(sel.Paging.Limit)
|
|
|
|
c.w.WriteString(`') :: integer`)
|
2019-03-24 14:57:29 +01:00
|
|
|
} else {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` LIMIT ('20') :: integer`)
|
2019-03-24 14:57:29 +01: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)
|
|
|
|
c.w.WriteString(`OFFSET ('`)
|
|
|
|
c.w.WriteString(sel.Paging.Offset)
|
|
|
|
c.w.WriteString(`') :: integer`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if sel.AsList {
|
|
|
|
//fmt.Fprintf(w, `) AS "%s_%d"`, c.sel.Table, c.sel.ID)
|
|
|
|
c.w.WriteString(`)`)
|
|
|
|
aliasWithID(c.w, sel.Table, sel.ID)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderJoin(sel *qcode.Select) error {
|
|
|
|
c.w.WriteString(` LEFT OUTER JOIN LATERAL (`)
|
2019-03-24 14:57:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderJoinClose(sel *qcode.Select) error {
|
|
|
|
//fmt.Fprintf(w, `) AS "%s_%d_join" ON ('true')`, c.sel.Table, c.sel.ID)
|
|
|
|
c.w.WriteString(`)`)
|
|
|
|
aliasWithIDSuffix(c.w, sel.Table, sel.ID, "_join")
|
|
|
|
c.w.WriteString(` ON ('true')`)
|
2019-03-24 14:57:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderJoinTable(sel *qcode.Select) {
|
|
|
|
rel, ok := c.schema.RelMap[sel.RelID]
|
2019-03-24 14:57:29 +01:00
|
|
|
if !ok {
|
|
|
|
panic(errors.New("no relationship found"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rel.Type != RelOneToManyThrough {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
parent := &c.s[sel.ParentID]
|
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, ` LEFT OUTER JOIN "%s" ON (("%s"."%s") = ("%s_%d"."%s"))`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//rel.Through, rel.Through, rel.ColT, c.parent.Table, c.parent.ID, rel.Col1)
|
|
|
|
c.w.WriteString(` LEFT OUTER JOIN "`)
|
|
|
|
c.w.WriteString(rel.Through)
|
|
|
|
c.w.WriteString(`" ON ((`)
|
|
|
|
colWithTable(c.w, rel.Through, rel.ColT)
|
|
|
|
c.w.WriteString(`) = (`)
|
|
|
|
colWithTableID(c.w, parent.Table, parent.ID, rel.Col1)
|
|
|
|
c.w.WriteString(`))`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *compilerContext) renderColumns(sel *qcode.Select) {
|
|
|
|
for i, col := range sel.Cols {
|
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
|
|
|
}
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d"."%s" AS "%s"`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//c.sel.Table, c.sel.ID, col.Name, col.FieldName)
|
|
|
|
colWithTableIDAlias(c.w, sel.Table, sel.ID, col.Name, col.FieldName)
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderRemoteRelColumns(sel *qcode.Select) {
|
2019-05-13 01:27:26 +02:00
|
|
|
i := 0
|
|
|
|
|
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-06-08 02:53:08 +02:00
|
|
|
rel, ok := c.schema.RelMap[child.RelID]
|
2019-05-13 01:27:26 +02:00
|
|
|
if !ok || rel.Type != RelRemote {
|
|
|
|
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
|
|
|
}
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d"."%s" AS "%s"`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//c.sel.Table, c.sel.ID, rel.Col1, rel.Col2)
|
|
|
|
colWithTableID(c.w, sel.Table, sel.ID, rel.Col1)
|
|
|
|
alias(c.w, rel.Col2)
|
2019-05-13 01:27:26 +02:00
|
|
|
i++
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderJoinedColumns(sel *qcode.Select, skipped uint32) error {
|
|
|
|
colsRendered := len(sel.Cols) != 0
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
for _, id := range sel.Children {
|
|
|
|
skipThis := hasBit(skipped, uint32(id))
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
if colsRendered && !skipThis {
|
2019-06-08 02:53:08 +02:00
|
|
|
io.WriteString(c.w, ", ")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
if skipThis {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
sel := &c.s[id]
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d_join"."%s" AS "%s"`,
|
|
|
|
//s.Table, s.ID, s.Table, s.FieldName)
|
2019-06-08 02:53:08 +02:00
|
|
|
colWithTableIDSuffixAlias(c.w, sel.Table, sel.ID, "_join", sel.Table, sel.FieldName)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderBaseSelect(sel *qcode.Select,
|
|
|
|
childCols []*qcode.Column, skipped uint32) error {
|
2019-03-31 17:18:33 +02:00
|
|
|
var groupBy []int
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
ti, err := c.getTable(sel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
isRoot := sel.ID == 0
|
|
|
|
isFil := sel.Where != nil
|
|
|
|
isSearch := sel.Args["search"] != nil
|
2019-04-01 07:18:14 +02:00
|
|
|
isAgg := false
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` FROM (SELECT `)
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
for i, col := range sel.Cols {
|
2019-03-31 17:18:33 +02:00
|
|
|
cn := col.Name
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
_, isRealCol := ti.Columns[cn]
|
2019-04-07 07:12:11 +02:00
|
|
|
|
2019-04-09 03:24:29 +02:00
|
|
|
if !isRealCol {
|
|
|
|
if isSearch {
|
|
|
|
switch {
|
|
|
|
case cn == "search_rank":
|
2019-06-08 02:53:08 +02:00
|
|
|
cn = ti.TSVCol
|
|
|
|
arg := sel.Args["search"]
|
2019-04-06 08:35:08 +02:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `ts_rank("%s"."%s", to_tsquery('%s')) AS %s`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//c.sel.Table, cn, arg.Val, col.Name)
|
|
|
|
c.w.WriteString(`ts_rank(`)
|
|
|
|
colWithTable(c.w, sel.Table, cn)
|
|
|
|
c.w.WriteString(`, to_tsquery('`)
|
|
|
|
c.w.WriteString(arg.Val)
|
|
|
|
c.w.WriteString(`')`)
|
|
|
|
alias(c.w, col.Name)
|
2019-04-07 07:12:11 +02:00
|
|
|
|
2019-04-09 03:24:29 +02:00
|
|
|
case strings.HasPrefix(cn, "search_headline_"):
|
|
|
|
cn = cn[16:]
|
2019-06-08 02:53:08 +02:00
|
|
|
arg := sel.Args["search"]
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `ts_headline("%s"."%s", to_tsquery('%s')) AS %s`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//c.sel.Table, cn, arg.Val, col.Name)
|
|
|
|
c.w.WriteString(`ts_headlinek(`)
|
|
|
|
colWithTable(c.w, sel.Table, cn)
|
|
|
|
c.w.WriteString(`, to_tsquery('`)
|
|
|
|
c.w.WriteString(arg.Val)
|
|
|
|
c.w.WriteString(`')`)
|
|
|
|
alias(c.w, col.Name)
|
2019-04-09 03:24:29 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-06 08:35:08 +02:00
|
|
|
pl := funcPrefixLen(cn)
|
|
|
|
if pl == 0 {
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `'%s not defined' AS %s`, cn, col.Name)
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`'`)
|
|
|
|
c.w.WriteString(cn)
|
|
|
|
c.w.WriteString(` not defined'`)
|
|
|
|
alias(c.w, col.Name)
|
2019-04-06 08:35:08 +02:00
|
|
|
} else {
|
|
|
|
isAgg = true
|
|
|
|
fn := cn[0 : pl-1]
|
|
|
|
cn := cn[pl:]
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `%s("%s"."%s") AS %s`, fn, c.sel.Table, cn, col.Name)
|
|
|
|
c.w.WriteString(fn)
|
|
|
|
c.w.WriteString(`(`)
|
|
|
|
colWithTable(c.w, sel.Table, cn)
|
|
|
|
c.w.WriteString(`)`)
|
|
|
|
alias(c.w, col.Name)
|
2019-04-06 08:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
} else {
|
2019-03-31 17:18:33 +02:00
|
|
|
groupBy = append(groupBy, i)
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `"%s"."%s"`, c.sel.Table, cn)
|
|
|
|
colWithTable(c.w, sel.Table, cn)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if i < len(sel.Cols)-1 || len(childCols) != 0 {
|
2019-06-01 08:03:09 +02:00
|
|
|
//io.WriteString(w, ", ")
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`, `)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-31 17:18:33 +02:00
|
|
|
|
|
|
|
for i, col := range childCols {
|
2019-05-13 01:27:26 +02:00
|
|
|
if i != 0 {
|
2019-06-01 08:03:09 +02:00
|
|
|
//io.WriteString(w, ", ")
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`, `)
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s"."%s"`, col.Table, col.Name)
|
2019-06-08 02:53:08 +02:00
|
|
|
colWithTable(c.w, col.Table, col.Name)
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` FROM `)
|
|
|
|
if tn, ok := c.tmap[sel.Table]; ok {
|
|
|
|
//fmt.Fprintf(w, ` FROM "%s" AS "%s"`, tn, c.sel.Table)
|
|
|
|
colWithAlias(c.w, tn, sel.Table)
|
2019-04-09 03:24:29 +02:00
|
|
|
} else {
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, ` FROM "%s"`, c.sel.Table)
|
|
|
|
c.w.WriteString(`"`)
|
|
|
|
c.w.WriteString(sel.Table)
|
|
|
|
c.w.WriteString(`"`)
|
2019-04-09 03:24:29 +02:00
|
|
|
}
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2019-04-04 06:53:24 +02:00
|
|
|
if isRoot && isFil {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` WHERE (`)
|
|
|
|
if err := c.renderWhere(sel); err != nil {
|
2019-04-04 06:53:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`)`)
|
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-06-08 02:53:08 +02:00
|
|
|
c.renderJoinTable(sel)
|
2019-04-04 06:53:24 +02:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` WHERE (`)
|
|
|
|
c.renderRelationship(sel)
|
2019-04-04 06:53:24 +02:00
|
|
|
|
2019-04-01 07:18:14 +02:00
|
|
|
if isFil {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` AND `)
|
|
|
|
if err := c.renderWhere(sel); err != nil {
|
2019-03-31 17:18:33 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`)`)
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 06:53:24 +02:00
|
|
|
if isAgg {
|
|
|
|
if len(groupBy) != 0 {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` GROUP BY `)
|
2019-03-31 17:18:33 +02:00
|
|
|
|
2019-04-04 06:53:24 +02:00
|
|
|
for i, id := range groupBy {
|
2019-05-13 01:27:26 +02:00
|
|
|
if i != 0 {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`, `)
|
2019-04-04 06:53:24 +02:00
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `"%s"."%s"`, c.sel.Table, c.sel.Cols[id].Name)
|
|
|
|
colWithTable(c.w, sel.Table, sel.Cols[id].Name)
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if len(sel.Paging.Limit) != 0 {
|
|
|
|
//fmt.Fprintf(w, ` LIMIT ('%s') :: integer`, c.sel.Paging.Limit)
|
|
|
|
c.w.WriteString(` LIMIT ('`)
|
|
|
|
c.w.WriteString(sel.Paging.Limit)
|
|
|
|
c.w.WriteString(`') :: integer`)
|
2019-03-31 17:18:33 +02:00
|
|
|
} else {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` 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)
|
|
|
|
c.w.WriteString(` OFFSET ('`)
|
|
|
|
c.w.WriteString(sel.Paging.Offset)
|
|
|
|
c.w.WriteString(`') :: integer`)
|
2019-03-31 17:18:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `) AS "%s_%d"`, c.sel.Table, c.sel.ID)
|
|
|
|
c.w.WriteString(`)`)
|
|
|
|
aliasWithID(c.w, sel.Table, sel.ID)
|
2019-03-31 17:18:33 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderOrderByColumns(sel *qcode.Select) {
|
|
|
|
colsRendered := len(sel.Cols) != 0
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
for i := range sel.OrderBy {
|
2019-05-13 01:27:26 +02:00
|
|
|
if colsRendered {
|
2019-06-01 08:03:09 +02:00
|
|
|
//io.WriteString(w, ", ")
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`, `)
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
col := sel.OrderBy[i].Col
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d"."%s" AS "%s_%d_%s_ob"`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//c.sel.Table, c.sel.ID, c,
|
|
|
|
//c.sel.Table, c.sel.ID, c)
|
|
|
|
colWithTableID(c.w, sel.Table, sel.ID, col)
|
|
|
|
c.w.WriteString(` AS `)
|
|
|
|
tableIDColSuffix(c.w, sel.Table, sel.ID, col, "_ob")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderRelationship(sel *qcode.Select) {
|
|
|
|
rel, ok := c.schema.RelMap[sel.RelID]
|
2019-03-24 14:57:29 +01:00
|
|
|
if !ok {
|
|
|
|
panic(errors.New("no relationship found"))
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
parent := c.s[sel.ParentID]
|
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
switch rel.Type {
|
|
|
|
case RelBelongTo:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s"."%s") = ("%s_%d"."%s"))`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//c.sel.Table, rel.Col1, c.parent.Table, c.parent.ID, rel.Col2)
|
|
|
|
c.w.WriteString(`((`)
|
|
|
|
colWithTable(c.w, sel.Table, rel.Col1)
|
|
|
|
c.w.WriteString(`) = (`)
|
|
|
|
colWithTableID(c.w, parent.Table, parent.ID, rel.Col2)
|
|
|
|
c.w.WriteString(`))`)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
case RelOneToMany:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s"."%s") = ("%s_%d"."%s"))`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//c.sel.Table, rel.Col1, c.parent.Table, c.parent.ID, rel.Col2)
|
|
|
|
c.w.WriteString(`((`)
|
|
|
|
colWithTable(c.w, sel.Table, rel.Col1)
|
|
|
|
c.w.WriteString(`) = (`)
|
|
|
|
colWithTableID(c.w, parent.Table, parent.ID, rel.Col2)
|
|
|
|
c.w.WriteString(`))`)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
case RelOneToManyThrough:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s"."%s") = ("%s"."%s"))`,
|
2019-06-08 02:53:08 +02:00
|
|
|
//c.sel.Table, rel.Col1, rel.Through, rel.Col2)
|
|
|
|
c.w.WriteString(`((`)
|
|
|
|
colWithTable(c.w, sel.Table, rel.Col1)
|
|
|
|
c.w.WriteString(`) = (`)
|
|
|
|
colWithTable(c.w, rel.Through, rel.Col2)
|
|
|
|
c.w.WriteString(`))`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderWhere(sel *qcode.Select) error {
|
2019-03-24 14:57:29 +01:00
|
|
|
st := util.NewStack()
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
if sel.Where != nil {
|
|
|
|
st.Push(sel.Where)
|
|
|
|
}
|
|
|
|
|
|
|
|
ti, err := c.getTable(sel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if st.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
intf := st.Pop()
|
|
|
|
|
|
|
|
switch val := intf.(type) {
|
|
|
|
case qcode.ExpOp:
|
|
|
|
switch val {
|
|
|
|
case qcode.OpAnd:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` AND `)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpOr:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(` OR `)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpNot:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`NOT `)
|
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
|
|
|
}
|
|
|
|
case *qcode.Exp:
|
|
|
|
switch val.Op {
|
2019-03-27 07:58:19 +01:00
|
|
|
case qcode.OpAnd, qcode.OpOr:
|
|
|
|
for i := len(val.Children) - 1; i >= 0; i-- {
|
|
|
|
st.Push(val.Children[i])
|
|
|
|
if i > 0 {
|
|
|
|
st.Push(val.Op)
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
continue
|
|
|
|
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
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if val.NestedCol {
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s") `, val.Col)
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`(("`)
|
|
|
|
c.w.WriteString(val.Col)
|
|
|
|
c.w.WriteString(`") `)
|
2019-04-06 08:35:08 +02:00
|
|
|
} else if len(val.Col) != 0 {
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s"."%s") `, c.sel.Table, val.Col)
|
|
|
|
c.w.WriteString(`((`)
|
|
|
|
colWithTable(c.w, sel.Table, val.Col)
|
|
|
|
c.w.WriteString(`) `)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-03-27 07:58:19 +01:00
|
|
|
valExists := true
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
switch val.Op {
|
|
|
|
case qcode.OpEquals:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`=`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpNotEquals:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`!=`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpGreaterOrEquals:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`>=`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpLesserOrEquals:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`<=`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpGreaterThan:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`>`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpLesserThan:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`<`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpIn:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`IN`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpNotIn:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`NOT IN`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpLike:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`LIKE`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpNotLike:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`NOT LIKE`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpILike:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`ILIKE`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpNotILike:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`NOT ILIKE`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpSimilar:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`SIMILAR TO`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpNotSimilar:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`NOT SIMILAR TO`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpContains:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`@>`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpContainedIn:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`<@`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OpHasKey:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`?`)
|
2019-03-27 07:58:19 +01:00
|
|
|
case qcode.OpHasKeyAny:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`?|`)
|
2019-03-27 07:58:19 +01:00
|
|
|
case qcode.OpHasKeyAll:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`?&`)
|
2019-03-27 07:58:19 +01:00
|
|
|
case qcode.OpIsNull:
|
|
|
|
if strings.EqualFold(val.Val, "true") {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`IS NULL)`)
|
2019-03-27 07:58:19 +01:00
|
|
|
} else {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`IS NOT NULL)`)
|
2019-04-06 08:35:08 +02:00
|
|
|
}
|
|
|
|
valExists = false
|
|
|
|
case qcode.OpEqID:
|
2019-06-08 02:53:08 +02:00
|
|
|
if len(ti.PrimaryCol) == 0 {
|
|
|
|
return fmt.Errorf("no primary key column defined for %s", sel.Table)
|
2019-04-06 08:35:08 +02:00
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s") =`, c.ti.PrimaryCol)
|
|
|
|
c.w.WriteString(`(("`)
|
|
|
|
c.w.WriteString(ti.PrimaryCol)
|
|
|
|
c.w.WriteString(`") =`)
|
2019-06-01 08:03:09 +02:00
|
|
|
|
2019-04-06 08:35:08 +02:00
|
|
|
case qcode.OpTsQuery:
|
2019-06-08 02:53:08 +02:00
|
|
|
if len(ti.TSVCol) == 0 {
|
|
|
|
return fmt.Errorf("no tsv column defined for %s", sel.Table)
|
2019-03-27 07:58:19 +01:00
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `(("%s") @@ to_tsquery('%s'))`, c.ti.TSVCol, val.Val)
|
|
|
|
c.w.WriteString(`(("`)
|
|
|
|
c.w.WriteString(ti.TSVCol)
|
|
|
|
c.w.WriteString(`") @@ to_tsquery('`)
|
|
|
|
c.w.WriteString(val.Val)
|
|
|
|
c.w.WriteString(`'))`)
|
2019-03-27 07:58:19 +01:00
|
|
|
valExists = false
|
2019-04-06 08:35:08 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("[Where] unexpected op code %d", val.Op)
|
|
|
|
}
|
|
|
|
|
2019-03-27 07:58:19 +01:00
|
|
|
if valExists {
|
|
|
|
if val.Type == qcode.ValList {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.renderList(val)
|
2019-03-27 07:58:19 +01:00
|
|
|
} else {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.renderVal(val, c.vars)
|
2019-03-27 07:58:19 +01:00
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`)`)
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderOrderBy(sel *qcode.Select) error {
|
|
|
|
c.w.WriteString(` 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-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`, `)
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
ob := sel.OrderBy[i]
|
|
|
|
|
|
|
|
switch ob.Order {
|
|
|
|
case qcode.OrderAsc:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d.ob.%s" ASC`, sel.Table, sel.ID, ob.Col)
|
2019-06-08 02:53:08 +02:00
|
|
|
tableIDColSuffix(c.w, sel.Table, sel.ID, ob.Col, "_ob")
|
|
|
|
c.w.WriteString(` ASC`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderDesc:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d.ob.%s" DESC`, sel.Table, sel.ID, ob.Col)
|
2019-06-08 02:53:08 +02:00
|
|
|
tableIDColSuffix(c.w, sel.Table, sel.ID, ob.Col, "_ob")
|
|
|
|
c.w.WriteString(` DESC`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderAscNullsFirst:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d.ob.%s" ASC NULLS FIRST`, sel.Table, sel.ID, ob.Col)
|
2019-06-08 02:53:08 +02:00
|
|
|
tableIDColSuffix(c.w, sel.Table, sel.ID, ob.Col, "_ob")
|
|
|
|
c.w.WriteString(` ASC NULLS FIRST`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderDescNullsFirst:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `%s_%d.ob.%s DESC NULLS FIRST`, sel.Table, sel.ID, ob.Col)
|
2019-06-08 02:53:08 +02:00
|
|
|
tableIDColSuffix(c.w, sel.Table, sel.ID, ob.Col, "_ob")
|
|
|
|
c.w.WriteString(` DESC NULLLS FIRST`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderAscNullsLast:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d.ob.%s ASC NULLS LAST`, sel.Table, sel.ID, ob.Col)
|
2019-06-08 02:53:08 +02:00
|
|
|
tableIDColSuffix(c.w, sel.Table, sel.ID, ob.Col, "_ob")
|
|
|
|
c.w.WriteString(` ASC NULLS LAST`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.OrderDescNullsLast:
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `%s_%d.ob.%s DESC NULLS LAST`, sel.Table, sel.ID, ob.Col)
|
2019-06-08 02:53:08 +02:00
|
|
|
tableIDColSuffix(c.w, sel.Table, sel.ID, ob.Col, "_ob")
|
|
|
|
c.w.WriteString(` 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-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderDistinctOn(sel *qcode.Select) {
|
|
|
|
io.WriteString(c.w, `DISTINCT ON (`)
|
|
|
|
for i := range sel.DistinctOn {
|
2019-05-13 01:27:26 +02:00
|
|
|
if i != 0 {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`, `)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
//fmt.Fprintf(w, `"%s_%d.ob.%s"`, c.sel.Table, c.sel.ID, c.sel.DistinctOn[i])
|
|
|
|
tableIDColSuffix(c.w, sel.Table, sel.ID, sel.DistinctOn[i], "_ob")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`) `)
|
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-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`, `)
|
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-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(ex.ListVal[i])
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.ValStr:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`'`)
|
|
|
|
c.w.WriteString(ex.ListVal[i])
|
|
|
|
c.w.WriteString(`'`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`)`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func (c *compilerContext) renderVal(ex *qcode.Exp,
|
|
|
|
vars map[string]string) {
|
|
|
|
|
|
|
|
io.WriteString(c.w, ` (`)
|
2019-03-24 14:57:29 +01:00
|
|
|
switch ex.Type {
|
|
|
|
case qcode.ValBool, qcode.ValInt, qcode.ValFloat:
|
2019-04-19 07:55:03 +02:00
|
|
|
if len(ex.Val) != 0 {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(ex.Val)
|
2019-04-19 07:55:03 +02:00
|
|
|
} else {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`''`)
|
2019-04-19 07:55:03 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.ValStr:
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`'`)
|
|
|
|
c.w.WriteString(ex.Val)
|
|
|
|
c.w.WriteString(`'`)
|
2019-03-24 14:57:29 +01:00
|
|
|
case qcode.ValVar:
|
|
|
|
if val, ok := vars[ex.Val]; ok {
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(val)
|
2019-03-24 14:57:29 +01:00
|
|
|
} else {
|
2019-06-01 08:03:09 +02:00
|
|
|
//fmt.Fprintf(w, `'{{%s}}'`, ex.Val)
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`'{{`)
|
|
|
|
c.w.WriteString(ex.Val)
|
|
|
|
c.w.WriteString(`}}'`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-08 02:53:08 +02:00
|
|
|
c.w.WriteString(`)`)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-03-31 17:18:33 +02:00
|
|
|
|
|
|
|
func funcPrefixLen(fn string) int {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
func alias(w *bytes.Buffer, alias string) {
|
|
|
|
w.WriteString(` AS "`)
|
|
|
|
w.WriteString(alias)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func aliasWithID(w *bytes.Buffer, alias string, id int32) {
|
2019-06-01 08:03:09 +02:00
|
|
|
w.WriteString(` AS "`)
|
|
|
|
w.WriteString(alias)
|
|
|
|
w.WriteString(`_`)
|
|
|
|
int2string(w, id)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func aliasWithIDSuffix(w *bytes.Buffer, alias string, id int32, suffix string) {
|
2019-06-01 08:03:09 +02:00
|
|
|
w.WriteString(` AS "`)
|
|
|
|
w.WriteString(alias)
|
|
|
|
w.WriteString(`_`)
|
|
|
|
int2string(w, id)
|
|
|
|
w.WriteString(suffix)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func colWithAlias(w *bytes.Buffer, col, alias string) {
|
|
|
|
w.WriteString(`"`)
|
|
|
|
w.WriteString(col)
|
|
|
|
w.WriteString(`" AS "`)
|
|
|
|
w.WriteString(alias)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func colWithTable(w *bytes.Buffer, table, col string) {
|
|
|
|
w.WriteString(`"`)
|
|
|
|
w.WriteString(table)
|
|
|
|
w.WriteString(`"."`)
|
|
|
|
w.WriteString(col)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func colWithTableID(w *bytes.Buffer, table string, id int32, col string) {
|
2019-06-01 08:03:09 +02:00
|
|
|
w.WriteString(`"`)
|
|
|
|
w.WriteString(table)
|
|
|
|
w.WriteString(`_`)
|
|
|
|
int2string(w, id)
|
|
|
|
w.WriteString(`"."`)
|
|
|
|
w.WriteString(col)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func colWithTableIDAlias(w *bytes.Buffer, table string, id int32, col, alias string) {
|
2019-06-01 08:03:09 +02:00
|
|
|
w.WriteString(`"`)
|
|
|
|
w.WriteString(table)
|
|
|
|
w.WriteString(`_`)
|
|
|
|
int2string(w, id)
|
|
|
|
w.WriteString(`"."`)
|
|
|
|
w.WriteString(col)
|
|
|
|
w.WriteString(`" AS "`)
|
|
|
|
w.WriteString(alias)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func colWithTableIDSuffixAlias(w *bytes.Buffer, table string, id int32,
|
2019-06-01 08:03:09 +02:00
|
|
|
suffix, col, alias string) {
|
|
|
|
w.WriteString(`"`)
|
|
|
|
w.WriteString(table)
|
|
|
|
w.WriteString(`_`)
|
|
|
|
int2string(w, id)
|
|
|
|
w.WriteString(suffix)
|
|
|
|
w.WriteString(`"."`)
|
|
|
|
w.WriteString(col)
|
|
|
|
w.WriteString(`" AS "`)
|
|
|
|
w.WriteString(alias)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func tableIDColSuffix(w *bytes.Buffer, table string, id int32, col, suffix string) {
|
2019-06-01 08:03:09 +02:00
|
|
|
w.WriteString(`"`)
|
|
|
|
w.WriteString(table)
|
|
|
|
w.WriteString(`_`)
|
|
|
|
int2string(w, id)
|
|
|
|
w.WriteString(`_`)
|
|
|
|
w.WriteString(col)
|
|
|
|
w.WriteString(suffix)
|
|
|
|
w.WriteString(`"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const charset = "0123456789"
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
func int2string(w *bytes.Buffer, val int32) {
|
2019-06-01 08:03:09 +02:00
|
|
|
if val < 10 {
|
|
|
|
w.WriteByte(charset[val])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-08 02:53:08 +02:00
|
|
|
temp := int32(0)
|
2019-06-01 08:03:09 +02:00
|
|
|
val2 := val
|
|
|
|
for val2 > 0 {
|
|
|
|
temp *= 10
|
|
|
|
temp += val2 % 10
|
2019-06-08 02:53:08 +02:00
|
|
|
val2 = int32(math.Floor(float64(val2 / 10)))
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val3 := temp
|
|
|
|
for val3 > 0 {
|
|
|
|
d := val3 % 10
|
|
|
|
val3 /= 10
|
|
|
|
w.WriteByte(charset[d])
|
|
|
|
}
|
|
|
|
}
|