diff --git a/db/db.go b/db/db.go index f1b697bd..e0d0f43c 100644 --- a/db/db.go +++ b/db/db.go @@ -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: diff --git a/db/info.go b/db/info.go new file mode 100644 index 00000000..f4d1de5a --- /dev/null +++ b/db/info.go @@ -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 + +} diff --git a/sql/ast.go b/sql/ast.go index 0b4087ce..58806d5a 100644 --- a/sql/ast.go +++ b/sql/ast.go @@ -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 // -------------------------------------------------- diff --git a/sql/info.go b/sql/info.go new file mode 100644 index 00000000..fd81de48 --- /dev/null +++ b/sql/info.go @@ -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 + +} diff --git a/sql/parser.go b/sql/parser.go index 8e88c0b4..1b74458a 100644 --- a/sql/parser.go +++ b/sql/parser.go @@ -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: diff --git a/sql/sql_test.go b/sql/sql_test.go index 6eb56072..1dbb9d9a 100644 --- a/sql/sql_test.go +++ b/sql/sql_test.go @@ -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`", }, } diff --git a/sql/token.go b/sql/token.go index 21aa1b00..4562044e 100644 --- a/sql/token.go +++ b/sql/token.go @@ -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",