2018-04-10 06:15:47 +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 (
|
|
|
|
"context"
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
"github.com/abcum/surreal/cnf"
|
2018-04-10 06:15:47 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
|
|
|
"github.com/abcum/surreal/util/data"
|
2018-04-14 18:14:47 +00:00
|
|
|
"github.com/abcum/surreal/util/diff"
|
2018-04-10 06:15:47 +00:00
|
|
|
)
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) cold(ctx context.Context) (doc *data.Doc, err error) {
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-26 23:40:59 +00:00
|
|
|
// We need to copy the document so that
|
|
|
|
// we can add and remove the fields which
|
|
|
|
// are relevant to the particular query.
|
|
|
|
|
|
|
|
doc = d.initial.Copy()
|
|
|
|
|
2018-04-24 23:00:36 +00:00
|
|
|
// If we are not authenticated using DB,
|
|
|
|
// NS, or KV level, then we need to check
|
|
|
|
// document permissions for this query.
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if k, ok := ctx.Value(ctxKeyKind).(cnf.Kind); ok {
|
2018-04-24 23:00:36 +00:00
|
|
|
if k == cnf.AuthSC {
|
2018-04-26 23:40:59 +00:00
|
|
|
if err = d.perms(ctx, doc); err != nil {
|
2018-04-24 23:00:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-14 18:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *document) cnow(ctx context.Context) (doc *data.Doc, err error) {
|
|
|
|
|
2018-04-26 23:40:59 +00:00
|
|
|
// We need to copy the document so that
|
|
|
|
// we can add and remove the fields which
|
|
|
|
// are relevant to the particular query.
|
|
|
|
|
|
|
|
doc = d.current.Copy()
|
|
|
|
|
2018-04-24 23:00:36 +00:00
|
|
|
// If we are not authenticated using DB,
|
|
|
|
// NS, or KV level, then we need to check
|
|
|
|
// document permissions for this query.
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if k, ok := ctx.Value(ctxKeyKind).(cnf.Kind); ok {
|
2018-04-24 23:00:36 +00:00
|
|
|
if k == cnf.AuthSC {
|
2018-04-26 23:40:59 +00:00
|
|
|
if err = d.perms(ctx, doc); err != nil {
|
2018-04-24 23:00:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-10 06:15:47 +00:00
|
|
|
}
|
2018-04-14 18:14:47 +00:00
|
|
|
}
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
return
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
}
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) diffs(initial, current *data.Doc) *data.Doc {
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
a, _ := initial.Data().(map[string]interface{})
|
|
|
|
b, _ := current.Data().(map[string]interface{})
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if c := diff.Diff(a, b); len(c) > 0 {
|
|
|
|
return data.Consume(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.Consume(nil)
|
|
|
|
|
|
|
|
}
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) yield(ctx context.Context, stm sql.Statement, output sql.Token) (interface{}, error) {
|
2018-04-10 06:15:47 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
var exps sql.Fields
|
|
|
|
var grps sql.Groups
|
2018-04-21 10:32:13 +00:00
|
|
|
var fchs sql.Fetchs
|
2018-04-14 18:14:47 +00:00
|
|
|
|
|
|
|
switch stm := stm.(type) {
|
|
|
|
case *sql.LiveStatement:
|
|
|
|
exps = stm.Expr
|
2018-04-24 22:57:23 +00:00
|
|
|
fchs = stm.Fetch
|
2018-04-14 18:14:47 +00:00
|
|
|
case *sql.SelectStatement:
|
|
|
|
exps = stm.Expr
|
|
|
|
grps = stm.Group
|
2018-04-21 10:32:13 +00:00
|
|
|
fchs = stm.Fetch
|
2018-04-14 18:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no field expressions
|
|
|
|
// then this was not a LIVE or SELECT
|
|
|
|
// query, and therefore the query will
|
|
|
|
// have an output format specified.
|
|
|
|
|
|
|
|
if len(exps) == 0 {
|
2018-04-10 06:15:47 +00:00
|
|
|
|
|
|
|
switch output {
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
case sql.DIFF:
|
2018-04-14 18:14:47 +00:00
|
|
|
|
|
|
|
old, err := d.cold(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
now, err := d.cnow(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.diffs(old, now).Data(), nil
|
|
|
|
|
2018-04-10 06:15:47 +00:00
|
|
|
case sql.AFTER:
|
2018-04-14 18:14:47 +00:00
|
|
|
|
|
|
|
doc, err := d.cnow(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return doc.Data(), nil
|
|
|
|
|
2018-04-10 06:15:47 +00:00
|
|
|
case sql.BEFORE:
|
2018-04-14 18:14:47 +00:00
|
|
|
|
|
|
|
doc, err := d.cold(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return doc.Data(), nil
|
|
|
|
|
2018-04-10 06:15:47 +00:00
|
|
|
case sql.BOTH:
|
2018-04-14 18:14:47 +00:00
|
|
|
|
|
|
|
old, err := d.cold(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
now, err := d.cnow(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:15:47 +00:00
|
|
|
return map[string]interface{}{
|
2018-04-14 18:14:47 +00:00
|
|
|
"after": now.Data(),
|
|
|
|
"before": old.Data(),
|
2018-04-10 06:15:47 +00:00
|
|
|
}, nil
|
2018-04-14 18:14:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// But if there are field expresions
|
|
|
|
// then this query is a LIVE or SELECT
|
|
|
|
// query, and we must output only the
|
|
|
|
// desired fields in the output.
|
|
|
|
|
|
|
|
var out = data.New()
|
|
|
|
|
|
|
|
doc, err := d.cnow(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-21 10:32:13 +00:00
|
|
|
// First of all, check to see if an ALL
|
|
|
|
// expression has been specified, and if
|
|
|
|
// it has then use the full document.
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
for _, e := range exps {
|
|
|
|
if _, ok := e.Expr.(*sql.All); ok {
|
|
|
|
out = doc
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 13:34:39 +00:00
|
|
|
// Ensure that all output fields are
|
|
|
|
// available in subsequent expressions
|
|
|
|
// using the $this parameter.
|
|
|
|
|
|
|
|
vars := data.New()
|
|
|
|
vars.Set(out.Data(), varKeyThis)
|
|
|
|
ctx = context.WithValue(ctx, ctxKeySpec, vars)
|
|
|
|
|
2018-04-21 10:32:13 +00:00
|
|
|
// Next let's see the field expressions
|
|
|
|
// which have been requested, and add
|
|
|
|
// these to the output document.
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
for _, e := range exps {
|
|
|
|
|
|
|
|
switch v := e.Expr.(type) {
|
|
|
|
case *sql.All:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
|
|
|
|
// If the query has a GROUP BY expression
|
|
|
|
// then let's check if this is an aggregate
|
|
|
|
// function, and if it is then pass the
|
|
|
|
// first argument directly through.
|
|
|
|
|
|
|
|
if len(grps) > 0 {
|
|
|
|
if f, ok := e.Expr.(*sql.FuncExpression); ok && f.Aggr {
|
|
|
|
v, err := d.i.e.fetch(ctx, f.Args[0], doc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out.Set(v, f.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise treat the field normally, and
|
|
|
|
// calculate the value to be inserted into
|
|
|
|
// the final output document.
|
|
|
|
|
2018-04-21 10:32:13 +00:00
|
|
|
o, err := d.i.e.fetch(ctx, v, doc)
|
2018-04-14 18:14:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-21 10:32:13 +00:00
|
|
|
switch o {
|
2018-04-14 18:14:47 +00:00
|
|
|
case doc:
|
|
|
|
out.Set(nil, e.Field)
|
|
|
|
default:
|
2018-04-21 10:32:13 +00:00
|
|
|
out.Set(o, e.Field)
|
2018-04-14 18:14:47 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 06:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-21 10:32:13 +00:00
|
|
|
// Finally let's see if there are any
|
|
|
|
// FETCH expressions, so that we can
|
|
|
|
// follow links to other records.
|
|
|
|
|
|
|
|
for _, e := range fchs {
|
|
|
|
|
|
|
|
switch v := e.Expr.(type) {
|
|
|
|
case *sql.All:
|
|
|
|
break
|
|
|
|
case *sql.Ident:
|
|
|
|
|
2018-04-26 23:40:36 +00:00
|
|
|
out.Walk(func(key string, val interface{}, exi bool) error {
|
2018-04-21 10:32:13 +00:00
|
|
|
|
|
|
|
switch res := val.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
val, _ = d.i.e.fetchArray(ctx, res, doc)
|
|
|
|
out.Set(val, key)
|
|
|
|
case *sql.Thing:
|
|
|
|
val, _ = d.i.e.fetchThing(ctx, res, doc)
|
|
|
|
out.Set(val, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}, v.ID)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:43:04 +00:00
|
|
|
// Remove all temporary metadata from
|
|
|
|
// the record. This is not visible when
|
|
|
|
// outputting, but is stored in the DB.
|
|
|
|
|
|
|
|
doc.Del("meta.__")
|
|
|
|
|
|
|
|
// Output the document with the correct
|
|
|
|
// specified fields, linked records and
|
|
|
|
// any aggregated group by clauses.
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
return out.Data(), nil
|
|
|
|
|
2018-04-10 06:15:47 +00:00
|
|
|
}
|