surrealpatch/db/remove.go

271 lines
6.2 KiB
Go
Raw Normal View History

// 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"
2019-01-31 10:03:50 +00:00
"github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/sql"
2016-07-04 10:37:29 +00:00
"github.com/abcum/surreal/util/keys"
)
2017-11-16 20:53:39 +00:00
func (e *executor) executeRemoveNamespace(ctx context.Context, ast *sql.RemoveNamespaceStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthKV); err != nil {
return nil, err
}
2019-11-20 13:20:27 +00:00
e.tx.DelNS(ast.Name.VA)
// Remove the namespace definition
2019-01-31 10:03:50 +00:00
nkey := &keys.NS{KV: KV, NS: ast.Name.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.Clr(ctx, nkey.Encode())
// Remove the namespace resource data
2019-01-31 10:03:50 +00:00
akey := &keys.Namespace{KV: KV, NS: ast.Name.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, akey.Encode(), 0)
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeRemoveDatabase(ctx context.Context, ast *sql.RemoveDatabaseStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthNS); err != nil {
return nil, err
}
2019-11-20 13:20:27 +00:00
e.tx.DelDB(e.ns, ast.Name.VA)
// Remove the database definition
2019-01-31 10:03:50 +00:00
dkey := &keys.DB{KV: KV, NS: e.ns, DB: ast.Name.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.Clr(ctx, dkey.Encode())
// Remove the database resource data
2019-01-31 10:03:50 +00:00
akey := &keys.Database{KV: KV, NS: e.ns, DB: ast.Name.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, akey.Encode(), 0)
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeRemoveLogin(ctx context.Context, ast *sql.RemoveLoginStatement) (out []interface{}, err error) {
2017-11-16 20:53:39 +00:00
switch ast.Kind {
case sql.NAMESPACE:
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthNS); err != nil {
return nil, err
}
// Remove the login definition
2019-01-31 10:03:50 +00:00
ukey := &keys.NU{KV: KV, NS: e.ns, US: ast.User.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, ukey.Encode(), 0)
2017-11-16 20:53:39 +00:00
case sql.DATABASE:
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
// Remove the login definition
2019-01-31 10:03:50 +00:00
ukey := &keys.DU{KV: KV, NS: e.ns, DB: e.db, US: ast.User.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, ukey.Encode(), 0)
2016-10-18 13:27:17 +00:00
}
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeRemoveToken(ctx context.Context, ast *sql.RemoveTokenStatement) (out []interface{}, err error) {
2016-07-04 10:37:29 +00:00
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
switch ast.Kind {
case sql.NAMESPACE:
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthNS); err != nil {
return nil, err
}
// Remove the token definition
2019-01-31 10:03:50 +00:00
tkey := &keys.NT{KV: KV, NS: e.ns, TK: ast.Name.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, tkey.Encode(), 0)
2017-11-16 20:53:39 +00:00
case sql.DATABASE:
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
// Remove the token definition
2019-01-31 10:03:50 +00:00
tkey := &keys.DT{KV: KV, NS: e.ns, DB: e.db, TK: ast.Name.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, tkey.Encode(), 0)
case sql.SCOPE:
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
// Remove the token definition
2019-01-31 10:03:50 +00:00
tkey := &keys.ST{KV: KV, NS: e.ns, DB: e.db, SC: ast.What.VA, TK: ast.Name.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, tkey.Encode(), 0)
2016-07-04 10:37:29 +00:00
}
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeRemoveScope(ctx context.Context, ast *sql.RemoveScopeStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
// Remove the scope definition
2019-01-31 10:03:50 +00:00
skey := &keys.SC{KV: KV, NS: e.ns, DB: e.db, SC: ast.Name.VA}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, skey.Encode(), 0)
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeRemoveEvent(ctx context.Context, ast *sql.RemoveEventStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
2017-11-16 20:53:39 +00:00
for _, TB := range ast.What {
2019-11-20 13:20:27 +00:00
e.tx.DelEV(e.ns, e.db, TB.TB, ast.Name.VA)
2017-11-16 20:53:39 +00:00
// Remove the event definition
2019-01-31 10:03:50 +00:00
ekey := &keys.EV{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB, EV: ast.Name.VA}
2019-11-20 13:20:27 +00:00
if _, err = e.tx.ClrP(ctx, ekey.Encode(), 0); err != nil {
2017-11-16 20:53:39 +00:00
return nil, err
}
}
2016-07-04 10:37:29 +00:00
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeRemoveField(ctx context.Context, ast *sql.RemoveFieldStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
2016-07-04 10:37:29 +00:00
for _, TB := range ast.What {
2019-11-20 13:20:27 +00:00
e.tx.DelFD(e.ns, e.db, TB.TB, ast.Name.VA)
2017-11-16 20:53:39 +00:00
// Remove the field definition
2019-01-31 10:03:50 +00:00
fkey := &keys.FD{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB, FD: ast.Name.VA}
2019-11-20 13:20:27 +00:00
if _, err = e.tx.ClrP(ctx, fkey.Encode(), 0); err != nil {
2017-11-16 20:53:39 +00:00
return nil, err
}
2016-07-04 10:37:29 +00:00
}
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeRemoveIndex(ctx context.Context, ast *sql.RemoveIndexStatement) (out []interface{}, err error) {
2016-07-04 10:37:29 +00:00
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
2016-07-04 10:37:29 +00:00
for _, TB := range ast.What {
2019-11-20 13:20:27 +00:00
e.tx.DelIX(e.ns, e.db, TB.TB, ast.Name.VA)
2017-11-16 20:53:39 +00:00
// Remove the index definition
2019-01-31 10:03:50 +00:00
ikey := &keys.IX{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB, IX: ast.Name.VA}
2019-11-20 13:20:27 +00:00
if _, err = e.tx.ClrP(ctx, ikey.Encode(), 0); err != nil {
2017-11-16 20:53:39 +00:00
return nil, err
}
2016-07-04 10:37:29 +00:00
// Remove the index resource data
2019-01-31 10:03:50 +00:00
dkey := &keys.Index{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB, IX: ast.Name.VA, FD: keys.Ignore}
2019-11-20 13:20:27 +00:00
if _, err = e.tx.ClrP(ctx, dkey.Encode(), 0); err != nil {
2017-11-16 20:53:39 +00:00
return nil, err
}
2016-07-04 10:37:29 +00:00
}
return
}
func (e *executor) executeRemoveTable(ctx context.Context, ast *sql.RemoveTableStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
for _, TB := range ast.What {
2019-11-20 13:20:27 +00:00
e.tx.DelTB(e.ns, e.db, TB.TB)
2019-11-20 13:20:27 +00:00
tb, err := e.tx.GetTB(ctx, e.ns, e.db, TB.TB)
if err != nil {
return nil, err
}
// Remove the table definition
2019-01-31 10:03:50 +00:00
tkey := &keys.TB{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB}
2019-11-20 13:20:27 +00:00
_, err = e.tx.Clr(ctx, tkey.Encode())
if err != nil {
return nil, err
}
// Remove the table resource data
2019-01-31 10:03:50 +00:00
dkey := &keys.Table{KV: KV, NS: e.ns, DB: e.db, TB: TB.TB}
2019-11-20 13:20:27 +00:00
_, err = e.tx.ClrP(ctx, dkey.Encode(), 0)
if err != nil {
return nil, err
}
if tb.Lock {
for _, FT := range tb.From {
// Remove the foreign table definition
2019-01-31 10:03:50 +00:00
tkey := &keys.FT{KV: KV, NS: e.ns, DB: e.db, TB: FT.TB, FT: TB.TB}
2019-11-20 13:20:27 +00:00
if _, err = e.tx.ClrP(ctx, tkey.Encode(), 0); err != nil {
return nil, err
}
}
}
}
return
}