Combining queries

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.

  1. Person node's email property matches the patreon information, but there is no relationship
  2. Person node's email property does not match the patreon information, but the IS_A_SUPPORTER relationship exists with the email address
  3. 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
  4. 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:

  1. Create a IS_A_SUPPORTER relationship and set the properties with items from the patreon information
  2. Update the relationship properties with items from the patreon information
  3. Update the Pledge node that matches the email from patreon
  4. 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

The first thing I see is that SET is not supported in the CASE statement. The CASE statement is meant to evaluate a series of boolean expressions and return the first one that evaluates to true, and then returns the specified value or expression that evaluates to a value.

That's the idea I was getting, but wanted to make sure I wasn't misunderstanding it. I think I'm going to have to leave it as it is. It's not a big deal, but I just don't like seeing multiple if statements in a row :)

Okay, so you do have working code but have not posted it here.
Just a helpful comment. I find that when I post a question here, I get responses faster if I can provide code to build a tiny dataset to use for showing my problem, along with my code (working or not). It becomes a fun puzzle for someone else to solve for you.

@leelandclay Neo4j APOC Procedures User Guide

please have a look at this..i think this can help you

@ganesanmithun323 That looks like exactly what I'm looking for. I've already moved onto another story at the moment so it'll be a while before I can test this, but I will :slight_smile:
Thanks!!!!!