surrealpatch/db/db.go

255 lines
5.2 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 (
"fmt"
"time"
2016-07-21 21:49:47 +00:00
"runtime/debug"
"github.com/abcum/fibre"
"github.com/abcum/surreal/cnf"
2016-05-25 11:33:05 +00:00
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/log"
"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"
)
type Response struct {
Time string `codec:"time,omitempty"`
Status interface{} `codec:"status,omitempty"`
Detail interface{} `codec:"detail,omitempty"`
Result interface{} `codec:"result,omitempty"`
}
2016-05-25 11:33:05 +00:00
var db *kvs.DB
// Setup sets up the connection with the data layer
func Setup(opts *cnf.Options) (err error) {
log.WithPrefix("db").Infof("Starting database")
2016-06-15 12:38:37 +00:00
db, err = kvs.New(opts)
2016-06-15 12:38:37 +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-25 11:33:05 +00:00
db.Close()
}
// 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-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 {
return
}
chn := make(chan interface{})
go execute(ctx, ast, chn)
for msg := range chn {
switch res := msg.(type) {
case error:
return nil, res
case *Response:
out = append(out, res)
}
2016-02-27 12:05:35 +00:00
}
return
}
2016-05-25 11:32:32 +00:00
func status(e error) interface{} {
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
}
}
func detail(e error) interface{} {
2016-06-15 12:38:37 +00:00
switch err := e.(type) {
2016-05-25 11:32:32 +00:00
default:
return nil
2016-06-15 12:38:37 +00:00
case error:
return err.Error()
2016-05-25 11:32:32 +00:00
}
}
2016-10-18 12:49:46 +00:00
func writable(cur kvs.TX, tmp bool) (txn kvs.TX, err error, loc bool) {
if cur == nil {
cur, err = db.Txn(true)
}
return cur, err, tmp
}
func readable(cur kvs.TX, tmp bool) (txn kvs.TX, err error, loc bool) {
if cur == nil {
cur, err = db.Txn(false)
}
return cur, err, tmp
}
func execute(ctx *fibre.Context, ast *sql.Query, chn chan<- interface{}) {
var txn kvs.TX
defer func() {
if txn != nil {
2016-10-18 12:49:46 +00:00
txn.Cancel()
}
if r := recover(); r != nil {
if err, ok := r.(error); ok {
2016-07-21 21:49:47 +00:00
fmt.Printf("%s", debug.Stack())
chn <- err
}
}
close(chn)
}()
2016-07-04 10:37:29 +00:00
2016-02-27 12:05:35 +00:00
for _, s := range ast.Statements {
2016-10-18 12:49:46 +00:00
var loc bool
var err error
2016-10-18 12:49:46 +00:00
var res []interface{}
now := time.Now()
2016-10-18 12:49:46 +00:00
switch s.(type) {
case *sql.UseStatement:
continue
2016-10-18 12:49:46 +00:00
case *sql.BeginStatement:
break
case *sql.CancelStatement:
break
case *sql.CommitStatement:
break
case *sql.InfoStatement:
txn, err, loc = readable(txn, txn == nil)
default:
txn, err, loc = writable(txn, txn == nil)
}
if err != nil {
chn <- err
}
switch stm := s.(type) {
case *sql.CommitStatement:
txn.Commit()
txn = nil
continue
case *sql.CancelStatement:
txn.Cancel()
txn = nil
continue
case *sql.BeginStatement:
txn, err, loc = writable(txn, false)
continue
2016-09-19 10:08:44 +00:00
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)
2016-10-18 13:27:17 +00:00
case *sql.DefineScopeStatement:
res, err = executeDefineScopeStatement(txn, stm)
case *sql.RemoveScopeStatement:
res, err = executeRemoveScopeStatement(txn, stm)
2016-05-25 10:47:11 +00:00
case *sql.DefineTableStatement:
res, err = executeDefineTableStatement(txn, stm)
2016-05-25 10:47:11 +00:00
case *sql.RemoveTableStatement:
res, err = executeRemoveTableStatement(txn, stm)
2016-05-25 10:47:11 +00:00
2016-09-19 10:12:08 +00:00
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)
}
2016-10-18 12:49:46 +00:00
if err != nil {
chn <- err
}
2016-10-18 12:49:46 +00:00
if loc {
if err != nil {
txn.Cancel()
} else {
txn.Commit()
}
txn = nil
}
chn <- &Response{
Time: time.Since(now).String(),
2016-05-25 11:32:32 +00:00
Status: status(err),
Detail: detail(err),
Result: append([]interface{}{}, res...),
}
}
}