surrealpatch/db/define.go

245 lines
6.8 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 (
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/sql"
2016-09-14 21:27:00 +00:00
"github.com/abcum/surreal/util/item"
2016-07-04 10:37:29 +00:00
"github.com/abcum/surreal/util/keys"
"github.com/abcum/surreal/util/pack"
)
func (e *executor) executeDefineNamespaceStatement(txn kvs.TX, ast *sql.DefineNamespaceStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.Name}
_, err = txn.Put(0, nkey.Encode(), ast.Encode())
return
}
2016-10-18 13:27:17 +00:00
func (e *executor) executeDefineDatabaseStatement(txn kvs.TX, ast *sql.DefineDatabaseStatement) (out []interface{}, err error) {
// Set the namespace
2016-10-18 13:27:17 +00:00
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.Name}
_, err = txn.Put(0, dkey.Encode(), ast.Encode())
return
}
func (e *executor) executeDefineLoginStatement(txn kvs.TX, ast *sql.DefineLoginStatement) (out []interface{}, err error) {
if ast.Kind == sql.NAMESPACE {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the login
ukey := &keys.NU{KV: ast.KV, NS: ast.NS, US: ast.User}
_, err = txn.Put(0, ukey.Encode(), ast.Encode())
2016-10-18 13:27:17 +00:00
}
if ast.Kind == sql.DATABASE {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
// Set the login
ukey := &keys.DU{KV: ast.KV, NS: ast.NS, DB: ast.DB, US: ast.User}
_, err = txn.Put(0, ukey.Encode(), ast.Encode())
2016-10-18 13:27:17 +00:00
}
return
}
func (e *executor) executeDefineTokenStatement(txn kvs.TX, ast *sql.DefineTokenStatement) (out []interface{}, err error) {
2016-07-04 10:37:29 +00:00
if ast.Kind == sql.NAMESPACE {
2016-07-04 10:37:29 +00:00
// Set the namespace
2016-10-18 12:48:07 +00:00
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the token
tkey := &keys.NT{KV: ast.KV, NS: ast.NS, TK: ast.Name}
_, err = txn.Put(0, tkey.Encode(), ast.Encode())
}
if ast.Kind == sql.DATABASE {
2016-10-18 12:48:07 +00:00
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
2016-07-04 10:37:29 +00:00
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
2016-07-04 10:37:29 +00:00
// Set the token
tkey := &keys.DT{KV: ast.KV, NS: ast.NS, DB: ast.DB, TK: ast.Name}
_, err = txn.Put(0, tkey.Encode(), ast.Encode())
2016-07-04 10:37:29 +00:00
}
return
}
func (e *executor) executeDefineScopeStatement(txn kvs.TX, ast *sql.DefineScopeStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
// Set the scope
skey := &keys.SC{KV: ast.KV, NS: ast.NS, DB: ast.DB, SC: ast.Name}
_, err = txn.Put(0, skey.Encode(), ast.Encode())
2016-10-18 12:48:07 +00:00
return
}
2016-07-04 10:37:29 +00:00
func (e *executor) executeDefineTableStatement(txn kvs.TX, ast *sql.DefineTableStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
for _, TB := range ast.What {
// Set the table
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
if _, err = txn.Put(0, tkey.Encode(), ast.Encode()); err != nil {
return nil, err
}
}
return
}
func (e *executor) executeDefineFieldStatement(txn kvs.TX, ast *sql.DefineFieldStatement) (out []interface{}, err error) {
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
txn.Put(0, nkey.Encode(), nil)
2016-07-04 10:37:29 +00:00
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
txn.Put(0, dkey.Encode(), nil)
2016-10-18 12:48:07 +00:00
for _, TB := range ast.What {
2016-07-04 10:37:29 +00:00
// Set the table
2016-07-04 10:37:29 +00:00
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
tval := &sql.DefineTableStatement{What: ast.What}
txn.PutC(0, tkey.Encode(), tval.Encode(), nil)
2016-07-04 10:37:29 +00:00
// Set the field
2016-07-04 10:37:29 +00:00
fkey := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, FD: ast.Name}
if _, err = txn.Put(0, fkey.Encode(), pack.Encode(ast)); err != nil {
2016-07-04 10:37:29 +00:00
return nil, err
}
}
return
}
func (e *executor) executeDefineIndexStatement(txn kvs.TX, ast *sql.DefineIndexStatement) (out []interface{}, err error) {
2016-07-04 10:37:29 +00:00
// Set the namespace
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
txn.Put(0, nkey.Encode(), nil)
2016-07-04 10:37:29 +00:00
// Set the database
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
txn.Put(0, dkey.Encode(), nil)
2016-10-18 12:48:07 +00:00
for _, TB := range ast.What {
2016-07-04 10:37:29 +00:00
// Set the table
2016-07-04 10:37:29 +00:00
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
tval := &sql.DefineTableStatement{What: ast.What}
txn.PutC(0, tkey.Encode(), tval.Encode(), nil)
2016-07-04 10:37:29 +00:00
// Set the index
2016-07-04 10:37:29 +00:00
ikey := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB, IX: ast.Name}
if _, err = txn.Put(0, ikey.Encode(), ast.Encode()); err != nil {
2016-07-04 10:37:29 +00:00
return nil, err
}
2016-09-14 21:27:00 +00:00
// 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.DelR(0, dbeg.Encode(), dend.Encode(), 0); err != nil {
2016-09-14 21:27:00 +00:00
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.GetR(0, ibeg.Encode(), iend.Encode(), 0)
2016-09-14 21:27:00 +00:00
for _, kv := range kvs {
doc := item.New(kv, txn, nil, e.ctx)
2016-09-14 21:27:00 +00:00
if err := doc.StoreIndex(); err != nil {
return nil, err
}
}
2016-07-04 10:37:29 +00:00
}
return
}