Giter Club home page Giter Club logo

Comments (1)

JonahPlusPlus avatar JonahPlusPlus commented on May 20, 2024

I realize that there needs to be clarification on how this would work with transactions.
Transactions in their current state are pretty convoluted. They only work at the top-level and provide no warning about this when used in lower scopes.
For example, the following doesn't work as expected:

{
  BEGIN;
  CREATE foo SET bar = "biz";
  CANCEL;
}

The expected result would be that the CREATE statement doesn't commit because of the CANCEL, but when executed we see that it does create a new record, and that the BEGIN/CANCEL/COMMIT statements have no apparent effect.

Anyways, the problem at hand is how RETURN statements change the result of a transaction. If returns break execution, it wouldn't allow calling COMMIT or CANCEL and getting a result.

IMO, the most intuitive solution is to make transactions work in blocks and make RETURN act like a CANCEL that breaks execution.

A couple examples:

BEGIN;
CREATE foo SET bar = "biz";
RETURN CREATE foo SET bar = "boo";
# This RETURN does nothing (unlike before, where it would make it the single return value of the transaction
COMMIT;

Result:

[
  [{id:"foo:f09uja09sdj0asd", bar:"biz"}],
  [{id:"foo:adsad0h80jfe09j", bar:"boo"}]
]
{
  BEGIN;
  CREATE foo SET bar = "biz";
  RETURN CREATE foo SET bar = "boo"; # Here, the RETURN cancels the transaction and returns the CREATE
  COMMIT; # This block doesn't return a value
}

Result:

[
  [{id:"foo:adsad0h80jfe09j", bar:"boo"}]
]
{
  BEGIN;
  CREATE foo SET bar = "biz";
  CREATE foo SET bar = "boo";
  COMMIT;
  true # the block returns true
}

Result: [true]

Edge cases:
What if a later statement depends on the result of a statement inside a canceled transaction?
This is currently covered:

BEGIN;
LET $foo = CREATE foo SET bar = "biz";
CANCEL;
RETURN $foo;

$foo produces a phantom result, a record that never existed and is worthless outside of the canceled transaction. To me, this represent the biggest flaw of this syntax: the transaction has a scope that is not expressed, but is instead merged into the current scope.
An improved syntax would be explicit about the scope:

BEGIN {
  LET $foo = CREATE foo SET bar = "biz";
  CANCEL false; # an optional return parameter
  $foo
}

Here, the scope is explicit and variables are not leaked outside. It also allows for an implicit commit as the final value. CANCEL and COMMIT would have an optional return value for setting the value of the transaction.

Anyways, I guess this does warrant an RFC considering how many breaking changes would be needed.

from surrealdb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.