Currently, I perform these updates with multiple calls that return success or failure and use that result to determine whether to continue. In order to try and reduce the number of queries I'm trying to see if it's possible to do this update with a single call.
Here's the background. I have an API Endpoint that receives calls from Patreon for new and updated pledges. However, the email that the datapacket from Patreon sends may or may not match the email I have in the database. There's 4 different possibilities that I handle.
- Person node's email property matches the patreon information, but there is no relationship
- Person node's email property does not match the patreon information, but the IS_A_SUPPORTER relationship exists with the email address
- Person node's email property does not match the patreon information and there is no existing relationship with that email address, however there's an existing Pledge node with the email
- Person node's email property does not match the patreon information and there is no existing relationship with that email address and no existing Pledge node with the email
The actions for each are:
- Create a IS_A_SUPPORTER relationship and set the properties with items from the patreon information
- Update the relationship properties with items from the patreon information
- Update the Pledge node that matches the email from patreon
- Create a Pledge node that holds the patreon information.
A user can then "claim" it by entering that email into a form where I query and create the Relationship. When the relationship is created, I delete the Pledge node.
Like I said, individually, these are very simple. I'm using the .Net Driver and simply create a session then call Run for the first query...if no results, run second...etc until final is to create the Pledge node.
My question is...can this be done with a single query? I've been trying to get it using CASE statements, but whenever I try to perform any operations I get an unexpected character error.
WITH 'lclay@kaolinsoftware.com' as patreonEmail
OPTIONAL MATCH (p:Person)-[r:IS_A_SUPPORTER {email: patreonEmail}]->(:Donation)
OPTIONAL MATCH (o:Person {email: patreonEmail})
OPTIONAL MATCH (m:Pledge {email: patreonEmail})
RETURN p, r, o, m,
CASE
WHEN p IS NOT NULL THEN
SET r.pledge_amount_cents = 100000
WHEN o IS NOT NULL THEN
'o not null'
WHEN m IS NOT NULL THEN
'm not null'
END AS results