Add SQL INFO query command

This commit is contained in:
Tobie Morgan Hitchcock 2016-09-19 11:08:44 +01:00
parent 486b666132
commit ab30c6b85e
7 changed files with 177 additions and 13 deletions

View file

@ -140,6 +140,9 @@ func execute(ctx *fibre.Context, ast *sql.Query, chn chan<- interface{}) {
case *sql.UseStatement:
continue
case *sql.InfoStatement:
res, err = executeInfoStatement(txn, stm)
case *sql.SelectStatement:
res, err = executeSelectStatement(txn, stm)
case *sql.CreateStatement:

104
db/info.go Normal file
View file

@ -0,0 +1,104 @@
// 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"
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/data"
"github.com/abcum/surreal/util/keys"
"github.com/abcum/surreal/util/pack"
)
func executeInfoStatement(txn kvs.TX, ast *sql.InfoStatement) (out []interface{}, err error) {
if ast.EX {
return append(out, ast), nil
}
if txn == nil {
txn, err = db.Txn(false)
if err != nil {
return
}
defer txn.Rollback()
}
if ast.What == "" {
res := data.New()
res.Array("tables")
res.Array("views")
// Get the table definitions
tbeg := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: keys.Prefix}
tend := &keys.TB{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: keys.Suffix}
kvs, _ := txn.RGet(tbeg.Encode(), tend.Encode(), 0)
for _, kv := range kvs {
key := &keys.TB{}
key.Decode(kv.Key())
res.Inc(key.TB, "tables")
}
out = append(out, res.Data())
} else {
res := data.New()
res.Object("rules")
res.Array("fields")
res.Array("indexes")
// Get the rules definitions
rbeg := &keys.RU{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: ast.What, RU: keys.Prefix}
rend := &keys.RU{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: ast.What, RU: keys.Suffix}
rkvs, _ := txn.RGet(rbeg.Encode(), rend.Encode(), 0)
for _, kv := range rkvs {
key, val := &keys.RU{}, &sql.DefineRulesStatement{}
key.Decode(kv.Key())
pack.Decode(kv.Val(), val)
res.Set(val, "rules", fmt.Sprintf("%v", key.RU))
}
// Get the field definitions
fbeg := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: ast.What, FD: keys.Prefix}
fend := &keys.FD{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: ast.What, FD: keys.Suffix}
fkvs, _ := txn.RGet(fbeg.Encode(), fend.Encode(), 0)
for _, kv := range fkvs {
key, val := &keys.FD{}, &sql.DefineFieldStatement{}
key.Decode(kv.Key())
pack.Decode(kv.Val(), val)
res.Inc(val, "fields")
}
// Get the field definitions
ibeg := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: ast.What, IX: keys.Prefix}
iend := &keys.IX{KV: ast.KV, NS: ast.NS, DB: ast.DB, TB: ast.What, IX: keys.Suffix}
ikvs, _ := txn.RGet(ibeg.Encode(), iend.Encode(), 0)
for _, kv := range ikvs {
key, val := &keys.IX{}, &sql.DefineIndexStatement{}
key.Decode(kv.Key())
pack.Decode(kv.Val(), val)
res.Inc(val, "indexes")
}
out = append(out, res.Data())
}
return
}

View file

@ -35,16 +35,6 @@ type Statement interface{}
// Statements represents multiple SQL ASTs
type Statements []Statement
// --------------------------------------------------
// Use
// --------------------------------------------------
// UseStatement represents a SQL USE statement.
type UseStatement struct {
NS string // Namespace
DB string // Database
}
// --------------------------------------------------
// Trans
// --------------------------------------------------
@ -58,6 +48,29 @@ type CancelStatement struct{}
// UseStatement represents a SQL COMMIT TRANSACTION statement.
type CommitStatement struct{}
// --------------------------------------------------
// Use
// --------------------------------------------------
// UseStatement represents a SQL USE statement.
type UseStatement struct {
NS string `codec:"-"`
DB string `codec:"-"`
}
// --------------------------------------------------
// Info
// --------------------------------------------------
// InfoStatement represents an SQL INFO statement.
type InfoStatement struct {
EX bool `codec:"-"`
KV string `codec:"-"`
NS string `codec:"-"`
DB string `codec:"-"`
What string `codec:"-"`
}
// --------------------------------------------------
// Normal
// --------------------------------------------------

39
sql/info.go Normal file
View file

@ -0,0 +1,39 @@
// 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 sql
func (p *parser) parseInfoStatement(explain bool) (stmt *InfoStatement, err error) {
stmt = &InfoStatement{}
stmt.EX = explain
stmt.KV = p.c.Get("KV").(string)
stmt.NS = p.c.Get("NS").(string)
stmt.DB = p.c.Get("DB").(string)
if _, _, exi := p.mightBe(FOR); exi {
if stmt.What, err = p.parseName(); err != nil {
return nil, err
}
}
if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil {
return nil, err
}
return
}

View file

@ -121,13 +121,16 @@ func (p *parser) parseSingle() (Statement, error) {
explain = true
}
tok, _, err := p.shouldBe(USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE)
tok, _, err := p.shouldBe(USE, INFO, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE)
switch tok {
case USE:
return p.parseUseStatement(explain)
case INFO:
return p.parseInfoStatement(explain)
case BEGIN:
return p.parseBeginStatement(explain)
case CANCEL, ROLLBACK:

View file

@ -140,11 +140,11 @@ func Test_Parse_Queries_Malformed(t *testing.T) {
},
{
sql: `!`,
err: "Found `!` but expected `USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE`",
err: "Found `!` but expected `USE, INFO, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE`",
},
{
sql: `SELECT * FROM person;;;`,
err: "Found `;` but expected `USE, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE`",
err: "Found `;` but expected `USE, INFO, LET, BEGIN, CANCEL, COMMIT, ROLLBACK, SELECT, CREATE, UPDATE, INSERT, UPSERT, MODIFY, DELETE, RELATE, RECORD, DEFINE, REMOVE`",
},
}

View file

@ -136,6 +136,7 @@ const (
ID
IN
INDEX
INFO
INSERT
INTO
IS
@ -296,6 +297,7 @@ var tokens = [...]string{
ID: "ID",
IN: "IN",
INDEX: "INDEX",
INFO: "INFO",
INSERT: "INSERT",
INTO: "INTO",
IS: "IS",