From a66c604c2fce164c49ff086030ed60c052692fcd Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 16 Nov 2016 22:43:36 +0000 Subject: [PATCH] Add SQL DEFINE LOGIN statement --- sql/ast.go | 23 +++++++++++ sql/cork.go | 58 ++++++++++++++++++++++++++++ sql/define.go | 4 +- sql/login.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ sql/remove.go | 4 +- 5 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 sql/login.go diff --git a/sql/ast.go b/sql/ast.go index 54cf1853..ae53e474 100644 --- a/sql/ast.go +++ b/sql/ast.go @@ -203,6 +203,29 @@ type RemoveDatabaseStatement struct { Name string `cork:"name" codec:"name"` } +// -------------------------------------------------- +// Login +// -------------------------------------------------- + +// DefineLoginStatement represents an SQL DEFINE LOGIN statement. +type DefineLoginStatement struct { + KV string `cork:"-" codec:"-"` + NS string `cork:"-" codec:"-"` + DB string `cork:"-" codec:"-"` + Kind Token `cork:"kind" codec:"kind"` + User string `cork:"user" codec:"user"` + Pass string `cork:"pass" codec:"pass"` +} + +// RemoveLoginStatement represents an SQL REMOVE LOGIN statement. +type RemoveLoginStatement struct { + KV string `cork:"-" codec:"-"` + NS string `cork:"-" codec:"-"` + DB string `cork:"-" codec:"-"` + Kind Token `cork:"kind" codec:"kind"` + User string `cork:"user" codec:"user"` +} + // -------------------------------------------------- // Scope // -------------------------------------------------- diff --git a/sql/cork.go b/sql/cork.go index e3f8fd2b..fedf0ff1 100644 --- a/sql/cork.go +++ b/sql/cork.go @@ -1064,6 +1064,64 @@ func (this *RemoveDatabaseStatement) UnmarshalCORK(src []byte) (err error) { return } +// -------------------------------------------------- +// DefineLoginStatement +// -------------------------------------------------- + +func init() { + cork.Register(&DefineLoginStatement{}) +} + +func (this *DefineLoginStatement) ExtendCORK() byte { + return 0x81 +} + +func (this *DefineLoginStatement) MarshalCORK() (dst []byte, err error) { + b := bytes.NewBuffer(dst) + e := cork.NewEncoder(b) + e.Encode(this.Kind) + e.Encode(this.User) + e.Encode(this.Pass) + return b.Bytes(), nil +} + +func (this *DefineLoginStatement) UnmarshalCORK(src []byte) (err error) { + b := bytes.NewBuffer(src) + d := cork.NewDecoder(b) + d.Decode(&this.Kind) + d.Decode(&this.User) + d.Decode(&this.Pass) + return +} + +// -------------------------------------------------- +// RemoveLoginStatement +// -------------------------------------------------- + +func init() { + cork.Register(&RemoveLoginStatement{}) +} + +func (this *RemoveLoginStatement) ExtendCORK() byte { + return 0x82 +} + +func (this *RemoveLoginStatement) MarshalCORK() (dst []byte, err error) { + b := bytes.NewBuffer(dst) + e := cork.NewEncoder(b) + e.Encode(this.Kind) + e.Encode(this.User) + return b.Bytes(), nil +} + +func (this *RemoveLoginStatement) UnmarshalCORK(src []byte) (err error) { + b := bytes.NewBuffer(src) + d := cork.NewDecoder(b) + d.Decode(&this.Kind) + d.Decode(&this.User) + return +} + // -------------------------------------------------- // DefineScopeStatement // -------------------------------------------------- diff --git a/sql/define.go b/sql/define.go index d3790a1d..36373f24 100644 --- a/sql/define.go +++ b/sql/define.go @@ -17,13 +17,15 @@ package sql func (p *parser) parseDefineStatement() (Statement, error) { // Inspect the next token. - tok, _, err := p.shouldBe(NAMESPACE, DATABASE, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) + tok, _, err := p.shouldBe(NAMESPACE, DATABASE, LOGIN, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) switch tok { case NAMESPACE: return p.parseDefineNamespaceStatement() case DATABASE: return p.parseDefineDatabaseStatement() + case LOGIN: + return p.parseDefineLoginStatement() case SCOPE: return p.parseDefineScopeStatement() case TABLE: diff --git a/sql/login.go b/sql/login.go new file mode 100644 index 00000000..6596fb2f --- /dev/null +++ b/sql/login.go @@ -0,0 +1,103 @@ +// 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) parseDefineLoginStatement() (stmt *DefineLoginStatement, err error) { + + stmt = &DefineLoginStatement{} + + 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(USERNAME); err != nil { + return nil, err + } + + if stmt.User, err = p.parseString(); err != nil { + return nil, err + } + + if _, _, err := p.shouldBe(PASSWORD); err != nil { + return nil, err + } + + if stmt.Pass, err = p.parseString(); err != nil { + return nil, err + } + + if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil { + return nil, err + } + + return + +} + +func (p *parser) parseRemoveLoginStatement() (stmt *RemoveLoginStatement, err error) { + + stmt = &RemoveLoginStatement{} + + 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(USERNAME); err != nil { + return nil, err + } + + if stmt.User, err = p.parseString(); err != nil { + return nil, err + } + + if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil { + return nil, err + } + + return + +} diff --git a/sql/remove.go b/sql/remove.go index c37b1afb..d8891333 100644 --- a/sql/remove.go +++ b/sql/remove.go @@ -17,13 +17,15 @@ package sql func (p *parser) parseRemoveStatement() (Statement, error) { // Inspect the next token. - tok, _, err := p.shouldBe(NAMESPACE, DATABASE, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) + tok, _, err := p.shouldBe(NAMESPACE, DATABASE, LOGIN, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) switch tok { case NAMESPACE: return p.parseRemoveNamespaceStatement() case DATABASE: return p.parseRemoveDatabaseStatement() + case LOGIN: + return p.parseRemoveLoginStatement() case SCOPE: return p.parseRemoveScopeStatement() case TABLE: