-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreturn.surql
48 lines (37 loc) · 1.28 KB
/
return.surql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
-- Return a simple value
RETURN 123;
RETURN "I am a string!";
RETURN {
prop: "value"
};
-- Return the result of a query
RETURN SELECT * FROM person;
RETURN (CREATE person).id;
// RETURN statements can set the result of any transaction. This includes transactions, blocks and functions.
BEGIN TRANSACTION;
-- We are executing quite a few queries here
LET $firstname = "John";
LET $lastname = "Doe";
LET $person = CREATE person CONTENT {
firstname: $firstname,
lastname: $lastname
};
-- But because we end with a RETURN query, only the person's ID will be returned
-- The results of the other queries will be omitted.
RETURN $person.id;
-- One issue with this approach is that query errors are generic.
-- To get around that, use a block, which is executed as a transaction by itself.
COMMIT TRANSACTION;
-- Early return
DEFINE FUNCTION fn::person::create($firstname: string, $lastname: string) {
LET $person = CREATE person CONTENT {
firstname: $firstname,
lastname: $lastname
};
-- The RETURN statement will set the return value of the custom function, and further queries will not be executed.
RETURN $person.id;
-- This query will never be executed
CREATE person SET firstname = "Stephen", lastname = "Strange";
};
fn::person::create("Thanos", "Johnson");
SELECT * FROM person;