2016-02-27 01:00:19 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2016-07-04 10:37:29 +00:00
|
|
|
// you may not use doc file except in compliance with the License.
|
2016-02-27 01:00:19 +00:00
|
|
|
// 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 (
|
2016-07-04 10:37:29 +00:00
|
|
|
"github.com/abcum/surreal/kvs"
|
2016-02-27 01:00:19 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2016-07-04 10:37:29 +00:00
|
|
|
"github.com/abcum/surreal/util/item"
|
2016-05-11 19:23:21 +00:00
|
|
|
"github.com/abcum/surreal/util/keys"
|
2016-02-27 01:00:19 +00:00
|
|
|
)
|
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
func executeDeleteStatement(txn kvs.TX, ast *sql.DeleteStatement) (out []interface{}, err error) {
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
var local bool
|
|
|
|
|
|
|
|
if ast.EX {
|
|
|
|
return append(out, ast), nil
|
2016-07-04 10:37:29 +00:00
|
|
|
}
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if txn == nil {
|
|
|
|
local = true
|
|
|
|
txn, err = db.Txn(true)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer txn.Rollback()
|
|
|
|
}
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-07-04 10:37:29 +00:00
|
|
|
for _, w := range ast.What {
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-07-04 10:37:29 +00:00
|
|
|
if what, ok := w.(*sql.Thing); ok {
|
|
|
|
key := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB, ID: what.ID}
|
|
|
|
kv, _ := txn.Get(key.Encode())
|
2016-09-06 13:33:02 +00:00
|
|
|
doc := item.New(kv, txn, key)
|
|
|
|
if ret, err := delete(doc, ast); err != nil {
|
2016-07-04 10:37:29 +00:00
|
|
|
return nil, err
|
|
|
|
} else if ret != nil {
|
|
|
|
out = append(out, ret)
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if what, ok := w.(*sql.Table); ok {
|
|
|
|
beg := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB, ID: keys.Prefix}
|
|
|
|
end := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: what.TB, ID: keys.Suffix}
|
2016-07-04 10:37:29 +00:00
|
|
|
kvs, _ := txn.RGet(beg.Encode(), end.Encode(), 0)
|
|
|
|
for _, kv := range kvs {
|
2016-09-06 13:33:02 +00:00
|
|
|
doc := item.New(kv, txn, nil)
|
|
|
|
if ret, err := delete(doc, ast); err != nil {
|
2016-07-04 10:37:29 +00:00
|
|
|
return nil, err
|
|
|
|
} else if ret != nil {
|
|
|
|
out = append(out, ret)
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:37:29 +00:00
|
|
|
}
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if local {
|
|
|
|
txn.Commit()
|
|
|
|
}
|
2016-05-11 19:23:21 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
func delete(doc *item.Doc, ast *sql.DeleteStatement) (out interface{}, err error) {
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if !doc.Allow("DELETE") {
|
|
|
|
return
|
2016-07-21 21:49:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if !doc.Check(ast.Cond) {
|
|
|
|
return
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if err = doc.Erase(); err != nil {
|
|
|
|
return
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if err = doc.PurgeIndex(); err != nil {
|
|
|
|
return
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if err = doc.PurgeThing(); err != nil {
|
|
|
|
return
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:37:29 +00:00
|
|
|
if ast.Hard {
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-09-06 13:33:02 +00:00
|
|
|
if err = doc.PurgePatch(); err != nil {
|
|
|
|
return
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:37:29 +00:00
|
|
|
out = doc.Yield(ast.Echo, sql.ID)
|
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
return
|
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|