2017-11-16 20:53:39 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/abcum/surreal/sql"
|
2018-05-02 01:43:04 +00:00
|
|
|
"github.com/abcum/surreal/util/data"
|
|
|
|
"github.com/abcum/surreal/util/fncs"
|
2017-11-16 20:53:39 +00:00
|
|
|
"github.com/abcum/surreal/util/keys"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Table checks if any table views are specified for
|
|
|
|
// this table, and executes them in name order.
|
|
|
|
func (d *document) table(ctx context.Context, when method) (err error) {
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
// Check if this query has been run
|
|
|
|
// in forced mode, because of an
|
|
|
|
// index or foreign table update.
|
|
|
|
|
|
|
|
forced := d.forced(ctx)
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
// If this document has not changed
|
|
|
|
// then there is no need to update
|
|
|
|
// any registered foreign tables.
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
if !forced && !d.changed(ctx) {
|
2017-11-16 20:53:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the foreign read-only tables
|
|
|
|
// specified for this table, and
|
|
|
|
// update values which have changed.
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
fts, err := d.getFT(ctx)
|
2017-11-16 20:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
for _, ft := range fts {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
var ok bool
|
|
|
|
var prv *sql.Thing
|
|
|
|
var doc *sql.Thing
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
ok, err = d.check(ctx, ft.Cond)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
if len(ft.Group) > 0 {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
// If there are GROUP BY clauses then
|
|
|
|
// let's calculate the
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
old := make([]interface{}, len(ft.Group))
|
|
|
|
now := make([]interface{}, len(ft.Group))
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
for k, e := range ft.Group {
|
|
|
|
old[k], _ = d.i.e.fetch(ctx, e.Expr, d.initial)
|
|
|
|
now[k], _ = d.i.e.fetch(ctx, e.Expr, d.current)
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-08-10 18:34:16 +00:00
|
|
|
prv = sql.NewThing(ft.Name.VA, fmt.Sprintf("%v", old))
|
|
|
|
doc = sql.NewThing(ft.Name.VA, fmt.Sprintf("%v", now))
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
} else {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
// Otherwise let's use the id of the
|
|
|
|
// current record as the basis of the
|
|
|
|
// new record in the other table.
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-08-10 18:34:16 +00:00
|
|
|
doc = sql.NewThing(ft.Name.VA, d.id.ID)
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
switch ok {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
// If the document does not match the table
|
|
|
|
// WHERE condition, then remove it from
|
|
|
|
// the table, or remove it from the aggregate.
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
case false:
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
if len(ft.Group) > 0 {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
if !forced && when != _CREATE {
|
|
|
|
err = d.tableModify(ctx, prv, ft.Expr, _REMOVE)
|
2017-11-16 20:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
err = d.tableDelete(ctx, doc, ft.Expr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the document does match the table
|
|
|
|
// WHERE condition, then add it to the
|
|
|
|
// table, or add it to the aggregate.
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
case true:
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
if len(ft.Group) > 0 {
|
|
|
|
|
|
|
|
if !forced && when != _CREATE {
|
|
|
|
err = d.tableModify(ctx, prv, ft.Expr, _REMOVE)
|
2017-11-16 20:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-02 01:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if when != _DELETE {
|
|
|
|
err = d.tableModify(ctx, doc, ft.Expr, _CHANGE)
|
2017-11-16 20:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
err = d.tableUpdate(ctx, doc, ft.Expr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
func (d *document) tableDelete(ctx context.Context, tng *sql.Thing, exp sql.Fields) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
stm := &sql.DeleteStatement{
|
2018-04-14 18:14:47 +00:00
|
|
|
KV: d.key.KV,
|
2017-11-16 20:53:39 +00:00
|
|
|
NS: d.key.NS,
|
|
|
|
DB: d.key.DB,
|
2018-05-02 01:43:04 +00:00
|
|
|
What: sql.Exprs{tng},
|
|
|
|
Hard: false,
|
2017-11-16 20:53:39 +00:00
|
|
|
Parallel: 1,
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: tng.TB, ID: tng.ID}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
i := newIterator(d.i.e, ctx, stm, true)
|
|
|
|
|
|
|
|
i.processThing(ctx, key)
|
|
|
|
|
|
|
|
_, err = i.Yield(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
func (d *document) tableUpdate(ctx context.Context, tng *sql.Thing, exp sql.Fields) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
res, err := d.yield(ctx, &sql.SelectStatement{Expr: exp}, sql.ILLEGAL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stm := &sql.UpdateStatement{
|
2018-04-14 18:14:47 +00:00
|
|
|
KV: d.key.KV,
|
2017-11-16 20:53:39 +00:00
|
|
|
NS: d.key.NS,
|
|
|
|
DB: d.key.DB,
|
2018-05-02 01:43:04 +00:00
|
|
|
What: sql.Exprs{tng},
|
2017-11-16 20:53:39 +00:00
|
|
|
Data: &sql.ContentExpression{Data: res},
|
|
|
|
Parallel: 1,
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: tng.TB, ID: tng.ID}
|
|
|
|
|
|
|
|
i := newIterator(d.i.e, ctx, stm, true)
|
|
|
|
|
|
|
|
i.processThing(ctx, key)
|
|
|
|
|
|
|
|
_, err = i.Yield(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *document) tableModify(ctx context.Context, tng *sql.Thing, exp sql.Fields, when modify) (err error) {
|
|
|
|
|
|
|
|
var doc *data.Doc
|
|
|
|
|
|
|
|
switch when {
|
|
|
|
case _REMOVE:
|
|
|
|
doc = d.initial
|
|
|
|
case _CHANGE:
|
|
|
|
doc = d.current
|
|
|
|
}
|
|
|
|
|
|
|
|
set := &sql.DataExpression{}
|
|
|
|
|
|
|
|
for _, e := range exp {
|
|
|
|
|
|
|
|
if f, ok := e.Expr.(*sql.FuncExpression); ok && f.Aggr {
|
|
|
|
|
|
|
|
var v interface{}
|
|
|
|
|
|
|
|
args := make([]interface{}, len(f.Args))
|
|
|
|
for x := 0; x < len(f.Args); x++ {
|
|
|
|
args[x], _ = d.i.e.fetch(ctx, f.Args[x], doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the function is math.stddev() or
|
|
|
|
// math.variance(), then we need to work
|
|
|
|
// out the value as a whole, and not the
|
|
|
|
// result of each record separately.
|
|
|
|
|
|
|
|
switch f.Name {
|
|
|
|
default:
|
|
|
|
v, err = fncs.Run(ctx, f.Name, args...)
|
|
|
|
case "math.stddev":
|
|
|
|
v = args[0]
|
|
|
|
case "math.variance":
|
|
|
|
v = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Name {
|
|
|
|
case "distinct":
|
|
|
|
tableChg(set, e.Field, v, when)
|
|
|
|
case "count":
|
|
|
|
tableChg(set, e.Field, v, when)
|
|
|
|
case "count.if":
|
|
|
|
tableChg(set, e.Field, v, when)
|
|
|
|
case "count.not":
|
|
|
|
tableChg(set, e.Field, v, when)
|
|
|
|
case "math.sum":
|
|
|
|
tableChg(set, e.Field, v, when)
|
|
|
|
case "math.min":
|
|
|
|
tableMin(set, e.Field, v, when)
|
|
|
|
case "math.max":
|
|
|
|
tableMax(set, e.Field, v, when)
|
|
|
|
case "math.mean":
|
|
|
|
tableMean(set, e.Field, v, when)
|
|
|
|
case "math.stddev":
|
|
|
|
switch a := v.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
for _, v := range a {
|
|
|
|
tableStddev(set, e.Field, v, when)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
tableStddev(set, e.Field, v, when)
|
|
|
|
}
|
|
|
|
case "math.variance":
|
|
|
|
switch a := v.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
for _, v := range a {
|
|
|
|
tableVariance(set, e.Field, v, when)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
tableVariance(set, e.Field, v, when)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
o, err := d.i.e.fetch(ctx, e.Expr, doc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tableSet(set, e.Field, o, when)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
stm := &sql.UpdateStatement{
|
|
|
|
KV: d.key.KV,
|
|
|
|
NS: d.key.NS,
|
|
|
|
DB: d.key.DB,
|
|
|
|
What: sql.Exprs{tng},
|
|
|
|
Data: set,
|
|
|
|
Parallel: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: tng.TB, ID: tng.ID}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
i := newIterator(d.i.e, ctx, stm, true)
|
|
|
|
|
|
|
|
i.processThing(ctx, key)
|
|
|
|
|
|
|
|
_, err = i.Yield(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
func tableSet(set *sql.DataExpression, key string, val interface{}, when modify) {
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: val,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableChg(set *sql.DataExpression, key string, val interface{}, when modify) {
|
|
|
|
|
|
|
|
var op sql.Token
|
|
|
|
|
|
|
|
switch when {
|
|
|
|
case _REMOVE:
|
|
|
|
op = sql.DEC
|
|
|
|
case _CHANGE:
|
|
|
|
op = sql.INC
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: op,
|
|
|
|
RHS: val,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableMin(set *sql.DataExpression, key string, val interface{}, when modify) {
|
|
|
|
|
|
|
|
if when == _CHANGE {
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.IfelExpression{
|
|
|
|
Cond: sql.Exprs{
|
|
|
|
&sql.BinaryExpression{
|
|
|
|
LHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.Empty{},
|
|
|
|
},
|
|
|
|
Op: sql.OR,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.GT,
|
|
|
|
RHS: val,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Then: sql.Exprs{
|
|
|
|
val,
|
|
|
|
},
|
|
|
|
Else: sql.NewIdent(key),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableMax(set *sql.DataExpression, key string, val interface{}, when modify) {
|
|
|
|
|
|
|
|
if when == _CHANGE {
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.IfelExpression{
|
|
|
|
Cond: sql.Exprs{
|
|
|
|
&sql.BinaryExpression{
|
|
|
|
LHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.Empty{},
|
|
|
|
},
|
|
|
|
Op: sql.OR,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.LT,
|
|
|
|
RHS: val,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Then: sql.Exprs{
|
|
|
|
val,
|
|
|
|
},
|
|
|
|
Else: sql.NewIdent(key),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableMean(set *sql.DataExpression, key string, val interface{}, when modify) {
|
|
|
|
|
|
|
|
var op sql.Token
|
|
|
|
|
|
|
|
switch when {
|
|
|
|
case _REMOVE:
|
|
|
|
op = sql.DEC
|
|
|
|
case _CHANGE:
|
|
|
|
op = sql.INC
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: op,
|
|
|
|
RHS: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
switch when {
|
|
|
|
case _REMOVE:
|
|
|
|
op = sql.SUB
|
|
|
|
case _CHANGE:
|
|
|
|
op = sql.ADD
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: &sql.SubExpression{
|
|
|
|
Expr: &sql.BinaryExpression{
|
|
|
|
LHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.MUL,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: sql.SUB,
|
|
|
|
RHS: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Op: op,
|
|
|
|
RHS: val,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Op: sql.DIV,
|
|
|
|
RHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.IfelExpression{
|
|
|
|
Cond: sql.Exprs{
|
|
|
|
&sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.Empty{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Then: sql.Exprs{0},
|
|
|
|
Else: sql.NewIdent(key),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableStddev(set *sql.DataExpression, key string, val interface{}, when modify) {
|
|
|
|
|
|
|
|
var op sql.Token
|
|
|
|
|
|
|
|
switch when {
|
|
|
|
case _REMOVE:
|
|
|
|
op = sql.DEC
|
|
|
|
case _CHANGE:
|
|
|
|
op = sql.INC
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: op,
|
|
|
|
RHS: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".t"),
|
|
|
|
Op: op,
|
|
|
|
RHS: val,
|
|
|
|
})
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".m"),
|
|
|
|
Op: op,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: val,
|
|
|
|
Op: sql.MUL,
|
|
|
|
RHS: val,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// FIXME Need to ensure removed values update correctly
|
|
|
|
|
|
|
|
switch when {
|
|
|
|
case _REMOVE:
|
|
|
|
op = sql.SUB // FIXME This is incorrect
|
|
|
|
case _CHANGE:
|
|
|
|
op = sql.ADD
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.FuncExpression{
|
|
|
|
Name: "math.sqrt",
|
|
|
|
Args: sql.Exprs{
|
|
|
|
&sql.BinaryExpression{
|
|
|
|
LHS: &sql.BinaryExpression{
|
|
|
|
LHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: sql.MUL,
|
|
|
|
RHS: sql.NewIdent("meta.__." + key + ".m"),
|
|
|
|
},
|
|
|
|
Op: sql.SUB,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".t"),
|
|
|
|
Op: sql.MUL,
|
|
|
|
RHS: sql.NewIdent("meta.__." + key + ".t"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Op: sql.DIV,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: sql.MUL,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: sql.SUB,
|
|
|
|
RHS: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.IfelExpression{
|
|
|
|
Cond: sql.Exprs{
|
|
|
|
&sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.Empty{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Then: sql.Exprs{0},
|
|
|
|
Else: sql.NewIdent(key),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableVariance(set *sql.DataExpression, key string, val interface{}, when modify) {
|
|
|
|
|
|
|
|
var op sql.Token
|
|
|
|
|
|
|
|
switch when {
|
|
|
|
case _REMOVE:
|
|
|
|
op = sql.DEC
|
|
|
|
case _CHANGE:
|
|
|
|
op = sql.INC
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: op,
|
|
|
|
RHS: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".t"),
|
|
|
|
Op: op,
|
|
|
|
RHS: val,
|
|
|
|
})
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".m"),
|
|
|
|
Op: op,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: val,
|
|
|
|
Op: sql.MUL,
|
|
|
|
RHS: val,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// FIXME Need to ensure removed values update correctly
|
|
|
|
|
|
|
|
switch when {
|
|
|
|
case _REMOVE:
|
|
|
|
op = sql.SUB // FIXME This is incorrect
|
|
|
|
case _CHANGE:
|
|
|
|
op = sql.ADD
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: &sql.BinaryExpression{
|
|
|
|
LHS: &sql.BinaryExpression{
|
|
|
|
LHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: sql.MUL,
|
|
|
|
RHS: sql.NewIdent("meta.__." + key + ".m"),
|
|
|
|
},
|
|
|
|
Op: sql.SUB,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".t"),
|
|
|
|
Op: sql.MUL,
|
|
|
|
RHS: sql.NewIdent("meta.__." + key + ".t"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Op: sql.DIV,
|
|
|
|
RHS: &sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
Op: sql.SUB,
|
|
|
|
RHS: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Op: sql.DIV,
|
|
|
|
RHS: sql.NewIdent("meta.__." + key + ".c"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
set.Data = append(set.Data, &sql.ItemExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.IfelExpression{
|
|
|
|
Cond: sql.Exprs{
|
|
|
|
&sql.BinaryExpression{
|
|
|
|
LHS: sql.NewIdent(key),
|
|
|
|
Op: sql.EQ,
|
|
|
|
RHS: &sql.Empty{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Then: sql.Exprs{0},
|
|
|
|
Else: sql.NewIdent(key),
|
|
|
|
},
|
|
|
|
})
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
}
|