Remove RETURN BOTH and RETURN DIFF
This commit is contained in:
parent
3c0192c00c
commit
7de6f54547
4 changed files with 11 additions and 188 deletions
45
db/yield.go
45
db/yield.go
|
@ -19,7 +19,6 @@ import (
|
||||||
|
|
||||||
"github.com/abcum/surreal/sql"
|
"github.com/abcum/surreal/sql"
|
||||||
"github.com/abcum/surreal/util/data"
|
"github.com/abcum/surreal/util/data"
|
||||||
"github.com/abcum/surreal/util/diff"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *document) cold(ctx context.Context) (doc *data.Doc, err error) {
|
func (d *document) cold(ctx context.Context) (doc *data.Doc, err error) {
|
||||||
|
@ -62,19 +61,6 @@ func (d *document) cnow(ctx context.Context) (doc *data.Doc, err error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *document) diffs(initial, current *data.Doc) *data.Doc {
|
|
||||||
|
|
||||||
a, _ := initial.Data().(map[string]interface{})
|
|
||||||
b, _ := current.Data().(map[string]interface{})
|
|
||||||
|
|
||||||
if c := diff.Diff(a, b); len(c) > 0 {
|
|
||||||
return data.Consume(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
return data.Consume(nil)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *document) yield(ctx context.Context, stm sql.Statement, output sql.Token) (interface{}, error) {
|
func (d *document) yield(ctx context.Context, stm sql.Statement, output sql.Token) (interface{}, error) {
|
||||||
|
|
||||||
var exps sql.Fields
|
var exps sql.Fields
|
||||||
|
@ -102,20 +88,6 @@ func (d *document) yield(ctx context.Context, stm sql.Statement, output sql.Toke
|
||||||
default:
|
default:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
||||||
case sql.DIFF:
|
|
||||||
|
|
||||||
old, err := d.cold(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
now, err := d.cnow(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return d.diffs(old, now).Data(), nil
|
|
||||||
|
|
||||||
case sql.AFTER:
|
case sql.AFTER:
|
||||||
|
|
||||||
doc, err := d.cnow(ctx)
|
doc, err := d.cnow(ctx)
|
||||||
|
@ -132,23 +104,6 @@ func (d *document) yield(ctx context.Context, stm sql.Statement, output sql.Toke
|
||||||
}
|
}
|
||||||
return doc.Data(), nil
|
return doc.Data(), nil
|
||||||
|
|
||||||
case sql.BOTH:
|
|
||||||
|
|
||||||
old, err := d.cold(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
now, err := d.cnow(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return map[string]interface{}{
|
|
||||||
"after": now.Data(),
|
|
||||||
"before": old.Data(),
|
|
||||||
}, nil
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,30 +31,20 @@ func TestYield(t *testing.T) {
|
||||||
USE NS test DB test;
|
USE NS test DB test;
|
||||||
CREATE person:test SET test=1 RETURN AFTER;
|
CREATE person:test SET test=1 RETURN AFTER;
|
||||||
UPDATE person:test SET test=2 RETURN BEFORE;
|
UPDATE person:test SET test=2 RETURN BEFORE;
|
||||||
UPDATE person:test SET test=3 RETURN BOTH;
|
UPDATE person:test SET test=3 RETURN NONE;
|
||||||
UPDATE person:test SET test=4 RETURN DIFF;
|
|
||||||
UPDATE person:test SET test=5 RETURN NONE;
|
|
||||||
DELETE person:test RETURN BEFORE;
|
DELETE person:test RETURN BEFORE;
|
||||||
`
|
`
|
||||||
|
|
||||||
res, err := Execute(permsKV(), txt, nil)
|
res, err := Execute(permsKV(), txt, nil)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(res, ShouldHaveLength, 7)
|
So(res, ShouldHaveLength, 5)
|
||||||
So(res[1].Result, ShouldHaveLength, 1)
|
So(res[1].Result, ShouldHaveLength, 1)
|
||||||
So(data.Consume(res[1].Result[0]).Get("test").Data(), ShouldEqual, 1)
|
So(data.Consume(res[1].Result[0]).Get("test").Data(), ShouldEqual, 1)
|
||||||
So(res[2].Result, ShouldHaveLength, 1)
|
So(res[2].Result, ShouldHaveLength, 1)
|
||||||
So(data.Consume(res[2].Result[0]).Get("test").Data(), ShouldEqual, 1)
|
So(data.Consume(res[2].Result[0]).Get("test").Data(), ShouldEqual, 1)
|
||||||
So(res[3].Result, ShouldHaveLength, 1)
|
So(res[3].Result, ShouldHaveLength, 0)
|
||||||
So(data.Consume(res[3].Result[0]).Get("before.test").Data(), ShouldEqual, 2)
|
|
||||||
So(data.Consume(res[3].Result[0]).Get("after.test").Data(), ShouldEqual, 3)
|
|
||||||
So(res[4].Result, ShouldHaveLength, 1)
|
So(res[4].Result, ShouldHaveLength, 1)
|
||||||
So(res[4].Result[0], ShouldHaveLength, 1)
|
So(data.Consume(res[4].Result[0]).Get("test").Data(), ShouldEqual, 3)
|
||||||
So(data.Consume(res[4].Result[0]).Get("0.op").Data(), ShouldEqual, "replace")
|
|
||||||
So(data.Consume(res[4].Result[0]).Get("0.path").Data(), ShouldEqual, "/test")
|
|
||||||
So(data.Consume(res[4].Result[0]).Get("0.value").Data(), ShouldEqual, 4)
|
|
||||||
So(res[5].Result, ShouldHaveLength, 0)
|
|
||||||
So(res[6].Result, ShouldHaveLength, 1)
|
|
||||||
So(data.Consume(res[6].Result[0]).Get("test").Data(), ShouldEqual, 5)
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ func (p *parser) parseEcho(fallback Token) (exp Token, err error) {
|
||||||
|
|
||||||
if _, _, exi := p.mightBe(RETURN); exi {
|
if _, _, exi := p.mightBe(RETURN); exi {
|
||||||
|
|
||||||
exp, _, err = p.shouldBe(NONE, INFO, BOTH, DIFF, BEFORE, AFTER)
|
exp, _, err = p.shouldBe(NONE, BEFORE, AFTER)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
134
sql/sql_test.go
134
sql/sql_test.go
|
@ -1464,7 +1464,7 @@ func Test_Parse_Queries_Create(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `CREATE person RETURN`,
|
sql: `CREATE person RETURN`,
|
||||||
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
|
err: "Found `` but expected `NONE, BEFORE, AFTER`",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `CREATE person RETURN NONE`,
|
sql: `CREATE person RETURN NONE`,
|
||||||
|
@ -1474,22 +1474,6 @@ func Test_Parse_Queries_Create(t *testing.T) {
|
||||||
Parallel: true,
|
Parallel: true,
|
||||||
}}},
|
}}},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
sql: `CREATE person RETURN BOTH`,
|
|
||||||
res: &Query{Statements: []Statement{&CreateStatement{
|
|
||||||
What: []Expr{&Ident{"person"}},
|
|
||||||
Echo: BOTH,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `CREATE person RETURN DIFF`,
|
|
||||||
res: &Query{Statements: []Statement{&CreateStatement{
|
|
||||||
What: []Expr{&Ident{"person"}},
|
|
||||||
Echo: DIFF,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
sql: `CREATE person RETURN BEFORE`,
|
sql: `CREATE person RETURN BEFORE`,
|
||||||
res: &Query{Statements: []Statement{&CreateStatement{
|
res: &Query{Statements: []Statement{&CreateStatement{
|
||||||
|
@ -1651,7 +1635,7 @@ func Test_Parse_Queries_Update(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `UPDATE person RETURN`,
|
sql: `UPDATE person RETURN`,
|
||||||
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
|
err: "Found `` but expected `NONE, BEFORE, AFTER`",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `UPDATE person RETURN NONE`,
|
sql: `UPDATE person RETURN NONE`,
|
||||||
|
@ -1661,22 +1645,6 @@ func Test_Parse_Queries_Update(t *testing.T) {
|
||||||
Parallel: true,
|
Parallel: true,
|
||||||
}}},
|
}}},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
sql: `UPDATE person RETURN BOTH`,
|
|
||||||
res: &Query{Statements: []Statement{&UpdateStatement{
|
|
||||||
What: []Expr{&Ident{"person"}},
|
|
||||||
Echo: BOTH,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `UPDATE person RETURN DIFF`,
|
|
||||||
res: &Query{Statements: []Statement{&UpdateStatement{
|
|
||||||
What: []Expr{&Ident{"person"}},
|
|
||||||
Echo: DIFF,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
sql: `UPDATE person RETURN BEFORE`,
|
sql: `UPDATE person RETURN BEFORE`,
|
||||||
res: &Query{Statements: []Statement{&UpdateStatement{
|
res: &Query{Statements: []Statement{&UpdateStatement{
|
||||||
|
@ -1740,7 +1708,7 @@ func Test_Parse_Queries_Delete(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `DELETE person RETURN`,
|
sql: `DELETE person RETURN`,
|
||||||
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
|
err: "Found `` but expected `NONE, BEFORE, AFTER`",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `DELETE person RETURN NONE`,
|
sql: `DELETE person RETURN NONE`,
|
||||||
|
@ -1751,22 +1719,6 @@ func Test_Parse_Queries_Delete(t *testing.T) {
|
||||||
Parallel: true,
|
Parallel: true,
|
||||||
}}},
|
}}},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
sql: `DELETE person RETURN BOTH`,
|
|
||||||
res: &Query{Statements: []Statement{&DeleteStatement{
|
|
||||||
What: []Expr{&Ident{"person"}},
|
|
||||||
Echo: BOTH,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `DELETE person RETURN DIFF`,
|
|
||||||
res: &Query{Statements: []Statement{&DeleteStatement{
|
|
||||||
What: []Expr{&Ident{"person"}},
|
|
||||||
Echo: DIFF,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
sql: `DELETE person RETURN BEFORE`,
|
sql: `DELETE person RETURN BEFORE`,
|
||||||
res: &Query{Statements: []Statement{&DeleteStatement{
|
res: &Query{Statements: []Statement{&DeleteStatement{
|
||||||
|
@ -1904,7 +1856,7 @@ func Test_Parse_Queries_Relate(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `RELATE person -> purchase -> item RETURN`,
|
sql: `RELATE person -> purchase -> item RETURN`,
|
||||||
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
|
err: "Found `` but expected `NONE, BEFORE, AFTER`",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `RELATE person -> purchase -> item RETURN NONE`,
|
sql: `RELATE person -> purchase -> item RETURN NONE`,
|
||||||
|
@ -1916,26 +1868,6 @@ func Test_Parse_Queries_Relate(t *testing.T) {
|
||||||
Parallel: true,
|
Parallel: true,
|
||||||
}}},
|
}}},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
sql: `RELATE person -> purchase -> item RETURN BOTH`,
|
|
||||||
res: &Query{Statements: []Statement{&RelateStatement{
|
|
||||||
Type: &Table{"purchase"},
|
|
||||||
From: []Expr{&Ident{"person"}},
|
|
||||||
With: []Expr{&Ident{"item"}},
|
|
||||||
Echo: BOTH,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `RELATE person -> purchase -> item RETURN DIFF`,
|
|
||||||
res: &Query{Statements: []Statement{&RelateStatement{
|
|
||||||
Type: &Table{"purchase"},
|
|
||||||
From: []Expr{&Ident{"person"}},
|
|
||||||
With: []Expr{&Ident{"item"}},
|
|
||||||
Echo: DIFF,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
sql: `RELATE person -> purchase -> item RETURN BEFORE`,
|
sql: `RELATE person -> purchase -> item RETURN BEFORE`,
|
||||||
res: &Query{Statements: []Statement{&RelateStatement{
|
res: &Query{Statements: []Statement{&RelateStatement{
|
||||||
|
@ -2010,7 +1942,7 @@ func Test_Parse_Queries_Insert(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `INSERT ["one","two","tre"] INTO person RETURN`,
|
sql: `INSERT ["one","two","tre"] INTO person RETURN`,
|
||||||
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
|
err: "Found `` but expected `NONE, BEFORE, AFTER`",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `INSERT ["one","two","tre"] INTO person RETURN NONE`,
|
sql: `INSERT ["one","two","tre"] INTO person RETURN NONE`,
|
||||||
|
@ -2021,33 +1953,6 @@ func Test_Parse_Queries_Insert(t *testing.T) {
|
||||||
Parallel: true,
|
Parallel: true,
|
||||||
}}},
|
}}},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
sql: `INSERT ["one","two","tre"] INTO person RETURN INFO`,
|
|
||||||
res: &Query{Statements: []Statement{&InsertStatement{
|
|
||||||
Data: []interface{}{"one", "two", "tre"},
|
|
||||||
Into: &Table{"person"},
|
|
||||||
Echo: INFO,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `INSERT ["one","two","tre"] INTO person RETURN BOTH`,
|
|
||||||
res: &Query{Statements: []Statement{&InsertStatement{
|
|
||||||
Data: []interface{}{"one", "two", "tre"},
|
|
||||||
Into: &Table{"person"},
|
|
||||||
Echo: BOTH,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `INSERT ["one","two","tre"] INTO person RETURN DIFF`,
|
|
||||||
res: &Query{Statements: []Statement{&InsertStatement{
|
|
||||||
Data: []interface{}{"one", "two", "tre"},
|
|
||||||
Into: &Table{"person"},
|
|
||||||
Echo: DIFF,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
sql: `INSERT ["one","two","tre"] INTO person RETURN BEFORE`,
|
sql: `INSERT ["one","two","tre"] INTO person RETURN BEFORE`,
|
||||||
res: &Query{Statements: []Statement{&InsertStatement{
|
res: &Query{Statements: []Statement{&InsertStatement{
|
||||||
|
@ -2119,7 +2024,7 @@ func Test_Parse_Queries_Upsert(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `UPSERT ["one","two","tre"] INTO person RETURN`,
|
sql: `UPSERT ["one","two","tre"] INTO person RETURN`,
|
||||||
err: "Found `` but expected `NONE, INFO, BOTH, DIFF, BEFORE, AFTER`",
|
err: "Found `` but expected `NONE, BEFORE, AFTER`",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `UPSERT ["one","two","tre"] INTO person RETURN NONE`,
|
sql: `UPSERT ["one","two","tre"] INTO person RETURN NONE`,
|
||||||
|
@ -2130,33 +2035,6 @@ func Test_Parse_Queries_Upsert(t *testing.T) {
|
||||||
Parallel: true,
|
Parallel: true,
|
||||||
}}},
|
}}},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
sql: `UPSERT ["one","two","tre"] INTO person RETURN INFO`,
|
|
||||||
res: &Query{Statements: []Statement{&UpsertStatement{
|
|
||||||
Data: []interface{}{"one", "two", "tre"},
|
|
||||||
Into: &Table{"person"},
|
|
||||||
Echo: INFO,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `UPSERT ["one","two","tre"] INTO person RETURN BOTH`,
|
|
||||||
res: &Query{Statements: []Statement{&UpsertStatement{
|
|
||||||
Data: []interface{}{"one", "two", "tre"},
|
|
||||||
Into: &Table{"person"},
|
|
||||||
Echo: BOTH,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sql: `UPSERT ["one","two","tre"] INTO person RETURN DIFF`,
|
|
||||||
res: &Query{Statements: []Statement{&UpsertStatement{
|
|
||||||
Data: []interface{}{"one", "two", "tre"},
|
|
||||||
Into: &Table{"person"},
|
|
||||||
Echo: DIFF,
|
|
||||||
Parallel: true,
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
sql: `UPSERT ["one","two","tre"] INTO person RETURN BEFORE`,
|
sql: `UPSERT ["one","two","tre"] INTO person RETURN BEFORE`,
|
||||||
res: &Query{Statements: []Statement{&UpsertStatement{
|
res: &Query{Statements: []Statement{&UpsertStatement{
|
||||||
|
|
Loading…
Reference in a new issue