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 (
|
2016-05-11 19:23:21 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
"net/http"
|
2016-07-21 21:49:47 +00:00
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
"github.com/abcum/fibre"
|
|
|
|
"github.com/abcum/surreal/cnf"
|
2016-05-25 11:33:05 +00:00
|
|
|
"github.com/abcum/surreal/kvs"
|
2016-05-11 19:23:21 +00:00
|
|
|
"github.com/abcum/surreal/log"
|
2016-02-27 01:00:19 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2016-07-16 13:43:53 +00:00
|
|
|
|
|
|
|
_ "github.com/abcum/surreal/kvs/boltdb"
|
|
|
|
_ "github.com/abcum/surreal/kvs/mysql"
|
|
|
|
_ "github.com/abcum/surreal/kvs/pgsql"
|
2016-02-27 01:00:19 +00:00
|
|
|
)
|
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
type Response struct {
|
2016-10-27 08:33:14 +00:00
|
|
|
Time string `codec:"time,omitempty"`
|
|
|
|
Status string `codec:"status,omitempty"`
|
|
|
|
Detail string `codec:"detail,omitempty"`
|
|
|
|
Result []interface{} `codec:"result,omitempty"`
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 11:33:05 +00:00
|
|
|
var db *kvs.DB
|
2016-05-11 19:23:21 +00:00
|
|
|
|
|
|
|
// Setup sets up the connection with the data layer
|
|
|
|
func Setup(opts *cnf.Options) (err error) {
|
|
|
|
|
2016-07-19 11:04:22 +00:00
|
|
|
log.WithPrefix("db").Infof("Starting database")
|
2016-06-15 12:38:37 +00:00
|
|
|
|
2016-07-18 12:33:26 +00:00
|
|
|
db, err = kvs.New(opts)
|
2016-06-15 12:38:37 +00:00
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit shuts down the connection with the data layer
|
|
|
|
func Exit() {
|
|
|
|
|
2016-06-15 12:38:37 +00:00
|
|
|
log.WithPrefix("db").Infof("Gracefully shutting down database")
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-05-25 11:33:05 +00:00
|
|
|
db.Close()
|
2016-05-11 19:23:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute parses the query and executes it against the data layer
|
2016-09-14 21:32:52 +00:00
|
|
|
func Execute(ctx *fibre.Context, txt interface{}, vars map[string]interface{}) (out []*Response, err error) {
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-09-14 21:32:52 +00:00
|
|
|
ast, err := sql.Parse(ctx, txt, vars)
|
2016-02-27 12:05:35 +00:00
|
|
|
if err != nil {
|
2016-05-11 19:23:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
quit := make(chan bool, 1)
|
|
|
|
recv := make(chan *Response)
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
defer close(quit)
|
|
|
|
|
|
|
|
if _, ok := ctx.Response().ResponseWriter.(http.CloseNotifier); ok {
|
|
|
|
|
|
|
|
exit := ctx.Response().CloseNotify()
|
|
|
|
done := make(chan bool)
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-exit:
|
|
|
|
quit <- true
|
|
|
|
}
|
|
|
|
}()
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go execute(ctx, ast, quit, recv)
|
|
|
|
|
|
|
|
for res := range recv {
|
|
|
|
out = append(out, res)
|
2016-02-27 12:05:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 19:23:21 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
func status(e error) (s string) {
|
2016-05-25 11:32:32 +00:00
|
|
|
switch e.(type) {
|
|
|
|
default:
|
|
|
|
return "OK"
|
|
|
|
case *kvs.DBError:
|
|
|
|
return "ERR_DB"
|
|
|
|
case *kvs.TXError:
|
2016-07-16 13:44:48 +00:00
|
|
|
return "ERR_TX"
|
2016-05-25 11:32:32 +00:00
|
|
|
case *kvs.CKError:
|
|
|
|
return "ERR_CK"
|
|
|
|
case *kvs.KVError:
|
2016-07-16 13:44:48 +00:00
|
|
|
return "ERR_KV"
|
|
|
|
case error:
|
|
|
|
return "ERR"
|
2016-05-25 11:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
func detail(e error) (s string) {
|
2016-06-15 12:38:37 +00:00
|
|
|
switch err := e.(type) {
|
2016-05-25 11:32:32 +00:00
|
|
|
default:
|
2016-10-27 08:33:14 +00:00
|
|
|
return
|
2016-06-15 12:38:37 +00:00
|
|
|
case error:
|
|
|
|
return err.Error()
|
2016-05-25 11:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
func begin(txn kvs.TX) (tmp kvs.TX, err error) {
|
|
|
|
if txn == nil {
|
|
|
|
txn, err = writable()
|
2016-10-18 12:49:46 +00:00
|
|
|
}
|
2016-10-27 08:33:14 +00:00
|
|
|
return txn, err
|
2016-10-18 12:49:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
func cancel(txn kvs.TX, buf []*Response, err error, chn chan<- *Response) (kvs.TX, error, []*Response) {
|
|
|
|
|
|
|
|
if txn == nil {
|
|
|
|
return nil, nil, buf
|
|
|
|
}
|
|
|
|
|
|
|
|
txn.Cancel()
|
|
|
|
|
|
|
|
for _, v := range buf {
|
|
|
|
v.Status = "ERR"
|
|
|
|
chn <- v
|
2016-10-18 12:49:46 +00:00
|
|
|
}
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
for i := len(buf) - 1; i >= 0; i-- {
|
|
|
|
buf[len(buf)-1] = nil
|
|
|
|
buf = buf[:len(buf)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil, buf
|
|
|
|
|
2016-10-18 12:49:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
func commit(txn kvs.TX, buf []*Response, err error, chn chan<- *Response) (kvs.TX, error, []*Response) {
|
|
|
|
|
|
|
|
if txn == nil {
|
|
|
|
return nil, nil, buf
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
txn.Cancel()
|
|
|
|
} else {
|
|
|
|
txn.Commit()
|
|
|
|
}
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
for _, v := range buf {
|
|
|
|
if err != nil {
|
|
|
|
v.Status = "ERR"
|
|
|
|
}
|
|
|
|
chn <- v
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(buf) - 1; i >= 0; i-- {
|
|
|
|
buf[len(buf)-1] = nil
|
|
|
|
buf = buf[:len(buf)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil, buf
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func writable() (txn kvs.TX, err error) {
|
|
|
|
return db.Txn(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readable() (txn kvs.TX, err error) {
|
|
|
|
return db.Txn(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func execute(ctx *fibre.Context, ast *sql.Query, quit <-chan bool, send chan<- *Response) {
|
|
|
|
|
|
|
|
var err error
|
2016-09-06 13:33:02 +00:00
|
|
|
var txn kvs.TX
|
2016-10-27 08:33:14 +00:00
|
|
|
var rsp *Response
|
|
|
|
var buf []*Response
|
|
|
|
var res []interface{}
|
|
|
|
|
|
|
|
// Ensure that the query responses channel is
|
|
|
|
// closed when the full query has been processed
|
|
|
|
// and dealt with.
|
|
|
|
|
|
|
|
defer close(send)
|
|
|
|
|
|
|
|
// If we are making use of a global transaction
|
|
|
|
// which is not committed at the end of the
|
|
|
|
// query set, then cancel the transaction.
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2016-07-16 13:44:28 +00:00
|
|
|
defer func() {
|
2016-09-06 13:33:02 +00:00
|
|
|
if txn != nil {
|
2016-10-18 12:49:46 +00:00
|
|
|
txn.Cancel()
|
2016-09-06 13:33:02 +00:00
|
|
|
}
|
2016-10-27 08:33:14 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// If we have paniced during query execution
|
|
|
|
// then ensure that we recover from the error
|
|
|
|
// and print the error to the log.
|
|
|
|
|
|
|
|
defer func() {
|
2016-07-16 13:44:28 +00:00
|
|
|
if r := recover(); r != nil {
|
|
|
|
if err, ok := r.(error); ok {
|
2016-10-27 08:33:14 +00:00
|
|
|
fmt.Println(err)
|
2016-07-16 13:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-07-04 10:37:29 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
stms := make(chan sql.Statement)
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// Loop over the defined query statements and
|
|
|
|
// pass them to the statement processing
|
|
|
|
// channel for execution.
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
go func() {
|
|
|
|
for _, stm := range ast.Statements {
|
|
|
|
stms <- stm
|
|
|
|
}
|
|
|
|
close(stms)
|
|
|
|
}()
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// Listen for any new statements to process and
|
|
|
|
// at the same time listen for the quit signal
|
|
|
|
// notifying us whether the client has gone away.
|
2016-05-11 19:23:21 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
for {
|
2016-10-18 12:49:46 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
select {
|
2016-10-18 12:49:46 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
case <-quit:
|
|
|
|
return
|
2016-10-18 12:49:46 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
case stm, open := <-stms:
|
2016-10-18 12:49:46 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// If we have reached the end of the statement
|
|
|
|
// processing channel then return out of this
|
|
|
|
// for loop and exit.
|
2016-10-18 12:49:46 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
if !open {
|
|
|
|
return
|
|
|
|
}
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then reset the error to nil so that the
|
|
|
|
// next statement is not ignored.
|
2016-09-06 13:33:02 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
if txn == nil {
|
|
|
|
err = nil
|
|
|
|
}
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// Check to see if the current statement is
|
|
|
|
// a TRANSACTION statement, and if it is
|
|
|
|
// then deal with it and move on to the next.
|
|
|
|
|
|
|
|
switch stm.(type) {
|
|
|
|
case *sql.BeginStatement:
|
|
|
|
txn, err = begin(txn)
|
|
|
|
continue
|
|
|
|
case *sql.CancelStatement:
|
|
|
|
txn, err, buf = cancel(txn, buf, err, send)
|
|
|
|
continue
|
|
|
|
case *sql.CommitStatement:
|
|
|
|
txn, err, buf = commit(txn, buf, err, send)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is not a TRANSACTION statement and
|
|
|
|
// therefore we must time the execution speed
|
|
|
|
// and process the statement response.
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
// If an error has occured and we are inside
|
|
|
|
// a global transaction, then ignore all
|
|
|
|
// subsequent statements in the transaction.
|
2016-02-27 01:00:19 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
if err == nil {
|
|
|
|
res, err = operate(txn, stm)
|
2016-10-18 12:49:46 +00:00
|
|
|
} else {
|
2016-10-27 08:33:14 +00:00
|
|
|
res, err = []interface{}{}, fmt.Errorf("Query not executed")
|
2016-10-18 12:49:46 +00:00
|
|
|
}
|
2016-10-27 08:33:14 +00:00
|
|
|
|
|
|
|
rsp = &Response{
|
|
|
|
Time: time.Since(now).String(),
|
|
|
|
Status: status(err),
|
|
|
|
Detail: detail(err),
|
|
|
|
Result: append([]interface{}{}, res...),
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then we can output the statement response
|
|
|
|
// immediately to the channel.
|
|
|
|
|
|
|
|
if txn == nil {
|
|
|
|
send <- rsp
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are inside a global transaction we
|
|
|
|
// must buffer the responses for output at
|
|
|
|
// the end of the transaction.
|
|
|
|
|
|
|
|
if txn != nil {
|
|
|
|
buf = append(buf, rsp)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-10-18 12:49:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func operate(txn kvs.TX, ast sql.Statement) (res []interface{}, err error) {
|
|
|
|
|
|
|
|
var loc bool
|
|
|
|
|
|
|
|
// If we are not inside a global transaction
|
|
|
|
// then grab a new transaction, ensuring that
|
|
|
|
// it is closed at the end.
|
|
|
|
|
|
|
|
if txn == nil {
|
|
|
|
|
|
|
|
loc = true
|
|
|
|
|
|
|
|
switch ast.(type) {
|
|
|
|
case *sql.InfoStatement:
|
|
|
|
txn, err = readable()
|
|
|
|
default:
|
|
|
|
txn, err = writable()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
2016-05-11 19:23:21 +00:00
|
|
|
}
|
2016-02-27 12:16:59 +00:00
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
defer txn.Close()
|
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:33:14 +00:00
|
|
|
// Execute the defined statement, receiving the
|
|
|
|
// result set, and any errors which occured
|
|
|
|
// while processing the query.
|
|
|
|
|
|
|
|
switch stm := ast.(type) {
|
|
|
|
|
|
|
|
case *sql.InfoStatement:
|
|
|
|
res, err = executeInfoStatement(txn, stm)
|
|
|
|
|
|
|
|
case *sql.SelectStatement:
|
|
|
|
res, err = executeSelectStatement(txn, stm)
|
|
|
|
case *sql.CreateStatement:
|
|
|
|
res, err = executeCreateStatement(txn, stm)
|
|
|
|
case *sql.UpdateStatement:
|
|
|
|
res, err = executeUpdateStatement(txn, stm)
|
|
|
|
case *sql.ModifyStatement:
|
|
|
|
res, err = executeModifyStatement(txn, stm)
|
|
|
|
case *sql.DeleteStatement:
|
|
|
|
res, err = executeDeleteStatement(txn, stm)
|
|
|
|
case *sql.RelateStatement:
|
|
|
|
res, err = executeRelateStatement(txn, stm)
|
|
|
|
|
|
|
|
case *sql.DefineScopeStatement:
|
|
|
|
res, err = executeDefineScopeStatement(txn, stm)
|
|
|
|
case *sql.RemoveScopeStatement:
|
|
|
|
res, err = executeRemoveScopeStatement(txn, stm)
|
|
|
|
|
|
|
|
case *sql.DefineTableStatement:
|
|
|
|
res, err = executeDefineTableStatement(txn, stm)
|
|
|
|
case *sql.RemoveTableStatement:
|
|
|
|
res, err = executeRemoveTableStatement(txn, stm)
|
|
|
|
|
|
|
|
case *sql.DefineRulesStatement:
|
|
|
|
res, err = executeDefineRulesStatement(txn, stm)
|
|
|
|
case *sql.RemoveRulesStatement:
|
|
|
|
res, err = executeRemoveRulesStatement(txn, stm)
|
|
|
|
|
|
|
|
case *sql.DefineFieldStatement:
|
|
|
|
res, err = executeDefineFieldStatement(txn, stm)
|
|
|
|
case *sql.RemoveFieldStatement:
|
|
|
|
res, err = executeRemoveFieldStatement(txn, stm)
|
|
|
|
|
|
|
|
case *sql.DefineIndexStatement:
|
|
|
|
res, err = executeDefineIndexStatement(txn, stm)
|
|
|
|
case *sql.RemoveIndexStatement:
|
|
|
|
res, err = executeRemoveIndexStatement(txn, stm)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a local transaction for only the
|
|
|
|
// current statement, then commit or cancel
|
|
|
|
// depending on the result error.
|
|
|
|
|
|
|
|
if loc {
|
|
|
|
if err != nil {
|
|
|
|
txn.Cancel()
|
|
|
|
} else {
|
|
|
|
txn.Commit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-02-27 01:00:19 +00:00
|
|
|
}
|