From b9327723f827e531ddc79163c6fe3bd925713434 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 4 Nov 2016 11:34:37 +0000 Subject: [PATCH] Add SQL command for DEFINE/REMOVE DATABASE --- sql/ast.go | 18 ++++++++++++ sql/cork.go | 76 +++++++++++++++++++++++++++++++++++++++++-------- sql/database.go | 55 +++++++++++++++++++++++++++++++++++ sql/define.go | 4 ++- sql/remove.go | 4 ++- 5 files changed, 143 insertions(+), 14 deletions(-) create mode 100644 sql/database.go diff --git a/sql/ast.go b/sql/ast.go index 08cd2ef2..1ab19483 100644 --- a/sql/ast.go +++ b/sql/ast.go @@ -184,6 +184,24 @@ type RemoveNamespaceStatement struct { Name string `cork:"name" codec:"name"` } +// -------------------------------------------------- +// Database +// -------------------------------------------------- + +type DefineDatabaseStatement struct { + KV string `cork:"-" codec:"-"` + NS string `cork:"-" codec:"-"` + DB string `cork:"-" codec:"-"` + Name string `cork:"name" codec:"name"` +} + +type RemoveDatabaseStatement struct { + KV string `cork:"-" codec:"-"` + NS string `cork:"-" codec:"-"` + DB string `cork:"-" codec:"-"` + Name string `cork:"name" codec:"name"` +} + // -------------------------------------------------- // Scope // -------------------------------------------------- diff --git a/sql/cork.go b/sql/cork.go index 41c7329a..67b75715 100644 --- a/sql/cork.go +++ b/sql/cork.go @@ -1014,6 +1014,58 @@ func (this *RemoveNamespaceStatement) UnmarshalCORK(src []byte) (err error) { return } +// -------------------------------------------------- +// DefineDatabaseStatement +// -------------------------------------------------- + +func init() { + cork.Register(&DefineDatabaseStatement{}) +} + +func (this *DefineDatabaseStatement) ExtendCORK() byte { + return 0x79 +} + +func (this *DefineDatabaseStatement) MarshalCORK() (dst []byte, err error) { + b := bytes.NewBuffer(dst) + e := cork.NewEncoder(b) + e.Encode(this.Name) + return b.Bytes(), nil +} + +func (this *DefineDatabaseStatement) UnmarshalCORK(src []byte) (err error) { + b := bytes.NewBuffer(src) + d := cork.NewDecoder(b) + d.Decode(&this.Name) + return +} + +// -------------------------------------------------- +// RemoveDatabaseStatement +// -------------------------------------------------- + +func init() { + cork.Register(&RemoveDatabaseStatement{}) +} + +func (this *RemoveDatabaseStatement) ExtendCORK() byte { + return 0x80 +} + +func (this *RemoveDatabaseStatement) MarshalCORK() (dst []byte, err error) { + b := bytes.NewBuffer(dst) + e := cork.NewEncoder(b) + e.Encode(this.Name) + return b.Bytes(), nil +} + +func (this *RemoveDatabaseStatement) UnmarshalCORK(src []byte) (err error) { + b := bytes.NewBuffer(src) + d := cork.NewDecoder(b) + d.Decode(&this.Name) + return +} + // -------------------------------------------------- // DefineScopeStatement // -------------------------------------------------- @@ -1023,7 +1075,7 @@ func init() { } func (this *DefineScopeStatement) ExtendCORK() byte { - return 0x76 + return 0x81 } func (this *DefineScopeStatement) MarshalCORK() (dst []byte, err error) { @@ -1053,7 +1105,7 @@ func init() { } func (this *RemoveScopeStatement) ExtendCORK() byte { - return 0x76 + return 0x82 } func (this *RemoveScopeStatement) MarshalCORK() (dst []byte, err error) { @@ -1079,7 +1131,7 @@ func init() { } func (this *DefineTableStatement) ExtendCORK() byte { - return 0x76 + return 0x83 } func (this *DefineTableStatement) MarshalCORK() (dst []byte, err error) { @@ -1107,7 +1159,7 @@ func init() { } func (this *RemoveTableStatement) ExtendCORK() byte { - return 0x76 + return 0x84 } func (this *RemoveTableStatement) MarshalCORK() (dst []byte, err error) { @@ -1133,7 +1185,7 @@ func init() { } func (this *DefineRulesStatement) ExtendCORK() byte { - return 0x76 + return 0x85 } func (this *DefineRulesStatement) MarshalCORK() (dst []byte, err error) { @@ -1165,7 +1217,7 @@ func init() { } func (this *RemoveRulesStatement) ExtendCORK() byte { - return 0x76 + return 0x86 } func (this *RemoveRulesStatement) MarshalCORK() (dst []byte, err error) { @@ -1193,7 +1245,7 @@ func init() { } func (this *DefineFieldStatement) ExtendCORK() byte { - return 0x76 + return 0x87 } func (this *DefineFieldStatement) MarshalCORK() (dst []byte, err error) { @@ -1243,7 +1295,7 @@ func init() { } func (this *RemoveFieldStatement) ExtendCORK() byte { - return 0x76 + return 0x88 } func (this *RemoveFieldStatement) MarshalCORK() (dst []byte, err error) { @@ -1271,7 +1323,7 @@ func init() { } func (this *DefineIndexStatement) ExtendCORK() byte { - return 0x76 + return 0x89 } func (this *DefineIndexStatement) MarshalCORK() (dst []byte, err error) { @@ -1303,7 +1355,7 @@ func init() { } func (this *RemoveIndexStatement) ExtendCORK() byte { - return 0x76 + return 0x90 } func (this *RemoveIndexStatement) MarshalCORK() (dst []byte, err error) { @@ -1331,7 +1383,7 @@ func init() { } func (this *DefineViewStatement) ExtendCORK() byte { - return 0x76 + return 0x91 } func (this *DefineViewStatement) MarshalCORK() (dst []byte, err error) { @@ -1365,7 +1417,7 @@ func init() { } func (this *RemoveViewStatement) ExtendCORK() byte { - return 0x76 + return 0x92 } func (this *RemoveViewStatement) MarshalCORK() (dst []byte, err error) { diff --git a/sql/database.go b/sql/database.go new file mode 100644 index 00000000..5b10b9c4 --- /dev/null +++ b/sql/database.go @@ -0,0 +1,55 @@ +// 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) parseDefineDatabaseStatement() (stmt *DefineDatabaseStatement, err error) { + + stmt = &DefineDatabaseStatement{} + + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { + return nil, err + } + + if _, _, err = p.shouldBe(DATABASE); err != nil { + return nil, err + } + + if stmt.Name, err = p.parseName(); err != nil { + return nil, err + } + + return + +} + +func (p *parser) parseRemoveDatabaseStatement() (stmt *RemoveDatabaseStatement, err error) { + + stmt = &RemoveDatabaseStatement{} + + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { + return nil, err + } + + if _, _, err = p.shouldBe(DATABASE); err != nil { + return nil, err + } + + if stmt.Name, err = p.parseName(); err != nil { + return nil, err + } + + return + +} diff --git a/sql/define.go b/sql/define.go index 3b9c467e..d3790a1d 100644 --- a/sql/define.go +++ b/sql/define.go @@ -17,11 +17,13 @@ package sql func (p *parser) parseDefineStatement() (Statement, error) { // Inspect the next token. - tok, _, err := p.shouldBe(NAMESPACE, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) + tok, _, err := p.shouldBe(NAMESPACE, DATABASE, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) switch tok { case NAMESPACE: return p.parseDefineNamespaceStatement() + case DATABASE: + return p.parseDefineDatabaseStatement() case SCOPE: return p.parseDefineScopeStatement() case TABLE: diff --git a/sql/remove.go b/sql/remove.go index 1ea563e5..c37b1afb 100644 --- a/sql/remove.go +++ b/sql/remove.go @@ -17,11 +17,13 @@ package sql func (p *parser) parseRemoveStatement() (Statement, error) { // Inspect the next token. - tok, _, err := p.shouldBe(NAMESPACE, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) + tok, _, err := p.shouldBe(NAMESPACE, DATABASE, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) switch tok { case NAMESPACE: return p.parseRemoveNamespaceStatement() + case DATABASE: + return p.parseRemoveDatabaseStatement() case SCOPE: return p.parseRemoveScopeStatement() case TABLE: