From cf79487b81715497a0654aae909d7f0036175959 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 16 Nov 2016 22:47:23 +0000 Subject: [PATCH] Add SQL DEFINE TOKEN statement --- sql/ast.go | 24 ++++++++++++++++ sql/cork.go | 60 ++++++++++++++++++++++++++++++++++++++ sql/define.go | 4 ++- sql/remove.go | 4 ++- sql/token.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 sql/token.go diff --git a/sql/ast.go b/sql/ast.go index ae53e474..44dd04e8 100644 --- a/sql/ast.go +++ b/sql/ast.go @@ -226,6 +226,30 @@ type RemoveLoginStatement struct { User string `cork:"user" codec:"user"` } +// -------------------------------------------------- +// Token +// -------------------------------------------------- + +// DefineTokenStatement represents an SQL DEFINE TOKEN statement. +type DefineTokenStatement struct { + KV string `cork:"-" codec:"-"` + NS string `cork:"-" codec:"-"` + DB string `cork:"-" codec:"-"` + Kind Token `cork:"kind" codec:"kind"` + Name string `cork:"name" codec:"name"` + Type string `cork:"type" codec:"type"` + Text string `cork:"text" codec:"text"` +} + +// RemoveTokenStatement represents an SQL REMOVE TOKEN statement. +type RemoveTokenStatement struct { + KV string `cork:"-" codec:"-"` + NS string `cork:"-" codec:"-"` + DB string `cork:"-" codec:"-"` + Kind Token `cork:"kind" codec:"kind"` + Name string `cork:"name" codec:"name"` +} + // -------------------------------------------------- // Scope // -------------------------------------------------- diff --git a/sql/cork.go b/sql/cork.go index fedf0ff1..5e52384e 100644 --- a/sql/cork.go +++ b/sql/cork.go @@ -1122,6 +1122,66 @@ func (this *RemoveLoginStatement) UnmarshalCORK(src []byte) (err error) { return } +// -------------------------------------------------- +// DefineTokenStatement +// -------------------------------------------------- + +func init() { + cork.Register(&DefineTokenStatement{}) +} + +func (this *DefineTokenStatement) ExtendCORK() byte { + return 0x81 +} + +func (this *DefineTokenStatement) MarshalCORK() (dst []byte, err error) { + b := bytes.NewBuffer(dst) + e := cork.NewEncoder(b) + e.Encode(this.Kind) + e.Encode(this.Name) + e.Encode(this.Type) + e.Encode(this.Text) + return b.Bytes(), nil +} + +func (this *DefineTokenStatement) UnmarshalCORK(src []byte) (err error) { + b := bytes.NewBuffer(src) + d := cork.NewDecoder(b) + d.Decode(&this.Kind) + d.Decode(&this.Name) + d.Decode(&this.Type) + d.Decode(&this.Text) + return +} + +// -------------------------------------------------- +// RemoveTokenStatement +// -------------------------------------------------- + +func init() { + cork.Register(&RemoveTokenStatement{}) +} + +func (this *RemoveTokenStatement) ExtendCORK() byte { + return 0x82 +} + +func (this *RemoveTokenStatement) MarshalCORK() (dst []byte, err error) { + b := bytes.NewBuffer(dst) + e := cork.NewEncoder(b) + e.Encode(this.Kind) + e.Encode(this.Name) + return b.Bytes(), nil +} + +func (this *RemoveTokenStatement) UnmarshalCORK(src []byte) (err error) { + b := bytes.NewBuffer(src) + d := cork.NewDecoder(b) + d.Decode(&this.Kind) + d.Decode(&this.Name) + return +} + // -------------------------------------------------- // DefineScopeStatement // -------------------------------------------------- diff --git a/sql/define.go b/sql/define.go index 36373f24..82211cd7 100644 --- a/sql/define.go +++ b/sql/define.go @@ -17,7 +17,7 @@ package sql func (p *parser) parseDefineStatement() (Statement, error) { // Inspect the next token. - tok, _, err := p.shouldBe(NAMESPACE, DATABASE, LOGIN, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) + tok, _, err := p.shouldBe(NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) switch tok { case NAMESPACE: @@ -26,6 +26,8 @@ func (p *parser) parseDefineStatement() (Statement, error) { return p.parseDefineDatabaseStatement() case LOGIN: return p.parseDefineLoginStatement() + case TOKEN: + return p.parseDefineTokenStatement() case SCOPE: return p.parseDefineScopeStatement() case TABLE: diff --git a/sql/remove.go b/sql/remove.go index d8891333..7e369b1c 100644 --- a/sql/remove.go +++ b/sql/remove.go @@ -17,7 +17,7 @@ package sql func (p *parser) parseRemoveStatement() (Statement, error) { // Inspect the next token. - tok, _, err := p.shouldBe(NAMESPACE, DATABASE, LOGIN, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) + tok, _, err := p.shouldBe(NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) switch tok { case NAMESPACE: @@ -26,6 +26,8 @@ func (p *parser) parseRemoveStatement() (Statement, error) { return p.parseRemoveDatabaseStatement() case LOGIN: return p.parseRemoveLoginStatement() + case TOKEN: + return p.parseRemoveTokenStatement() case SCOPE: return p.parseRemoveScopeStatement() case TABLE: diff --git a/sql/token.go b/sql/token.go new file mode 100644 index 00000000..8020aa8c --- /dev/null +++ b/sql/token.go @@ -0,0 +1,79 @@ +// 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) parseDefineTokenStatement() (stmt *DefineTokenStatement, err error) { + + stmt = &DefineTokenStatement{} + + if _, _, err = p.shouldBe(ON); err != nil { + return nil, err + } + + if stmt.Kind, _, err = p.shouldBe(NAMESPACE, DATABASE); err != nil { + return nil, err + } + + if p.is(stmt.Kind, NAMESPACE) { + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { + return nil, err + } + } + + if p.is(stmt.Kind, DATABASE) { + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { + return nil, err + } + } + + if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil { + return nil, err + } + + return + +} + +func (p *parser) parseRemoveTokenStatement() (stmt *RemoveTokenStatement, err error) { + + stmt = &RemoveTokenStatement{} + + if _, _, err = p.shouldBe(ON); err != nil { + return nil, err + } + + if stmt.Kind, _, err = p.shouldBe(NAMESPACE, DATABASE); err != nil { + return nil, err + } + + if p.is(stmt.Kind, NAMESPACE) { + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { + return nil, err + } + } + + if p.is(stmt.Kind, DATABASE) { + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { + return nil, err + } + } + + if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil { + return nil, err + } + + return + +}