surrealpatch/db/delete.go

176 lines
3.7 KiB
Go
Raw Normal View History

// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
2016-09-20 23:36:50 +00:00
// 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"
2019-01-31 10:03:50 +00:00
"github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/sql"
2017-11-16 20:53:39 +00:00
"github.com/abcum/surreal/util/data"
"github.com/abcum/surreal/util/keys"
)
2017-11-16 20:53:39 +00:00
func (e *executor) executeDelete(ctx context.Context, stm *sql.DeleteStatement) ([]interface{}, error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthNO); err != nil {
return nil, err
}
2017-11-16 20:53:39 +00:00
var what sql.Exprs
for _, val := range stm.What {
w, err := e.fetch(ctx, val, nil)
if err != nil {
return nil, err
}
2017-11-16 20:53:39 +00:00
what = append(what, w)
}
2017-11-16 20:53:39 +00:00
i := newIterator(e, ctx, stm, false)
for _, w := range what {
switch what := w.(type) {
default:
2017-11-16 20:53:39 +00:00
return nil, fmt.Errorf("Can not execute DELETE query using value '%v'", what)
case *sql.Table:
2019-01-31 10:03:50 +00:00
key := &keys.Table{KV: KV, NS: e.ns, DB: e.db, TB: what.TB}
2017-11-16 20:53:39 +00:00
i.processTable(ctx, key)
case *sql.Ident:
2019-01-31 10:03:50 +00:00
key := &keys.Table{KV: KV, NS: e.ns, DB: e.db, TB: what.VA}
2017-11-16 20:53:39 +00:00
i.processTable(ctx, key)
case *sql.Thing:
2019-01-31 10:03:50 +00:00
key := &keys.Thing{KV: KV, NS: e.ns, DB: e.db, TB: what.TB, ID: what.ID}
2017-11-16 20:53:39 +00:00
i.processThing(ctx, key)
2017-11-16 20:53:39 +00:00
case *sql.Model:
2019-01-31 10:03:50 +00:00
key := &keys.Thing{KV: KV, NS: e.ns, DB: e.db, TB: what.TB, ID: nil}
2017-11-16 20:53:39 +00:00
i.processModel(ctx, key, what)
case *sql.Batch:
2019-01-31 10:03:50 +00:00
key := &keys.Thing{KV: KV, NS: e.ns, DB: e.db, TB: what.TB, ID: nil}
2017-11-16 20:53:39 +00:00
i.processBatch(ctx, key, what)
// Result of subquery
case []interface{}:
2019-01-31 10:03:50 +00:00
key := &keys.Thing{KV: KV, NS: e.ns, DB: e.db}
i.processOther(ctx, key, what)
// Result of subquery with LIMIT 1
case map[string]interface{}:
2019-01-31 10:03:50 +00:00
key := &keys.Thing{KV: KV, NS: e.ns, DB: e.db}
i.processOther(ctx, key, []interface{}{what})
}
2016-07-04 10:37:29 +00:00
}
2017-11-16 20:53:39 +00:00
return i.Yield(ctx)
}
2017-11-16 20:53:39 +00:00
func (e *executor) fetchDelete(ctx context.Context, stm *sql.DeleteStatement, doc *data.Doc) (interface{}, error) {
ctx = dive(ctx)
2017-11-16 20:53:39 +00:00
if doc != nil {
vars := data.New()
vars.Set(doc.Data(), varKeyParent)
2017-11-16 20:53:39 +00:00
ctx = context.WithValue(ctx, ctxKeySubs, vars)
}
2017-11-16 20:53:39 +00:00
out, err := e.executeDelete(ctx, stm)
if err != nil {
return nil, err
}
2017-11-16 20:53:39 +00:00
switch len(out) {
case 1:
return data.Consume(out).Get(docKeyOne, docKeyId).Data(), nil
default:
return data.Consume(out).Get(docKeyAll, docKeyId).Data(), nil
}
2017-11-16 20:53:39 +00:00
}
func (d *document) runDelete(ctx context.Context, stm *sql.DeleteStatement) (interface{}, error) {
var ok bool
var err error
var met = _DELETE
if err = d.init(ctx); err != nil {
return nil, err
}
2018-04-28 19:32:05 +00:00
if err = d.lock(ctx); err != nil {
return nil, err
}
2017-11-16 20:53:39 +00:00
if err = d.setup(ctx); err != nil {
2017-11-16 20:53:39 +00:00
return nil, err
}
2017-11-16 20:53:39 +00:00
if d.val.Exi() == false {
return nil, nil
}
2017-11-16 20:53:39 +00:00
if ok, err = d.allow(ctx, met); err != nil {
return nil, err
} else if ok == false {
return nil, nil
}
if ok, err = d.check(ctx, stm.Cond); err != nil {
return nil, err
} else if ok == false {
return nil, nil
}
if err = d.erase(); err != nil {
return nil, err
}
if err = d.purgeIndex(ctx); err != nil {
2017-11-16 20:53:39 +00:00
return nil, err
}
if err = d.purgeThing(ctx); err != nil {
return nil, err
2017-11-16 20:53:39 +00:00
}
2017-11-16 20:53:39 +00:00
if err = d.table(ctx, met); err != nil {
return nil, err
}
2017-11-16 20:53:39 +00:00
if err = d.event(ctx, met); err != nil {
return nil, err
}
if err = d.lives(ctx, met); err != nil {
return nil, err
}
2016-07-04 10:37:29 +00:00
2017-11-16 20:53:39 +00:00
return d.yield(ctx, stm, stm.Echo)
}