2016-05-23 12:32:02 +00:00
|
|
|
// 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) parseDefineFieldStatement(explain bool) (stmt *DefineFieldStatement, err error) {
|
|
|
|
|
|
|
|
stmt = &DefineFieldStatement{}
|
|
|
|
|
|
|
|
stmt.EX = explain
|
|
|
|
|
|
|
|
stmt.KV = p.c.Get("KV").(string)
|
|
|
|
stmt.NS = p.c.Get("NS").(string)
|
|
|
|
stmt.DB = p.c.Get("DB").(string)
|
|
|
|
|
2016-09-06 13:30:59 +00:00
|
|
|
if stmt.Name, err = p.parseName(); err != nil {
|
2016-05-23 12:32:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err = p.shouldBe(ON); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-06 13:30:59 +00:00
|
|
|
if stmt.What, err = p.parseNames(); err != nil {
|
2016-05-23 12:32:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
2016-07-21 21:48:32 +00:00
|
|
|
tok, _, exi := p.mightBe(MIN, MAX, TYPE, ENUM, CODE, MATCH, DEFAULT, NOTNULL, READONLY, MANDATORY, VALIDATE)
|
2016-05-23 12:32:02 +00:00
|
|
|
if !exi {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if is(tok, MIN) {
|
2016-07-04 10:37:37 +00:00
|
|
|
if stmt.Min, err = p.parseDouble(); err != nil {
|
2016-05-23 12:32:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is(tok, MAX) {
|
2016-07-04 10:37:37 +00:00
|
|
|
if stmt.Max, err = p.parseDouble(); err != nil {
|
2016-05-23 12:32:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is(tok, TYPE) {
|
|
|
|
if stmt.Type, err = p.parseType(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
if is(tok, ENUM) {
|
|
|
|
if stmt.Enum, err = p.parseArray(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
if is(tok, CODE) {
|
2016-07-04 10:37:37 +00:00
|
|
|
if stmt.Code, err = p.parseScript(); err != nil {
|
2016-05-23 12:32:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:48:32 +00:00
|
|
|
if is(tok, MATCH) {
|
|
|
|
if stmt.Match, err = p.parseRegexp(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
if is(tok, DEFAULT) {
|
|
|
|
if stmt.Default, err = p.parseDefault(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is(tok, NOTNULL) {
|
|
|
|
stmt.Notnull = true
|
|
|
|
if tok, _, exi := p.mightBe(TRUE, FALSE); exi {
|
|
|
|
if tok == FALSE {
|
|
|
|
stmt.Notnull = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is(tok, READONLY) {
|
|
|
|
stmt.Readonly = true
|
|
|
|
if tok, _, exi := p.mightBe(TRUE, FALSE); exi {
|
|
|
|
if tok == FALSE {
|
|
|
|
stmt.Readonly = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is(tok, MANDATORY) {
|
|
|
|
stmt.Mandatory = true
|
|
|
|
if tok, _, exi := p.mightBe(TRUE, FALSE); exi {
|
|
|
|
if tok == FALSE {
|
|
|
|
stmt.Mandatory = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:48:32 +00:00
|
|
|
if is(tok, VALIDATE) {
|
|
|
|
stmt.Validate = true
|
|
|
|
if tok, _, exi := p.mightBe(TRUE, FALSE); exi {
|
|
|
|
if tok == FALSE {
|
|
|
|
stmt.Validate = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:48:32 +00:00
|
|
|
if stmt.Type == "" {
|
2016-05-23 12:32:02 +00:00
|
|
|
return nil, &ParseError{Found: "", Expected: []string{"TYPE"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) parseRemoveFieldStatement(explain bool) (stmt *RemoveFieldStatement, err error) {
|
|
|
|
|
|
|
|
stmt = &RemoveFieldStatement{}
|
|
|
|
|
|
|
|
stmt.EX = explain
|
|
|
|
|
|
|
|
stmt.KV = p.c.Get("KV").(string)
|
|
|
|
stmt.NS = p.c.Get("NS").(string)
|
|
|
|
stmt.DB = p.c.Get("DB").(string)
|
|
|
|
|
2016-09-06 13:30:59 +00:00
|
|
|
if stmt.Name, err = p.parseName(); err != nil {
|
2016-05-23 12:32:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err = p.shouldBe(ON); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-06 13:30:59 +00:00
|
|
|
if stmt.What, err = p.parseNames(); err != nil {
|
2016-05-23 12:32:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|