surrealpatch/sql/exprs.go

315 lines
6 KiB
Go
Raw Normal View History

2016-02-26 17:27:07 +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
2016-05-24 12:51:52 +00:00
import (
"regexp"
2016-10-18 13:27:17 +00:00
"time"
2016-05-24 12:51:52 +00:00
)
2016-09-14 21:22:18 +00:00
func (p *parser) parseWhat() (mul []Expr, err error) {
for {
tok, lit, err := p.shouldBe(IDENT, NUMBER, DOUBLE, THING, PARAM)
if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"table name or record id"}}
}
if p.is(tok, IDENT, NUMBER, DOUBLE) {
one, _ := p.declare(TABLE, lit)
mul = append(mul, one)
}
if p.is(tok, THING) {
one, _ := p.declare(THING, lit)
mul = append(mul, one)
}
if p.is(tok, PARAM) {
one, _ := p.declare(PARAM, lit)
2016-09-14 21:22:18 +00:00
mul = append(mul, one)
}
// If the next token is not a comma then break the loop.
if _, _, exi := p.mightBe(COMMA); !exi {
break
}
}
return
}
2016-09-06 13:30:59 +00:00
func (p *parser) parseName() (string, error) {
2016-09-14 21:22:18 +00:00
_, lit, err := p.shouldBe(IDENT, NUMBER, DOUBLE)
2016-09-06 13:30:59 +00:00
if err != nil {
return string(""), &ParseError{Found: lit, Expected: []string{"name"}}
}
val, err := p.declare(STRING, lit)
2016-09-06 13:30:59 +00:00
return val.(string), err
}
func (p *parser) parseNames() (mul []string, err error) {
2016-09-06 13:30:59 +00:00
for {
one, err := p.parseName()
if err != nil {
return nil, err
}
mul = append(mul, one)
// If the next token is not a comma then break the loop.
if _, _, exi := p.mightBe(COMMA); !exi {
break
}
}
return
}
2016-05-23 12:32:02 +00:00
// --------------------------------------------------
//
// --------------------------------------------------
func (p *parser) parseIdent() (*Ident, error) {
2016-05-24 12:51:52 +00:00
_, lit, err := p.shouldBe(IDENT)
2016-05-24 12:51:52 +00:00
if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"name"}}
2016-05-24 12:51:52 +00:00
}
2016-02-26 17:27:07 +00:00
val, err := p.declare(IDENT, lit)
2016-02-26 17:27:07 +00:00
return val.(*Ident), err
2016-02-26 17:27:07 +00:00
}
2016-10-30 16:59:16 +00:00
func (p *parser) parseTable() (*Table, error) {
_, lit, err := p.shouldBe(IDENT, NUMBER, DOUBLE)
if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"name"}}
}
val, err := p.declare(TABLE, lit)
return val.(*Table), err
}
func (p *parser) parseThing() (*Thing, error) {
2016-02-26 17:27:07 +00:00
_, lit, err := p.shouldBe(THING)
2016-02-26 17:27:07 +00:00
if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"record id"}}
2016-02-26 17:27:07 +00:00
}
val, err := p.declare(THING, lit)
2016-05-23 12:32:02 +00:00
return val.(*Thing), err
2016-05-23 12:32:02 +00:00
}
func (p *parser) parseArray() ([]interface{}, error) {
2016-05-23 12:32:02 +00:00
2016-07-04 10:37:37 +00:00
_, lit, err := p.shouldBe(ARRAY)
2016-05-23 12:32:02 +00:00
if err != nil {
2016-07-04 10:37:37 +00:00
return nil, &ParseError{Found: lit, Expected: []string{"array"}}
2016-02-26 17:27:07 +00:00
}
val, err := p.declare(ARRAY, lit)
2016-05-23 12:32:02 +00:00
2016-07-04 10:37:37 +00:00
return val.([]interface{}), err
2016-05-23 12:32:02 +00:00
}
func (p *parser) parseNumber() (int64, error) {
2016-05-23 12:32:02 +00:00
2016-07-04 10:37:37 +00:00
_, lit, err := p.shouldBe(NUMBER)
2016-02-26 17:27:07 +00:00
if err != nil {
2016-07-04 10:37:37 +00:00
return int64(0), &ParseError{Found: lit, Expected: []string{"number"}}
2016-02-26 17:27:07 +00:00
}
val, err := p.declare(NUMBER, lit)
2016-07-04 10:37:37 +00:00
return val.(int64), err
}
func (p *parser) parseDouble() (float64, error) {
2016-07-04 10:37:37 +00:00
_, lit, err := p.shouldBe(NUMBER, DOUBLE)
if err != nil {
return float64(0), &ParseError{Found: lit, Expected: []string{"number"}}
}
val, err := p.declare(DOUBLE, lit)
2016-05-23 12:32:02 +00:00
2016-07-04 10:37:37 +00:00
return val.(float64), err
2016-02-26 17:27:07 +00:00
}
func (p *parser) parseString() (string, error) {
2016-07-04 10:37:37 +00:00
_, lit, err := p.shouldBe(STRING)
if err != nil {
return string(""), &ParseError{Found: lit, Expected: []string{"string"}}
}
val, err := p.declare(STRING, lit)
2016-07-04 10:37:37 +00:00
return val.(string), err
}
func (p *parser) parseRegion() (string, error) {
2016-02-26 17:27:07 +00:00
2016-05-23 12:32:02 +00:00
tok, lit, err := p.shouldBe(IDENT, STRING, REGION)
2016-02-26 17:27:07 +00:00
if err != nil {
2016-07-04 10:37:37 +00:00
return string(""), &ParseError{Found: lit, Expected: []string{"string"}}
}
val, err := p.declare(tok, lit)
2016-07-04 10:37:37 +00:00
return val.(string), err
}
func (p *parser) parseScript() (string, error) {
2016-07-04 10:37:37 +00:00
tok, lit, err := p.shouldBe(STRING, REGION)
if err != nil {
2016-09-06 13:30:59 +00:00
return string(""), &ParseError{Found: lit, Expected: []string{"js/lua script"}}
2016-02-26 17:27:07 +00:00
}
val, err := p.declare(tok, lit)
2016-02-26 17:27:07 +00:00
2016-07-04 10:37:37 +00:00
return val.(string), err
2016-02-26 17:27:07 +00:00
}
func (p *parser) parseRegexp() (string, error) {
tok, lit, err := p.shouldBe(REGEX)
if err != nil {
return string(""), &ParseError{Found: lit, Expected: []string{"regular expression"}}
}
val, err := p.declare(tok, lit)
return val.(*regexp.Regexp).String(), err
}
func (p *parser) parseBoolean() (bool, error) {
2016-02-26 17:27:07 +00:00
2016-05-23 12:32:02 +00:00
tok, lit, err := p.shouldBe(TRUE, FALSE)
if err != nil {
2016-07-04 10:37:37 +00:00
return bool(false), &ParseError{Found: lit, Expected: []string{"boolean"}}
2016-02-26 17:27:07 +00:00
}
val, err := p.declare(tok, lit)
2016-05-23 12:32:02 +00:00
2016-07-04 10:37:37 +00:00
return val.(bool), err
2016-05-23 12:32:02 +00:00
}
2016-10-18 13:27:17 +00:00
func (p *parser) parseDuration() (time.Duration, error) {
tok, lit, err := p.shouldBe(DURATION)
if err != nil {
return 0, &ParseError{Found: lit, Expected: []string{"duration"}}
}
val, err := p.declare(tok, lit)
return val.(time.Duration), err
}
func (p *parser) parseDefault() (interface{}, error) {
2016-05-23 12:32:02 +00:00
tok, lit, err := p.shouldBe(NULL, NOW, DATE, TIME, TRUE, FALSE, NUMBER, DOUBLE, STRING, REGION, IDENT, THING, ARRAY, JSON)
2016-02-26 17:27:07 +00:00
if err != nil {
2016-05-23 12:32:02 +00:00
return nil, err
2016-02-26 17:27:07 +00:00
}
return p.declare(tok, lit)
2016-05-23 12:32:02 +00:00
}
func (p *parser) parseExpr() (mul []*Field, err error) {
2016-05-23 12:32:02 +00:00
var tok Token
var lit string
var exi bool
2016-09-06 13:30:59 +00:00
var val interface{}
2016-05-23 12:32:02 +00:00
for {
one := &Field{}
tok, lit, err = p.shouldBe(IDENT, ID, NOW, PATH, NULL, ALL, TIME, TRUE, FALSE, STRING, REGION, NUMBER, DOUBLE, JSON, ARRAY, PARAM)
2016-05-23 12:32:02 +00:00
if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"field name"}}
}
one.Expr, err = p.declare(tok, lit)
2016-05-23 12:32:02 +00:00
if err != nil {
return
}
2016-09-06 13:30:59 +00:00
one.Alias = lit
2016-05-23 12:32:02 +00:00
// Next token might be AS
if _, _, exi = p.mightBe(AS); exi {
2016-09-06 13:30:59 +00:00
_, lit, err = p.shouldBe(IDENT)
2016-05-23 12:32:02 +00:00
if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"field alias"}}
}
val, err = p.declare(STRING, lit)
2016-05-23 12:32:02 +00:00
if err != nil {
return
}
2016-09-06 13:30:59 +00:00
one.Alias = val.(string)
2016-05-23 12:32:02 +00:00
}
mul = append(mul, one)
// If the next token is not a comma then break the loop.
if _, _, exi = p.mightBe(COMMA); !exi {
break
}
}
2016-02-26 17:27:07 +00:00
return
}