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 (
|
2017-12-05 01:56:33 +00:00
|
|
|
"sort"
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
"context"
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
"github.com/abcum/surreal/cnf"
|
2017-11-16 20:53:39 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
|
|
|
"github.com/abcum/surreal/util/conv"
|
|
|
|
"github.com/abcum/surreal/util/data"
|
|
|
|
"github.com/abcum/surreal/util/diff"
|
|
|
|
)
|
|
|
|
|
|
|
|
var main = map[string]struct{}{
|
|
|
|
"id": {},
|
|
|
|
"meta": {},
|
|
|
|
"meta.tb": {},
|
|
|
|
"meta.id": {},
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) merge(ctx context.Context, met method, data sql.Expr) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.defFld(ctx, met); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch expr := data.(type) {
|
|
|
|
case *sql.DataExpression:
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.mrgSet(ctx, met, expr); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *sql.DiffExpression:
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.mrgDpm(ctx, met, expr); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *sql.MergeExpression:
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.mrgAny(ctx, met, expr); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *sql.ContentExpression:
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.mrgAll(ctx, met, expr); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.defFld(ctx, met); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.mrgFld(ctx, met); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.defFld(ctx, met); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
if err = d.delFld(ctx, met); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) defFld(ctx context.Context, met method) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
d.current.Set(d.id, "id")
|
|
|
|
d.current.Set(d.md, "meta")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) delFld(ctx context.Context, met method) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
tb, err := d.getTB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tb.Full {
|
|
|
|
|
|
|
|
var keys = map[string]struct{}{}
|
|
|
|
|
|
|
|
// Get the defined fields
|
|
|
|
|
|
|
|
fds, err := d.getFD()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over the allowed keys
|
|
|
|
|
|
|
|
for _, fd := range fds {
|
2018-04-26 23:40:36 +00:00
|
|
|
d.current.Walk(func(key string, val interface{}, ok bool) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
keys[key] = struct{}{}
|
|
|
|
return
|
|
|
|
}, fd.Name.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete any keys which aren't allowed
|
|
|
|
|
|
|
|
d.current.Each(func(key string, val interface{}) (err error) {
|
|
|
|
if _, ok := main[key]; !ok {
|
|
|
|
if _, ok := keys[key]; !ok {
|
|
|
|
d.current.Del(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) mrgAll(ctx context.Context, met method, expr *sql.ContentExpression) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
var obj map[string]interface{}
|
|
|
|
|
|
|
|
switch v := expr.Data.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
obj = v
|
|
|
|
case *sql.Param:
|
2017-12-03 00:46:46 +00:00
|
|
|
|
|
|
|
p, err := d.i.e.fetch(ctx, v, d.current)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
switch v := p.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
obj = v
|
|
|
|
}
|
2017-12-03 00:46:46 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.current.Reset()
|
|
|
|
|
|
|
|
for k, v := range obj {
|
|
|
|
d.current.Set(v, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) mrgAny(ctx context.Context, met method, expr *sql.MergeExpression) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
var obj map[string]interface{}
|
|
|
|
|
|
|
|
switch v := expr.Data.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
obj = v
|
|
|
|
case *sql.Param:
|
2017-12-03 00:46:46 +00:00
|
|
|
|
|
|
|
p, err := d.i.e.fetch(ctx, v, d.current)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
switch v := p.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
obj = v
|
|
|
|
}
|
2017-12-03 00:46:46 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range obj {
|
|
|
|
d.current.Set(v, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) mrgDpm(ctx context.Context, met method, expr *sql.DiffExpression) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
var obj []interface{}
|
|
|
|
var old map[string]interface{}
|
|
|
|
var now map[string]interface{}
|
|
|
|
|
|
|
|
switch v := expr.Data.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
obj = v
|
|
|
|
case *sql.Param:
|
2017-12-03 00:46:46 +00:00
|
|
|
|
|
|
|
p, err := d.i.e.fetch(ctx, v, d.current)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
switch v := p.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
obj = v
|
|
|
|
}
|
2017-12-03 00:46:46 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
old = d.current.Data().(map[string]interface{})
|
|
|
|
now = diff.Patch(old, obj)
|
|
|
|
|
|
|
|
d.current = data.Consume(now)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) mrgSet(ctx context.Context, met method, expr *sql.DataExpression) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
for _, v := range expr.Data {
|
|
|
|
|
|
|
|
if i, ok := v.LHS.(*sql.Ident); ok {
|
|
|
|
|
|
|
|
n, err := d.i.e.fetch(ctx, v.RHS, d.current)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.Op {
|
|
|
|
case sql.EQ:
|
|
|
|
switch n.(type) {
|
|
|
|
default:
|
|
|
|
d.current.Set(n, i.ID)
|
|
|
|
case *sql.Void:
|
|
|
|
d.current.Del(i.ID)
|
|
|
|
}
|
|
|
|
case sql.INC:
|
|
|
|
d.current.Inc(n, i.ID)
|
|
|
|
case sql.DEC:
|
|
|
|
d.current.Dec(n, i.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
func (d *document) mrgFld(ctx context.Context, met method) (err error) {
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
fds, err := d.getFD()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-05 01:56:33 +00:00
|
|
|
// Sort the fields according to their
|
2018-01-10 13:17:50 +00:00
|
|
|
// priority so that fields which depend
|
|
|
|
// on another field can be processed
|
|
|
|
// after that field in a specific order.
|
2017-12-05 01:56:33 +00:00
|
|
|
|
|
|
|
sort.Slice(fds, func(i, j int) bool {
|
|
|
|
return fds[i].Priority < fds[j].Priority
|
|
|
|
})
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-01-10 13:17:50 +00:00
|
|
|
// Loop through each field and check to
|
|
|
|
// see if it might be a specific type.
|
|
|
|
// This is because when updating records
|
|
|
|
// using json, there is no specific type
|
|
|
|
// for a 'datetime' and 'record'.
|
|
|
|
|
|
|
|
d.current.Each(func(key string, val interface{}) (err error) {
|
|
|
|
if val, ok := conv.MightBe(val); ok {
|
|
|
|
d.current.Iff(val, key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Loop over each of the defined fields
|
|
|
|
// and process them fully, applying the
|
|
|
|
// VALUE and ASSERT clauses sequentially.
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
for _, fd := range fds {
|
|
|
|
|
2018-04-26 23:40:36 +00:00
|
|
|
err = d.current.Walk(func(key string, val interface{}, exi bool) error {
|
2017-12-04 10:09:49 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
var old = d.initial.Get(key).Data()
|
|
|
|
|
|
|
|
// Ensure the field is the correct type
|
|
|
|
|
|
|
|
if val != nil {
|
2018-02-14 15:48:42 +00:00
|
|
|
if now, err := conv.ConvertTo(fd.Type, fd.Kind, val); err != nil {
|
|
|
|
val = nil
|
|
|
|
} else {
|
|
|
|
val = now
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are setting the value of the field
|
|
|
|
|
|
|
|
if fd.Value != nil {
|
2018-04-14 18:14:47 +00:00
|
|
|
|
|
|
|
// Reset the variables
|
|
|
|
|
2018-04-26 23:40:36 +00:00
|
|
|
vars := data.New()
|
2018-04-14 18:14:47 +00:00
|
|
|
vars.Set(val, varKeyValue)
|
|
|
|
vars.Set(val, varKeyAfter)
|
|
|
|
vars.Set(old, varKeyBefore)
|
|
|
|
ctx = context.WithValue(ctx, ctxKeySpec, vars)
|
|
|
|
|
2018-02-14 15:48:42 +00:00
|
|
|
if now, err := d.i.e.fetch(ctx, fd.Value, d.current); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return err
|
2018-02-14 15:48:42 +00:00
|
|
|
} else {
|
|
|
|
val = now
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
2017-12-04 10:09:49 +00:00
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
}
|
2017-12-04 10:09:49 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
// We are checking the value of the field
|
|
|
|
|
|
|
|
if fd.Assert != nil {
|
2018-04-14 18:14:47 +00:00
|
|
|
|
|
|
|
// Reset the variables
|
|
|
|
|
2018-04-26 23:40:36 +00:00
|
|
|
vars := data.New()
|
2018-04-14 18:14:47 +00:00
|
|
|
vars.Set(val, varKeyValue)
|
|
|
|
vars.Set(val, varKeyAfter)
|
|
|
|
vars.Set(old, varKeyBefore)
|
|
|
|
ctx = context.WithValue(ctx, ctxKeySpec, vars)
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
if chk, err := d.i.e.fetch(ctx, fd.Assert, d.current); err != nil {
|
|
|
|
return err
|
|
|
|
} else if chk, ok := chk.(bool); ok && !chk {
|
|
|
|
return &FieldError{field: key, found: val, check: fd.Assert}
|
|
|
|
}
|
2018-04-14 18:14:47 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 18:14:47 +00:00
|
|
|
// We are checking the permissions of the field
|
|
|
|
|
|
|
|
if fd.Perms != nil {
|
|
|
|
|
|
|
|
if k, ok := ctx.Value(ctxKeyKind).(cnf.Kind); ok {
|
|
|
|
if k > cnf.AuthDB {
|
|
|
|
|
|
|
|
// Reset the variables
|
|
|
|
|
2018-04-26 23:40:36 +00:00
|
|
|
vars := data.New()
|
2018-04-14 18:14:47 +00:00
|
|
|
vars.Set(val, varKeyValue)
|
|
|
|
vars.Set(val, varKeyAfter)
|
|
|
|
vars.Set(old, varKeyBefore)
|
|
|
|
ctx = context.WithValue(ctx, ctxKeySpec, vars)
|
|
|
|
|
|
|
|
switch p := fd.Perms.(type) {
|
|
|
|
case *sql.PermExpression:
|
|
|
|
switch met {
|
|
|
|
case _CREATE:
|
|
|
|
if v, err := d.i.e.fetch(ctx, p.Create, d.current); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if b, ok := v.(bool); !ok || !b {
|
|
|
|
val = old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case _UPDATE:
|
|
|
|
if v, err := d.i.e.fetch(ctx, p.Update, d.current); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if b, ok := v.(bool); !ok || !b {
|
|
|
|
val = old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case _DELETE:
|
|
|
|
if v, err := d.i.e.fetch(ctx, p.Delete, d.current); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if b, ok := v.(bool); !ok || !b {
|
|
|
|
val = old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are setting the value of the field
|
|
|
|
|
2017-12-04 10:10:29 +00:00
|
|
|
switch val.(type) {
|
|
|
|
default:
|
2018-04-26 23:40:36 +00:00
|
|
|
if exi {
|
|
|
|
d.current.Set(val, key)
|
|
|
|
} else {
|
|
|
|
d.current.Iff(val, key)
|
|
|
|
}
|
2017-12-04 10:10:29 +00:00
|
|
|
case *sql.Void:
|
|
|
|
d.current.Del(key)
|
|
|
|
}
|
2017-11-16 20:53:39 +00:00
|
|
|
|
2018-02-14 15:48:42 +00:00
|
|
|
return nil
|
2017-11-16 20:53:39 +00:00
|
|
|
|
|
|
|
}, fd.Name.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|