Wolfgang-Schuetzelhofer / jcypher

Java access to Neo4J graph databases at multiple levels of abstraction
Apache License 2.0
86 stars 15 forks source link

CASE and WHEN with assigning a value #36

Closed suresh-bojjam closed 6 years ago

suresh-bojjam commented 6 years ago
            JcNode r=new JcNode("r");
    JcNode s_r=new JcNode("s_r");
    Node node1=MERGE.node(r);
    Node node2=MERGE.node(s_r);

    node1
    .property("NAME").value(tName)
    .property("CUSTID").value(tName)
    .label(Constants.LABEL_R);

    node2
    .property("NAME").value(tName)
    .property("CUSTID").value(tName)
    .label(Constants.LABEL_S_R)
    .relation().type(Constants.LABEL_S_EDGE).out().node(r);

START.node(u).byId(Id), node1, node2, ON_CREATE.SET(s_r.property("aa")).to(1), ON_MATCH.SET(s_r.property("aa")).to(null), CASE.result(), WHEN.valueOf(s_r.property("aa")).IS_NULL(), 50, //assigning the value to on_match set property ELSE.perform(), 20, //else part END.caseXpr()

query should be: START u = node(12345) MERGE (r:RELATION{NAME:'T', CUSTID:'123'}) MERGE (s_r:STATE_R{NAME:'T', CUSTID:'456'})-[:RELATION1]->(r) ON CREATE SET s_r.aa = 1 ON MATCH SET s_r.aa = CASE WHEN s_r.aa IS NULL THEN 50 ELSE 20 END

problem: 50& 20 are integer values where we need to give the IClause as input.

Wolfgang-Schuetzelhofer commented 6 years ago

Hi Suresh Bojjam,

I will be back in office by tomorrow and have a look into your problem. You can expect me to be back with an answer within the next 2 or 3 days.

Best regards, Wolfgang

On Mon, Dec 18, 2017 at 11:38 AM, suresh bojjam notifications@github.com wrote:

        JcNode r=new JcNode("r");

JcNode s_r=new JcNode("s_r"); Node node1=MERGE.node(r); Node node2=MERGE.node(s_r);

node1 .property("NAME").value(tName) .property("CUSTID").value(tName) .label(Constants.LABEL_R);

node2 .property("NAME").value(tName) .property("CUSTID").value(tName) .label(Constants.LABEL_S_R) .relation().type(Constants.LABEL_S_EDGE).out().node(r);

START.node(u).byId(Id), node1, node2, ON_CREATE.SET(s_r.property("aa")).to(1), ON_MATCH.SET(s_r.property("aa")).to(null), CASE.result(), WHEN.valueOf(s_r.property("aa")).IS_NULL(), 50, //assigning the value to on_match set property ELSE.perform(), 20, //else part END.caseXpr()

query should be: START u = node(12345) MERGE (r:RELATION{NAME:'T', CUSTID:'123'}) MERGE (s_r:STATE_R{NAME:'T', CUSTID:'456'})-[:SNAPSHOT]->(r) ON CREATE SET s_r.aa = 1 ON MATCH SET s_r.aa = CASE WHEN s_r.aa IS NULL THEN 50 ELSE 20 END MERGE (u)-[:BELONGSTO]-(r)

problem: 50& 20 are integer values where we need to give the IClause as input.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Wolfgang-Schuetzelhofer/jcypher/issues/36, or mute the thread https://github.com/notifications/unsubscribe-auth/AHI-36Jru7XTmiD3XrDMonMBYEkYd77Fks5tBkC6gaJpZM4RFRbi .

Wolfgang-Schuetzelhofer commented 6 years ago

Hi Suresh Bojjam, the following code should do:

            IClause[] clauses;
    JcQuery query;
    String cypher;

    JcNode u = new JcNode("u");
    JcNode r = new JcNode("r");
    JcNode s_r = new JcNode("s_r");

    clauses = new IClause[]{
            START.node(u).byId(12345),
            MERGE.node(r).label("RELATION").property("NAME").value("T").property("CUSTID").value("123"),
            MERGE.node(s_r).label("STATE_R").property("NAME").value("T").property("CUSTID").value("456")
                .relation().out().type("SNAPSHOT").node(r),
            ON_CREATE.SET(s_r.property("aa")).to(1),
            ON_MATCH.SET(s_r.property("aa")).to(null),
                    CASE.result(),
                        WHEN.valueOf(s_r.property("aa")).IS_NULL(),
                            NATIVE.cypher("50"),
                        ELSE.perform(),
                            NATIVE.cypher("20"),
                    END.caseXpr(),
            MERGE.node(u).relation().type("BELONGSTO").node(r)
    };
    query = new JcQuery();
    query.setClauses(clauses);
    // You can at any time see to what CYPHER query this translates
    cypher = Util.toCypher(query, Format.PRETTY_1);

    System.out.println(cypher);

The null parameter in ON_MATCH ...to(null) admittedly is not intuitive. With the December release I will try to provide a more intuitive expression for such a case. Additionally with the December release you will have the possibility to use different planner rules globally as well as on a per query basis.

Best regards, Wolfgang