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-09-20 23:34:21 +00:00
|
|
|
func (p *parser) parseSelectStatement() (stmt *SelectStatement, err error) {
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
stmt = &SelectStatement{}
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-11-14 13:26:07 +00:00
|
|
|
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthSC); err != nil {
|
2016-11-04 11:25:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-11-04 11:17:11 +00:00
|
|
|
if stmt.Expr, err = p.parseField(); err != nil {
|
2016-02-26 17:27:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = p.shouldBe(FROM)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
if stmt.What, err = p.parseWhat(); err != nil {
|
2016-02-26 17:27:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
if stmt.Cond, err = p.parseCond(); err != nil {
|
2016-02-26 17:27:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stmt.Group, err = p.parseGroup(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stmt.Order, err = p.parseOrder(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stmt.Limit, err = p.parseLimit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stmt.Start, err = p.parseStart(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if stmt.Version, err = p.parseVersion(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-03 17:20:18 +00:00
|
|
|
if _, _, err = p.shouldBe(EOF, RPAREN, SEMICOLON); err != nil {
|
2016-02-26 17:27:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stmt, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
func (p *parser) parseField() (mul []*Field, err error) {
|
|
|
|
|
|
|
|
var lit string
|
|
|
|
var exi bool
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
one := &Field{}
|
|
|
|
|
|
|
|
one.Expr, err = p.parseExpr()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
one.Alias = "*" // TODO need to implement default field name
|
|
|
|
|
|
|
|
// Chec to see if the next token is an AS
|
|
|
|
// clause, and if it is read the defined
|
|
|
|
// field alias name from the scanner.
|
|
|
|
|
|
|
|
if _, _, exi = p.mightBe(AS); exi {
|
|
|
|
|
|
|
|
if _, one.Alias, err = p.shouldBe(IDENT); err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"field alias"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the single expression to the array
|
|
|
|
// of return statement expressions.
|
|
|
|
|
|
|
|
mul = append(mul, one)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
if _, _, exi = p.mightBe(COMMA); !exi {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) parseWhere() (exp Expr, err error) {
|
|
|
|
|
|
|
|
// The next token that we expect to see is a
|
|
|
|
// WHERE token, and if we don't find one then
|
|
|
|
// return nil, with no error.
|
|
|
|
|
|
|
|
if _, _, exi := p.mightBe(WHERE); !exi {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.parseExpr()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseGroup() (mul []*Group, err error) {
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// The next token that we expect to see is a
|
|
|
|
// GROUP token, and if we don't find one then
|
|
|
|
// return nil, with no error.
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
if _, _, exi := p.mightBe(GROUP); !exi {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// We don't need to have a BY token, but we
|
|
|
|
// allow it so that the SQL query would read
|
|
|
|
// better when compared to english.
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
_, _, _ = p.mightBe(BY)
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
for {
|
|
|
|
|
|
|
|
var tok Token
|
|
|
|
var lit string
|
|
|
|
|
|
|
|
one := &Group{}
|
|
|
|
|
|
|
|
tok, lit, err = p.shouldBe(IDENT, ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"field name"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
one.Expr, err = p.declare(tok, lit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the single expression to the array
|
|
|
|
// of return statement expressions.
|
|
|
|
|
|
|
|
mul = append(mul, one)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
if _, _, exi := p.mightBe(COMMA); !exi {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
return
|
2016-02-26 17:27:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseOrder() (mul []*Order, err error) {
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// The next token that we expect to see is a
|
|
|
|
// ORDER token, and if we don't find one then
|
|
|
|
// return nil, with no error.
|
2016-02-26 17:27:07 +00:00
|
|
|
|
|
|
|
if _, _, exi := p.mightBe(ORDER); !exi {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// We don't need to have a BY token, but we
|
|
|
|
// allow it so that the SQL query would read
|
|
|
|
// better when compared to english.
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
_, _, _ = p.mightBe(BY)
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
var exi bool
|
|
|
|
var tok Token
|
|
|
|
var lit string
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
one := &Order{}
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
tok, lit, err = p.shouldBe(IDENT, ID)
|
2016-02-26 17:27:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"field name"}}
|
|
|
|
}
|
2016-05-23 12:32:02 +00:00
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
one.Expr, err = p.declare(tok, lit)
|
2016-05-23 12:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
if tok, lit, exi = p.mightBe(ASC, DESC); !exi {
|
2016-09-20 23:37:03 +00:00
|
|
|
tok = ASC
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
one.Dir, 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
|
|
|
// Append the single expression to the array
|
|
|
|
// of return statement expressions.
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
mul = append(mul, one)
|
2016-02-26 17:27:07 +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-02-26 17:27:07 +00:00
|
|
|
if _, _, exi := p.mightBe(COMMA); !exi {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
return
|
2016-02-26 17:27:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseLimit() (Expr, error) {
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// The next token that we expect to see is a
|
|
|
|
// LIMIT token, and if we don't find one then
|
|
|
|
// return nil, with no error.
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
if _, _, exi := p.mightBe(LIMIT); !exi {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// We don't need to have a BY token, but we
|
|
|
|
// allow it so that the SQL query would read
|
|
|
|
// better when compared to english.
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
_, _, _ = p.mightBe(BY)
|
|
|
|
|
2016-07-04 10:37:37 +00:00
|
|
|
tok, lit, err := p.shouldBe(NUMBER)
|
2016-02-26 17:27:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"limit number"}}
|
|
|
|
}
|
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
return p.declare(tok, lit)
|
2016-02-26 17:27:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseStart() (Expr, error) {
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// The next token that we expect to see is a
|
|
|
|
// START token, and if we don't find one then
|
|
|
|
// return nil, with no error.
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
if _, _, exi := p.mightBe(START); !exi {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
// We don't need to have a AT token, but we
|
|
|
|
// allow it so that the SQL query would read
|
|
|
|
// better when compared to english.
|
|
|
|
|
2016-02-26 17:27:07 +00:00
|
|
|
_, _, _ = p.mightBe(AT)
|
|
|
|
|
2016-11-04 09:57:17 +00:00
|
|
|
tok, lit, err := p.shouldBe(NUMBER)
|
2016-09-20 23:37:03 +00:00
|
|
|
if err != nil {
|
2016-11-04 09:57:17 +00:00
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"start number"}}
|
2016-02-26 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 23:37:03 +00:00
|
|
|
return p.declare(tok, lit)
|
2016-02-26 17:27:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:20:26 +00:00
|
|
|
func (p *parser) parseVersion() (Expr, error) {
|
2016-02-26 17:27:07 +00:00
|
|
|
|
2016-09-20 23:37:03 +00:00
|
|
|
if _, _, exi := p.mightBe(VERSION, ON); !exi {
|
2016-02-26 17:27:07 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:32:02 +00:00
|
|
|
tok, lit, err := p.shouldBe(DATE, TIME)
|
2016-02-26 17:27:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &ParseError{Found: lit, Expected: []string{"timestamp"}}
|
|
|
|
}
|
|
|
|
|
2016-09-07 15:40:05 +00:00
|
|
|
return p.declare(tok, lit)
|
2016-02-26 17:27:07 +00:00
|
|
|
|
|
|
|
}
|