cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Use optional Parameters to skip sub queries

Hi everyone,

after an initial DB initialization, we try to regularly update our DB. However, due to certain limitations, we cannot use .csv files but generate our cypher queries using an own toolchain. We have large data sets and, thus, we aim to use parameters and unwind statements as much as possible. When updating certain entries, it may occur that some parameters are sometimes null. If such a parameter is null, we want to skip part of our query. The following is a simplified example:

:param verbuende => [{verbundId:"verbund1", swNr:"verbund1Sw", swVersion:"verbund1swv"}, {verbundId:"verbund2", swNr:"verbund2Sw"}, {verbundId:"verbund3", swVersion:"verbund3swv"}]

UNWIND $verbuende AS verbundEntry
MERGE (verbund: HardwareSoftwareVersionsVerbund {id: verbundEntry.verbundId})
CALL {WITH verbund, verbundEntry WHERE NOT verbundEntry.swNr IS NULL
MERGE (software:Software {name: verbundEntry.swNr})
MERGE (verbund)-[:hwswVersionsVerbundSoftware]->(software)
RETURN software
}
CALL {WITH verbund, verbundEntry WHERE NOT verbundEntry.swVersion IS NULL
MERGE (softwareVersion:SoftwareVersion {name: verbundEntry.swVersion})
MERGE (verbund)-[:hwswVersionsVerbundSoftwareVersion]->(softwareVersion)
RETURN softwareVersion
}

 

Depending on whether verbundEntry.swNr verbundEntry.swVersion is set, we want to execute only some of the sub queries. However, it seems that we cannot use this "WHERE NOT ...." in the WITH of a subquery.

Do you have any idea how we can selectively execute these subqueries based on the parameters being null?

2 REPLIES 2

By accident, I fixed it myself. A working solution seems to be:

UNWIND $verbuende AS verbundEntry
MERGE (verbund: HardwareSoftwareVersionsVerbund {id: verbundEntry.verbundId})
WITH verbund, verbundEntry
CALL {WITH verbund, verbundEntry
WITH verbund, verbundEntry.swNr as swNr WHERE swNr IS NOT NULL
MERGE (software:Software {name: swNr})
MERGE (verbund)-[:hwswVersionsVerbundSoftware]->(software)
}
WITH verbund, verbundEntry
CALL {WITH verbund, verbundEntry
WITH verbund, verbundEntry.swVersion as swVersion WHERE swVersion IS NOT NULL
MERGE (softwareVersion:SoftwareVersion {name: swVersion})
MERGE (verbund)-[:hwswVersionsVerbundSoftwareVersion]->(softwareVersion)
}
RETURN verbund

The double WITH seems a bit strange and can most likely be improved somehow oO

That is the solution. Have used in several solutions.