diff --git a/sql/ast.go b/sql/ast.go index ca354332..08cd2ef2 100644 --- a/sql/ast.go +++ b/sql/ast.go @@ -166,6 +166,24 @@ type RelateStatement struct { Echo Token `cork:"echo" codec:"echo"` } +// -------------------------------------------------- +// Namespace +// -------------------------------------------------- + +type DefineNamespaceStatement struct { + KV string `cork:"-" codec:"-"` + NS string `cork:"-" codec:"-"` + DB string `cork:"-" codec:"-"` + Name string `cork:"name" codec:"name"` +} + +type RemoveNamespaceStatement 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 e583583b..41c7329a 100644 --- a/sql/cork.go +++ b/sql/cork.go @@ -962,6 +962,58 @@ func (this *RelateStatement) UnmarshalCORK(src []byte) (err error) { return } +// -------------------------------------------------- +// DefineNamespaceStatement +// -------------------------------------------------- + +func init() { + cork.Register(&DefineNamespaceStatement{}) +} + +func (this *DefineNamespaceStatement) ExtendCORK() byte { + return 0x77 +} + +func (this *DefineNamespaceStatement) MarshalCORK() (dst []byte, err error) { + b := bytes.NewBuffer(dst) + e := cork.NewEncoder(b) + e.Encode(this.Name) + return b.Bytes(), nil +} + +func (this *DefineNamespaceStatement) UnmarshalCORK(src []byte) (err error) { + b := bytes.NewBuffer(src) + d := cork.NewDecoder(b) + d.Decode(&this.Name) + return +} + +// -------------------------------------------------- +// RemoveNamespaceStatement +// -------------------------------------------------- + +func init() { + cork.Register(&RemoveNamespaceStatement{}) +} + +func (this *RemoveNamespaceStatement) ExtendCORK() byte { + return 0x78 +} + +func (this *RemoveNamespaceStatement) MarshalCORK() (dst []byte, err error) { + b := bytes.NewBuffer(dst) + e := cork.NewEncoder(b) + e.Encode(this.Name) + return b.Bytes(), nil +} + +func (this *RemoveNamespaceStatement) UnmarshalCORK(src []byte) (err error) { + b := bytes.NewBuffer(src) + d := cork.NewDecoder(b) + d.Decode(&this.Name) + return +} + // -------------------------------------------------- // DefineScopeStatement // -------------------------------------------------- diff --git a/sql/define.go b/sql/define.go index 708b1e25..3b9c467e 100644 --- a/sql/define.go +++ b/sql/define.go @@ -17,9 +17,11 @@ package sql func (p *parser) parseDefineStatement() (Statement, error) { // Inspect the next token. - tok, _, err := p.shouldBe(SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) + tok, _, err := p.shouldBe(NAMESPACE, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) switch tok { + case NAMESPACE: + return p.parseDefineNamespaceStatement() case SCOPE: return p.parseDefineScopeStatement() case TABLE: diff --git a/sql/namespace.go b/sql/namespace.go new file mode 100644 index 00000000..7dece959 --- /dev/null +++ b/sql/namespace.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) parseDefineNamespaceStatement() (stmt *DefineNamespaceStatement, err error) { + + stmt = &DefineNamespaceStatement{} + + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthKV); err != nil { + return nil, err + } + + if _, _, err = p.shouldBe(NAMESPACE); err != nil { + return nil, err + } + + if stmt.Name, err = p.parseName(); err != nil { + return nil, err + } + + return + +} + +func (p *parser) parseRemoveNamespaceStatement() (stmt *RemoveNamespaceStatement, err error) { + + stmt = &RemoveNamespaceStatement{} + + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthKV); err != nil { + return nil, err + } + + if _, _, err = p.shouldBe(NAMESPACE); err != nil { + return nil, err + } + + if stmt.Name, err = p.parseName(); err != nil { + return nil, err + } + + return + +} diff --git a/sql/remove.go b/sql/remove.go index 4fc5a265..1ea563e5 100644 --- a/sql/remove.go +++ b/sql/remove.go @@ -17,9 +17,11 @@ package sql func (p *parser) parseRemoveStatement() (Statement, error) { // Inspect the next token. - tok, _, err := p.shouldBe(SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) + tok, _, err := p.shouldBe(NAMESPACE, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) switch tok { + case NAMESPACE: + return p.parseRemoveNamespaceStatement() case SCOPE: return p.parseRemoveScopeStatement() case TABLE: