surrealpatch/sql/sql_test.go

3348 lines
87 KiB
Go
Raw Normal View History

2016-02-26 17:27:07 +00:00
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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
2016-05-23 12:32:02 +00:00
"github.com/abcum/fibre"
2016-11-04 12:15:51 +00:00
"github.com/abcum/surreal/cnf"
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 ""
}
2016-05-23 12:32:02 +00:00
var c *fibre.Context
2016-02-26 17:27:07 +00:00
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
}
2017-11-16 20:53:13 +00:00
s, e := Parse(c, 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-11-04 12:15:51 +00:00
cnf.Settings = &cnf.Options{}
2017-11-16 20:53:13 +00:00
cnf.Settings.DB.Path = "memory"
2016-11-04 12:15:51 +00:00
cnf.Settings.DB.Base = "*"
2016-02-26 17:27:07 +00:00
2017-02-23 15:09:02 +00:00
auth := &cnf.Auth{}
2017-04-14 12:24:53 +00:00
auth.Kind = cnf.AuthKV
2017-02-23 15:09:02 +00:00
auth.Possible.NS = "*"
auth.Selected.NS = "*"
auth.Possible.DB = "*"
auth.Selected.DB = "*"
2016-11-04 12:15:51 +00:00
c = fibre.NewContext(nil, nil, nil)
2017-02-23 15:09:02 +00:00
c.Set("auth", auth)
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: `BEGIN; USE NS name DB name; COMMIT;`,
err: "You can't change NAMESPACE or DATABASE inside of a transaction",
},
{
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)
}
}
// Ensure the parser can parse a multi-statement query.
func Test_Parse_General(t *testing.T) {
s := `SELECT a FROM b`
2017-11-16 20:53:13 +00:00
q, err := Parse(c, 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`
2017-11-16 20:53:13 +00:00
q, err := Parse(c, 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`
2017-11-16 20:53:13 +00:00
q, err := Parse(c, 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: `!`,
2017-11-16 20:53:13 +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`",
},
}
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{
KV: "*", NS: "*", DB: "*",
Kind: ALL,
}}},
2017-11-16 20:53:13 +00:00
},
{
sql: `INFO FOR NAMESPACE`,
res: &Query{Statements: []Statement{&InfoStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Kind: DATABASE,
}}},
},
2018-10-24 10:24:24 +00:00
{
sql: `INFO FOR DB`,
str: `INFO FOR DATABASE`,
res: &Query{Statements: []Statement{&InfoStatement{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
RW: false, KV: "*", NS: "*", DB: "*",
Name: &Ident{"name"},
What: &Thing{TB: "thing", ID: "test"},
}}},
},
{
sql: `LET name = thing:test`,
res: &Query{Statements: []Statement{&LetStatement{
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"name"},
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{
RW: true, KV: "*", NS: "*", DB: "*",
Name: &Ident{"name"},
What: &SubExpression{Expr: &CreateStatement{
KV: "*", NS: "*", DB: "*",
What: Exprs{&Ident{"person"}},
2017-11-16 20:53:13 +00:00
Echo: AFTER,
}},
}}},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
What: Exprs{true},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN true`,
res: &Query{Statements: []Statement{&ReturnStatement{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
What: Exprs{true},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN false`,
res: &Query{Statements: []Statement{&ReturnStatement{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
What: Exprs{false},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN "test"`,
res: &Query{Statements: []Statement{&ReturnStatement{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
What: Exprs{&Value{"test"}},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN 1`,
res: &Query{Statements: []Statement{&ReturnStatement{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
What: Exprs{float64(1)},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN 1.1`,
res: &Query{Statements: []Statement{&ReturnStatement{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
What: Exprs{[]interface{}{"key", "val"}},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN $test`,
res: &Query{Statements: []Statement{&ReturnStatement{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
RW: true, KV: "*", NS: "*", DB: "*",
What: Exprs{&SubExpression{Expr: &CreateStatement{
KV: "*", NS: "*", DB: "*",
What: Exprs{&Ident{"person"}},
2017-11-16 20:53:13 +00:00
Echo: AFTER,
}}},
}}},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{float64(111)},
}}},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: "1a"}},
}}},
},
{
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: "1a"}},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: float64(123.456)}},
}}},
},
{
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: float64(123.456)}},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-05-24 12:51:52 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-07 15:58:50 +00:00
What: []Expr{&Thing{TB: "person", ID: "123.456.789.012"}},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: date}},
}}},
},
{
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: nano}},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-07 15:58:50 +00:00
What: []Expr{&Thing{TB: "person", ID: "A250C5A3-948F-4657-88AD-FF5F27B5B24E"}},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-07 15:58:50 +00:00
What: []Expr{&Thing{TB: "person", ID: "8250C5A3-948F-4657-88AD-FF5F27B5B24E"}},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-07 15:58:50 +00:00
What: []Expr{&Thing{TB: "person", ID: "Tobie Morgan Hitchcock"}},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-07 15:58:50 +00:00
What: []Expr{&Thing{TB: "email addresses", ID: "tobie@abcum.com"}},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
2016-09-06 13:30:59 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{
{Expr: &All{}, Field: "*"},
{Expr: &Ident{"temp"}, Field: "test", Alias: "test"},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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
},
2017-11-16 20:53:13 +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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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
},
2017-11-16 20:53:13 +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{
2017-11-16 20:53:13 +00:00
RW: true, KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
What: Exprs{&Ident{"person"}},
2017-11-16 20:53:13 +00:00
Echo: AFTER,
}}},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2017-04-14 12:24:53 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2017-04-14 12:24:53 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
Cond: &Ident{"id"},
}}},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
Cond: &BinaryExpression{LHS: &Ident{"old"}, Op: EQ, RHS: false},
2016-02-26 17:27:07 +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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
2016-11-04 11:13:34 +00:00
Cond: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &Ident{"id"},
Op: NEQ,
2017-12-12 01:09:08 +00:00
RHS: &Null{},
},
Op: AND,
RHS: &BinaryExpression{
2016-11-04 11:13:34 +00:00
LHS: &BinaryExpression{
LHS: &Ident{"id"},
Op: GT,
RHS: float64(13.9),
},
Op: AND,
RHS: &BinaryExpression{
2016-11-04 11:13:34 +00:00
LHS: &BinaryExpression{
LHS: &Ident{"id"},
Op: LT,
RHS: float64(31),
},
Op: AND,
RHS: &BinaryExpression{
2016-11-04 11:13:34 +00:00
LHS: &BinaryExpression{
LHS: &Ident{"id"},
Op: GTE,
RHS: float64(15),
2016-11-04 11:13:34 +00:00
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{"id"},
Op: LTE,
RHS: float64(29.9),
2016-11-04 11:13:34 +00:00
},
},
},
},
2016-02-26 17:27:07 +00:00
},
}}},
},
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: false, KV: "*", NS: "*", DB: "*",
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{
2017-11-16 20:53:13 +00:00
RW: true, KV: "*", NS: "*", DB: "*",
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{
LHS: &Ident{"bday"},
2016-11-04 11:13:34 +00:00
Op: GTE,
RHS: bday1,
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{"bday"},
2016-11-04 11:13:34 +00:00
Op: GTE,
RHS: bday2,
},
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{"bday"},
2016-11-04 11:13:34 +00:00
Op: GTE,
RHS: bday3,
},
},
Op: AND,
RHS: &BinaryExpression{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-04-14 12:24:53 +00:00
Data: &DataExpression{Data: []*ItemExpression{{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Void{}}}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-04-14 12:24:53 +00:00
Data: &DataExpression{Data: []*ItemExpression{{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Empty{}}}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-04-14 12:24:53 +00:00
Data: &DataExpression{Data: []*ItemExpression{{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Value{"Tobie"}}}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Data: &MergeExpression{Data: map[string]interface{}{"firstname": "Tobie"}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Data: &ContentExpression{Data: map[string]interface{}{"firstname": "Tobie"}},
2017-02-23 15:09:02 +00:00
Echo: AFTER,
2016-05-23 12:32:02 +00:00
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `CREATE person RETURN`,
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
2016-05-23 12:32:02 +00:00
},
{
sql: `CREATE person RETURN NONE`,
res: &Query{Statements: []Statement{&CreateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: NONE,
}}},
},
{
sql: `CREATE person RETURN BOTH`,
res: &Query{Statements: []Statement{&CreateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BOTH,
}}},
},
{
sql: `CREATE person RETURN DIFF`,
res: &Query{Statements: []Statement{&CreateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: DIFF,
}}},
},
{
sql: `CREATE person RETURN BEFORE`,
res: &Query{Statements: []Statement{&CreateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BEFORE,
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: AFTER,
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `CREATE person TIMEOUT 1s`,
res: &Query{Statements: []Statement{&CreateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Echo: AFTER,
Timeout: 1 * time.Second,
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-04-14 12:24:53 +00:00
Data: &DataExpression{Data: []*ItemExpression{{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Void{}}}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-04-14 12:24:53 +00:00
Data: &DataExpression{Data: []*ItemExpression{{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Empty{}}}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-04-14 12:24:53 +00:00
Data: &DataExpression{Data: []*ItemExpression{{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Value{"Tobie"}}}},
2017-02-23 15:09:02 +00:00
Echo: AFTER,
2016-02-26 17:27:07 +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`",
},
{
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 `;`",
},
{
2017-02-23 15:09:02 +00:00
sql: `UPDATE person DIFF []`,
res: &Query{Statements: []Statement{&UpdateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Data: &DiffExpression{Data: []interface{}{}},
2017-02-23 15:09:02 +00:00
Echo: AFTER,
}}},
},
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Data: &MergeExpression{Data: map[string]interface{}{"firstname": "Tobie"}},
2017-02-23 15:09:02 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Data: &ContentExpression{Data: map[string]interface{}{"firstname": "Tobie"}},
2017-02-23 15:09:02 +00:00
Echo: AFTER,
2016-05-23 12:32:02 +00:00
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `UPDATE person RETURN`,
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
2016-05-23 12:32:02 +00:00
},
{
sql: `UPDATE person RETURN NONE`,
res: &Query{Statements: []Statement{&UpdateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: NONE,
}}},
},
{
sql: `UPDATE person RETURN BOTH`,
res: &Query{Statements: []Statement{&UpdateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BOTH,
}}},
},
{
sql: `UPDATE person RETURN DIFF`,
res: &Query{Statements: []Statement{&UpdateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: DIFF,
}}},
},
{
sql: `UPDATE person RETURN BEFORE`,
res: &Query{Statements: []Statement{&UpdateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BEFORE,
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: AFTER,
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `UPDATE person TIMEOUT 1s`,
res: &Query{Statements: []Statement{&UpdateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Echo: AFTER,
Timeout: 1 * time.Second,
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2017-02-23 15:09:02 +00:00
Echo: NONE,
2016-05-23 12:32:02 +00:00
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `DELETE AND EXPUNGE person`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&DeleteStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Hard: true,
Echo: NONE,
2016-05-23 12:32:02 +00:00
}}},
},
2017-11-16 20:53:13 +00:00
{
sql: `DELETE person RETURN`,
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
},
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: NONE,
}}},
},
{
sql: `DELETE person RETURN BOTH`,
res: &Query{Statements: []Statement{&DeleteStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BOTH,
}}},
},
{
sql: `DELETE person RETURN DIFF`,
res: &Query{Statements: []Statement{&DeleteStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: DIFF,
}}},
},
{
sql: `DELETE person RETURN BEFORE`,
res: &Query{Statements: []Statement{&DeleteStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BEFORE,
}}},
},
{
sql: `DELETE person RETURN AFTER`,
res: &Query{Statements: []Statement{&DeleteStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
2016-05-23 12:32:02 +00:00
Echo: AFTER,
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `DELETE person TIMEOUT 1s`,
res: &Query{Statements: []Statement{&DeleteStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: []Expr{&Ident{"person"}},
Echo: NONE,
Timeout: 1 * time.Second,
}}},
},
{
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`,
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
{
2017-11-16 20:53:13 +00:00
sql: `RELATE purchase`,
err: "Found `` but expected `FROM`",
2016-09-14 21:22:18 +00:00
},
{
2017-11-16 20:53:13 +00:00
sql: `RELATE purchase FROM`,
err: "Found `` but expected `expression`",
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: `RELATE purchase FROM person`,
err: "Found `` but expected `TO, WITH`",
2016-05-23 12:32:02 +00:00
},
{
2017-11-16 20:53:13 +00:00
sql: `RELATE purchase FROM person WITH`,
err: "Found `` but expected `expression`",
2016-05-23 12:32:02 +00:00
},
{
2017-11-16 20:53:13 +00:00
sql: `RELATE purchase FROM person WITH item`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Type: &Table{"purchase"},
From: []Expr{&Ident{"person"}},
With: []Expr{&Ident{"item"}},
Echo: AFTER,
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `RELATE purchase FROM person WITH item UNIQUE`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Type: &Table{"purchase"},
From: []Expr{&Ident{"person"}},
With: []Expr{&Ident{"item"}},
Uniq: true,
Echo: AFTER,
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `RELATE purchase FROM person WITH item SET 123`,
err: "Found `123` but expected `field name`",
},
{
2017-11-16 20:53:13 +00:00
sql: `RELATE purchase FROM person WITH item SET firstname`,
err: "Found `` but expected `=, +=, -=`",
},
{
sql: `RELATE purchase FROM person WITH item SET public = true`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +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,
}}},
},
{
sql: `RELATE purchase FROM person WITH item RETURN`,
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
},
{
sql: `RELATE purchase FROM person WITH item RETURN NONE`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Type: &Table{"purchase"},
From: []Expr{&Ident{"person"}},
With: []Expr{&Ident{"item"}},
Echo: NONE,
}}},
},
{
sql: `RELATE purchase FROM person WITH item RETURN BOTH`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Type: &Table{"purchase"},
From: []Expr{&Ident{"person"}},
With: []Expr{&Ident{"item"}},
Echo: BOTH,
}}},
},
{
sql: `RELATE purchase FROM person WITH item RETURN DIFF`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Type: &Table{"purchase"},
From: []Expr{&Ident{"person"}},
With: []Expr{&Ident{"item"}},
Echo: DIFF,
}}},
},
{
sql: `RELATE purchase FROM person WITH item RETURN BEFORE`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Type: &Table{"purchase"},
From: []Expr{&Ident{"person"}},
With: []Expr{&Ident{"item"}},
Echo: BEFORE,
}}},
},
{
sql: `RELATE purchase FROM person WITH item RETURN AFTER`,
str: `RELATE purchase FROM person WITH item`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Type: &Table{"purchase"},
From: []Expr{&Ident{"person"}},
With: []Expr{&Ident{"item"}},
Echo: AFTER,
}}},
},
{
sql: `RELATE purchase FROM person WITH item TIMEOUT 1s`,
res: &Query{Statements: []Statement{&RelateStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Type: &Table{"purchase"},
From: []Expr{&Ident{"person"}},
With: []Expr{&Ident{"item"}},
Echo: AFTER,
Timeout: 1 * time.Second,
}}},
},
{
sql: `RELATE purchase FROM person WITH item TIMEOUT null`,
err: "Found `null` but expected `duration`",
},
{
sql: `RELATE purchase FROM person WITH item something`,
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: AFTER,
}}},
},
{
sql: `INSERT ["one","two","tre"] INTO person RETURN`,
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
},
{
sql: `INSERT ["one","two","tre"] INTO person RETURN NONE`,
res: &Query{Statements: []Statement{&InsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: NONE,
}}},
},
{
sql: `INSERT ["one","two","tre"] INTO person RETURN INFO`,
res: &Query{Statements: []Statement{&InsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: INFO,
}}},
},
{
sql: `INSERT ["one","two","tre"] INTO person RETURN BOTH`,
res: &Query{Statements: []Statement{&InsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: BOTH,
}}},
},
{
sql: `INSERT ["one","two","tre"] INTO person RETURN DIFF`,
res: &Query{Statements: []Statement{&InsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: DIFF,
}}},
},
{
sql: `INSERT ["one","two","tre"] INTO person RETURN BEFORE`,
res: &Query{Statements: []Statement{&InsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: BEFORE,
}}},
},
{
sql: `INSERT ["one","two","tre"] INTO person RETURN AFTER`,
str: `INSERT ["one","two","tre"] INTO person`,
res: &Query{Statements: []Statement{&InsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: AFTER,
}}},
},
{
sql: `INSERT ["one","two","tre"] INTO person TIMEOUT 1s`,
res: &Query{Statements: []Statement{&InsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: AFTER,
Timeout: 1 * time.Second,
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: AFTER,
}}},
},
{
sql: `UPSERT ["one","two","tre"] INTO person RETURN`,
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
},
{
sql: `UPSERT ["one","two","tre"] INTO person RETURN NONE`,
res: &Query{Statements: []Statement{&UpsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: NONE,
}}},
},
{
sql: `UPSERT ["one","two","tre"] INTO person RETURN INFO`,
res: &Query{Statements: []Statement{&UpsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: INFO,
}}},
},
{
sql: `UPSERT ["one","two","tre"] INTO person RETURN BOTH`,
res: &Query{Statements: []Statement{&UpsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: BOTH,
}}},
},
{
sql: `UPSERT ["one","two","tre"] INTO person RETURN DIFF`,
res: &Query{Statements: []Statement{&UpsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: DIFF,
}}},
},
{
sql: `UPSERT ["one","two","tre"] INTO person RETURN BEFORE`,
res: &Query{Statements: []Statement{&UpsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: BEFORE,
}}},
},
{
sql: `UPSERT ["one","two","tre"] INTO person RETURN AFTER`,
str: `UPSERT ["one","two","tre"] INTO person`,
res: &Query{Statements: []Statement{&UpsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: AFTER,
}}},
},
{
sql: `UPSERT ["one","two","tre"] INTO person TIMEOUT 1s`,
res: &Query{Statements: []Statement{&UpsertStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Data: []interface{}{"one", "two", "tre"},
Into: &Table{"person"},
Echo: AFTER,
Timeout: 1 * time.Second,
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
What: Exprs{&Ident{"identifier"}},
}}},
2017-11-16 20:53:13 +00:00
},
{
sql: `KILL "identifier"`,
res: &Query{Statements: []Statement{&KillStatement{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*", 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{
KV: "*", NS: "*", DB: "*", 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`,
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"`,
str: `DEFINE LOGIN test ON NAMESPACE PASSHASH "123456"`,
2017-11-16 20:53:13 +00:00
res: &Query{Statements: []Statement{&DefineLoginStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Kind: NAMESPACE,
User: &Ident{"test"},
Pass: []byte("123456"),
}}},
},
{
sql: `DEFINE LOGIN test ON DATABASE PASSWORD "123456"`,
str: `DEFINE LOGIN test ON DATABASE PASSHASH "123456"`,
2017-11-16 20:53:13 +00:00
res: &Query{Statements: []Statement{&DefineLoginStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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"`,
str: `DEFINE TOKEN test ON NAMESPACE TYPE HS512 VALUE "secret"`,
2017-11-16 20:53:13 +00:00
res: &Query{Statements: []Statement{&DefineTokenStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Kind: NAMESPACE,
Name: &Ident{"test"},
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"`,
str: `DEFINE TOKEN test ON DATABASE TYPE HS512 VALUE "secret"`,
2017-11-16 20:53:13 +00:00
res: &Query{Statements: []Statement{&DefineTokenStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Kind: DATABASE,
Name: &Ident{"test"},
What: &Ident{""},
2017-11-16 20:53:13 +00:00
Type: "HS512",
Code: []byte("secret"),
}}},
},
{
sql: `DEFINE TOKEN test ON SCOPE test TYPE HS512 VALUE "secret"`,
str: `DEFINE TOKEN test ON SCOPE test TYPE HS512 VALUE "secret"`,
2017-11-16 20:53:13 +00:00
res: &Query{Statements: []Statement{&DefineTokenStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Kind: SCOPE,
Name: &Ident{"test"},
What: &Ident{"test"},
2017-11-16 20:53:13 +00:00
Type: "HS512",
Code: []byte("secret"),
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Name: &Ident{"test"},
Time: 1 * time.Hour,
}}},
},
{
sql: `DEFINE SCOPE test SESSION 1d`,
str: `DEFINE SCOPE test SESSION 24h0m0s`,
res: &Query{Statements: []Statement{&DefineScopeStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Name: &Ident{"test"},
Time: 24 * time.Hour,
}}},
},
{
sql: `DEFINE SCOPE test SESSION 1w`,
str: `DEFINE SCOPE test SESSION 168h0m0s`,
res: &Query{Statements: []Statement{&DefineScopeStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Name: &Ident{"test"},
Signup: &SubExpression{
Expr: &CreateStatement{
KV: "*", NS: "*", DB: "*",
What: Exprs{&Ident{"person"}},
2017-11-16 20:53:13 +00:00
Echo: AFTER,
},
},
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Name: &Ident{"test"},
Signin: &SubExpression{
Expr: &SelectStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Ident{"person"}},
},
},
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Name: &Ident{"test"},
Connect: &SubExpression{
Expr: &SelectStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Expr: []*Field{{Expr: &All{}, Field: "*"}},
What: []Expr{&Param{"id"}},
},
},
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: Tables{&Table{"person"}},
Drop: true,
}}},
},
{
sql: `DEFINE TABLE person SCHEMALESS`,
str: `DEFINE TABLE person`,
res: &Query{Statements: []Statement{&DefineTableStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: Tables{&Table{"person"}},
Full: false,
}}},
},
{
sql: `DEFINE TABLE person SCHEMAFULL`,
res: &Query{Statements: []Statement{&DefineTableStatement{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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,
},
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
What: Tables{&Table{"person"}},
Lock: true,
Expr: Fields{
&Field{
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{
LHS: &Ident{"public"},
2017-11-16 20:53:13 +00:00
Op: EQ,
RHS: true,
},
Group: Groups{
&Group{
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-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`,
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{
KV: "*", NS: "*", DB: "*",
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"},
},
Then: &MultExpression{
Expr: []Expr{
&UpdateStatement{
KV: "*", NS: "*", DB: "*",
What: Exprs{&Param{"this"}},
Data: &DataExpression{[]*ItemExpression{
{
LHS: &Ident{"increased"},
Op: EQ,
RHS: true,
},
}},
Echo: AFTER,
},
},
},
}}},
},
{
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{
KV: "*", NS: "*", DB: "*",
Name: &Ident{"temp"},
What: Tables{&Table{"person"}},
When: &BinaryExpression{
LHS: &Param{"before.price"},
Op: LT,
RHS: &Param{"after.price"},
},
Then: &MultExpression{
Expr: []Expr{
&UpdateStatement{
KV: "*", NS: "*", DB: "*",
What: Exprs{&Param{"this"}},
Data: &DataExpression{[]*ItemExpression{
{
LHS: &Ident{"increased"},
Op: EQ,
RHS: true,
},
}},
Echo: AFTER,
},
&UpdateStatement{
KV: "*", NS: "*", DB: "*",
What: Exprs{&Param{"this"}},
Data: &DataExpression{[]*ItemExpression{
{
LHS: &Ident{"finished"},
Op: EQ,
RHS: true,
},
}},
Echo: AFTER,
},
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{
KV: "*", NS: "*", DB: "*",
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`,
2018-10-17 23:39:09 +00:00
err: "Found `` but expected `array, boolean, circle, datetime, number, object, point, polygon, record, string, uuid`",
2017-11-16 20:53:13 +00:00
},
{
sql: `DEFINE FIELD temp ON person TYPE something`,
2018-10-17 23:39:09 +00:00
err: "Found `something` but expected `array, boolean, circle, datetime, number, object, point, polygon, record, string, uuid`",
2016-05-23 12:32:02 +00:00
},
{
sql: `DEFINE FIELD temp ON person TYPE array`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Name: &Ident{"temp"},
What: Tables{&Table{"person"}},
Assert: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &Param{"value"},
Op: GT,
RHS: 0.0,
},
Op: AND,
2017-11-16 20:53:13 +00:00
RHS: &BinaryExpression{
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*", 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{
KV: "*", NS: "*", DB: "*", 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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Kind: NAMESPACE,
Name: &Ident{"test"},
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Kind: DATABASE,
Name: &Ident{"test"},
What: &Ident{""},
2016-02-26 17:27:07 +00:00
}}},
},
{
2017-11-16 20:53:13 +00:00
sql: `REMOVE TOKEN test ON SCOPE`,
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
Kind: SCOPE,
Name: &Ident{"test"},
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{
KV: "*", NS: "*", DB: "*", 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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
2017-11-16 20:53:13 +00:00
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`,
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{
KV: "*", NS: "*", DB: "*",
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{
KV: "*", NS: "*", DB: "*",
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)
}
}