2016-02-27 01:00:19 +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-11-16 20:53:39 +00:00
|
|
|
"fmt"
|
|
|
|
|
2017-03-03 21:39:37 +00:00
|
|
|
"context"
|
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2017-11-16 20:53:39 +00:00
|
|
|
"github.com/abcum/surreal/util/data"
|
2017-11-26 16:54:49 +00:00
|
|
|
"github.com/abcum/surreal/util/guid"
|
2017-11-16 20:53:39 +00:00
|
|
|
"github.com/abcum/surreal/util/keys"
|
2016-02-27 01:00:19 +00:00
|
|
|
)
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
func (e *executor) executeRelate(ctx context.Context, stm *sql.RelateStatement) ([]interface{}, error) {
|
|
|
|
|
|
|
|
var from, with sql.Exprs
|
|
|
|
|
|
|
|
for _, val := range stm.From {
|
|
|
|
f, err := e.fetch(ctx, val, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
from = append(from, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range stm.With {
|
|
|
|
w, err := e.fetch(ctx, val, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
with = append(with, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
i := newIterator(e, ctx, stm, false)
|
|
|
|
|
|
|
|
switch what := stm.Type.(type) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Can not execute RELATE query using value '%v'", what)
|
|
|
|
|
|
|
|
case *sql.Table:
|
2017-11-26 16:54:49 +00:00
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: what.TB, ID: guid.New().String()}
|
2017-11-16 20:53:39 +00:00
|
|
|
i.processThing(ctx, key)
|
|
|
|
|
|
|
|
case *sql.Ident:
|
2017-11-26 16:54:49 +00:00
|
|
|
key := &keys.Thing{KV: stm.KV, NS: stm.NS, DB: stm.DB, TB: what.ID, ID: guid.New().String()}
|
2017-11-16 20:53:39 +00:00
|
|
|
i.processThing(ctx, key)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.Yield(ctx)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) fetchRelate(ctx context.Context, stm *sql.RelateStatement, doc *data.Doc) (interface{}, error) {
|
|
|
|
|
|
|
|
stm.Echo = sql.AFTER
|
|
|
|
|
|
|
|
if doc != nil {
|
|
|
|
vars := data.New()
|
2017-12-04 22:20:39 +00:00
|
|
|
vars.Set(doc.Data(), varKeyParent)
|
2017-11-16 20:53:39 +00:00
|
|
|
ctx = context.WithValue(ctx, ctxKeySubs, vars)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := e.executeRelate(ctx, stm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(out) {
|
|
|
|
case 1:
|
|
|
|
return data.Consume(out).Get(docKeyOne, docKeyId).Data(), nil
|
|
|
|
default:
|
|
|
|
return data.Consume(out).Get(docKeyAll, docKeyId).Data(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *document) runRelate(ctx context.Context, stm *sql.RelateStatement) (interface{}, error) {
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
var err error
|
|
|
|
var met = _CREATE
|
|
|
|
|
|
|
|
defer d.close()
|
|
|
|
|
|
|
|
if err = d.setup(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.val.Exi() == false {
|
|
|
|
met = _UPDATE
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.merge(ctx, stm.Data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err = d.allow(ctx, met); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if ok == false {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.storeIndex(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-08 10:59:54 +00:00
|
|
|
if err = d.storeThing(); err != nil {
|
2017-11-16 20:53:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.table(ctx, met); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.event(ctx, met); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.lives(ctx, met); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-20 01:11:50 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
return d.yield(ctx, stm, stm.Echo)
|
2017-02-20 01:11:50 +00:00
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|