surrealpatch/db/define.go

230 lines
5.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"
"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) {
// Save the namespace definition
nkey := &keys.NS{KV: ast.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) {
e.dbo.AddNS(ctx, ast.NS)
// Save the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.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)
ast.Pass, _ = bcrypt.GenerateFromPassword(ast.Pass, bcrypt.DefaultCost)
2017-11-16 20:53:39 +00:00
switch ast.Kind {
case sql.NAMESPACE:
e.dbo.AddNS(ctx, ast.NS)
// Save the login definition
ukey := &keys.NU{KV: ast.KV, NS: ast.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:
e.dbo.AddDB(ctx, ast.NS, ast.DB)
// Save the login definition
ukey := &keys.DU{KV: ast.KV, NS: ast.NS, DB: ast.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:
e.dbo.AddNS(ctx, ast.NS)
// Save the token definition
tkey := &keys.NT{KV: ast.KV, NS: ast.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:
e.dbo.AddDB(ctx, ast.NS, ast.DB)
// Save the token definition
tkey := &keys.DT{KV: ast.KV, NS: ast.NS, DB: ast.DB, 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) {
ast.Code = rand.New(128)
e.dbo.AddDB(ctx, ast.NS, ast.DB)
// Remove the scope definition
skey := &keys.SC{KV: ast.KV, NS: ast.NS, DB: ast.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) {
for _, TB := range ast.What {
e.dbo.AddTB(ctx, ast.NS, ast.DB, TB.TB)
2017-11-16 20:53:39 +00:00
// Remove the event definition
ekey := &keys.EV{KV: ast.KV, NS: ast.NS, DB: ast.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
for _, TB := range ast.What {
2016-07-04 10:37:29 +00:00
e.dbo.AddTB(ctx, ast.NS, ast.DB, TB.TB)
2016-07-04 10:37:29 +00:00
// Save the field definition
fkey := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.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
for _, TB := range ast.What {
2016-07-04 10:37:29 +00:00
e.dbo.AddTB(ctx, ast.NS, ast.DB, TB.TB)
2016-07-04 10:37:29 +00:00
// Save the index definition
ikey := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.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
dkey := &keys.Index{KV: ast.KV, NS: ast.NS, DB: ast.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)
ustm := &sql.UpdateStatement{KV: ast.KV, NS: ast.NS, DB: ast.DB, 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) {
e.dbo.AddDB(ctx, ast.NS, ast.DB)
for _, TB := range ast.What {
ast.Name = sql.NewIdent(TB.TB)
// Save the table definition
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.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
dkey := &keys.Table{KV: ast.KV, NS: ast.NS, DB: ast.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
tkey := &keys.FT{KV: ast.KV, NS: ast.NS, DB: ast.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)
ustm := &sql.UpdateStatement{KV: ast.KV, NS: ast.NS, DB: ast.DB, What: []sql.Expr{FT}}
if _, err = e.executeUpdate(uctx, ustm); err != nil {
return nil, err
}
}
}
2016-07-04 10:37:29 +00:00
}
return
}