surrealpatch/sql/sql_test.go

2400 lines
62 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 (
"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
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
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
}
s, e := Parse(c, test.sql, nil)
2016-05-23 12:32:02 +00:00
2016-02-26 17:27:07 +00:00
Convey(test.sql, t, func() {
if test.err == "" {
So(e, ShouldBeNil)
So(s, ShouldResemble, test.res)
}
if test.err != "" {
Convey(testerr(e), func() {
So(testerr(e), ShouldResemble, test.err)
})
}
})
}
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{}
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-02-23 15:09:02 +00:00
err: "Found `` but expected `NAMESPACE, NS, DATABASE, 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-04-14 12:24:53 +00:00
err: "Found `` but expected `name`",
},
{
sql: `USE NAMESPACE ''`,
err: "Found `` but expected `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-04-14 12:24:53 +00:00
err: "Found `1` but expected `name`",
2016-05-23 12:32:02 +00:00
},
{
sql: `USE NAMESPACE 1.3000`,
2017-04-14 12:24:53 +00:00
err: "Found `1.3000` but expected `name`",
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-04-14 12:24:53 +00:00
err: "Found `{\"some\":\"thing\"}` but expected `name`",
2016-05-23 12:32:02 +00:00
},
{
sql: `USE NAMESPACE name something`,
err: "Found `something` but expected `EOF, ;`",
},
{
2017-04-14 12:24:53 +00:00
sql: `USE DATABASE`,
err: "Found `` but expected `name`",
2016-05-23 12:32:02 +00:00
},
{
2017-04-14 12:24:53 +00:00
sql: `USE DATABASE ''`,
err: "Found `` but expected `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-04-14 12:24:53 +00:00
err: "Found `1` but expected `name`",
2016-05-23 12:32:02 +00:00
},
{
sql: `USE DATABASE 1.3000`,
2017-04-14 12:24:53 +00:00
err: "Found `1.3000` but expected `name`",
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",
}}},
},
{
sql: `USE DATABASE {"some":"thing"}`,
2017-04-14 12:24:53 +00:00
err: "Found `{\"some\":\"thing\"}` but expected `name`",
2016-05-23 12:32:02 +00:00
},
{
sql: `USE DATABASE name something`,
err: "Found `something` but expected `EOF, ;`",
},
{
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`
q, err := Parse(c, s, nil)
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`
q, err := Parse(c, s, nil)
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`
q, err := Parse(c, s, nil)
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: `!`,
err: "Found `!` but expected `USE, INFO, BEGIN, CANCEL, COMMIT, LET, RETURN, SELECT, CREATE, UPDATE, DELETE, RELATE, DEFINE, REMOVE`",
},
{
sql: `SELECT * FROM person;;;`,
err: "Found `;` but expected `USE, INFO, BEGIN, CANCEL, COMMIT, LET, RETURN, SELECT, CREATE, UPDATE, DELETE, RELATE, DEFINE, REMOVE`",
},
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{
2016-11-04 12:15:51 +00:00
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{
2016-11-04 12:15:51 +00:00
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{
2016-11-04 12:15:51 +00:00
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"name"},
2016-11-04 11:13:34 +00:00
What: int64(1),
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `LET name = 1.0`,
res: &Query{Statements: []Statement{&LetStatement{
2016-11-04 12:15:51 +00:00
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{
2016-11-04 12:15:51 +00:00
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
}}},
},
{
sql: `LET name = @thing:test`,
res: &Query{Statements: []Statement{&LetStatement{
2016-11-04 12:15:51 +00:00
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"}`,
res: &Query{Statements: []Statement{&LetStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"name"},
What: Object{"key": "val"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `LET name = ["key", "val"]`,
res: &Query{Statements: []Statement{&LetStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"name"},
What: Array{"key", "val"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `LET name = $test`,
res: &Query{Statements: []Statement{&LetStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"name"},
2016-11-04 11:13:34 +00:00
What: &Param{ID: "test"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `LET name = {"key"::"val"}`,
err: "Invalid JSON: {\"key\"::\"val\"}",
},
{
sql: `LET name = "test" something`,
err: "Found `something` but expected `EOF, ;`",
},
}
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: true,
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN true`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: true,
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN false`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: false,
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN "test"`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
What: &Value{"test"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN 1`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: int64(1),
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN 1.0`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: float64(1),
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN 1.1`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: float64(1.1),
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN @thing:test`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: &Thing{TB: "thing", ID: "test"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN {"key": "val"}`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
What: Object{"key": "val"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN ["key", "val"]`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
What: Array{"key", "val"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN $test`,
res: &Query{Statements: []Statement{&ReturnStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: &Param{ID: "test"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `RETURN {"key"::"val"}`,
err: "Invalid JSON: {\"key\"::\"val\"}",
},
{
sql: `RETURN $test something`,
err: "Found `something` but expected `EOF, ;`",
},
}
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-04-14 12:24:53 +00:00
err: "Found `` but expected `table, or thing`",
2016-02-26 17:27:07 +00:00
},
{
sql: `SELECT * FROM per!son`,
2016-11-04 11:13:34 +00:00
err: "Found `!` but expected `EOF, ), ;`",
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-02-26 17:27:07 +00:00
}}},
},
{
sql: `SELECT * FROM @`,
2017-04-14 12:24:53 +00:00
err: "Found `@` but expected `table, or thing`",
2016-02-26 17:27:07 +00:00
},
{
sql: `SELECT * FROM @person`,
2017-04-14 12:24:53 +00:00
err: "Found `@person` but expected `table, or thing`",
2016-02-26 17:27:07 +00:00
},
{
sql: `SELECT * FROM @person:`,
2017-04-14 12:24:53 +00:00
err: "Found `@person:` but expected `table, or thing`",
2016-09-07 15:58:50 +00:00
},
{
sql: `SELECT * FROM @person WHERE`,
2017-04-14 12:24:53 +00:00
err: "Found `@person` but expected `table, or thing`",
2016-09-07 15:58:50 +00:00
},
{
sql: `SELECT * FROM person:uuid`,
2016-11-04 11:13:34 +00:00
err: "Found `:` but expected `EOF, ), ;`",
2016-02-26 17:27:07 +00:00
},
2016-09-14 21:22:18 +00:00
{
sql: "SELECT * FROM 111",
2017-04-14 12:24:53 +00:00
err: "Found `111` but expected `table, or thing`",
2016-09-14 21:22:18 +00:00
},
{
sql: "SELECT * FROM `111`",
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Table{"111"}},
}}},
},
{
sql: "SELECT * FROM `2006-01-02`",
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Table{"2006-01-02"}},
}}},
},
{
sql: "SELECT * FROM `2006-01-02T15:04:05+07:00`",
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Table{"2006-01-02T15:04:05+07:00"}},
}}},
},
{
sql: "SELECT * FROM `2006-01-02T15:04:05.999999999+07:00`",
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Table{"2006-01-02T15:04:05.999999999+07: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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}, &Table{"tweet"}},
2016-05-23 12:32:02 +00:00
}}},
},
2016-09-14 21:22:18 +00:00
{
sql: `SELECT * FROM @111:1a`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "111", ID: "1a"}},
}}},
},
2016-05-23 12:32:02 +00:00
{
sql: `SELECT * FROM @person:1a`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-05-24 12:51:52 +00:00
What: []Expr{&Thing{TB: "person", ID: "1a"}},
2016-02-26 17:27:07 +00:00
}}},
},
2016-09-14 21:22:18 +00:00
{
sql: `SELECT * FROM @person:⟨1a⟩`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: "1a"}},
}}},
},
{
sql: `SELECT * FROM @person:{1a}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: "1a"}},
}}},
},
2016-02-26 17:27:07 +00:00
{
sql: `SELECT * FROM @person:123456`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
What: []Expr{&Thing{TB: "person", ID: int64(123456)}},
2016-02-26 17:27:07 +00:00
}}},
},
2016-09-14 21:22:18 +00:00
{
sql: `SELECT * FROM @person:⟨123456⟩`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
What: []Expr{&Thing{TB: "person", ID: int64(123456)}},
2016-09-14 21:22:18 +00:00
}}},
},
{
sql: `SELECT * FROM @person:{123456}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
What: []Expr{&Thing{TB: "person", ID: int64(123456)}},
2016-09-14 21:22:18 +00:00
}}},
},
2016-02-26 17:27:07 +00:00
{
sql: `SELECT * FROM @person:123.456`,
2016-05-24 12:51:52 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-05-24 12:51:52 +00:00
What: []Expr{&Thing{TB: "person", ID: float64(123.456)}},
}}},
2016-02-26 17:27:07 +00:00
},
2016-09-14 21:22:18 +00:00
{
sql: `SELECT * FROM @person:⟨123.456⟩`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: float64(123.456)}},
}}},
},
{
sql: `SELECT * FROM @person:{123.456}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
{
sql: `SELECT * FROM @person:123.456.789.012`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
}}},
},
{
sql: `SELECT * FROM @person:⟨123.456.789.012⟩`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
{
sql: `SELECT * FROM @person:{123.456.789.012}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
{
sql: `SELECT * FROM @person:⟨1987-06-22⟩`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: date}},
}}},
},
{
sql: `SELECT * FROM @person:{1987-06-22}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: date}},
}}},
},
{
sql: `SELECT * FROM @person:⟨1987-06-22T08:30:30.511Z⟩`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: nano}},
}}},
},
{
sql: `SELECT * FROM @person:{1987-06-22T08:30:30.511Z}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-14 21:22:18 +00:00
What: []Expr{&Thing{TB: "person", ID: nano}},
}}},
},
2016-05-23 12:32:02 +00:00
{
2016-09-06 13:30:59 +00:00
sql: `SELECT * FROM @person:⟨A250C5A3-948F-4657-88AD-FF5F27B5B24E⟩`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Thing{TB: "person", ID: "A250C5A3-948F-4657-88AD-FF5F27B5B24E"}},
2016-05-23 12:32:02 +00:00
}}},
},
2016-09-07 15:58:50 +00:00
{
sql: `SELECT * FROM @person:{A250C5A3-948F-4657-88AD-FF5F27B5B24E}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
{
2016-09-06 13:30:59 +00:00
sql: `SELECT * FROM @person:⟨8250C5A3-948F-4657-88AD-FF5F27B5B24E⟩`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Thing{TB: "person", ID: "8250C5A3-948F-4657-88AD-FF5F27B5B24E"}},
2016-05-23 12:32:02 +00:00
}}},
},
2016-09-07 15:58:50 +00:00
{
sql: `SELECT * FROM @person:{8250C5A3-948F-4657-88AD-FF5F27B5B24E}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
{
2016-09-06 13:30:59 +00:00
sql: `SELECT * FROM @person:⟨Tobie Morgan Hitchcock⟩`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Thing{TB: "person", ID: "Tobie Morgan Hitchcock"}},
2016-02-26 17:27:07 +00:00
}}},
},
2016-09-07 15:58:50 +00:00
{
sql: `SELECT * FROM @person:{Tobie Morgan Hitchcock}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
{
2016-09-06 13:30:59 +00:00
sql: `SELECT * FROM @⟨email addresses⟩:⟨tobie@abcum.com⟩`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Thing{TB: "email addresses", ID: "tobie@abcum.com"}},
2016-02-26 17:27:07 +00:00
}}},
},
2016-09-07 15:58:50 +00:00
{
sql: `SELECT * FROM @{email addresses}:{tobie@abcum.com}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
{
2016-09-06 13:30:59 +00:00
sql: `SELECT * FROM @⟨email addresses⟩:⟨tobie+spam@abcum.com⟩`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
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
{
sql: `SELECT * FROM @{email addresses}:{tobie+spam@abcum.com}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-07 15:58:50 +00:00
What: []Expr{&Thing{TB: "email addresses", ID: "tobie+spam@abcum.com"}},
}}},
},
2016-09-14 21:22:18 +00:00
{
sql: `SELECT * FROM @{person}test:id`,
2017-04-14 12:24:53 +00:00
err: "Found `@person` but expected `table, or thing`",
2016-09-14 21:22:18 +00:00
},
2016-05-23 12:32:02 +00:00
{
2016-09-06 13:30:59 +00:00
sql: `SELECT * FROM @⟨email addresses⟩:⟨this\qis\nodd⟩`,
2017-04-14 12:24:53 +00:00
err: "Found `@email addresses:thisqis\nodd` but expected `table, or thing`",
2016-09-07 15:58:50 +00:00
},
{
sql: `SELECT * FROM @{email addresses}:{this\qis\nodd}`,
2017-04-14 12:24:53 +00:00
err: "Found `@email addresses:thisqis\nodd` but expected `table, or thing`",
2016-09-06 13:30:59 +00:00
},
{
sql: `SELECT *, temp AS test FROM person`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
Expr: []*Field{
2017-04-14 12:24:53 +00:00
{Expr: &All{}},
{Expr: &Ident{"temp"}, Alias: &Ident{"test"}},
2016-09-06 13:30:59 +00:00
},
What: []Expr{&Table{"person"}},
2016-02-26 17:27:07 +00:00
}}},
},
{
2016-09-06 13:30:59 +00:00
sql: "SELECT `email addresses` AS emails FROM person",
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
Expr: []*Field{
2017-04-14 12:24:53 +00:00
{Expr: &Ident{"email addresses"}, Alias: &Ident{"emails"}},
2016-09-06 13:30:59 +00:00
},
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
}}},
},
{
2016-09-06 13:30:59 +00:00
sql: "SELECT emails AS `email addresses` FROM person",
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
Expr: []*Field{
2017-04-14 12:24:53 +00:00
{Expr: &Ident{"emails"}, Alias: &Ident{"email addresses"}},
2016-09-06 13:30:59 +00:00
},
What: []Expr{&Table{"person"}},
2016-02-26 17:27:07 +00:00
}}},
},
{
2016-09-06 13:30:59 +00:00
sql: "SELECT * FROM person WHERE id = '\x0A'",
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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
}}},
},
{
2016-09-06 13:30:59 +00:00
sql: "SELECT * FROM person WHERE id = '\x0D'",
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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
}}},
},
{
2016-09-06 13:30:59 +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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"id"}, Op: EQ, RHS: &Value{"\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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-11-04 11:13:34 +00:00
What: []Expr{&Table{"person"}},
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-11-04 11:13:34 +00:00
Cond: &BinaryExpression{LHS: &Ident{"id"}, Op: EQ, RHS: int64(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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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
}}},
},
{
2016-09-06 13:30:59 +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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-11-04 11:13:34 +00:00
Cond: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &Ident{ID: "id"},
Op: NEQ,
RHS: &Null{},
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{ID: "id"},
Op: GT,
RHS: float64(13.9),
},
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{ID: "id"},
Op: LT,
RHS: int64(31),
},
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{ID: "id"},
Op: GTE,
RHS: int64(15),
},
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{ID: "id"},
Op: LTE,
RHS: float64(29.9),
},
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"]`,
err: `Invalid JSON: ["London":"Paris"]`,
},
{
sql: `SELECT * FROM person WHERE test IN ["London","Paris"]`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: INS, RHS: Array{"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"]`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-10-29 17:47:09 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: INS, RHS: Array{"London", "Paris"}},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `SELECT * FROM person WHERE test IS NOT IN ["London","Paris"]`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-10-29 17:47:09 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: NIS, RHS: Array{"London", "Paris"}},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `SELECT * FROM person WHERE ["London","Paris"] CONTAINS test`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-10-29 17:47:09 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: Array{"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`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-10-29 17:47:09 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: Array{"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
},
{
sql: `SELECT * FROM person WHERE test = {"name","London"}`,
err: `Invalid JSON: {"name","London"}`,
},
2016-10-29 17:47:09 +00:00
{
sql: "SELECT * FROM person WHERE {\"name\":\"\x0A\"} = test",
2017-02-23 15:09:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2017-02-23 15:09:02 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: Object{"name": "\n"}, Op: EQ, RHS: &Ident{"test"}},
2017-02-23 15:09:02 +00:00
}}},
2016-10-29 17:47:09 +00:00
},
2016-05-23 12:32:02 +00:00
{
sql: "SELECT * FROM person WHERE test = {\"name\":\"\x0A\"}",
2017-02-23 15:09:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2017-02-23 15:09:02 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: EQ, RHS: Object{"name": "\n"}},
2017-02-23 15:09:02 +00:00
}}},
2016-05-23 12:32:02 +00:00
},
{
sql: "SELECT * FROM person WHERE test = {\"name\":\"\x0D\"}",
2017-02-23 15:09:02 +00:00
res: &Query{Statements: []Statement{&SelectStatement{
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2017-02-23 15:09:02 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: EQ, RHS: Object{"name": "\r"}},
2017-02-23 15:09:02 +00:00
}}},
2016-05-23 12:32:02 +00:00
},
{
sql: `SELECT * FROM person WHERE test = {"name":"London"}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: EQ, RHS: Object{"name": "London"}},
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `SELECT * FROM person WHERE test = {"name":"\b\t\r\n\f\"\\"}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: EQ, RHS: Object{"name": "\b\t\r\n\f\"\\"}},
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `SELECT * FROM person WHERE test = {"name":{"f":"first", "l":"last"}}`,
res: &Query{Statements: []Statement{&SelectStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: EQ, RHS: Object{"name": map[string]interface{}{"f": "first", "l": "last"}}},
2016-05-23 12:32:02 +00:00
}}},
2016-02-26 17:27:07 +00:00
},
}
2016-05-23 12:32:02 +00:00
bday1, _ := time.Parse("2006-01-02", "1987-06-22")
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Expr: []*Field{{Expr: &All{}}},
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-11-04 11:13:34 +00:00
Cond: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &BinaryExpression{
LHS: &Ident{ID: "bday"},
Op: GTE,
RHS: bday1,
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{ID: "bday"},
Op: GTE,
RHS: bday2,
},
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{ID: "bday"},
Op: GTE,
RHS: bday3,
},
},
Op: AND,
RHS: &BinaryExpression{
LHS: &Ident{ID: "bday"},
Op: LTE,
RHS: bday4,
},
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-04-14 12:24:53 +00:00
err: "Found `` but expected `table, or thing`",
2016-02-26 17:27:07 +00:00
},
{
sql: `CREATE person`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-05-23 12:32:02 +00:00
What: []Expr{
2016-09-06 13:30:59 +00:00
&Table{"person"},
2016-05-23 12:32:02 +00:00
},
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: []Expr{&Table{"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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-11-04 11:13:34 +00:00
err: "Found `something` but expected `EOF, ), ;`",
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-11-04 11:13:34 +00:00
err: "Found `something` but expected `EOF, ), ;`",
2016-05-23 12:32:02 +00:00
},
{
sql: `CREATE person MERGE {"firstname":"Tobie"}`,
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Data: &MergeExpression{Data: Object{"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`,
2016-11-04 11:13:34 +00:00
err: "Found `something` but expected `EOF, ), ;`",
2016-05-23 12:32:02 +00:00
},
{
sql: `CREATE person CONTENT {"firstname":"Tobie"}`,
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Data: &ContentExpression{Data: Object{"firstname": "Tobie"}},
2017-02-23 15:09:02 +00:00
Echo: AFTER,
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `CREATE person RETURN ID`,
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: ID,
}}},
},
{
sql: `CREATE person RETURN NONE`,
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: NONE,
}}},
},
{
sql: `CREATE person RETURN BOTH`,
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BOTH,
}}},
},
{
sql: `CREATE person RETURN DIFF`,
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: DIFF,
}}},
},
{
sql: `CREATE person RETURN BEFORE`,
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BEFORE,
}}},
},
{
sql: `CREATE person RETURN AFTER`,
res: &Query{Statements: []Statement{&CreateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: AFTER,
}}},
},
{
sql: `CREATE person RETURN SOMETHING`,
2016-10-14 07:01:56 +00:00
err: "Found `SOMETHING` but expected `ID, NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
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-04-14 12:24:53 +00:00
err: "Found `` but expected `table, or thing`",
2016-02-26 17:27:07 +00:00
},
{
2016-05-23 12:32:02 +00:00
sql: `UPDATE person`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-05-23 12:32:02 +00:00
What: []Expr{
2016-09-06 13:30:59 +00:00
&Table{"person"},
2016-05-23 12:32:02 +00:00
},
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-11-04 11:13:34 +00:00
What: []Expr{&Table{"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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-11-04 11:13:34 +00:00
err: "Found `something` but expected `EOF, ), ;`",
2016-05-23 12:32:02 +00:00
},
{
sql: `UPDATE person SET firstname = "Tobie"`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"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`,
2016-11-04 11:13:34 +00:00
err: "Found `something` but expected `EOF, ), ;`",
},
{
2017-02-23 15:09:02 +00:00
sql: `UPDATE person DIFF []`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Data: &DiffExpression{Data: Array{}},
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`,
2016-11-04 11:13:34 +00:00
err: "Found `something` but expected `EOF, ), ;`",
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{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Data: &MergeExpression{Data: Object{"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`,
2016-11-04 11:13:34 +00:00
err: "Found `something` but expected `EOF, ), ;`",
2016-05-23 12:32:02 +00:00
},
{
sql: `UPDATE person CONTENT {"firstname":"Tobie"}`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2017-04-14 12:24:53 +00:00
Data: &ContentExpression{Data: Object{"firstname": "Tobie"}},
2017-02-23 15:09:02 +00:00
Echo: AFTER,
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `UPDATE person RETURN ID`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: ID,
}}},
},
{
sql: `UPDATE person RETURN NONE`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: NONE,
}}},
},
{
sql: `UPDATE person RETURN BOTH`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BOTH,
}}},
},
{
sql: `UPDATE person RETURN DIFF`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: DIFF,
}}},
},
{
sql: `UPDATE person RETURN BEFORE`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BEFORE,
}}},
},
{
sql: `UPDATE person RETURN AFTER`,
res: &Query{Statements: []Statement{&UpdateStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: AFTER,
}}},
},
{
sql: `UPDATE person RETURN SOMETHING`,
2016-10-14 07:01:56 +00:00
err: "Found `SOMETHING` but expected `ID, NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
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-04-14 12:24:53 +00:00
err: "Found `` but expected `table, or thing`",
2016-05-23 12:32:02 +00:00
},
{
sql: `DELETE FROM`,
2017-04-14 12:24:53 +00:00
err: "Found `` but expected `table, or thing`",
2016-05-23 12:32:02 +00:00
},
{
sql: `DELETE person`,
res: &Query{Statements: []Statement{&DeleteStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-05-23 12:32:02 +00:00
What: []Expr{
2016-09-06 13:30:59 +00:00
&Table{"person"},
2016-05-23 12:32:02 +00:00
},
2017-02-23 15:09:02 +00:00
Echo: NONE,
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DELETE person RETURN ID`,
res: &Query{Statements: []Statement{&DeleteStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: ID,
}}},
},
{
sql: `DELETE person RETURN NONE`,
res: &Query{Statements: []Statement{&DeleteStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: NONE,
}}},
},
{
sql: `DELETE person RETURN BOTH`,
res: &Query{Statements: []Statement{&DeleteStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BOTH,
}}},
},
{
sql: `DELETE person RETURN DIFF`,
res: &Query{Statements: []Statement{&DeleteStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: DIFF,
}}},
},
{
sql: `DELETE person RETURN BEFORE`,
res: &Query{Statements: []Statement{&DeleteStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: BEFORE,
}}},
},
{
sql: `DELETE person RETURN AFTER`,
res: &Query{Statements: []Statement{&DeleteStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2016-09-06 13:30:59 +00:00
What: []Expr{&Table{"person"}},
2016-05-23 12:32:02 +00:00
Echo: AFTER,
}}},
},
{
sql: `DELETE person RETURN SOMETHING`,
2016-10-14 07:01:56 +00:00
err: "Found `SOMETHING` but expected `ID, NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
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{}
for _, test := range tests {
testsql(t, test)
}
}
func Test_Parse_Queries_Record(t *testing.T) {
var tests = []tester{}
for _, test := range tests {
testsql(t, test)
}
}
func Test_Parse_Queries_Define(t *testing.T) {
var tests = []tester{
{
sql: `DEFINE`,
2017-02-23 15:09:02 +00:00
err: "Found `` but expected `NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, FIELD, INDEX, VIEW`",
2016-02-26 17:27:07 +00:00
},
2016-09-06 13:30:59 +00:00
// ----------------------------------------------------------------------
2016-02-26 17:27:07 +00:00
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE TABLE`,
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
{
sql: `DEFINE 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: `DEFINE 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-05-23 12:32:02 +00:00
{
sql: `DEFINE TABLE person`,
res: &Query{Statements: []Statement{&DefineTableStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
What: Tables{&Table{"person"}},
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE TABLE person something`,
err: "Found `something` but expected `EOF, ;`",
},
{
sql: `DEFINE TABLE person SCHEMALESS`,
res: &Query{Statements: []Statement{&DefineTableStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
What: Tables{&Table{"person"}},
Full: false,
}}},
},
{
sql: `DEFINE TABLE person SCHEMAFULL`,
res: &Query{Statements: []Statement{&DefineTableStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
What: Tables{&Table{"person"}},
Full: true,
}}},
},
{
sql: `DEFINE TABLE person SCHEMALESS something`,
err: "Found `something` but expected `EOF, ;`",
},
{
sql: `DEFINE TABLE person SCHEMAFULL something`,
err: "Found `something` but expected `EOF, ;`",
},
2016-05-23 12:32:02 +00:00
// ----------------------------------------------------------------------
{
sql: `DEFINE FIELD`,
2016-02-27 12:02:41 +00:00
err: "Found `` but expected `name`",
2016-02-26 17:27:07 +00:00
},
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE FIELD temp`,
err: "Found `` but expected `ON`",
2016-02-26 17:27:07 +00:00
},
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE FIELD temp ON`,
2017-04-14 12:24:53 +00:00
err: "Found `` but expected `table`",
2016-02-26 17:27:07 +00:00
},
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE FIELD temp ON person`,
2017-02-23 15:09: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-02-23 15:09:02 +00:00
}}},
2016-02-26 17:27:07 +00:00
},
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE FIELD temp ON person TYPE`,
2017-02-23 15:09:02 +00:00
err: "Found `` but expected `any, url, uuid, color, email, phone, array, object, domain, record, string, number, double, custom, boolean, password, datetime, latitude, longitude`",
2016-09-06 13:30:59 +00:00
},
{
sql: `DEFINE FIELD temp ON person TYPE something`,
2017-02-23 15:09:02 +00:00
err: "Found `something` but expected `any, url, uuid, color, email, phone, array, object, domain, record, string, number, double, custom, boolean, password, datetime, latitude, longitude`",
2016-02-26 17:27:07 +00:00
},
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE FIELD temp ON person TYPE any`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-02-26 17:27:07 +00:00
}}},
},
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE FIELD temp ON person TYPE url`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "url",
2016-02-26 17:27:07 +00:00
}}},
},
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE FIELD temp ON person TYPE email`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "email",
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE phone`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "phone",
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE array`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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{
2016-11-04 12:15:51 +00:00
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{
2016-11-04 12:15:51 +00:00
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{
2016-11-04 12:15:51 +00:00
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
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE double`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"temp"},
What: Tables{&Table{"person"}},
Type: "double",
}}},
},
2016-05-23 12:32:02 +00:00
{
2016-07-04 10:37:37 +00:00
sql: `DEFINE FIELD temp ON person TYPE custom`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "custom",
2016-07-04 10:37:37 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE custom ENUM ["default","notdefault"]`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "custom",
2017-04-14 12:24:53 +00:00
Enum: Array{"default", "notdefault"},
2016-05-23 12:32:02 +00:00
}}},
},
2016-09-06 13:30:59 +00:00
{
sql: `DEFINE FIELD temp ON person TYPE custom ENUM ["default" "notdefault"]`,
2017-02-23 15:09: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-02-23 15:09:02 +00:00
Type: "custom",
2017-04-14 12:24:53 +00:00
Enum: Array{"default", "notdefault"},
2017-02-23 15:09:02 +00:00
}}},
2016-09-06 13:30:59 +00:00
},
2016-05-23 12:32:02 +00:00
{
sql: `DEFINE FIELD temp ON person TYPE any DEFAULT true`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-07-04 10:37:37 +00:00
Default: true,
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any DEFAULT false`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-07-04 10:37:37 +00:00
Default: false,
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any DEFAULT 100`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
Default: int64(100),
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any DEFAULT "default"`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2017-04-14 12:24:53 +00:00
Default: &Value{"default"},
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any DEFAULT "this\nis\nsome\ntext"`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2017-04-14 12:24:53 +00:00
Default: &Value{"this\nis\nsome\ntext"},
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any DEFAULT {"default":true}`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2017-04-14 12:24:53 +00:00
Default: Object{"default": true},
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any DEFAULT something`,
2016-09-06 13:30:59 +00:00
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
Default: &Ident{"something"},
}}},
2016-05-23 12:32:02 +00:00
},
{
sql: `DEFINE FIELD temp ON person TYPE any MIN`,
err: "Found `` but expected `number`",
},
{
sql: `DEFINE FIELD temp ON person TYPE any MIN 1`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-07-04 10:37:37 +00:00
Min: 1,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MIN 1.0`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-07-04 10:37:37 +00:00
Min: 1,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MIN 1.0001`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-07-04 10:37:37 +00:00
Min: 1.0001,
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MIN something`,
err: "Found `something` but expected `number`",
},
{
sql: `DEFINE FIELD temp ON person TYPE any MAX`,
err: "Found `` but expected `number`",
},
{
sql: `DEFINE FIELD temp ON person TYPE any MAX 100`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-07-04 10:37:37 +00:00
Max: 100,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MAX 100.0`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-07-04 10:37:37 +00:00
Max: 100,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MAX 100.0001`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-07-04 10:37:37 +00:00
Max: 100.0001,
2016-05-23 12:32:02 +00:00
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MAX something`,
err: "Found `something` but expected `number`",
},
2016-09-06 13:30:59 +00:00
{
sql: `DEFINE FIELD temp ON person TYPE any CODE`,
2017-04-14 12:24:53 +00:00
err: "Found `` but expected `script`",
2016-09-06 13:30:59 +00:00
},
{
sql: `DEFINE FIELD temp ON person TYPE any CODE "return doc.data.id"`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
Code: "return doc.data.id",
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any CODE something`,
2017-04-14 12:24:53 +00:00
err: "Found `something` but expected `script`",
2016-09-06 13:30:59 +00:00
},
{
sql: `DEFINE FIELD temp ON person TYPE any MATCH`,
err: "Found `` but expected `regular expression`",
},
{
sql: `DEFINE FIELD temp ON person TYPE any MATCH /.*/`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
Match: ".*",
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MATCH something`,
err: "Found `something` but expected `regular expression`",
},
2016-05-23 12:32:02 +00:00
{
sql: `DEFINE FIELD temp ON person TYPE any NOTNULL`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Notnull: true,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any NOTNULL true`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Notnull: true,
2016-02-26 17:27:07 +00:00
}}},
},
{
2016-05-23 12:32:02 +00:00
sql: `DEFINE FIELD temp ON person TYPE any NOTNULL false`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Notnull: false,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any READONLY`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Readonly: true,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any READONLY true`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Readonly: true,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any READONLY false`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Readonly: false,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MANDATORY`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Mandatory: true,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MANDATORY true`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Mandatory: true,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any MANDATORY false`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
2016-05-23 12:32:02 +00:00
Mandatory: false,
}}},
},
2016-09-06 13:30:59 +00:00
{
sql: `DEFINE FIELD temp ON person TYPE any VALIDATE`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
Validate: true,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any VALIDATE true`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
Validate: true,
}}},
},
{
sql: `DEFINE FIELD temp ON person TYPE any VALIDATE false`,
res: &Query{Statements: []Statement{&DefineFieldStatement{
2016-11-04 12:15:51 +00:00
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: "any",
Validate: false,
}}},
},
2016-05-23 12:32:02 +00:00
{
sql: `DEFINE FIELD temp ON person TYPE any something`,
2016-02-27 12:02:41 +00:00
err: "Found `something` but expected `EOF, ;`",
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: `DEFINE INDEX`,
2016-02-27 12:02:41 +00:00
err: "Found `` but expected `name`",
2016-02-26 17:27:07 +00:00
},
{
sql: `DEFINE INDEX temp`,
2016-02-27 12:02:41 +00:00
err: "Found `` but expected `ON`",
2016-02-26 17:27:07 +00:00
},
{
sql: `DEFINE 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: `DEFINE INDEX temp ON person`,
2016-09-06 13:30:59 +00:00
err: "Found `` but expected `COLUMNS`",
2016-02-26 17:27:07 +00:00
},
{
sql: `DEFINE INDEX temp ON person COLUMNS`,
2017-04-14 12:24:53 +00:00
err: "Found `` but expected `name, or expression`",
2016-02-26 17:27:07 +00:00
},
{
sql: `DEFINE INDEX temp ON person COLUMNS firstname, lastname`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&DefineIndexStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"temp"},
What: Tables{&Table{"person"}},
Cols: Idents{&Ident{"firstname"}, &Ident{"lastname"}},
2016-05-23 12:32:02 +00:00
Uniq: false,
2016-02-26 17:27:07 +00:00
}}},
},
{
sql: `DEFINE INDEX temp ON person COLUMNS firstname, lastname UNIQUE`,
2016-05-23 12:32:02 +00:00
res: &Query{Statements: []Statement{&DefineIndexStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"temp"},
What: Tables{&Table{"person"}},
Cols: Idents{&Ident{"firstname"}, &Ident{"lastname"}},
2016-05-23 12:32:02 +00:00
Uniq: true,
2016-02-26 17:27:07 +00:00
}}},
},
{
sql: `DEFINE INDEX temp ON person COLUMNS firstname, lastname something UNIQUE`,
2016-02-27 12:02:41 +00:00
err: "Found `something` but expected `EOF, ;`",
2016-02-26 17:27:07 +00:00
},
{
sql: `DEFINE INDEX temp ON person COLUMNS firstname, lastname UNIQUE something`,
2016-02-27 12:02:41 +00:00
err: "Found `something` but expected `EOF, ;`",
2016-02-26 17:27:07 +00:00
},
2016-10-14 21:15:40 +00:00
// ----------------------------------------------------------------------
{
sql: `DEFINE VIEW`,
err: "Found `` but expected `name`",
},
{
sql: `DEFINE VIEW temp`,
err: "Found `` but expected `AS`",
},
{
sql: `DEFINE VIEW temp AS`,
err: "Found `` but expected `SELECT`",
},
{
sql: `DEFINE VIEW temp AS SELECT`,
2016-11-04 11:13:34 +00:00
err: "Found `` but expected `expression`",
2016-10-14 21:15:40 +00:00
},
{
sql: `DEFINE VIEW temp AS SELECT *`,
err: "Found `` but expected `FROM`",
},
{
sql: `DEFINE VIEW temp AS SELECT * FROM`,
2017-04-14 12:24:53 +00:00
err: "Found `` but expected `table`",
2016-10-14 21:15:40 +00:00
},
{
sql: `DEFINE VIEW temp AS SELECT * FROM person`,
res: &Query{Statements: []Statement{&DefineViewStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"temp"},
Expr: []*Field{{Expr: &All{}}},
What: Tables{&Table{"person"}},
2016-10-14 21:15:40 +00:00
}}},
},
{
sql: `DEFINE VIEW temp AS SELECT * FROM person something`,
err: "Found `something` but expected `EOF, ;`",
},
2016-02-26 17:27:07 +00:00
}
for _, test := range tests {
testsql(t, test)
}
}
func Test_Parse_Queries_Remove(t *testing.T) {
var tests = []tester{
{
sql: `REMOVE`,
2017-02-23 15:09:02 +00:00
err: "Found `` but expected `NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, FIELD, INDEX, VIEW`",
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{
2016-11-04 12:15:51 +00:00
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`,
err: "Found `something` but expected `EOF, ;`",
},
2016-09-06 13:30:59 +00:00
// ----------------------------------------------------------------------
2016-05-23 12:32:02 +00:00
{
sql: `REMOVE FIELD`,
2016-02-27 12:02:41 +00:00
err: "Found `` but expected `name`",
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{
2016-11-04 12:15:51 +00:00
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`,
2016-02-27 12:02:41 +00:00
err: "Found `something` but expected `EOF, ;`",
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{
2016-11-04 12:15:51 +00:00
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`,
2016-02-27 12:02:41 +00:00
err: "Found `something` but expected `EOF, ;`",
2016-02-26 17:27:07 +00:00
},
2016-10-29 17:47:09 +00:00
// ----------------------------------------------------------------------
{
sql: `REMOVE VIEW`,
err: "Found `` but expected `name`",
},
{
sql: `REMOVE VIEW temp`,
res: &Query{Statements: []Statement{&RemoveViewStatement{
2016-11-04 12:15:51 +00:00
KV: "*", NS: "*", DB: "*",
2017-04-14 12:24:53 +00:00
Name: &Ident{"temp"},
2016-10-29 17:47:09 +00:00
}}},
},
{
sql: `REMOVE VIEW temp something`,
err: "Found `something` but expected `EOF, ;`",
},
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`,
res: &Query{Statements: []Statement{&BeginStatement{}}},
},
{
sql: `BEGIN something`,
err: "Found `something` but expected `EOF, ;`",
},
{
sql: `BEGIN TRANSACTION`,
res: &Query{Statements: []Statement{&BeginStatement{}}},
},
{
sql: `BEGIN TRANSACTION something`,
err: "Found `something` but expected `EOF, ;`",
},
}
for _, test := range tests {
testsql(t, test)
}
}
func Test_Parse_Queries_Cancel(t *testing.T) {
var tests = []tester{
{
sql: `CANCEL`,
res: &Query{Statements: []Statement{&CancelStatement{}}},
},
{
sql: `CANCEL something`,
err: "Found `something` but expected `EOF, ;`",
},
{
sql: `CANCEL TRANSACTION`,
res: &Query{Statements: []Statement{&CancelStatement{}}},
},
{
sql: `CANCEL TRANSACTION something`,
err: "Found `something` but expected `EOF, ;`",
},
}
for _, test := range tests {
testsql(t, test)
}
}
func Test_Parse_Queries_Commit(t *testing.T) {
var tests = []tester{
{
sql: `Commit`,
res: &Query{Statements: []Statement{&CommitStatement{}}},
},
{
sql: `Commit something`,
err: "Found `something` but expected `EOF, ;`",
},
{
sql: `Commit TRANSACTION`,
res: &Query{Statements: []Statement{&CommitStatement{}}},
},
{
sql: `Commit TRANSACTION something`,
err: "Found `something` but expected `EOF, ;`",
},
}
for _, test := range tests {
testsql(t, test)
}
}