conditional_execution
Your queries might require conditional execution logic that canβt be adequately
expressed in Cypher. The do
module makes it possible to define complex logic
and use it to control query execution.
Trait | Value |
---|---|
Module type | module |
Implementation | Python |
Parallelism | sequential |
Proceduresβ
Using the following procedures to run queries that execute global operations is currently not supported and returns a warning. The operations in question are:
- index creation/deletion
- constraint creation/deletion
- changing the isolation level globally
- setting the storage mode
case(conditionals, else_query, params)
β
Given a list of condition-query pairs, do.case
executes the query associated
with the first condition evaluating to true
(or the else query
if none are
true
) with the given parameters.
Parameters are prefixed with $
like $param_name
. For examples, see
here.
Input:β
conditionals: List[Any]
β‘ Variable-length list of condition-query pairs structured as[condition, query, condition, query, β¦β]
. Conditions areboolean
and queries arestring
.else_query: string (default = "")
β‘ The query to be executed if no condition evaluates totrue
.params: Map (default = NULL)
β‘ If any of the given queries is parameterized, provide a{param_name: param_value}
map to be applied to them.
Output:β
value: Map
β‘ Contains the result record of the executed query. Eachvalue
corresponds to one result record.
Usage:β
MATCH (n)
WITH size(collect(n)) as n_nodes
CALL do.case([n_nodes = 0,
"RETURN 'empty' AS graph_status;",
n_nodes = 1,
"RETURN 'one_node' AS graph_status;"],
"RETURN 'multiple nodes' AS graph_status;")
YIELD value
RETURN value.graph_status AS graph_status;
when(condition, if_query, else_query, params)
β
do.when
evaluates the given condition and executes the if query
or the
else query
depending on whether the condition is satisfied.
Parameters are prefixed with $
like $param_name
. For examples, see
here.
Input:β
condition: boolean
β‘ Determines what query to execute.if_query: string
β‘ The query to be executed if the condition is satisfied.else_query: string (default = "")
β‘ The query to be executed if the condition isnβt satisfied.params: Map (default = NULL)
β‘ Ifif_query
orelse_query
are parameterized, provide a{param_name: param_value}
map to be applied.
Output:β
value: Map
β‘ Contains the result record of the executed query. Eachvalue
corresponds to one result record.
Usage:β
MATCH (n)
WITH size(collect(n)) as n_nodes
CALL do.when(n_nodes = 0,
"RETURN 'empty' AS graph_status;",
"RETURN 'not empty' as graph_status;")
YIELD value
RETURN value.graph_status AS graph_status;
Exampleβ
- Step 1: Input graph
- Step 2: Cypher load commands
- Step 3: Running command
- Step 4: Results
MERGE (a:Node {id: 0}) MERGE (b:Node {id: 1}) CREATE (a)-[:RELATION]->(b);
MERGE (a:Node {id: 1}) MERGE (b:Node {id: 2}) CREATE (a)-[:RELATION]->(b);
MERGE (a:Node {id: 2}) MERGE (b:Node {id: 0}) CREATE (a)-[:RELATION]->(b);
MERGE (a:Node {id: 3}) MERGE (b:Node {id: 3}) CREATE (a)-[:RELATION]->(b);
MERGE (a:Node {id: 3}) MERGE (b:Node {id: 4}) CREATE (a)-[:RELATION]->(b);
MERGE (a:Node {id: 3}) MERGE (b:Node {id: 5}) CREATE (a)-[:RELATION]->(b);
MATCH (n:Node)
WITH size(collect(n)) as n_nodes
CALL do.when(n_nodes = 0,
"RETURN 'empty' AS graph_status;",
"RETURN 'not empty' as graph_status;")
YIELD value
RETURN value.graph_status AS graph_status;
ββββββββββββββββ
β graph_status β
ββββββββββββββββ€
β not empty β
ββββββββββββββββ