surrealpatch/sql/field.go

167 lines
3.3 KiB
Go
Raw Normal View History

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) {
2016-05-23 12:32:02 +00:00
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 {
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 p.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 p.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 p.is(tok, TYPE) {
2016-05-23 12:32:02 +00:00
if stmt.Type, err = p.parseType(); err != nil {
return nil, err
}
}
if p.is(tok, ENUM) {
2016-07-04 10:37:37 +00:00
if stmt.Enum, err = p.parseArray(); err != nil {
return nil, err
}
}
if p.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
}
}
if p.is(tok, MATCH) {
if stmt.Match, err = p.parseRegexp(); err != nil {
return nil, err
}
}
if p.is(tok, DEFAULT) {
2016-05-23 12:32:02 +00:00
if stmt.Default, err = p.parseDefault(); err != nil {
return nil, err
}
}
if p.is(tok, NOTNULL) {
2016-05-23 12:32:02 +00:00
stmt.Notnull = true
if tok, _, exi := p.mightBe(TRUE, FALSE); exi {
if tok == FALSE {
stmt.Notnull = false
}
}
}
if p.is(tok, READONLY) {
2016-05-23 12:32:02 +00:00
stmt.Readonly = true
if tok, _, exi := p.mightBe(TRUE, FALSE); exi {
if tok == FALSE {
stmt.Readonly = false
}
}
}
if p.is(tok, MANDATORY) {
2016-05-23 12:32:02 +00:00
stmt.Mandatory = true
if tok, _, exi := p.mightBe(TRUE, FALSE); exi {
if tok == FALSE {
stmt.Mandatory = false
}
}
}
if p.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
}
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) {
2016-05-23 12:32:02 +00:00
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
}