2016-02-27 01:00:19 +00:00
|
|
|
// 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 (
|
2017-02-20 01:11:50 +00:00
|
|
|
"github.com/abcum/surreal/log"
|
2016-02-27 01:00:19 +00:00
|
|
|
"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"
|
2016-09-06 13:33:02 +00:00
|
|
|
"github.com/abcum/surreal/util/pack"
|
2016-02-27 01:00:19 +00:00
|
|
|
)
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeDefineNamespaceStatement(ast *sql.DefineNamespaceStatement) (out []interface{}, err error) {
|
2017-02-09 20:39:19 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the namespace
|
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.Name}
|
2017-02-09 20:43:24 +00:00
|
|
|
_, err = e.txn.Put(0, nkey.Encode(), ast.Encode())
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
2016-10-18 13:27:17 +00:00
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeDefineDatabaseStatement(ast *sql.DefineDatabaseStatement) (out []interface{}, err error) {
|
2017-02-09 20:39:19 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the namespace
|
2016-10-18 13:27:17 +00:00
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
|
2017-02-09 20:39:19 +00:00
|
|
|
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
// Set the database
|
|
|
|
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.Name}
|
2017-02-09 20:43:24 +00:00
|
|
|
_, err = e.txn.Put(0, dkey.Encode(), ast.Encode())
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeDefineLoginStatement(ast *sql.DefineLoginStatement) (out []interface{}, err error) {
|
2017-02-09 20:39:19 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
if ast.Kind == sql.NAMESPACE {
|
|
|
|
|
|
|
|
// Set the namespace
|
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
|
|
|
|
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
// Set the login
|
|
|
|
ukey := &keys.NU{KV: ast.KV, NS: ast.NS, US: ast.User}
|
2017-02-09 20:43:24 +00:00
|
|
|
_, err = e.txn.Put(0, ukey.Encode(), ast.Encode())
|
2016-10-18 13:27:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:39:19 +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}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
// Set the database
|
|
|
|
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
|
|
|
|
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
// Set the login
|
|
|
|
ukey := &keys.DU{KV: ast.KV, NS: ast.NS, DB: ast.DB, US: ast.User}
|
2017-02-09 20:43:24 +00:00
|
|
|
_, err = e.txn.Put(0, ukey.Encode(), ast.Encode())
|
2017-02-09 20:39:19 +00:00
|
|
|
|
2016-10-18 13:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeDefineTokenStatement(ast *sql.DefineTokenStatement) (out []interface{}, err error) {
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
if ast.Kind == sql.NAMESPACE {
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the namespace
|
2016-10-18 12:48:07 +00:00
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
|
2017-02-09 20:39:19 +00:00
|
|
|
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
// Set the token
|
|
|
|
tkey := &keys.NT{KV: ast.KV, NS: ast.NS, TK: ast.Name}
|
2017-02-09 20:43:24 +00:00
|
|
|
_, err = e.txn.Put(0, tkey.Encode(), ast.Encode())
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ast.Kind == sql.DATABASE {
|
2016-10-18 12:48:07 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the namespace
|
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
|
|
|
|
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
// Set the database
|
2016-07-04 10:37:29 +00:00
|
|
|
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
|
2017-02-09 20:39:19 +00:00
|
|
|
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the token
|
|
|
|
tkey := &keys.DT{KV: ast.KV, NS: ast.NS, DB: ast.DB, TK: ast.Name}
|
2017-02-09 20:43:24 +00:00
|
|
|
_, err = e.txn.Put(0, tkey.Encode(), ast.Encode())
|
2016-07-04 10:37:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeDefineScopeStatement(ast *sql.DefineScopeStatement) (out []interface{}, err error) {
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the namespace
|
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
|
|
|
|
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the database
|
|
|
|
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
|
|
|
|
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the scope
|
|
|
|
skey := &keys.SC{KV: ast.KV, NS: ast.NS, DB: ast.DB, SC: ast.Name}
|
2017-02-09 20:43:24 +00:00
|
|
|
_, err = e.txn.Put(0, skey.Encode(), ast.Encode())
|
2016-10-18 12:48:07 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
return
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
}
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeDefineTableStatement(ast *sql.DefineTableStatement) (out []interface{}, err error) {
|
2017-02-09 20:39:19 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the namespace
|
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
|
|
|
|
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the database
|
|
|
|
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
|
|
|
|
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
|
2017-02-09 20:39:19 +00:00
|
|
|
|
|
|
|
for _, TB := range ast.What {
|
|
|
|
|
|
|
|
// Set the table
|
|
|
|
tkey := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: TB}
|
2017-02-09 20:43:24 +00:00
|
|
|
if _, err = e.txn.Put(0, tkey.Encode(), ast.Encode()); err != nil {
|
2017-02-09 20:39:19 +00:00
|
|
|
return nil, err
|
2016-09-06 13:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeDefineFieldStatement(ast *sql.DefineFieldStatement) (out []interface{}, err error) {
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the namespace
|
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
|
2017-02-09 20:43:24 +00:00
|
|
|
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
|
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the database
|
|
|
|
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
|
2017-02-09 20:43:24 +00:00
|
|
|
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
|
|
|
|
e.txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
|
2016-10-18 12:48:07 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
for _, TB := range ast.What {
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:39:19 +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}
|
2017-02-09 20:39:19 +00:00
|
|
|
tval := &sql.DefineTableStatement{What: ast.What}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, tkey.Encode(), tval.Encode(), nil)
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:39:19 +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}
|
2017-02-09 20:43:24 +00:00
|
|
|
if _, err = e.txn.Put(0, fkey.Encode(), pack.Encode(ast)); err != nil {
|
2016-07-04 10:37:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:43:24 +00:00
|
|
|
func (e *executor) executeDefineIndexStatement(ast *sql.DefineIndexStatement) (out []interface{}, err error) {
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-20 01:11:50 +00:00
|
|
|
log.WithPrefix("sql").WithFields(map[string]interface{}{
|
|
|
|
"ns": ast.NS,
|
|
|
|
"db": ast.DB,
|
|
|
|
}).Debugln(ast)
|
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the namespace
|
|
|
|
nkey := &keys.NS{KV: ast.KV, NS: ast.NS}
|
2017-02-09 20:43:24 +00:00
|
|
|
nval := &sql.DefineNamespaceStatement{Name: ast.NS}
|
|
|
|
e.txn.PutC(0, nkey.Encode(), nval.Encode(), nil)
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
// Set the database
|
|
|
|
dkey := &keys.DB{KV: ast.KV, NS: ast.NS, DB: ast.DB}
|
2017-02-09 20:43:24 +00:00
|
|
|
dval := &sql.DefineDatabaseStatement{Name: ast.DB}
|
|
|
|
e.txn.PutC(0, dkey.Encode(), dval.Encode(), nil)
|
2016-10-18 12:48:07 +00:00
|
|
|
|
2017-02-09 20:39:19 +00:00
|
|
|
for _, TB := range ast.What {
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:39:19 +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}
|
2017-02-09 20:39:19 +00:00
|
|
|
tval := &sql.DefineTableStatement{What: ast.What}
|
2017-02-09 20:43:24 +00:00
|
|
|
e.txn.PutC(0, tkey.Encode(), tval.Encode(), nil)
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2017-02-09 20:39:19 +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}
|
2017-02-09 20:43:24 +00:00
|
|
|
if _, err = e.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}
|
2017-02-09 20:43:24 +00:00
|
|
|
if _, err = e.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}
|
2017-02-09 20:43:24 +00:00
|
|
|
kvs, _ := e.txn.GetR(0, ibeg.Encode(), iend.Encode(), 0)
|
2016-09-14 21:27:00 +00:00
|
|
|
for _, kv := range kvs {
|
2017-02-09 20:43:24 +00:00
|
|
|
doc := item.New(kv, e.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
|
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|