2021-12-14 08:12:26 +00:00
|
|
|
// Copyright © 2016 SurrealDB Ltd.
|
2016-05-23 12:32:02 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2017-03-02 10:47:10 +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
|
|
|
|
2018-01-10 13:07:09 +00:00
|
|
|
if is(tok, SET) {
|
2016-05-23 12:32:02 +00:00
|
|
|
if exp, err = p.parseSet(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:07:09 +00:00
|
|
|
if is(tok, DIFF) {
|
2016-10-29 22:50:02 +00:00
|
|
|
if exp, err = p.parseDiff(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:07:09 +00:00
|
|
|
if is(tok, MERGE) {
|
2016-05-23 12:32:02 +00:00
|
|
|
if exp, err = p.parseMerge(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:07:09 +00:00
|
|
|
if is(tok, CONTENT) {
|
2016-05-23 12:32:02 +00:00
|
|
|
if exp, err = p.parseContent(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
func (p *parser) parseSet() (mul Expr, err error) {
|
|
|
|
|
2017-04-12 17:44:01 +00:00
|
|
|
out := &DataExpression{}
|
2016-05-23 12:32:02 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
var tok Token
|
|
|
|
var lit string
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
one := &ItemExpression{}
|
2016-11-04 09:57:17 +00:00
|
|
|
|
|
|
|
// The first part of a SET expression must
|
|
|
|
// always be an identifier, specifying a
|
|
|
|
// record field to set.
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2017-11-16 20:53:13 +00:00
|
|
|
tok, lit, err = p.shouldBe(IDENT, EXPR)
|
2016-05-23 12:32:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// The next query part must be a =, +=, or
|
|
|
|
// -= operator, as this is a SET expression
|
|
|
|
// and not a binary expression.
|
|
|
|
|
|
|
|
one.Op, lit, err = p.shouldBe(EQ, INC, DEC)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// The next query part can be any expression
|
|
|
|
// including a parenthesised expression or a
|
|
|
|
// binary expression so handle accordingly.
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
one.RHS, err = p.parseExpr()
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// Append the single SET data expression to
|
|
|
|
// the array of data expressions.
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
out.Data = append(out.Data, one)
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// Check to see if the next token is a comma
|
|
|
|
// and if not, then break out of the loop,
|
|
|
|
// otherwise repeat until we find no comma.
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
if _, _, exi := p.mightBe(COMMA); !exi {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
return out, err
|
2016-05-23 12:32:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
func (p *parser) parseDiff() (exp *DiffExpression, err error) {
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
exp = &DiffExpression{}
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2017-02-10 09:49:32 +00:00
|
|
|
tok, lit, err := p.shouldBe(ARRAY, PARAM)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
exp.Data, err = p.declare(tok, lit)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
func (p *parser) parseMerge() (exp *MergeExpression, err error) {
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
exp = &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"}}
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
exp.Data, 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
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
func (p *parser) parseContent() (exp *ContentExpression, err error) {
|
2016-07-04 10:37:37 +00:00
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
exp = &ContentExpression{}
|
2016-07-04 10:37:37 +00:00
|
|
|
|
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"}}
|
|
|
|
}
|
|
|
|
|
2017-03-02 10:47:10 +00:00
|
|
|
exp.Data, err = p.declare(tok, lit)
|
2016-07-04 10:37:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"json"}}
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|