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.
2016-05-23 12:32:02 +00:00
package sql
2016-02-26 17:27:07 +00:00
import (
2017-11-16 20:53:13 +00:00
"fmt"
2016-02-26 17:27:07 +00:00
"testing"
2016-05-23 12:32:02 +00:00
"time"
2016-02-26 17:27:07 +00:00
. "github.com/smartystreets/goconvey/convey"
)
type tester struct {
skip bool
sql string
err string
2017-11-16 20:53:13 +00:00
str string
2016-05-23 12:32:02 +00:00
res Statement
2016-02-26 17:27:07 +00:00
}
func testerr ( err error ) string {
if err != nil {
return err . Error ( )
}
return ""
}
2017-11-16 20:53:13 +00:00
type Selfer interface { }
2016-05-23 12:32:02 +00:00
func testsql ( t * testing . T , test tester ) {
2016-02-26 17:27:07 +00:00
if test . skip {
Convey ( " ❗️ " + test . sql , t , nil )
return
}
2019-01-31 10:03:50 +00:00
s , e := Parse ( test . sql )
2016-05-23 12:32:02 +00:00
2016-02-26 17:27:07 +00:00
Convey ( test . sql , t , func ( ) {
if test . err != "" {
Convey ( testerr ( e ) , func ( ) {
So ( testerr ( e ) , ShouldResemble , test . err )
} )
}
2017-11-16 20:53:13 +00:00
if test . err == "" {
So ( e , ShouldBeNil )
So ( s , ShouldResemble , test . res )
if test . str != "" {
So ( fmt . Sprint ( test . res . ( * Query ) . Statements [ 0 ] ) , ShouldEqual , test . str )
} else {
So ( fmt . Sprint ( test . res . ( * Query ) . Statements [ 0 ] ) , ShouldEqual , test . sql )
}
}
2016-02-26 17:27:07 +00:00
} )
}
2016-05-23 12:32:02 +00:00
func TestMain ( t * testing . T ) {
2016-02-26 17:27:07 +00:00
var tests = [ ] tester {
2016-02-28 09:38:12 +00:00
{
2016-05-23 12:32:02 +00:00
sql : ` USE ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `NAMESPACE, DATABASE, NS, DB`" ,
2016-02-28 09:38:12 +00:00
} ,
2016-02-26 17:27:07 +00:00
{
2016-05-23 12:32:02 +00:00
sql : ` USE NAMESPACE ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `IDENT, STRING, NUMBER, DOUBLE, DATE, TIME`" ,
2017-04-14 12:24:53 +00:00
} ,
{
sql : ` USE NAMESPACE '' ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `namespace name`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` USE NAMESPACE name ` ,
res : & Query { Statements : [ ] Statement { & UseStatement {
NS : "name" ,
} } } ,
} ,
{
sql : ` USE NAMESPACE 1 ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & UseStatement {
NS : "1" ,
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` USE NAMESPACE 1.3000 ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & UseStatement {
NS : "1.3000" ,
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` USE NAMESPACE 123.123.123.123 ` ,
res : & Query { Statements : [ ] Statement { & UseStatement {
NS : "123.123.123.123" ,
} } } ,
} ,
{
sql : ` USE NAMESPACE { "some":"thing"} ` ,
2017-11-16 20:53:13 +00:00
err : "Found `{\"some\":\"thing\"}` but expected `IDENT, STRING, NUMBER, DOUBLE, DATE, TIME`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` USE NAMESPACE name something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-04-14 12:24:53 +00:00
sql : ` USE DATABASE ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `IDENT, STRING, NUMBER, DOUBLE, DATE, TIME`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-04-14 12:24:53 +00:00
sql : ` USE DATABASE '' ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `database name`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` USE DATABASE name ` ,
res : & Query { Statements : [ ] Statement { & UseStatement {
DB : "name" ,
} } } ,
} ,
{
sql : ` USE DATABASE 1 ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & UseStatement {
DB : "1" ,
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` USE DATABASE 1.3000 ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & UseStatement {
DB : "1.3000" ,
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` USE DATABASE 123.123.123.123 ` ,
res : & Query { Statements : [ ] Statement { & UseStatement {
DB : "123.123.123.123" ,
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` USE DATABASE { } ` ,
err : "Found `{}` but expected `IDENT, STRING, NUMBER, DOUBLE, DATE, TIME`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` USE DATABASE name something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
} ,
{
sql : "USE NS `*` DB `*`" ,
str : "USE NAMESPACE `*` DATABASE `*`" ,
res : & Query { Statements : [ ] Statement { & UseStatement {
NS : "*" ,
DB : "*" ,
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-04-14 12:24:53 +00:00
sql : "USE NAMESPACE `*` DATABASE `*`" ,
2016-11-04 12:15:51 +00:00
res : & Query { Statements : [ ] Statement { & UseStatement {
NS : "*" ,
DB : "*" ,
} } } ,
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
2021-05-12 15:37:50 +00:00
func Test_Parse_IDs ( t * testing . T ) {
Convey ( "All IDs should parse correctly" , t , func ( ) {
So ( ParseThing ( "test:tester" ) , ShouldResemble , & Thing {
TB : "test" ,
ID : string ( "tester" ) ,
} )
So ( ParseThing ( "test:123456" ) , ShouldResemble , & Thing {
TB : "test" ,
ID : float64 ( 123456 ) ,
} )
So ( ParseThing ( "test:abcdef" ) , ShouldResemble , & Thing {
TB : "test" ,
ID : string ( "abcdef" ) ,
} )
So ( ParseThing ( "test:a1b2c3" ) , ShouldResemble , & Thing {
TB : "test" ,
ID : string ( "a1b2c3" ) ,
} )
So ( ParseThing ( "test:1a2b3c" ) , ShouldResemble , & Thing {
TB : "test" ,
ID : string ( "1a2b3c" ) ,
} )
So ( ParseThing ( "test:4m9sms" ) , ShouldResemble , & Thing {
TB : "test" ,
ID : string ( "4m9sms" ) ,
} )
So ( ParseThing ( "test:4m98m85ms" ) , ShouldResemble , & Thing {
TB : "test" ,
ID : string ( "4m98m85ms" ) ,
} )
So ( ParseThing ( "test:00s5w36sm" ) , ShouldResemble , & Thing {
TB : "test" ,
ID : string ( "00s5w36sm" ) ,
} )
} )
}
2016-11-04 12:15:51 +00:00
// Ensure the parser can parse a multi-statement query.
func Test_Parse_General ( t * testing . T ) {
s := ` SELECT a FROM b `
2019-01-31 10:03:50 +00:00
q , err := Parse ( s )
2016-11-04 12:15:51 +00:00
if err != nil {
t . Fatalf ( "unexpected error: %s" , err )
} else if len ( q . Statements ) != 1 {
t . Fatalf ( "unexpected statement count: %d" , len ( q . Statements ) )
}
}
// Ensure the parser can parse a multi-statement query.
func Test_Parse_General_Single ( t * testing . T ) {
s := ` SELECT a FROM b `
2019-01-31 10:03:50 +00:00
q , err := Parse ( s )
2016-11-04 12:15:51 +00:00
if err != nil {
t . Fatalf ( "unexpected error: %s" , err )
} else if len ( q . Statements ) != 1 {
t . Fatalf ( "unexpected statement count: %d" , len ( q . Statements ) )
}
}
// Ensure the parser can parse a multi-statement query.
func Test_Parse_General_Multi ( t * testing . T ) {
s := ` SELECT a FROM b; SELECT c FROM d `
2019-01-31 10:03:50 +00:00
q , err := Parse ( s )
2016-11-04 12:15:51 +00:00
if err != nil {
t . Fatalf ( "unexpected error: %s" , err )
} else if len ( q . Statements ) != 2 {
t . Fatalf ( "unexpected statement count: %d" , len ( q . Statements ) )
}
}
func Test_Parse_Queries_Malformed ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` ` ,
err : "Your SQL query is empty" ,
} ,
{
sql : "SELECT ` FROM person" ,
err : "Found ` FROM person` but expected `expression`" ,
} ,
{
sql : ` SELECT ' FROM person ` ,
err : "Found ` FROM person` but expected `expression`" ,
} ,
{
sql : ` SELECT " FROM person ` ,
err : "Found ` FROM person` but expected `expression`" ,
} ,
{
sql : ` SELECT "\" FROM person ` ,
err : "Found `\" FROM person` but expected `expression`" ,
} ,
{
sql : ` ! ` ,
2019-01-23 01:27:30 +00:00
err : "Found `!` but expected `USE, INFO, BEGIN, CANCEL, COMMIT, IF, LET, RETURN, LIVE, KILL, SELECT, CREATE, UPDATE, DELETE, RELATE, INSERT, UPSERT, DEFINE, REMOVE, OPTION`" ,
2017-11-16 20:53:13 +00:00
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Info ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` INFO ` ,
err : "Found `` but expected `FOR`" ,
} ,
{
sql : ` INFO FOR ` ,
2018-10-24 10:24:24 +00:00
err : "Found `` but expected `ALL, NAMESPACE, DATABASE, SCOPE, TABLE, NS, DB`" ,
} ,
{
sql : ` INFO FOR ALL ` ,
res : & Query { Statements : [ ] Statement { & InfoStatement {
Kind : ALL ,
} } } ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` INFO FOR NAMESPACE ` ,
res : & Query { Statements : [ ] Statement { & InfoStatement {
Kind : NAMESPACE ,
} } } ,
2016-11-04 12:15:51 +00:00
} ,
2018-10-24 10:24:24 +00:00
{
sql : ` INFO FOR NS ` ,
str : ` INFO FOR NAMESPACE ` ,
res : & Query { Statements : [ ] Statement { & InfoStatement {
Kind : NS ,
} } } ,
} ,
2016-11-04 12:15:51 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` INFO FOR DATABASE ` ,
res : & Query { Statements : [ ] Statement { & InfoStatement {
Kind : DATABASE ,
} } } ,
} ,
2018-10-24 10:24:24 +00:00
{
sql : ` INFO FOR DB ` ,
str : ` INFO FOR DATABASE ` ,
res : & Query { Statements : [ ] Statement { & InfoStatement {
Kind : DB ,
} } } ,
} ,
2018-09-13 14:23:17 +00:00
{
sql : ` INFO FOR SCOPE ` ,
err : "Found `` but expected `name`" ,
} ,
{
sql : ` INFO FOR SCOPE test ` ,
res : & Query { Statements : [ ] Statement { & InfoStatement {
Kind : SCOPE ,
What : & Ident { "test" } ,
} } } ,
} ,
2017-11-16 20:53:13 +00:00
{
sql : ` INFO FOR TABLE ` ,
2018-09-13 14:23:17 +00:00
err : "Found `` but expected `name`" ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` INFO FOR TABLE test ` ,
res : & Query { Statements : [ ] Statement { & InfoStatement {
Kind : TABLE ,
2018-09-13 14:23:17 +00:00
What : & Ident { "test" } ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` INFO FOR TABLE test something ` ,
err : "Found `something` but expected `;`" ,
2016-11-04 12:15:51 +00:00
} ,
2016-05-23 12:32:02 +00:00
}
for _ , test := range tests {
testsql ( t , test )
}
}
2016-10-29 17:47:09 +00:00
func Test_Parse_Queries_Let ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` LET ` ,
2017-04-14 12:24:53 +00:00
err : "Found `` but expected `name`" ,
2016-10-29 17:47:09 +00:00
} ,
{
sql : ` LET name ` ,
err : "Found `` but expected `=`" ,
} ,
{
sql : ` LET name = ` ,
2016-11-04 11:13:34 +00:00
err : "Found `=` but expected `expression`" ,
2016-10-29 17:47:09 +00:00
} ,
{
sql : ` LET name = true ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2016-11-04 11:13:34 +00:00
What : true ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` LET name = false ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2016-11-04 11:13:34 +00:00
What : false ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` LET name = "test" ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
What : & Value { "test" } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` LET name = 1 ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2017-11-16 20:53:13 +00:00
What : float64 ( 1 ) ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` LET name = 1.0 ` ,
2017-11-16 20:53:13 +00:00
str : ` LET name = 1 ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2016-11-04 11:13:34 +00:00
What : float64 ( 1 ) ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` LET name = 1.1 ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2016-11-04 11:13:34 +00:00
What : float64 ( 1.1 ) ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
2017-11-16 20:53:13 +00:00
{
sql : ` LET name = thing:test ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
Name : & Ident { "name" } ,
What : & Thing { TB : "thing" , ID : "test" } ,
} } } ,
} ,
{
sql : ` LET name = thing:test ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
Name : & Ident { "name" } ,
What : & Thing { TB : "thing" , ID : "test" } ,
} } } ,
} ,
2016-10-29 17:47:09 +00:00
{
sql : ` LET name = @thing:test ` ,
2017-11-16 20:53:13 +00:00
str : ` LET name = thing:test ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2016-11-04 11:13:34 +00:00
What : & Thing { TB : "thing" , ID : "test" } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` LET name = { "key": "val"} ` ,
2017-11-16 20:53:13 +00:00
str : ` LET name = { "key":"val"} ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2017-11-16 20:53:13 +00:00
What : map [ string ] interface { } { "key" : "val" } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` LET name = ["key", "val"] ` ,
2017-11-16 20:53:13 +00:00
str : ` LET name = ["key","val"] ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2017-11-16 20:53:13 +00:00
What : [ ] interface { } { "key" , "val" } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` LET name = $test ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-04-14 12:24:53 +00:00
Name : & Ident { "name" } ,
2018-08-10 18:34:16 +00:00
What : & Param { "test" } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` LET name = (CREATE person) ` ,
res : & Query { Statements : [ ] Statement { & LetStatement {
2019-01-31 10:03:50 +00:00
RW : true ,
2017-11-16 20:53:13 +00:00
Name : & Ident { "name" } ,
What : & SubExpression { Expr : & CreateStatement {
2020-03-06 15:44:53 +00:00
What : Exprs { & Ident { "person" } } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } ,
} } } ,
2016-10-29 17:47:09 +00:00
} ,
{
sql : ` LET name = "test" something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-10-29 17:47:09 +00:00
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Return ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` RETURN ` ,
2016-11-04 11:13:34 +00:00
err : "Found `` but expected `expression`" ,
2016-10-29 17:47:09 +00:00
} ,
{
sql : ` RETURN true ` ,
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { true } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN true ` ,
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { true } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN false ` ,
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { false } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN "test" ` ,
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { & Value { "test" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN 1 ` ,
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { float64 ( 1 ) } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN 1.0 ` ,
2017-11-16 20:53:13 +00:00
str : ` RETURN 1 ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { float64 ( 1 ) } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN 1.1 ` ,
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { float64 ( 1.1 ) } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN @thing:test ` ,
2017-11-16 20:53:13 +00:00
str : ` RETURN thing:test ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { & Thing { TB : "thing" , ID : "test" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN { "key": "val"} ` ,
2017-11-16 20:53:13 +00:00
str : ` RETURN { "key":"val"} ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { map [ string ] interface { } { "key" : "val" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN ["key", "val"] ` ,
2017-11-16 20:53:13 +00:00
str : ` RETURN ["key","val"] ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
What : Exprs { [ ] interface { } { "key" , "val" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` RETURN $test ` ,
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2018-08-10 18:34:16 +00:00
What : Exprs { & Param { "test" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` RETURN (CREATE person) ` ,
res : & Query { Statements : [ ] Statement { & ReturnStatement {
2019-01-31 10:03:50 +00:00
RW : true ,
2017-11-16 20:53:13 +00:00
What : Exprs { & SubExpression { Expr : & CreateStatement {
2020-03-06 15:44:53 +00:00
What : Exprs { & Ident { "person" } } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } } ,
} } } ,
2016-10-29 17:47:09 +00:00
} ,
{
sql : ` RETURN $test something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-10-29 17:47:09 +00:00
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
2016-05-23 12:32:02 +00:00
func Test_Parse_Queries_Select ( t * testing . T ) {
2016-09-14 21:22:18 +00:00
date , _ := time . Parse ( "2006-01-02" , "1987-06-22" )
nano , _ := time . Parse ( time . RFC3339 , "1987-06-22T08:30:30.511Z" )
2016-05-23 12:32:02 +00:00
var tests = [ ] tester {
2016-02-26 17:27:07 +00:00
{
sql : ` SELECT ` ,
2016-11-04 11:13:34 +00:00
err : "Found `` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` SELECT FROM ` ,
2016-11-04 11:13:34 +00:00
err : "Found `FROM` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` SELECT * ` ,
2016-02-27 12:02:41 +00:00
err : "Found `` but expected `FROM`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` SELECT * FROM ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` SELECT * FROM per!son ` ,
2017-11-16 20:53:13 +00:00
err : "Found `!` but expected `;`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
sql : ` SELECT * FROM @ ` ,
2017-11-16 20:53:13 +00:00
err : "Found `@` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` SELECT * FROM @person ` ,
2017-11-16 20:53:13 +00:00
err : "Found `@person` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` SELECT * FROM @person: ` ,
2017-11-16 20:53:13 +00:00
err : "Found `@person:` but expected `expression`" ,
2016-09-07 15:58:50 +00:00
} ,
{
sql : ` SELECT * FROM @person WHERE ` ,
2017-11-16 20:53:13 +00:00
err : "Found `@person` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
2016-09-14 21:22:18 +00:00
{
sql : "SELECT * FROM 111" ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { float64 ( 111 ) } ,
2017-11-16 20:53:13 +00:00
} } } ,
2016-09-14 21:22:18 +00:00
} ,
{
sql : "SELECT * FROM `111`" ,
2017-11-16 20:53:13 +00:00
str : "SELECT * FROM 111" ,
2016-09-14 21:22:18 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "111" } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
{
sql : "SELECT * FROM `2006-01-02`" ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "2006-01-02" } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
{
sql : "SELECT * FROM `2006-01-02T15:04:05+07:00`" ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "2006-01-02T15:04:05+07:00" } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
{
sql : "SELECT * FROM `2006-01-02T15:04:05.999999999+07:00`" ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "2006-01-02T15:04:05.999999999+07:00" } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
2016-02-26 17:27:07 +00:00
{
sql : ` SELECT * FROM person ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
sql : ` SELECT * FROM person, tweet ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } , & Ident { "tweet" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
2016-09-14 21:22:18 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:1a ` ,
2016-09-14 21:22:18 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : "1a" } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨1a⟩ ` ,
str : ` SELECT * FROM person:1a ` ,
2016-09-14 21:22:18 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : "1a" } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
2016-02-26 17:27:07 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:123456 ` ,
2016-09-14 21:22:18 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : float64 ( 123456 ) } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
2016-02-26 17:27:07 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨123456⟩ ` ,
str : ` SELECT * FROM person:123456 ` ,
2016-05-24 12:51:52 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : float64 ( 123456 ) } } ,
2016-05-24 12:51:52 +00:00
} } } ,
2016-02-26 17:27:07 +00:00
} ,
2016-09-14 21:22:18 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:123.456 ` ,
2016-09-14 21:22:18 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : float64 ( 123.456 ) } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨123.456⟩ ` ,
str : ` SELECT * FROM person:123.456 ` ,
2016-09-14 21:22:18 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : float64 ( 123.456 ) } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
2016-02-26 17:27:07 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:123.456.789.012 ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : "123.456.789.012" } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
2016-09-07 15:58:50 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨123.456.789.012⟩ ` ,
str : ` SELECT * FROM person:123.456.789.012 ` ,
2016-09-07 15:58:50 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : "123.456.789.012" } } ,
2016-09-07 15:58:50 +00:00
} } } ,
} ,
2016-09-14 21:22:18 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨1987-06-22⟩ ` ,
str : ` SELECT * FROM person:⟨1987-06-22T00:00:00Z⟩ ` ,
2016-09-14 21:22:18 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : date } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨1987-06-22T08:30:30.511Z⟩ ` ,
2016-09-14 21:22:18 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : nano } } ,
2016-09-14 21:22:18 +00:00
} } } ,
} ,
2016-05-23 12:32:02 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨A250C5A3-948F-4657-88AD-FF5F27B5B24E⟩ ` ,
2016-09-07 15:58:50 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : "A250C5A3-948F-4657-88AD-FF5F27B5B24E" } } ,
2016-09-07 15:58:50 +00:00
} } } ,
} ,
2016-05-23 12:32:02 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨8250C5A3-948F-4657-88AD-FF5F27B5B24E⟩ ` ,
2016-09-07 15:58:50 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : "8250C5A3-948F-4657-88AD-FF5F27B5B24E" } } ,
2016-09-07 15:58:50 +00:00
} } } ,
} ,
2016-05-23 12:32:02 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person:⟨Tobie Morgan Hitchcock⟩ ` ,
2016-09-07 15:58:50 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "person" , ID : "Tobie Morgan Hitchcock" } } ,
2016-09-07 15:58:50 +00:00
} } } ,
} ,
2016-02-26 17:27:07 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM ⟨email addresses⟩:⟨tobie@abcum.com⟩ ` ,
2016-09-07 15:58:50 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "email addresses" , ID : "tobie@abcum.com" } } ,
2016-09-07 15:58:50 +00:00
} } } ,
} ,
2016-02-26 17:27:07 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM ⟨email addresses⟩:⟨tobie+spam@abcum.com⟩ ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Thing { TB : "email addresses" , ID : "tobie+spam@abcum.com" } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
2016-09-07 15:58:50 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT *, temp AS test FROM person ` ,
2016-09-07 15:58:50 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
Expr : [ ] * Field {
{ Expr : & All { } , Field : "*" } ,
{ Expr : & Ident { "temp" } , Field : "test" , Alias : "test" } ,
} ,
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
2016-09-07 15:58:50 +00:00
} } } ,
} ,
2016-09-14 21:22:18 +00:00
{
2017-11-16 20:53:13 +00:00
sql : "SELECT `email addresses` AS emails FROM person" ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2016-09-06 13:30:59 +00:00
Expr : [ ] * Field {
2017-11-16 20:53:13 +00:00
{ Expr : & Ident { "email addresses" } , Field : "emails" , Alias : "emails" } ,
2016-09-06 13:30:59 +00:00
} ,
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : "SELECT emails AS `email addresses` FROM person" ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2016-09-06 13:30:59 +00:00
Expr : [ ] * Field {
2017-11-16 20:53:13 +00:00
{ Expr : & Ident { "emails" } , Field : "email addresses" , Alias : "email addresses" } ,
2016-09-06 13:30:59 +00:00
} ,
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM (CREATE person) ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2019-01-31 10:03:50 +00:00
RW : true ,
2016-09-06 13:30:59 +00:00
Expr : [ ] * Field {
2017-11-16 20:53:13 +00:00
{ Expr : & All { } , Field : "*" } ,
2016-09-06 13:30:59 +00:00
} ,
2017-11-16 20:53:13 +00:00
What : Exprs { & SubExpression { Expr : & CreateStatement {
2020-03-06 15:44:53 +00:00
What : Exprs { & Ident { "person" } } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : "SELECT * FROM person WHERE id = \"\x0A\"" ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "id" } , Op : EQ , RHS : & Value { "\n" } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : "SELECT * FROM person WHERE id = \"\x0D\"" ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "id" } , Op : EQ , RHS : & Value { "\r" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : "SELECT * FROM person WHERE id = \"\b\n\r\t\"" ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "id" } , Op : EQ , RHS : & Value { "\b\n\r\t" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE ` ,
2016-11-04 11:13:34 +00:00
err : "Found `` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE id ` ,
2016-11-04 11:13:34 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & Ident { "id" } ,
2016-11-04 11:13:34 +00:00
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE id = ` ,
2016-11-04 11:13:34 +00:00
err : "Found `` but expected `expression`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE id = 1 ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "id" } , Op : EQ , RHS : float64 ( 1 ) } ,
2016-05-23 12:32:02 +00:00
} } } ,
2016-02-26 17:27:07 +00:00
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old = EMPTY ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : EQ , RHS : & Empty { } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old != EMPTY ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : NEQ , RHS : & Empty { } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old = MISSING ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE old = VOID ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : EQ , RHS : & Void { } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old != MISSING ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE old != VOID ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : NEQ , RHS : & Void { } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old IS EMPTY ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE old = EMPTY ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : EQ , RHS : & Empty { } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old IS NOT EMPTY ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE old != EMPTY ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : NEQ , RHS : & Empty { } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old IS MISSING ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE old = VOID ` ,
2016-09-06 13:30:59 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : EQ , RHS : & Void { } } ,
2016-09-06 13:30:59 +00:00
} } } ,
2016-02-26 17:27:07 +00:00
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old IS NOT MISSING ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE old != VOID ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : NEQ , RHS : & Void { } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old = true ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : EQ , RHS : true } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2016-09-06 13:30:59 +00:00
sql : ` SELECT * FROM person WHERE old = false ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "old" } , Op : EQ , RHS : false } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
2017-12-06 20:06:28 +00:00
{
sql : ` SELECT * FROM person WHERE id != NULL AND id > 13.9 AND id < 31 AND id >= 15 AND id <= 29.9 ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2019-01-31 10:03:50 +00:00
RW : false ,
2017-11-16 20:53:13 +00:00
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
2016-11-04 11:13:34 +00:00
Cond : & BinaryExpression {
LHS : & BinaryExpression {
2017-12-06 20:06:28 +00:00
LHS : & Ident { "id" } ,
Op : NEQ ,
2017-12-12 01:09:08 +00:00
RHS : & Null { } ,
2017-12-06 20:06:28 +00:00
} ,
Op : AND ,
RHS : & BinaryExpression {
2016-11-04 11:13:34 +00:00
LHS : & BinaryExpression {
2017-12-06 20:06:28 +00:00
LHS : & Ident { "id" } ,
Op : GT ,
RHS : float64 ( 13.9 ) ,
} ,
Op : AND ,
RHS : & BinaryExpression {
2016-11-04 11:13:34 +00:00
LHS : & BinaryExpression {
2017-12-06 20:06:28 +00:00
LHS : & Ident { "id" } ,
Op : LT ,
RHS : float64 ( 31 ) ,
} ,
Op : AND ,
RHS : & BinaryExpression {
2016-11-04 11:13:34 +00:00
LHS : & BinaryExpression {
2017-12-06 20:06:28 +00:00
LHS : & Ident { "id" } ,
Op : GTE ,
RHS : float64 ( 15 ) ,
2016-11-04 11:13:34 +00:00
} ,
Op : AND ,
RHS : & BinaryExpression {
2018-08-10 18:34:16 +00:00
LHS : & Ident { "id" } ,
2017-12-06 20:06:28 +00:00
Op : LTE ,
RHS : float64 ( 29.9 ) ,
2016-11-04 11:13:34 +00:00
} ,
} ,
} ,
} ,
2016-02-26 17:27:07 +00:00
} ,
} } } ,
2017-12-06 20:06:28 +00:00
} ,
2019-01-31 10:03:50 +00:00
{
sql : ` SELECT * FROM person WHERE 2 * 3 + 4 = 10 ` ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression {
LHS : & BinaryExpression {
LHS : float64 ( 2 ) ,
Op : MUL ,
RHS : & BinaryExpression {
LHS : float64 ( 3 ) ,
Op : ADD ,
RHS : float64 ( 4 ) ,
} ,
} ,
Op : EQ ,
RHS : float64 ( 10 ) ,
} ,
} } } ,
} ,
{
sql : ` SELECT * FROM person WHERE 2 + 3 * 4 = 14 ` ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression {
LHS : & BinaryExpression {
LHS : float64 ( 2 ) ,
Op : ADD ,
RHS : & BinaryExpression {
LHS : float64 ( 3 ) ,
Op : MUL ,
RHS : float64 ( 4 ) ,
} ,
} ,
Op : EQ ,
RHS : float64 ( 14 ) ,
} ,
} } } ,
} ,
{
sql : ` SELECT * FROM person WHERE 2 * 3 + 3 * 4 = 18 ` ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression {
LHS : & BinaryExpression {
LHS : float64 ( 2 ) ,
Op : MUL ,
RHS : & BinaryExpression {
LHS : float64 ( 3 ) ,
Op : ADD ,
RHS : & BinaryExpression {
LHS : float64 ( 3 ) ,
Op : MUL ,
RHS : float64 ( 4 ) ,
} ,
} ,
} ,
Op : EQ ,
RHS : float64 ( 18 ) ,
} ,
} } } ,
} ,
{
sql : ` SELECT * FROM person WHERE 2 + 3 + 3 * 4 = 17 ` ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression {
LHS : & BinaryExpression {
LHS : float64 ( 2 ) ,
Op : ADD ,
RHS : & BinaryExpression {
LHS : float64 ( 3 ) ,
Op : ADD ,
RHS : & BinaryExpression {
LHS : float64 ( 3 ) ,
Op : MUL ,
RHS : float64 ( 4 ) ,
} ,
} ,
} ,
Op : EQ ,
RHS : float64 ( 17 ) ,
} ,
} } } ,
} ,
{
sql : ` SELECT * FROM person WHERE (2 + 3 + 3) * 4 = 32 ` ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression {
LHS : & BinaryExpression {
LHS : & SubExpression {
Expr : & BinaryExpression {
LHS : float64 ( 2 ) ,
Op : ADD ,
RHS : & BinaryExpression {
LHS : float64 ( 3 ) ,
Op : ADD ,
RHS : float64 ( 3 ) ,
} ,
} ,
} ,
Op : MUL ,
RHS : float64 ( 4 ) ,
} ,
Op : EQ ,
RHS : float64 ( 32 ) ,
} ,
} } } ,
} ,
2016-05-23 12:32:02 +00:00
{
sql : ` SELECT * FROM person WHERE test IN ["London","Paris"] ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE test ∈ ["London","Paris"] ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "test" } , Op : INS , RHS : [ ] interface { } { "London" , "Paris" } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
2016-10-29 17:47:09 +00:00
{
sql : ` SELECT * FROM person WHERE test IS IN ["London","Paris"] ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE test ∈ ["London","Paris"] ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "test" } , Op : INS , RHS : [ ] interface { } { "London" , "Paris" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` SELECT * FROM person WHERE test IS NOT IN ["London","Paris"] ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE test ∉ ["London","Paris"] ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "test" } , Op : NIS , RHS : [ ] interface { } { "London" , "Paris" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` SELECT * FROM person WHERE ["London","Paris"] CONTAINS test ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE ["London","Paris"] ∋ test ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : [ ] interface { } { "London" , "Paris" } , Op : SIN , RHS : & Ident { "test" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
{
sql : ` SELECT * FROM person WHERE ["London","Paris"] CONTAINS NOT test ` ,
2017-11-16 20:53:13 +00:00
str : ` SELECT * FROM person WHERE ["London","Paris"] ∌ test ` ,
2016-10-29 17:47:09 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : [ ] interface { } { "London" , "Paris" } , Op : SNI , RHS : & Ident { "test" } } ,
2016-10-29 17:47:09 +00:00
} } } ,
} ,
2016-05-23 12:32:02 +00:00
{
sql : ` SELECT * FROM person WHERE test = { ` ,
2016-11-04 11:13:34 +00:00
err : "Found `{` but expected `expression`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person WHERE test = { "name":"London"} ` ,
2017-02-23 15:09:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "test" } , Op : EQ , RHS : map [ string ] interface { } { "name" : "London" } } ,
2017-02-23 15:09:02 +00:00
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person WHERE test = { "name": { "f":"first","l":"last"}} ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Cond : & BinaryExpression { LHS : & Ident { "test" } , Op : EQ , RHS : map [ string ] interface { } { "name" : map [ string ] interface { } { "f" : "first" , "l" : "last" } } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person TIMEOUT 1s ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
Timeout : 1 * time . Second ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` SELECT * FROM person TIMEOUT null ` ,
err : "Found `null` but expected `duration`" ,
2016-02-26 17:27:07 +00:00
} ,
}
2017-11-16 20:53:13 +00:00
/ * bday1 , _ := time . Parse ( "2006-01-02" , "1987-06-22" )
2016-05-23 12:32:02 +00:00
bday2 , _ := time . Parse ( time . RFC3339 , "1987-06-22T08:00:00Z" )
bday3 , _ := time . Parse ( time . RFC3339 , "1987-06-22T08:30:00.193943735Z" )
bday4 , _ := time . Parse ( time . RFC3339 , "2016-03-14T11:19:31.193943735Z" )
tests = append ( tests , tester {
sql : ` SELECT * FROM person WHERE bday >= "1987-06-22" AND bday >= "1987-06-22T08:00:00Z" AND bday >= "1987-06-22T08:30:00.193943735Z" AND bday <= "2016-03-14T11:19:31.193943735Z" ` ,
res : & Query { Statements : [ ] Statement { & SelectStatement {
2019-01-31 10:03:50 +00:00
RW : true ,
2017-11-16 20:53:13 +00:00
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
2016-11-04 11:13:34 +00:00
Cond : & BinaryExpression {
LHS : & BinaryExpression {
LHS : & BinaryExpression {
LHS : & BinaryExpression {
2018-08-10 18:34:16 +00:00
LHS : & Ident { "bday" } ,
2016-11-04 11:13:34 +00:00
Op : GTE ,
RHS : bday1 ,
} ,
Op : AND ,
RHS : & BinaryExpression {
2018-08-10 18:34:16 +00:00
LHS : & Ident { "bday" } ,
2016-11-04 11:13:34 +00:00
Op : GTE ,
RHS : bday2 ,
} ,
} ,
Op : AND ,
RHS : & BinaryExpression {
2018-08-10 18:34:16 +00:00
LHS : & Ident { "bday" } ,
2016-11-04 11:13:34 +00:00
Op : GTE ,
RHS : bday3 ,
} ,
} ,
Op : AND ,
RHS : & BinaryExpression {
2018-08-10 18:34:16 +00:00
LHS : & Ident { "bday" } ,
2016-11-04 11:13:34 +00:00
Op : LTE ,
RHS : bday4 ,
} ,
2016-05-23 12:32:02 +00:00
} ,
} } } ,
2017-11-16 20:53:13 +00:00
} ) * /
2016-05-23 12:32:02 +00:00
2016-02-26 17:27:07 +00:00
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Create ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` CREATE ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` CREATE person ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` CREATE person SET 123 ` ,
err : "Found `123` but expected `field name`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` CREATE person SET firstname ` ,
2016-02-27 12:02:41 +00:00
err : "Found `` but expected `=, +=, -=`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2016-11-04 11:13:34 +00:00
sql : ` CREATE person SET firstname = VOID ` ,
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & DataExpression { Data : [ ] * ItemExpression { { LHS : & Ident { "firstname" } , Op : EQ , RHS : & Void { } } } } ,
Echo : AFTER ,
2016-11-04 11:13:34 +00:00
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
2016-11-04 11:13:34 +00:00
sql : ` CREATE person SET firstname = EMPTY ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & DataExpression { Data : [ ] * ItemExpression { { LHS : & Ident { "firstname" } , Op : EQ , RHS : & Empty { } } } } ,
Echo : AFTER ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
sql : ` CREATE person SET firstname = "Tobie" something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-02-26 17:27:07 +00:00
} ,
2016-05-23 12:32:02 +00:00
{
sql : ` CREATE person SET firstname = "Tobie" ` ,
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & DataExpression { Data : [ ] * ItemExpression { { LHS : & Ident { "firstname" } , Op : EQ , RHS : & Value { "Tobie" } } } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` CREATE person MERGE something ` ,
err : "Found `something` but expected `json`" ,
} ,
{
sql : ` CREATE person MERGE { "firstname"::"Tobie"} ` ,
err : "Found `{\"firstname\"::\"Tobie\"}` but expected `json`" ,
} ,
{
sql : ` CREATE person MERGE { "firstname":"Tobie"} something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` CREATE person MERGE { "firstname":"Tobie"} ` ,
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & MergeExpression { Data : map [ string ] interface { } { "firstname" : "Tobie" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
2020-11-19 21:41:49 +00:00
{
sql : ` CREATE person CONTENT { "test":" {{ { " } ` ,
res : & Query { Statements : [ ] Statement { & CreateStatement {
What : [ ] Expr { & Ident { "person" } } ,
Data : & ContentExpression { Data : map [ string ] interface { } { "test" : "{{{" } } ,
Echo : AFTER ,
} } } ,
} ,
2016-05-23 12:32:02 +00:00
{
sql : ` CREATE person CONTENT something ` ,
err : "Found `something` but expected `json`" ,
} ,
{
sql : ` CREATE person CONTENT { "firstname"::"Tobie"} ` ,
err : "Found `{\"firstname\"::\"Tobie\"}` but expected `json`" ,
} ,
{
sql : ` CREATE person CONTENT { "firstname":"Tobie"} something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` CREATE person CONTENT { "firstname":"Tobie"} ` ,
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & ContentExpression { Data : map [ string ] interface { } { "firstname" : "Tobie" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` CREATE person RETURN ` ,
2019-11-15 13:23:58 +00:00
err : "Found `` but expected `NONE, BEFORE, AFTER`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` CREATE person RETURN NONE ` ,
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : NONE ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` CREATE person RETURN BEFORE ` ,
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : BEFORE ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` CREATE person RETURN AFTER ` ,
2017-11-16 20:53:13 +00:00
str : ` CREATE person ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` CREATE person TIMEOUT 1s ` ,
res : & Query { Statements : [ ] Statement { & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : AFTER ,
Timeout : 1 * time . Second ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` CREATE person TIMEOUT null ` ,
err : "Found `null` but expected `duration`" ,
} ,
{
sql : ` CREATE person something ` ,
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
2016-02-26 17:27:07 +00:00
}
for _ , test := range tests {
testsql ( t , test )
}
}
2016-05-23 12:32:02 +00:00
func Test_Parse_Queries_Update ( t * testing . T ) {
2016-02-26 17:27:07 +00:00
var tests = [ ] tester {
{
2016-05-23 12:32:02 +00:00
sql : ` UPDATE ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2016-05-23 12:32:02 +00:00
sql : ` UPDATE person ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` UPDATE person SET 123 ` ,
err : "Found `123` but expected `field name`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2016-05-23 12:32:02 +00:00
sql : ` UPDATE person SET firstname ` ,
2016-02-27 12:02:41 +00:00
err : "Found `` but expected `=, +=, -=`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2016-11-04 11:13:34 +00:00
sql : ` UPDATE person SET firstname = VOID ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & DataExpression { Data : [ ] * ItemExpression { { LHS : & Ident { "firstname" } , Op : EQ , RHS : & Void { } } } } ,
Echo : AFTER ,
2016-11-04 11:13:34 +00:00
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
2016-11-04 11:13:34 +00:00
sql : ` UPDATE person SET firstname = EMPTY ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & DataExpression { Data : [ ] * ItemExpression { { LHS : & Ident { "firstname" } , Op : EQ , RHS : & Empty { } } } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` UPDATE person SET firstname = "Tobie" something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` UPDATE person SET firstname = "Tobie" ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & DataExpression { Data : [ ] * ItemExpression { { LHS : & Ident { "firstname" } , Op : EQ , RHS : & Value { "Tobie" } } } } ,
Echo : AFTER ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
2016-10-29 22:50:02 +00:00
{
sql : ` UPDATE person DIFF something ` ,
err : "Found `something` but expected `json`" ,
} ,
{
2017-02-23 15:09:02 +00:00
sql : ` UPDATE person DIFF { } something ` ,
err : "Found `{}` but expected `json`" ,
2016-10-29 22:50:02 +00:00
} ,
{
2017-02-23 15:09:02 +00:00
sql : ` UPDATE person DIFF [] something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-10-29 22:50:02 +00:00
} ,
{
2017-02-23 15:09:02 +00:00
sql : ` UPDATE person DIFF [] ` ,
2016-10-29 22:50:02 +00:00
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & DiffExpression { Data : [ ] interface { } { } } ,
Echo : AFTER ,
2016-10-29 22:50:02 +00:00
} } } ,
} ,
2016-02-26 17:27:07 +00:00
{
2016-05-23 12:32:02 +00:00
sql : ` UPDATE person MERGE something ` ,
err : "Found `something` but expected `json`" ,
} ,
{
sql : ` UPDATE person MERGE { "firstname"::"Tobie"} ` ,
err : "Found `{\"firstname\"::\"Tobie\"}` but expected `json`" ,
} ,
{
sql : ` UPDATE person MERGE { "firstname":"Tobie"} something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-02-26 17:27:07 +00:00
} ,
2016-05-23 12:32:02 +00:00
{
sql : ` UPDATE person MERGE { "firstname":"Tobie"} ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & MergeExpression { Data : map [ string ] interface { } { "firstname" : "Tobie" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` UPDATE person CONTENT something ` ,
err : "Found `something` but expected `json`" ,
} ,
{
sql : ` UPDATE person CONTENT { "firstname"::"Tobie"} ` ,
err : "Found `{\"firstname\"::\"Tobie\"}` but expected `json`" ,
} ,
{
sql : ` UPDATE person CONTENT { "firstname":"Tobie"} something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` UPDATE person CONTENT { "firstname":"Tobie"} ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Data : & ContentExpression { Data : map [ string ] interface { } { "firstname" : "Tobie" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
2020-11-19 21:41:49 +00:00
{
sql : ` UPDATE person CONTENT { "test":" {{ { " } ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
What : [ ] Expr { & Ident { "person" } } ,
Data : & ContentExpression { Data : map [ string ] interface { } { "test" : "{{{" } } ,
Echo : AFTER ,
} } } ,
} ,
2016-05-23 12:32:02 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` UPDATE person RETURN ` ,
2019-11-15 13:23:58 +00:00
err : "Found `` but expected `NONE, BEFORE, AFTER`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` UPDATE person RETURN NONE ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : NONE ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` UPDATE person RETURN BEFORE ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : BEFORE ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` UPDATE person RETURN AFTER ` ,
2017-11-16 20:53:13 +00:00
str : ` UPDATE person ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` UPDATE person TIMEOUT 1s ` ,
res : & Query { Statements : [ ] Statement { & UpdateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : AFTER ,
Timeout : 1 * time . Second ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` UPDATE person TIMEOUT null ` ,
err : "Found `null` but expected `duration`" ,
} ,
{
sql : ` UPDATE person something ` ,
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
2016-02-26 17:27:07 +00:00
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Delete ( t * testing . T ) {
2016-05-23 12:32:02 +00:00
var tests = [ ] tester {
{
sql : ` DELETE ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `expression`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` DELETE FROM ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `expression`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` DELETE person ` ,
res : & Query { Statements : [ ] Statement { & DeleteStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : NONE ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
2017-11-16 20:53:13 +00:00
{
sql : ` DELETE person RETURN ` ,
2019-11-15 13:23:58 +00:00
err : "Found `` but expected `NONE, BEFORE, AFTER`" ,
2017-11-16 20:53:13 +00:00
} ,
2016-05-23 12:32:02 +00:00
{
sql : ` DELETE person RETURN NONE ` ,
2017-11-16 20:53:13 +00:00
str : ` DELETE person ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & DeleteStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : NONE ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` DELETE person RETURN BEFORE ` ,
res : & Query { Statements : [ ] Statement { & DeleteStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : BEFORE ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` DELETE person RETURN AFTER ` ,
res : & Query { Statements : [ ] Statement { & DeleteStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : AFTER ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DELETE person TIMEOUT 1s ` ,
res : & Query { Statements : [ ] Statement { & DeleteStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "person" } } ,
Echo : NONE ,
Timeout : 1 * time . Second ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` DELETE person TIMEOUT null ` ,
err : "Found `null` but expected `duration`" ,
} ,
{
sql : ` DELETE person something ` ,
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
}
2016-02-26 17:27:07 +00:00
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Relate ( t * testing . T ) {
var tests = [ ] tester {
{
2017-11-16 20:53:13 +00:00
sql : ` RELATE ` ,
2019-06-15 07:11:33 +00:00
err : "Found `` but expected `expression`" ,
} ,
{
sql : ` RELATE person ` ,
err : "Found `` but expected `->, <-`" ,
} ,
{
sql : ` RELATE person -> ` ,
2017-04-14 12:24:53 +00:00
err : "Found `` but expected `table`" ,
2016-05-23 12:32:02 +00:00
} ,
2016-09-14 21:22:18 +00:00
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase ` ,
err : "Found `` but expected `->`" ,
2016-09-14 21:22:18 +00:00
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE item <- purchase ` ,
err : "Found `` but expected `<-`" ,
2016-09-14 21:22:18 +00:00
} ,
2016-05-23 12:32:02 +00:00
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> ` ,
err : "Found `` but expected `expression`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE item <- purchase <- ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `expression`" ,
2016-05-23 12:32:02 +00:00
} ,
2016-10-14 20:31:45 +00:00
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Echo : AFTER ,
2016-10-14 20:31:45 +00:00
} } } ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE item <- purchase <- person ` ,
str : ` RELATE person -> purchase -> item ` ,
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Echo : AFTER ,
2019-06-15 07:11:33 +00:00
} } } ,
} ,
{
sql : ` RELATE person -> purchase -> item UNIQUE ` ,
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Uniq : true ,
Echo : AFTER ,
2019-06-15 07:11:33 +00:00
} } } ,
} ,
{
sql : ` RELATE item <- purchase <- person UNIQUE ` ,
str : ` RELATE person -> purchase -> item UNIQUE ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Uniq : true ,
Echo : AFTER ,
2016-10-14 20:31:45 +00:00
} } } ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item SET 123 ` ,
2017-11-16 20:53:13 +00:00
err : "Found `123` but expected `field name`" ,
2016-10-14 20:31:45 +00:00
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item SET firstname ` ,
2017-11-16 20:53:13 +00:00
err : "Found `` but expected `=, +=, -=`" ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item SET public = true ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Data : & DataExpression { Data : [ ] * ItemExpression { { LHS : & Ident { "public" } , Op : EQ , RHS : true } } } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item RETURN ` ,
2019-11-15 13:23:58 +00:00
err : "Found `` but expected `NONE, BEFORE, AFTER`" ,
2017-11-16 20:53:13 +00:00
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item RETURN NONE ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Echo : NONE ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item RETURN BEFORE ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Echo : BEFORE ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item RETURN AFTER ` ,
str : ` RELATE person -> purchase -> item ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item TIMEOUT 1s ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & RelateStatement {
2020-03-06 15:44:53 +00:00
Type : & Table { "purchase" } ,
From : [ ] Expr { & Ident { "person" } } ,
With : [ ] Expr { & Ident { "item" } } ,
Echo : AFTER ,
Timeout : 1 * time . Second ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item TIMEOUT null ` ,
2017-11-16 20:53:13 +00:00
err : "Found `null` but expected `duration`" ,
} ,
{
2019-06-15 07:11:33 +00:00
sql : ` RELATE person -> purchase -> item something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Insert ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` INSERT ` ,
err : "Found `` but expected `expression`" ,
} ,
{
sql : ` INSERT ["one","two","tre"] ` ,
err : "Found `` but expected `INTO`" ,
} ,
{
sql : ` INSERT ["one","two","tre"] INTO ` ,
err : "Found `` but expected `table`" ,
} ,
{
sql : ` INSERT ["one","two","tre"] INTO person ` ,
res : & Query { Statements : [ ] Statement { & InsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` INSERT ["one","two","tre"] INTO person RETURN ` ,
2019-11-15 13:23:58 +00:00
err : "Found `` but expected `NONE, BEFORE, AFTER`" ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` INSERT ["one","two","tre"] INTO person RETURN NONE ` ,
res : & Query { Statements : [ ] Statement { & InsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : NONE ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` INSERT ["one","two","tre"] INTO person RETURN BEFORE ` ,
res : & Query { Statements : [ ] Statement { & InsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : BEFORE ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` INSERT ["one","two","tre"] INTO person RETURN AFTER ` ,
str : ` INSERT ["one","two","tre"] INTO person ` ,
res : & Query { Statements : [ ] Statement { & InsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` INSERT ["one","two","tre"] INTO person TIMEOUT 1s ` ,
res : & Query { Statements : [ ] Statement { & InsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : AFTER ,
Timeout : 1 * time . Second ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` INSERT ["one","two","tre"] INTO person TIMEOUT null ` ,
err : "Found `null` but expected `duration`" ,
} ,
{
sql : ` INSERT ["one","two","tre"] INTO person something ` ,
err : "Found `something` but expected `;`" ,
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Upsert ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` UPSERT ` ,
err : "Found `` but expected `expression`" ,
} ,
{
sql : ` UPSERT ["one","two","tre"] ` ,
err : "Found `` but expected `INTO`" ,
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO ` ,
err : "Found `` but expected `table`" ,
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO person ` ,
res : & Query { Statements : [ ] Statement { & UpsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO person RETURN ` ,
2019-11-15 13:23:58 +00:00
err : "Found `` but expected `NONE, BEFORE, AFTER`" ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO person RETURN NONE ` ,
res : & Query { Statements : [ ] Statement { & UpsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : NONE ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO person RETURN BEFORE ` ,
res : & Query { Statements : [ ] Statement { & UpsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : BEFORE ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO person RETURN AFTER ` ,
str : ` UPSERT ["one","two","tre"] INTO person ` ,
res : & Query { Statements : [ ] Statement { & UpsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO person TIMEOUT 1s ` ,
res : & Query { Statements : [ ] Statement { & UpsertStatement {
2020-03-06 15:44:53 +00:00
Data : [ ] interface { } { "one" , "two" , "tre" } ,
Into : & Table { "person" } ,
Echo : AFTER ,
Timeout : 1 * time . Second ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO person TIMEOUT null ` ,
err : "Found `null` but expected `duration`" ,
} ,
{
sql : ` UPSERT ["one","two","tre"] INTO person something ` ,
err : "Found `something` but expected `;`" ,
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Live ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` LIVE ` ,
err : "Found `` but expected `SELECT`" ,
} ,
{
sql : ` LIVE SELECT ` ,
err : "Found `` but expected `expression`" ,
} ,
{
sql : ` LIVE SELECT * ` ,
err : "Found `` but expected `FROM`" ,
} ,
{
sql : ` LIVE SELECT * FROM ` ,
2017-12-08 10:05:21 +00:00
err : "Found `` but expected `expression`" ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` LIVE SELECT * FROM person ` ,
res : & Query { Statements : [ ] Statement { & LiveStatement {
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
2017-12-08 10:05:21 +00:00
What : Exprs { & Ident { "person" } } ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` LIVE SELECT * FROM person WHERE ` ,
err : "Found `` but expected `expression`" ,
} ,
{
sql : ` LIVE SELECT * FROM person WHERE public = true ` ,
res : & Query { Statements : [ ] Statement { & LiveStatement {
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
2017-12-08 10:05:21 +00:00
What : Exprs { & Ident { "person" } } ,
2017-11-16 20:53:13 +00:00
Cond : & BinaryExpression {
LHS : & Ident { "public" } ,
Op : EQ ,
RHS : true ,
} ,
} } } ,
} ,
{
sql : ` LIVE SELECT * FROM person WHERE public = true something ` ,
err : "Found `something` but expected `;`" ,
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Kill ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` KILL ` ,
2017-12-08 10:05:21 +00:00
err : "Found `` but expected `expression`" ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` KILL identifier ` ,
2017-12-08 10:05:21 +00:00
res : & Query { Statements : [ ] Statement { & KillStatement {
What : Exprs { & Ident { "identifier" } } ,
} } } ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` KILL "identifier" ` ,
res : & Query { Statements : [ ] Statement { & KillStatement {
2017-12-08 10:05:21 +00:00
What : Exprs { & Value { "identifier" } } ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` KILL "identifier" something ` ,
err : "Found `something` but expected `;`" ,
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Define ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` DEFINE ` ,
err : "Found `` but expected `NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, EVENT, FIELD, INDEX`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` DEFINE NAMESPACE ` ,
err : "Found `` but expected `name`" ,
} ,
{
sql : ` DEFINE NAMESPACE 111 ` ,
err : "Found `111` but expected `name`" ,
} ,
{
sql : ` DEFINE NAMESPACE 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
} ,
{
sql : ` DEFINE NAMESPACE test ` ,
res : & Query { Statements : [ ] Statement { & DefineNamespaceStatement {
2019-01-31 10:03:50 +00:00
Name : & Ident { "test" } ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` DEFINE NAMESPACE test something ` ,
err : "Found `something` but expected `;`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` DEFINE DATABASE ` ,
err : "Found `` but expected `name`" ,
} ,
{
sql : ` DEFINE DATABASE 111 ` ,
err : "Found `111` but expected `name`" ,
} ,
{
sql : ` DEFINE DATABASE 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
} ,
{
sql : ` DEFINE DATABASE test ` ,
res : & Query { Statements : [ ] Statement { & DefineDatabaseStatement {
2019-01-31 10:03:50 +00:00
Name : & Ident { "test" } ,
2017-11-16 20:53:13 +00:00
} } } ,
} ,
{
sql : ` DEFINE DATABASE test something ` ,
err : "Found `something` but expected `;`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` DEFINE LOGIN ` ,
err : "Found `` but expected `name`" ,
} ,
{
sql : ` DEFINE LOGIN 111 ` ,
err : "Found `111` but expected `name`" ,
} ,
{
sql : ` DEFINE LOGIN 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
} ,
{
sql : ` DEFINE LOGIN test ` ,
err : "Found `` but expected `ON`" ,
} ,
{
sql : ` DEFINE LOGIN test ON ` ,
err : "Found `` but expected `NAMESPACE, DATABASE`" ,
} ,
{
sql : ` DEFINE LOGIN test ON something ` ,
err : "Found `something` but expected `NAMESPACE, DATABASE`" ,
} ,
{
sql : ` DEFINE LOGIN test ON NAMESPACE ` ,
2018-10-18 00:22:44 +00:00
err : "Found `` but expected `PASSWORD, PASSHASH`" ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` DEFINE LOGIN test ON NAMESPACE PASSWORD ` ,
err : "Found `` but expected `string`" ,
} ,
{
sql : ` DEFINE LOGIN test ON NAMESPACE PASSWORD "123456" ` ,
2018-10-18 00:22:44 +00:00
str : ` DEFINE LOGIN test ON NAMESPACE PASSHASH "123456" ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & DefineLoginStatement {
Kind : NAMESPACE ,
User : & Ident { "test" } ,
Pass : [ ] byte ( "123456" ) ,
} } } ,
} ,
{
sql : ` DEFINE LOGIN test ON DATABASE PASSWORD "123456" ` ,
2018-10-18 00:22:44 +00:00
str : ` DEFINE LOGIN test ON DATABASE PASSHASH "123456" ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & DefineLoginStatement {
Kind : DATABASE ,
User : & Ident { "test" } ,
Pass : [ ] byte ( "123456" ) ,
} } } ,
} ,
{
sql : ` DEFINE LOGIN test ON NAMESPACE PASSWORD "123456" something ` ,
err : "Found `something` but expected `;`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` DEFINE TOKEN ` ,
err : "Found `` but expected `name`" ,
} ,
{
sql : ` DEFINE TOKEN 111 ` ,
err : "Found `111` but expected `name`" ,
} ,
{
sql : ` DEFINE TOKEN 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
} ,
{
sql : ` DEFINE TOKEN test ` ,
err : "Found `` but expected `ON`" ,
} ,
{
sql : ` DEFINE TOKEN test ON ` ,
err : "Found `` but expected `NAMESPACE, DATABASE, SCOPE`" ,
} ,
{
sql : ` DEFINE TOKEN test ON something ` ,
err : "Found `something` but expected `NAMESPACE, DATABASE, SCOPE`" ,
} ,
{
sql : ` DEFINE TOKEN test ON NAMESPACE ` ,
err : "Found `` but expected `TYPE`" ,
} ,
{
sql : ` DEFINE TOKEN test ON NAMESPACE TYPE 100 ` ,
err : "Found `100` but expected `ES256, ES384, ES512, HS256, HS384, HS512, PS256, PS384, PS512, RS256, RS384, RS512`" ,
} ,
{
sql : ` DEFINE TOKEN test ON NAMESPACE TYPE XX512 ` ,
err : "Found `XX512` but expected `ES256, ES384, ES512, HS256, HS384, HS512, PS256, PS384, PS512, RS256, RS384, RS512`" ,
} ,
{
sql : ` DEFINE TOKEN test ON NAMESPACE TYPE HS512 ` ,
err : "Found `` but expected `VALUE`" ,
} ,
{
sql : ` DEFINE TOKEN test ON NAMESPACE TYPE HS512 ` ,
err : "Found `` but expected `VALUE`" ,
} ,
{
sql : ` DEFINE TOKEN test ON NAMESPACE TYPE HS512 VALUE ` ,
err : "Found `` but expected `string`" ,
} ,
{
sql : ` DEFINE TOKEN test ON NAMESPACE TYPE HS512 VALUE "secret" ` ,
2018-10-18 00:22:44 +00:00
str : ` DEFINE TOKEN test ON NAMESPACE TYPE HS512 VALUE "secret" ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & DefineTokenStatement {
Kind : NAMESPACE ,
Name : & Ident { "test" } ,
2018-09-13 15:35:06 +00:00
What : & Ident { "" } ,
2017-11-16 20:53:13 +00:00
Type : "HS512" ,
Code : [ ] byte ( "secret" ) ,
} } } ,
} ,
{
sql : ` DEFINE TOKEN test ON DATABASE TYPE HS512 VALUE "secret" ` ,
2018-10-18 00:22:44 +00:00
str : ` DEFINE TOKEN test ON DATABASE TYPE HS512 VALUE "secret" ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & DefineTokenStatement {
Kind : DATABASE ,
Name : & Ident { "test" } ,
2018-09-13 15:35:06 +00:00
What : & Ident { "" } ,
2017-11-16 20:53:13 +00:00
Type : "HS512" ,
Code : [ ] byte ( "secret" ) ,
} } } ,
} ,
{
2018-09-13 15:35:06 +00:00
sql : ` DEFINE TOKEN test ON SCOPE test TYPE HS512 VALUE "secret" ` ,
2018-10-18 00:22:44 +00:00
str : ` DEFINE TOKEN test ON SCOPE test TYPE HS512 VALUE "secret" ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & DefineTokenStatement {
Kind : SCOPE ,
Name : & Ident { "test" } ,
2018-09-13 15:35:06 +00:00
What : & Ident { "test" } ,
2017-11-16 20:53:13 +00:00
Type : "HS512" ,
Code : [ ] byte ( "secret" ) ,
} } } ,
} ,
{
2018-09-13 15:35:06 +00:00
sql : ` DEFINE TOKEN test ON SCOPE test TYPE HS512 VALUE "secret" something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` DEFINE SCOPE ` ,
err : "Found `` but expected `name`" ,
} ,
{
sql : ` DEFINE SCOPE 111 ` ,
err : "Found `111` but expected `name`" ,
} ,
{
sql : ` DEFINE SCOPE 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
} ,
{
sql : ` DEFINE SCOPE test ` ,
res : & Query { Statements : [ ] Statement { & DefineScopeStatement {
Name : & Ident { "test" } ,
} } } ,
} ,
{
sql : ` DEFINE SCOPE test SESSION null ` ,
err : "Found `null` but expected `duration`" ,
} ,
{
sql : ` DEFINE SCOPE test SESSION 1h ` ,
str : ` DEFINE SCOPE test SESSION 1h0m0s ` ,
res : & Query { Statements : [ ] Statement { & DefineScopeStatement {
Name : & Ident { "test" } ,
Time : 1 * time . Hour ,
} } } ,
} ,
{
sql : ` DEFINE SCOPE test SESSION 1d ` ,
str : ` DEFINE SCOPE test SESSION 24h0m0s ` ,
res : & Query { Statements : [ ] Statement { & DefineScopeStatement {
Name : & Ident { "test" } ,
Time : 24 * time . Hour ,
} } } ,
} ,
{
sql : ` DEFINE SCOPE test SESSION 1w ` ,
str : ` DEFINE SCOPE test SESSION 168h0m0s ` ,
res : & Query { Statements : [ ] Statement { & DefineScopeStatement {
Name : & Ident { "test" } ,
Time : 168 * time . Hour ,
} } } ,
} ,
{
sql : ` DEFINE SCOPE test SIGNUP AS NONE ` ,
err : "Found `NONE` but expected `expression`" ,
} ,
{
sql : ` DEFINE SCOPE test SIGNUP AS (CREATE person) ` ,
res : & Query { Statements : [ ] Statement { & DefineScopeStatement {
Name : & Ident { "test" } ,
Signup : & SubExpression {
Expr : & CreateStatement {
2020-03-06 15:44:53 +00:00
What : Exprs { & Ident { "person" } } ,
Echo : AFTER ,
2017-11-16 20:53:13 +00:00
} ,
} ,
} } } ,
} ,
{
sql : ` DEFINE SCOPE test SIGNIN AS NONE ` ,
err : "Found `NONE` but expected `expression`" ,
} ,
{
sql : ` DEFINE SCOPE test SIGNIN AS (SELECT * FROM person) ` ,
res : & Query { Statements : [ ] Statement { & DefineScopeStatement {
Name : & Ident { "test" } ,
Signin : & SubExpression {
Expr : & SelectStatement {
2020-03-06 15:44:53 +00:00
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "person" } } ,
2017-11-16 20:53:13 +00:00
} ,
} ,
} } } ,
} ,
{
sql : ` DEFINE SCOPE test CONNECT AS NONE ` ,
err : "Found `NONE` but expected `expression`" ,
} ,
{
sql : ` DEFINE SCOPE test CONNECT AS (SELECT * FROM $id) ` ,
res : & Query { Statements : [ ] Statement { & DefineScopeStatement {
Name : & Ident { "test" } ,
Connect : & SubExpression {
Expr : & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Param { "id" } } ,
2019-11-21 00:44:31 +00:00
} ,
} ,
} } } ,
} ,
{
sql : ` DEFINE SCOPE test SIGNUP AS (CREATE tester) SIGNIN AS (SELECT * FROM tester) CONNECT AS (SELECT * FROM $id) ` ,
res : & Query { Statements : [ ] Statement { & DefineScopeStatement {
Name : & Ident { "test" } ,
Signup : & SubExpression {
Expr : & CreateStatement {
2020-03-06 15:44:53 +00:00
What : [ ] Expr { & Ident { "tester" } } ,
Echo : AFTER ,
2019-11-21 00:44:31 +00:00
} ,
} ,
Signin : & SubExpression {
Expr : & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Ident { "tester" } } ,
2019-11-21 00:44:31 +00:00
} ,
} ,
Connect : & SubExpression {
Expr : & SelectStatement {
2020-03-06 15:44:53 +00:00
RW : false ,
Expr : [ ] * Field { { Expr : & All { } , Field : "*" } } ,
What : [ ] Expr { & Param { "id" } } ,
2017-11-16 20:53:13 +00:00
} ,
} ,
} } } ,
} ,
{
sql : ` DEFINE SCOPE test something ` ,
err : "Found `something` but expected `;`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` DEFINE TABLE ` ,
err : "Found `` but expected `table`" ,
} ,
{
sql : ` DEFINE TABLE 111 ` ,
err : "Found `111` but expected `table`" ,
} ,
{
sql : ` DEFINE TABLE 111.111 ` ,
err : "Found `111.111` but expected `table`" ,
} ,
{
sql : ` DEFINE TABLE person ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person something ` ,
err : "Found `something` but expected `;`" ,
} ,
{
sql : ` DEFINE TABLE person DROP ` ,
str : ` DEFINE TABLE person DROP ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Drop : true ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person SCHEMALESS ` ,
str : ` DEFINE TABLE person ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Full : false ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person SCHEMAFULL ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Full : true ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person PERMISSIONS SOME ` ,
err : "Found `SOME` but expected `FOR, NONE, FULL, WHERE`" ,
} ,
{
sql : ` DEFINE TABLE person PERMISSIONS NONE ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Perms : & PermExpression {
Select : false ,
Create : false ,
Update : false ,
Delete : false ,
} ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person PERMISSIONS FULL ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Perms : & PermExpression {
Select : true ,
Create : true ,
Update : true ,
Delete : true ,
} ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person PERMISSIONS WHERE public = true ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Perms : & PermExpression {
Select : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Create : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Update : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Delete : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
} ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person PERMISSIONS FOR select FULL FOR insert, upsert NONE ` ,
err : "Found `insert` but expected `SELECT, CREATE, UPDATE, DELETE`" ,
} ,
{
sql : ` DEFINE TABLE person PERMISSIONS FOR select FULL FOR create, update, delete SOME ` ,
err : "Found `SOME` but expected `FULL, NONE, WHERE`" ,
} ,
{
sql : ` DEFINE TABLE person PERMISSIONS FOR select FULL FOR create, update, delete NONE ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Perms : & PermExpression {
Select : true ,
Create : false ,
Update : false ,
Delete : false ,
} ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person PERMISSIONS FOR select, create, update WHERE public = true FOR delete NONE ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Perms : & PermExpression {
Select : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Create : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Update : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Delete : false ,
} ,
} } } ,
} ,
{
2018-04-04 21:58:14 +00:00
sql : ` DEFINE TABLE person AS SELECT nationality, math.midhinge(age) AS mid FROM users GROUP BY nationality ` ,
2017-11-16 20:53:13 +00:00
err : "Found 'mid' but field is not an aggregate function, and is not present in GROUP expression" ,
} ,
{
sql : ` DEFINE TABLE person AS SELECT nationality, count(*) AS total FROM users WHERE public = true GROUP BY nationality ` ,
res : & Query { Statements : [ ] Statement { & DefineTableStatement {
What : Tables { & Table { "person" } } ,
Lock : true ,
Expr : Fields {
& Field {
2018-08-10 18:34:16 +00:00
Expr : & Ident { "nationality" } ,
2017-11-16 20:53:13 +00:00
Field : "nationality" ,
} ,
& Field {
Expr : & FuncExpression {
Name : "count" ,
Args : Exprs { & All { } } ,
Aggr : true ,
} ,
Field : "total" ,
Alias : "total" ,
} ,
} ,
From : Tables {
& Table { TB : "users" } ,
} ,
Cond : & BinaryExpression {
2018-08-10 18:34:16 +00:00
LHS : & Ident { "public" } ,
2017-11-16 20:53:13 +00:00
Op : EQ ,
RHS : true ,
} ,
Group : Groups {
& Group {
2018-08-10 18:34:16 +00:00
Expr : & Ident { "nationality" } ,
2017-11-16 20:53:13 +00:00
} ,
} ,
} } } ,
} ,
{
sql : ` DEFINE TABLE person SCHEMALESS something ` ,
err : "Found `something` but expected `;`" ,
} ,
{
sql : ` DEFINE TABLE person SCHEMAFULL something ` ,
err : "Found `something` but expected `;`" ,
2016-10-14 20:31:45 +00:00
} ,
2016-05-23 12:32:02 +00:00
// ----------------------------------------------------------------------
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE EVENT ` ,
2016-02-27 12:02:41 +00:00
err : "Found `` but expected `name`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE EVENT temp ` ,
2016-05-23 12:32:02 +00:00
err : "Found `` but expected `ON`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE EVENT temp ON ` ,
2017-04-14 12:24:53 +00:00
err : "Found `` but expected `table`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE EVENT temp ON person ` ,
err : "Found `` but expected `WHEN`" ,
} ,
{
sql : ` DEFINE EVENT temp ON person WHEN ` ,
err : "Found `` but expected `expression`" ,
} ,
{
sql : ` DEFINE EVENT temp ON person WHEN $before.price < $after.price ` ,
err : "Found `` but expected `THEN`" ,
} ,
{
sql : ` DEFINE EVENT temp ON person WHEN $before.price < $after.price THEN ` ,
2018-04-04 18:20:07 +00:00
err : "Found `` but expected `(`" ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` DEFINE EVENT temp ON person WHEN $before.price < $after.price THEN (UPDATE $this SET increased = true) ` ,
res : & Query { Statements : [ ] Statement { & DefineEventStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2017-11-16 20:53:13 +00:00
When : & BinaryExpression {
LHS : & Param { "before.price" } ,
Op : LT ,
RHS : & Param { "after.price" } ,
} ,
2018-04-04 18:20:07 +00:00
Then : & MultExpression {
Expr : [ ] Expr {
& UpdateStatement {
What : Exprs { & Param { "this" } } ,
Data : & DataExpression { [ ] * ItemExpression {
{
LHS : & Ident { "increased" } ,
Op : EQ ,
RHS : true ,
} ,
} } ,
2020-03-06 15:44:53 +00:00
Echo : AFTER ,
2018-04-04 18:20:07 +00:00
} ,
} ,
} ,
} } } ,
} ,
{
sql : ` DEFINE EVENT temp ON person WHEN $before.price < $after.price THEN (UPDATE $this SET increased = true; UPDATE $this SET finished = true) ` ,
res : & Query { Statements : [ ] Statement { & DefineEventStatement {
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
When : & BinaryExpression {
LHS : & Param { "before.price" } ,
Op : LT ,
RHS : & Param { "after.price" } ,
} ,
Then : & MultExpression {
Expr : [ ] Expr {
& UpdateStatement {
What : Exprs { & Param { "this" } } ,
Data : & DataExpression { [ ] * ItemExpression {
{
LHS : & Ident { "increased" } ,
Op : EQ ,
RHS : true ,
} ,
} } ,
2020-03-06 15:44:53 +00:00
Echo : AFTER ,
2018-04-04 18:20:07 +00:00
} ,
& UpdateStatement {
What : Exprs { & Param { "this" } } ,
Data : & DataExpression { [ ] * ItemExpression {
{
LHS : & Ident { "finished" } ,
Op : EQ ,
RHS : true ,
} ,
} } ,
2020-03-06 15:44:53 +00:00
Echo : AFTER ,
2018-04-04 18:20:07 +00:00
} ,
2017-11-16 20:53:13 +00:00
} ,
} ,
2017-02-23 15:09:02 +00:00
} } } ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE EVENT temp ON person WHEN $before.price < $after.price THEN (UPDATE $this SET increased = true) something ` ,
err : "Found `something` but expected `;`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` DEFINE FIELD ` ,
err : "Found `` but expected `name, or expression`" ,
2016-09-06 13:30:59 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ` ,
err : "Found `` but expected `ON`" ,
} ,
{
sql : ` DEFINE FIELD temp ON ` ,
err : "Found `` but expected `table`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
2017-11-16 20:53:13 +00:00
{
sql : ` DEFINE FIELD temp ON person TYPE ` ,
2019-01-23 01:34:59 +00:00
err : "Found `` but expected `array, boolean, circle, datetime, number, object, point, polygon, record, string`" ,
2017-11-16 20:53:13 +00:00
} ,
{
sql : ` DEFINE FIELD temp ON person TYPE something ` ,
2019-01-23 01:34:59 +00:00
err : "Found `something` but expected `array, boolean, circle, datetime, number, object, point, polygon, record, string`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` DEFINE FIELD temp ON person TYPE array ` ,
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2016-09-06 13:30:59 +00:00
Type : "array" ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` DEFINE FIELD temp ON person TYPE object ` ,
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2016-09-06 13:30:59 +00:00
Type : "object" ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` DEFINE FIELD temp ON person TYPE string ` ,
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2016-09-06 13:30:59 +00:00
Type : "string" ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
sql : ` DEFINE FIELD temp ON person TYPE number ` ,
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2016-09-06 13:30:59 +00:00
Type : "number" ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person TYPE record ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2017-11-16 20:53:13 +00:00
Type : "record" ,
2016-07-04 10:37:37 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person TYPE record (item) ` ,
2016-07-04 10:37:37 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2017-11-16 20:53:13 +00:00
Type : "record" ,
Kind : "item" ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
2016-09-06 13:30:59 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person VALUE ` ,
err : "Found `` but expected `expression`" ,
2016-09-06 13:30:59 +00:00
} ,
2016-05-23 12:32:02 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person VALUE string.uppercase($value) ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-11-16 20:53:13 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
Value : & FuncExpression { Name : "string.uppercase" , Args : Exprs { & Param { "value" } } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person ASSERT ` ,
err : "Found `` but expected `expression`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person ASSERT $value > 0 AND $value < 100 ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-11-16 20:53:13 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
Assert : & BinaryExpression {
2017-12-06 20:06:28 +00:00
LHS : & BinaryExpression {
LHS : & Param { "value" } ,
Op : GT ,
RHS : 0.0 ,
} ,
Op : AND ,
2017-11-16 20:53:13 +00:00
RHS : & BinaryExpression {
2017-12-06 20:06:28 +00:00
LHS : & Param { "value" } ,
Op : LT ,
RHS : 100.0 ,
2017-11-16 20:53:13 +00:00
} ,
} ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person PERMISSIONS SOME ` ,
err : "Found `SOME` but expected `FOR, NONE, FULL, WHERE`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person PERMISSIONS NONE ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-11-16 20:53:13 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
Perms : & PermExpression {
Select : false ,
Create : false ,
Update : false ,
Delete : false ,
} ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person PERMISSIONS FULL ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-11-16 20:53:13 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
Perms : & PermExpression {
Select : true ,
Create : true ,
Update : true ,
Delete : true ,
} ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person PERMISSIONS WHERE public = true ` ,
2016-09-06 13:30:59 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-11-16 20:53:13 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
Perms : & PermExpression {
Select : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Create : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Update : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Delete : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
} ,
2016-09-06 13:30:59 +00:00
} } } ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person PERMISSIONS FOR select FULL FOR insert, upsert NONE ` ,
err : "Found `insert` but expected `SELECT, CREATE, UPDATE, DELETE`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person PERMISSIONS FOR select FULL FOR create, update, delete SOME ` ,
err : "Found `SOME` but expected `FULL, NONE, WHERE`" ,
2016-07-04 10:37:37 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person PERMISSIONS FOR select FULL FOR create, update, delete NONE ` ,
2016-07-04 10:37:37 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2017-11-16 20:53:13 +00:00
Perms : & PermExpression {
Select : true ,
Create : false ,
Update : false ,
Delete : false ,
} ,
2016-07-04 10:37:37 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person PERMISSIONS FOR select, create, update WHERE public = true FOR delete NONE ` ,
2016-07-04 10:37:37 +00:00
res : & Query { Statements : [ ] Statement { & DefineFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2017-11-16 20:53:13 +00:00
Perms : & PermExpression {
Select : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Create : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Update : & BinaryExpression { LHS : & Ident { "public" } , Op : EQ , RHS : true } ,
Delete : false ,
} ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE FIELD temp ON person something ` ,
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
2017-11-16 20:53:13 +00:00
// ----------------------------------------------------------------------
2016-05-23 12:32:02 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE INDEX ` ,
err : "Found `` but expected `name`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE INDEX temp ` ,
err : "Found `` but expected `ON`" ,
2016-07-04 10:37:37 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE INDEX temp ON ` ,
err : "Found `` but expected `table`" ,
} ,
{
sql : ` DEFINE INDEX temp ON person ` ,
err : "Found `` but expected `COLUMNS`" ,
} ,
{
sql : ` DEFINE INDEX temp ON person COLUMNS ` ,
err : "Found `` but expected `name, or expression`" ,
} ,
{
sql : ` DEFINE INDEX temp ON person COLUMNS firstname, lastname ` ,
res : & Query { Statements : [ ] Statement { & DefineIndexStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2017-11-16 20:53:13 +00:00
Cols : Idents { & Ident { "firstname" } , & Ident { "lastname" } } ,
Uniq : false ,
2016-07-04 10:37:37 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE INDEX temp ON person COLUMNS firstname, lastname UNIQUE ` ,
res : & Query { Statements : [ ] Statement { & DefineIndexStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2017-11-16 20:53:13 +00:00
Cols : Idents { & Ident { "firstname" } , & Ident { "lastname" } } ,
Uniq : true ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` DEFINE INDEX temp ON person COLUMNS firstname, lastname something UNIQUE ` ,
err : "Found `something` but expected `;`" ,
} ,
{
sql : ` DEFINE INDEX temp ON person COLUMNS firstname, lastname UNIQUE something ` ,
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
2017-11-16 20:53:13 +00:00
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Remove ( t * testing . T ) {
var tests = [ ] tester {
2016-09-06 13:30:59 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE ` ,
err : "Found `` but expected `NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, EVENT, FIELD, INDEX`" ,
2016-09-06 13:30:59 +00:00
} ,
2017-11-16 20:53:13 +00:00
// ----------------------------------------------------------------------
2016-09-06 13:30:59 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE NAMESPACE ` ,
err : "Found `` but expected `name`" ,
2016-09-06 13:30:59 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE NAMESPACE 111 ` ,
err : "Found `111` but expected `name`" ,
2016-09-06 13:30:59 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE NAMESPACE 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
2016-09-06 13:30:59 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE NAMESPACE test ` ,
res : & Query { Statements : [ ] Statement { & RemoveNamespaceStatement {
2019-01-31 10:03:50 +00:00
Name : & Ident { "test" } ,
2016-09-06 13:30:59 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE NAMESPACE test something ` ,
err : "Found `something` but expected `;`" ,
2016-09-06 13:30:59 +00:00
} ,
2017-11-16 20:53:13 +00:00
// ----------------------------------------------------------------------
2016-05-23 12:32:02 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE DATABASE ` ,
err : "Found `` but expected `name`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE DATABASE 111 ` ,
err : "Found `111` but expected `name`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE DATABASE 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE DATABASE test ` ,
res : & Query { Statements : [ ] Statement { & RemoveDatabaseStatement {
2019-01-31 10:03:50 +00:00
Name : & Ident { "test" } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE DATABASE test something ` ,
err : "Found `something` but expected `;`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` REMOVE LOGIN ` ,
err : "Found `` but expected `name`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE LOGIN 111 ` ,
err : "Found `111` but expected `name`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE LOGIN 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE LOGIN test ` ,
err : "Found `` but expected `ON`" ,
2016-05-23 12:32:02 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE LOGIN test ON ` ,
err : "Found `` but expected `NAMESPACE, DATABASE`" ,
2016-05-23 12:32:02 +00:00
} ,
2016-09-06 13:30:59 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE LOGIN test ON something ` ,
err : "Found `something` but expected `NAMESPACE, DATABASE`" ,
2016-09-06 13:30:59 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE LOGIN test ON NAMESPACE ` ,
res : & Query { Statements : [ ] Statement { & RemoveLoginStatement {
Kind : NAMESPACE ,
User : & Ident { "test" } ,
2016-09-06 13:30:59 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE LOGIN test ON DATABASE ` ,
res : & Query { Statements : [ ] Statement { & RemoveLoginStatement {
Kind : DATABASE ,
User : & Ident { "test" } ,
2016-09-06 13:30:59 +00:00
} } } ,
} ,
2016-05-23 12:32:02 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE LOGIN test ON DATABASE something ` ,
err : "Found `something` but expected `;`" ,
2016-02-26 17:27:07 +00:00
} ,
2016-05-23 12:32:02 +00:00
// ----------------------------------------------------------------------
2016-02-26 17:27:07 +00:00
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN ` ,
2016-02-27 12:02:41 +00:00
err : "Found `` but expected `name`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN 111 ` ,
err : "Found `111` but expected `name`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN test ` ,
err : "Found `` but expected `ON`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN test ON ` ,
err : "Found `` but expected `NAMESPACE, DATABASE, SCOPE`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN test ON something ` ,
err : "Found `something` but expected `NAMESPACE, DATABASE, SCOPE`" ,
} ,
{
sql : ` REMOVE TOKEN test ON NAMESPACE ` ,
res : & Query { Statements : [ ] Statement { & RemoveTokenStatement {
Kind : NAMESPACE ,
Name : & Ident { "test" } ,
2018-09-13 15:35:06 +00:00
What : & Ident { "" } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN test ON DATABASE ` ,
res : & Query { Statements : [ ] Statement { & RemoveTokenStatement {
Kind : DATABASE ,
Name : & Ident { "test" } ,
2018-09-13 15:35:06 +00:00
What : & Ident { "" } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN test ON SCOPE ` ,
2018-09-13 15:35:06 +00:00
err : "Found `` but expected `name`" ,
} ,
{
sql : ` REMOVE TOKEN test ON SCOPE test ` ,
2017-11-16 20:53:13 +00:00
res : & Query { Statements : [ ] Statement { & RemoveTokenStatement {
Kind : SCOPE ,
Name : & Ident { "test" } ,
2018-09-13 15:35:06 +00:00
What : & Ident { "test" } ,
2017-11-16 20:53:13 +00:00
} } } ,
2016-02-26 17:27:07 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE TOKEN test ON DATABASE something ` ,
err : "Found `something` but expected `;`" ,
2016-02-26 17:27:07 +00:00
} ,
2016-10-14 21:15:40 +00:00
// ----------------------------------------------------------------------
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE SCOPE ` ,
2016-10-14 21:15:40 +00:00
err : "Found `` but expected `name`" ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE SCOPE 111 ` ,
err : "Found `111` but expected `name`" ,
2016-10-14 21:15:40 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE SCOPE 111.111 ` ,
err : "Found `111.111` but expected `name`" ,
2016-10-14 21:15:40 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE SCOPE test ` ,
res : & Query { Statements : [ ] Statement { & RemoveScopeStatement {
2019-01-31 10:03:50 +00:00
Name : & Ident { "test" } ,
2016-10-14 21:15:40 +00:00
} } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` REMOVE SCOPE test something ` ,
err : "Found `something` but expected `;`" ,
2016-05-23 12:32:02 +00:00
} ,
2016-09-06 13:30:59 +00:00
// ----------------------------------------------------------------------
2016-05-23 12:32:02 +00:00
{
sql : ` REMOVE TABLE ` ,
2017-04-14 12:24:53 +00:00
err : "Found `` but expected `table`" ,
2016-02-26 17:27:07 +00:00
} ,
2016-09-14 21:22:18 +00:00
{
sql : ` REMOVE TABLE 111 ` ,
2017-04-14 12:24:53 +00:00
err : "Found `111` but expected `table`" ,
2016-09-14 21:22:18 +00:00
} ,
{
sql : ` REMOVE TABLE 111.111 ` ,
2017-04-14 12:24:53 +00:00
err : "Found `111.111` but expected `table`" ,
2016-09-14 21:22:18 +00:00
} ,
2016-02-26 17:27:07 +00:00
{
2016-05-23 12:32:02 +00:00
sql : ` REMOVE TABLE person ` ,
res : & Query { Statements : [ ] Statement { & RemoveTableStatement {
2017-04-14 12:24:53 +00:00
What : Tables { & Table { "person" } } ,
2016-05-23 12:32:02 +00:00
} } } ,
} ,
2016-09-14 21:22:18 +00:00
{
sql : ` REMOVE TABLE person something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
} ,
// ----------------------------------------------------------------------
{
sql : ` REMOVE EVENT ` ,
err : "Found `` but expected `name`" ,
} ,
{
sql : ` REMOVE EVENT temp ` ,
err : "Found `` but expected `ON`" ,
} ,
{
sql : ` REMOVE EVENT temp ON ` ,
err : "Found `` but expected `table`" ,
} ,
{
sql : ` REMOVE EVENT temp ON person ` ,
res : & Query { Statements : [ ] Statement { & RemoveEventStatement {
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
} } } ,
} ,
{
sql : ` REMOVE EVENT temp ON person something ` ,
err : "Found `something` but expected `;`" ,
2016-09-14 21:22:18 +00:00
} ,
2016-09-06 13:30:59 +00:00
// ----------------------------------------------------------------------
2016-05-23 12:32:02 +00:00
{
sql : ` REMOVE FIELD ` ,
2018-04-26 23:41:28 +00:00
err : "Found `` but expected `name, or expression`" ,
2016-02-26 17:27:07 +00:00
} ,
{
2016-05-23 12:32:02 +00:00
sql : ` REMOVE FIELD temp ` ,
err : "Found `` but expected `ON`" ,
} ,
{
sql : ` REMOVE FIELD temp ON ` ,
2017-04-14 12:24:53 +00:00
err : "Found `` but expected `table`" ,
2016-05-23 12:32:02 +00:00
} ,
{
sql : ` REMOVE FIELD temp ON person ` ,
res : & Query { Statements : [ ] Statement { & RemoveFieldStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2016-05-23 12:32:02 +00:00
sql : ` REMOVE FIELD temp ON person something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-02-26 17:27:07 +00:00
} ,
2016-05-23 12:32:02 +00:00
// ----------------------------------------------------------------------
2016-02-26 17:27:07 +00:00
{
sql : ` REMOVE INDEX ` ,
2016-02-27 12:02:41 +00:00
err : "Found `` but expected `name`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` REMOVE INDEX temp ` ,
2016-02-27 12:02:41 +00:00
err : "Found `` but expected `ON`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` REMOVE INDEX temp ON ` ,
2017-04-14 12:24:53 +00:00
err : "Found `` but expected `table`" ,
2016-02-26 17:27:07 +00:00
} ,
{
sql : ` REMOVE INDEX temp ON person ` ,
2016-05-23 12:32:02 +00:00
res : & Query { Statements : [ ] Statement { & RemoveIndexStatement {
2017-04-14 12:24:53 +00:00
Name : & Ident { "temp" } ,
What : Tables { & Table { "person" } } ,
2016-02-26 17:27:07 +00:00
} } } ,
} ,
{
2016-05-23 12:32:02 +00:00
sql : ` REMOVE INDEX temp ON person something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-10-29 17:47:09 +00:00
} ,
2016-02-26 17:27:07 +00:00
}
for _ , test := range tests {
testsql ( t , test )
}
}
2016-09-06 13:30:59 +00:00
func Test_Parse_Queries_Begin ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` BEGIN ` ,
2017-11-16 20:53:13 +00:00
str : ` BEGIN TRANSACTION ` ,
2016-09-06 13:30:59 +00:00
res : & Query { Statements : [ ] Statement { & BeginStatement { } } } ,
} ,
{
sql : ` BEGIN something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-09-06 13:30:59 +00:00
} ,
{
sql : ` BEGIN TRANSACTION ` ,
res : & Query { Statements : [ ] Statement { & BeginStatement { } } } ,
} ,
{
sql : ` BEGIN TRANSACTION something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-09-06 13:30:59 +00:00
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Cancel ( t * testing . T ) {
var tests = [ ] tester {
{
sql : ` CANCEL ` ,
2017-11-16 20:53:13 +00:00
str : ` CANCEL TRANSACTION ` ,
2016-09-06 13:30:59 +00:00
res : & Query { Statements : [ ] Statement { & CancelStatement { } } } ,
} ,
{
sql : ` CANCEL something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-09-06 13:30:59 +00:00
} ,
{
sql : ` CANCEL TRANSACTION ` ,
res : & Query { Statements : [ ] Statement { & CancelStatement { } } } ,
} ,
{
sql : ` CANCEL TRANSACTION something ` ,
2017-11-16 20:53:13 +00:00
err : "Found `something` but expected `;`" ,
2016-09-06 13:30:59 +00:00
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}
func Test_Parse_Queries_Commit ( t * testing . T ) {
var tests = [ ] tester {
{
2017-11-16 20:53:13 +00:00
sql : ` COMMIT ` ,
str : ` COMMIT TRANSACTION ` ,
2016-09-06 13:30:59 +00:00
res : & Query { Statements : [ ] Statement { & CommitStatement { } } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` COMMIT something ` ,
err : "Found `something` but expected `;`" ,
2016-09-06 13:30:59 +00:00
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` COMMIT TRANSACTION ` ,
2016-09-06 13:30:59 +00:00
res : & Query { Statements : [ ] Statement { & CommitStatement { } } } ,
} ,
{
2017-11-16 20:53:13 +00:00
sql : ` COMMIT TRANSACTION something ` ,
err : "Found `something` but expected `;`" ,
2016-09-06 13:30:59 +00:00
} ,
}
for _ , test := range tests {
testsql ( t , test )
}
}