Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for extra parentheses, if necessary

  • Loading branch information...
commit d8adc3ab9a4bb98d85a140bd4a45768af3574238 1 parent 04c0eee
@rickardoberg rickardoberg authored
View
49 src/main/java/org/neo4j/cypherdsl/CypherQuery.java
@@ -919,16 +919,51 @@ public static PathExpression allShortestPaths( PathExpression path )
}
// Return -------------------------------------------------------
+ /**
+ * Use this to rename identifiers for RETURN or WITH
+ *
+ * Corresponds to:
+ * <pre>
+ * expression AS name
+ * </pre>
+ *
+ * @param expression
+ * @param name
+ * @return
+ */
public static Expression as(Expression expression, String name)
{
return new Value(new Operator(expression, " AS "), identifier( name ));
}
+ /**
+ * Use this to rename identifiers for RETURN or WITH
+ *
+ * Corresponds to:
+ * <pre>
+ * expression AS name
+ * </pre>
+ *
+ * @param expression
+ * @param name
+ * @return
+ */
public static Expression as(Expression expression, Identifier name)
{
return new Value(new Operator(expression, " AS "), name);
}
+ /**
+ * Use this to declare DISTINCT
+ *
+ * Corresponds to:
+ * <pre>
+ * DISTINCT expression
+ * </pre>
+ *
+ * @param expression
+ * @return
+ */
public static Expression distinct(Expression expression)
{
return new Value(new Operator("DISTINCT "), expression);
@@ -1564,6 +1599,20 @@ public static CollectionExpression range(NumericExpression start, NumericExpress
// Mathematical expressions
/**
+ * Declare extra parentheses. This can be useful to ensure that operators are performed in the order you desire.
+ *
+ * Corresponds to:
+ * <pre>
+ * (numericExpression)
+ * </pre>
+ *
+ */
+ public static NumericExpression p(NumericExpression numericExpression)
+ {
+ return new Value( new FunctionExpression( "", numericExpression ) );
+ }
+
+ /**
* Declare an abs expression. Corresponds to:
* <pre>
* abs(expression)
View
6 src/test/java/org/neo4j/cypherdsl/CypherCookbookTest.java
@@ -421,13 +421,13 @@ public void test5_14_1()
"WHERE has(me.name) and me.name=\"me\" " +
"WITH me,count(DISTINCT r1) AS H1,count(DISTINCT r2) AS H2,you " +
"MATCH (me)-[r1:ATE]->(food)<-[r2:ATE]-(you) " +
- "RETURN sum(1-abs(r1.times/H1-r2.times/H2)*r1.times+r2.times/H1+H2) AS similarity",
+ "RETURN sum((1-abs(r1.times/H1-r2.times/H2))*(r1.times+r2.times)/(H1+H2)) AS similarity",
start( allNodes( "me" ) ).
match( node( "me" ).out( "ATE").as("r1" ).node( "food" ).in( "ATE" ).as("r2").node( "you" ) ).
where( has( identifier( "me" ).property( "name" ) ).and( identifier( "me" ).property( "name" ).eq( "me" ) ) ).
with( identifier( "me" ), as( count( distinct( identifier( "r1" ) ) ), "H1" ), as( count( distinct( identifier( "r2" ) ) ), "H2" ),identifier( "you" ) ).
match( node( "me" ).out( "ATE").as("r1").node( "food" ).in( "ATE").as("r2").node( "you" ) ).
- returns( as( sum( literal( 1 ).subtract( abs( identifier( "r1" ).property( "times" ).divideBy( identifier( "H1" ) ).subtract( identifier( "r2" ).property( "times" ).divideBy( identifier( "H2" )))).
- times( identifier( "r1" ).property( "times" ).add( identifier( "r2" ).property( "times" ) ) ).divideBy( identifier( "H1" ).add( identifier( "H2" ) ) )) ) , "similarity" ) ).toString());
+ returns( as( sum( p(literal( 1 ).subtract( abs( identifier( "r1" ).property( "times" ).divideBy( identifier( "H1" ) ).subtract( identifier( "r2" ).property( "times" ).divideBy( identifier( "H2" )))))).
+ times( p(identifier( "r1" ).property( "times" ).add( identifier( "r2" ).property( "times" ) ) ).divideBy( p( identifier( "H1" ).add( identifier( "H2" ) )) )) ) , "similarity" ) ).toString());
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.