Skip to main content

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.

docs-source

TraitValue
Module typemodule
ImplementationPython
Parallelismsequential

Procedures​

info

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 are boolean and queries are string.
  • else_query: string (default = "") ➑ The query to be executed if no condition evaluates to true.
  • 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. Each value 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) ➑ If if_query or else_query are parameterized, provide a {param_name: param_value} map to be applied.

Output:​

  • value: Map ➑ Contains the result record of the executed query. Each value 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​