surrealpatch/db/define.go

292 lines
6.8 KiB
Go
Raw Normal View History

// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
2017-11-16 20:53:39 +00:00
// you may not use this file except in compliance wdbh 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"
"golang.org/x/crypto/bcrypt"
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"
"github.com/abcum/surreal/util/rand"
)
2017-11-16 20:53:39 +00:00
func (e *executor) executeDefineNamespace(ctx context.Context, ast *sql.DefineNamespaceStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthKV); err != nil {
return nil, err
}
// Save the namespace definition
2019-01-31 10:03:50 +00:00
nkey := &keys.NS{KV: KV, NS: ast.Name.VA}
_, err = e.dbo.Put(ctx, 0, nkey.Encode(), ast.Encode())
return
}
2016-10-18 13:27:17 +00:00
2017-11-16 20:53:39 +00:00
func (e *executor) executeDefineDatabase(ctx context.Context, ast *sql.DefineDatabaseStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthNS); err != nil {
return nil, err
}
e.dbo.AddNS(ctx, e.ns)
// Save the database definition
2019-01-31 10:03:50 +00:00
dkey := &keys.DB{KV: KV, NS: e.ns, DB: ast.Name.VA}
_, err = e.dbo.Put(ctx, 0, dkey.Encode(), ast.Encode())
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeDefineLogin(ctx context.Context, ast *sql.DefineLoginStatement) (out []interface{}, err error) {
ast.Code = rand.New(128)
switch len(ast.Hash) {
default:
ast.Pass = ast.Hash
case 0:
ast.Pass, _ = bcrypt.GenerateFromPassword(ast.Pass, bcrypt.DefaultCost)
}
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
}
e.dbo.AddNS(ctx, e.ns)
// Save the login definition
2019-01-31 10:03:50 +00:00
ukey := &keys.NU{KV: KV, NS: e.ns, US: ast.User.VA}
_, err = e.dbo.Put(ctx, 0, ukey.Encode(), ast.Encode())
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
}
e.dbo.AddDB(ctx, e.ns, e.db)
// Save 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}
_, err = e.dbo.Put(ctx, 0, ukey.Encode(), ast.Encode())
2016-10-18 13:27:17 +00:00
}
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeDefineToken(ctx context.Context, ast *sql.DefineTokenStatement) (out []interface{}, err error) {
2016-07-04 10:37:29 +00:00
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
}
e.dbo.AddNS(ctx, e.ns)
// Save the token definition
2019-01-31 10:03:50 +00:00
tkey := &keys.NT{KV: KV, NS: e.ns, TK: ast.Name.VA}
_, err = e.dbo.Put(ctx, 0, tkey.Encode(), ast.Encode())
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
}
e.dbo.AddDB(ctx, e.ns, e.db)
// Save 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}
_, err = e.dbo.Put(ctx, 0, tkey.Encode(), ast.Encode())
case sql.SCOPE:
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
e.dbo.AddDB(ctx, e.ns, e.db)
// Save 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}
_, err = e.dbo.Put(ctx, 0, tkey.Encode(), ast.Encode())
2016-07-04 10:37:29 +00:00
}
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeDefineScope(ctx context.Context, ast *sql.DefineScopeStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
ast.Code = rand.New(128)
2019-01-31 10:03:50 +00:00
e.dbo.AddDB(ctx, e.ns, e.db)
// 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}
_, err = e.dbo.Put(ctx, 0, skey.Encode(), ast.Encode())
2016-10-18 12:48:07 +00:00
return
}
2016-07-04 10:37:29 +00:00
2017-11-16 20:53:39 +00:00
func (e *executor) executeDefineEvent(ctx context.Context, ast *sql.DefineEventStatement) (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-01-31 10:03:50 +00:00
e.dbo.AddTB(ctx, e.ns, e.db, TB.TB)
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}
if _, err = e.dbo.Put(ctx, 0, ekey.Encode(), ast.Encode()); err != nil {
2017-11-16 20:53:39 +00:00
return nil, err
}
}
return
2017-11-16 20:53:39 +00:00
}
2016-07-04 10:37:29 +00:00
2017-11-16 20:53:39 +00:00
func (e *executor) executeDefineField(ctx context.Context, ast *sql.DefineFieldStatement) (out []interface{}, err error) {
2016-10-18 12:48:07 +00:00
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 {
2016-07-04 10:37:29 +00:00
2019-01-31 10:03:50 +00:00
e.dbo.AddTB(ctx, e.ns, e.db, TB.TB)
2016-07-04 10:37:29 +00:00
// Save 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}
if _, err = e.dbo.Put(ctx, 0, fkey.Encode(), ast.Encode()); err != nil {
2016-07-04 10:37:29 +00:00
return nil, err
}
}
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeDefineIndex(ctx context.Context, ast *sql.DefineIndexStatement) (out []interface{}, err error) {
2016-10-18 12:48:07 +00:00
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 {
2016-07-04 10:37:29 +00:00
2019-01-31 10:03:50 +00:00
e.dbo.AddTB(ctx, e.ns, e.db, TB.TB)
2016-07-04 10:37:29 +00:00
// Save 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}
if _, err = e.dbo.Put(ctx, 0, ikey.Encode(), ast.Encode()); err != nil {
2016-07-04 10:37:29 +00:00
return nil, err
}
// 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}
if _, err = e.dbo.ClrP(ctx, dkey.Encode(), 0); err != nil {
2016-09-14 21:27:00 +00:00
return nil, err
}
// Process the index resource data
uctx := context.WithValue(ctx, ctxKeyForce, true)
2019-01-31 10:03:50 +00:00
ustm := &sql.UpdateStatement{What: []sql.Expr{TB}}
if _, err = e.executeUpdate(uctx, ustm); err != nil {
return nil, err
}
}
return
}
func (e *executor) executeDefineTable(ctx context.Context, ast *sql.DefineTableStatement) (out []interface{}, err error) {
2019-01-31 10:03:50 +00:00
if err := e.access(ctx, cnf.AuthDB); err != nil {
return nil, err
}
e.dbo.AddDB(ctx, e.ns, e.db)
for _, TB := range ast.What {
ast.Name = sql.NewIdent(TB.TB)
// Save 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}
if _, err = e.dbo.Put(ctx, 0, tkey.Encode(), ast.Encode()); err != nil {
2017-11-16 20:53:39 +00:00
return nil, err
2016-09-14 21:27:00 +00:00
}
if ast.Lock {
// 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}
if _, err = e.dbo.ClrP(ctx, dkey.Encode(), 0); err != nil {
return nil, err
}
for _, FT := range ast.From {
// Save 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}
if _, err = e.dbo.Put(ctx, 0, tkey.Encode(), ast.Encode()); err != nil {
return nil, err
}
// Process the table resource data
uctx := context.WithValue(ctx, ctxKeyForce, true)
2019-01-31 10:03:50 +00:00
ustm := &sql.UpdateStatement{What: []sql.Expr{FT}}
if _, err = e.executeUpdate(uctx, ustm); err != nil {
return nil, err
}
}
}
2016-07-04 10:37:29 +00:00
}
return
}