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
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseData() (exp []Expr, err error) {
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-10-29 22:50:02 +00:00
|
|
|
if tok, _, exi := p.mightBe(SET, DIFF, MERGE, CONTENT); exi {
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
if p.is(tok, SET) {
|
2016-05-23 12:32:02 +00:00
|
|
|
if exp, err = p.parseSet(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 22:50:02 +00:00
|
|
|
if p.is(tok, DIFF) {
|
|
|
|
if exp, err = p.parseDiff(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
if p.is(tok, MERGE) {
|
2016-05-23 12:32:02 +00:00
|
|
|
if exp, err = p.parseMerge(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
if p.is(tok, CONTENT) {
|
2016-05-23 12:32:02 +00:00
|
|
|
if exp, err = p.parseContent(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseSet() (mul []Expr, err error) {
|
2016-05-23 12:32:02 +00:00
|
|
|
|
|
|
|
var tok Token
|
|
|
|
var lit string
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
one := &BinaryExpression{}
|
|
|
|
|
|
|
|
tok, lit, err = p.shouldBe(IDENT)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"field name"}}
|
|
|
|
}
|
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
one.LHS, err = p.declare(tok, lit)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, lit, err = p.shouldBe(EQ, INC, DEC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-06 13:30:59 +00:00
|
|
|
one.Op = tok
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-09-07 15:59:01 +00:00
|
|
|
tok, lit, err = p.shouldBe(IDENT, THING, NULL, VOID, NOW, DATE, 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 value"}}
|
|
|
|
}
|
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
one.RHS, err = p.declare(tok, lit)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mul = append(mul, one)
|
|
|
|
|
|
|
|
if _, _, exi := p.mightBe(COMMA); !exi {
|
|
|
|
p.unscan()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return mul, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseDiff() (exp []Expr, err error) {
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
one := &DiffExpression{}
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-09-14 20:49:27 +00:00
|
|
|
tok, lit, err := p.shouldBe(JSON, ARRAY, PARAM)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:45:11 +00:00
|
|
|
one.JSON, err = p.declare(tok, lit)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
exp = append(exp, one)
|
2016-05-23 12:32:02 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseMerge() (exp []Expr, err error) {
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
one := &MergeExpression{}
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-09-14 20:49:27 +00:00
|
|
|
tok, lit, err := p.shouldBe(JSON, PARAM)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:45:11 +00:00
|
|
|
one.JSON, err = p.declare(tok, lit)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
exp = append(exp, one)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseContent() (exp []Expr, err error) {
|
2016-07-04 10:37:37 +00:00
|
|
|
|
|
|
|
one := &ContentExpression{}
|
|
|
|
|
2016-09-14 20:49:27 +00:00
|
|
|
tok, lit, err := p.shouldBe(JSON, PARAM)
|
2016-07-04 10:37:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:45:11 +00:00
|
|
|
one.JSON, err = p.declare(tok, lit)
|
2016-07-04 10:37:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = append(exp, one)
|
2016-05-23 12:32:02 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|