Skip to content
Browse files

Relationship declarations can now take enums for names

Continue query works properly now
  • Loading branch information...
1 parent 6ac8445 commit 2f5ccf5cc6e920c3ca559bcb13f41ade80a8eabc @rickardoberg rickardoberg committed
View
17 src/main/java/org/neo4j/cypherdsl/CypherQuery.java
@@ -128,9 +128,16 @@ public static UpdateNext create( PathExpression... paths )
* @param query a previously created query object
* @return CypherQuery DSL that can be used to continue building the query
*/
- public static CypherQuery newQuery( Query query )
+ public static <T> T continueQuery( Query query, Class<T> asClause)
+ throws ClassCastException
{
- return new CypherQuery( query );
+ try
+ {
+ return new CypherQuery( (Query) query.clone() ).continueQuery(asClause);
+ } catch (CloneNotSupportedException e)
+ {
+ throw new RuntimeException(e);
+ }
}
// The internal query object. Methods in the DSL work on this
@@ -485,6 +492,12 @@ protected UpdateNext creates( PathExpression... paths )
return new Grammar();
}
+ protected <T> T continueQuery(Class<T> asClause)
+ throws ClassCastException
+ {
+ return asClause.cast(new Grammar());
+ }
+
/**
* Declare start nodes. Corresponds to:
* <pre>
View
69 src/main/java/org/neo4j/cypherdsl/Path.java
@@ -20,8 +20,10 @@
package org.neo4j.cypherdsl;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.List;
import org.neo4j.cypherdsl.expression.Expression;
import org.neo4j.cypherdsl.expression.PathExpression;
@@ -30,6 +32,8 @@
import org.neo4j.cypherdsl.query.PropertyValue;
import org.neo4j.cypherdsl.query.PropertyValues;
+import static org.neo4j.cypherdsl.CypherQuery.identifier;
+
/**
* Represents either a single node or a path from one node to another.
*/
@@ -133,7 +137,7 @@ public PathRelationship out()
*/
public PathRelationship out( String... relationships )
{
- return new PathRelationship( this, Direction.OUT, Arrays.asList( CypherQuery.identifiers( relationships ) ) );
+ return new PathRelationship( this, Direction.OUT, Arrays.asList(CypherQuery.identifiers(relationships)) );
}
/**
@@ -156,6 +160,27 @@ public PathRelationship out( Identifier... relationships )
* <p/>
* Corresponds to:
* <pre>
+ * (n)-[:relationship1|relationship2]->(m)
+ * </pre>
+ *
+ * @return
+ */
+ public PathRelationship out( Enum<?>... relationships )
+ {
+ List<Identifier> relationshipNames = new ArrayList<Identifier>();
+ for (Enum<?> relationship : relationships)
+ {
+ relationshipNames.add(identifier(relationship.name()));
+ }
+
+ return new PathRelationship( this, Direction.OUT, relationshipNames );
+ }
+
+ /**
+ * Declare a new outgoing relationship from this node.
+ * <p/>
+ * Corresponds to:
+ * <pre>
* (n)<--(m)
* </pre>
*
@@ -197,6 +222,27 @@ public PathRelationship in( Identifier... relationships )
}
/**
+ * Declare a new incoming relationship to this node.
+ * <p/>
+ * Corresponds to:
+ * <pre>
+ * (n)<-[:relationship1|relationship2]-(m)
+ * </pre>
+ *
+ * @return
+ */
+ public PathRelationship in( Enum<?>... relationships )
+ {
+ List<Identifier> relationshipNames = new ArrayList<Identifier>();
+ for (Enum<?> relationship : relationships)
+ {
+ relationshipNames.add(identifier(relationship.name()));
+ }
+
+ return new PathRelationship( this, Direction.IN, relationshipNames );
+ }
+
+ /**
* Declare a new relationship on this node.
* <p/>
* Corresponds to:
@@ -241,6 +287,27 @@ public PathRelationship both( Identifier... relationships )
return new PathRelationship( this, Direction.BOTH, Arrays.asList( relationships ) );
}
+ /**
+ * Declare a new relationship on this node.
+ * <p/>
+ * Corresponds to:
+ * <pre>
+ * (n)-[:relationship1|relationship2]-(m)
+ * </pre>
+ *
+ * @return
+ */
+ public PathRelationship both( Enum<?>... relationships )
+ {
+ List<Identifier> relationshipNames = new ArrayList<Identifier>();
+ for (Enum<?> relationship : relationships)
+ {
+ relationshipNames.add(identifier(relationship.name()));
+ }
+
+ return new PathRelationship( this, Direction.BOTH, relationshipNames );
+ }
+
@Override
public void asString( StringBuilder builder )
{
View
5 src/test/java/org/neo4j/cypherdsl/CypherCookbookTest.java
@@ -21,6 +21,7 @@
package org.neo4j.cypherdsl;
import org.junit.Test;
+import org.neo4j.cypherdsl.grammar.Start;
import org.neo4j.cypherdsl.query.Query;
import static org.junit.Assert.*;
@@ -44,13 +45,13 @@ public void test5_1_1()
node( "hyperEdge" ).out( "hasRole" ).node( "role" ) ).toQuery();
assertEquals( CYPHER + "START n=node:node_auto_index(name=\"User1\") MATCH (n)-[:hasRoleInGroup]->(hyperEdge)-[:hasGroup]->(group),(hyperEdge)-[:hasRole]->(role) WHERE group.name=\"Group2\" RETURN role.name",
- CypherQuery.newQuery( query ).starts().
+ CypherQuery.continueQuery(query, Start.class).starts().
where( identifier( "group" ).string( "name" ).eq( "Group2" ) ).
returns( identifier( "role" ).string( "name" ) ).
toString() );
assertEquals( CYPHER + "START n=node:node_auto_index(name=\"User1\") MATCH (n)-[:hasRoleInGroup]->(hyperEdge)-[:hasGroup]->(group),(hyperEdge)-[:hasRole]->(role) RETURN role.name,group.name ORDER BY role.name ASCENDING",
- CypherQuery.newQuery( query ).starts().
+ CypherQuery.continueQuery( query, Start.class ).starts().
returns( identifier( "role" ).property( "name" ), identifier( "group" ).property( "name" ) ).
orderBy( order( identifier( "role" ).string( "name" ), ASCENDING ) ).
toString() );

0 comments on commit 2f5ccf5

Please sign in to comment.
Something went wrong with that request. Please try again.