surrealpatch/db/define.go
2016-10-29 12:28:20 +01:00

199 lines
5.3 KiB
Go

// 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 (
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/item"
"github.com/abcum/surreal/util/keys"
"github.com/abcum/surreal/util/pack"
)
func (e *executor) executeDefineScopeStatement(txn kvs.TX, ast *sql.DefineScopeStatement) (out []interface{}, err error) {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the scope definition
skey := &keys.SC{KV: ast.KV, NS: ast.NS, DB: ast.DB, SC: ast.Name}
if err := txn.Put(skey.Encode(), nil); err != nil {
return nil, err
}
return
}
func (e *executor) executeDefineTableStatement(txn kvs.TX, ast *sql.DefineTableStatement) (out []interface{}, err error) {
for _, TB := range ast.What {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the table definition
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Put(tkey.Encode(), nil); err != nil {
return nil, err
}
}
return
}
func (e *executor) executeDefineRulesStatement(txn kvs.TX, ast *sql.DefineRulesStatement) (out []interface{}, err error) {
for _, TB := range ast.What {
for _, RU := range ast.When {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the table definition
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Put(tkey.Encode(), nil); err != nil {
return nil, err
}
// Set the field definition
rkey := &keys.RU{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, RU: RU}
if err := txn.Put(rkey.Encode(), pack.Encode(ast)); err != nil {
return nil, err
}
}
}
return
}
func (e *executor) executeDefineFieldStatement(txn kvs.TX, ast *sql.DefineFieldStatement) (out []interface{}, err error) {
for _, TB := range ast.What {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the table definition
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Put(tkey.Encode(), nil); err != nil {
return nil, err
}
// Set the field definition
fkey := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, FD: ast.Name}
if err := txn.Put(fkey.Encode(), pack.Encode(ast)); err != nil {
return nil, err
}
}
return
}
func (e *executor) executeDefineIndexStatement(txn kvs.TX, ast *sql.DefineIndexStatement) (out []interface{}, err error) {
for _, TB := range ast.What {
// Set the namespace definition
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
if err := txn.Put(nkey.Encode(), nil); err != nil {
return nil, err
}
// Set the database definition
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
if err := txn.Put(dkey.Encode(), nil); err != nil {
return nil, err
}
// Set the table definition
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if err := txn.Put(tkey.Encode(), nil); err != nil {
return nil, err
}
// Set the index definition
ikey := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: ast.Name}
if err := txn.Put(ikey.Encode(), pack.Encode(ast)); err != nil {
return nil, err
}
// Remove all index data
dbeg := &keys.Index{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: keys.Prefix, FD: keys.Ignore}
dend := &keys.Index{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: keys.Suffix, FD: keys.Ignore}
if err := txn.RDel(dbeg.Encode(), dend.Encode(), 0); err != nil {
return nil, err
}
// Fetch the items
ibeg := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, ID: keys.Prefix}
iend := &keys.Thing{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, ID: keys.Suffix}
kvs, _ := txn.RGet(ibeg.Encode(), iend.Encode(), 0)
for _, kv := range kvs {
doc := item.New(kv, txn, nil, e.ctx)
if err := doc.StoreIndex(); err != nil {
return nil, err
}
}
}
return
}