Skip to content

Commit

Permalink
Relationship declarations can now take enums for names
Browse files Browse the repository at this point in the history
Continue query works properly now
  • Loading branch information
rickardoberg committed Jul 5, 2012
1 parent 6ac8445 commit 2f5ccf5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/main/java/org/neo4j/cypherdsl/CypherQuery.java
Expand Up @@ -128,9 +128,16 @@ public static UpdateNext create( PathExpression... paths )
* @param query a previously created query object * @param query a previously created query object
* @return CypherQuery DSL that can be used to continue building the query * @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 // The internal query object. Methods in the DSL work on this
Expand Down Expand Up @@ -485,6 +492,12 @@ protected UpdateNext creates( PathExpression... paths )
return new Grammar(); return new Grammar();
} }


protected <T> T continueQuery(Class<T> asClause)
throws ClassCastException
{
return asClause.cast(new Grammar());
}

/** /**
* Declare start nodes. Corresponds to: * Declare start nodes. Corresponds to:
* <pre> * <pre>
Expand Down
69 changes: 68 additions & 1 deletion src/main/java/org/neo4j/cypherdsl/Path.java
Expand Up @@ -20,8 +20,10 @@


package org.neo4j.cypherdsl; package org.neo4j.cypherdsl;


import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List;


import org.neo4j.cypherdsl.expression.Expression; import org.neo4j.cypherdsl.expression.Expression;
import org.neo4j.cypherdsl.expression.PathExpression; import org.neo4j.cypherdsl.expression.PathExpression;
Expand All @@ -30,6 +32,8 @@
import org.neo4j.cypherdsl.query.PropertyValue; import org.neo4j.cypherdsl.query.PropertyValue;
import org.neo4j.cypherdsl.query.PropertyValues; 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. * Represents either a single node or a path from one node to another.
*/ */
Expand Down Expand Up @@ -133,7 +137,7 @@ public PathRelationship out()
*/ */
public PathRelationship out( String... relationships ) 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)) );
} }


/** /**
Expand All @@ -151,6 +155,27 @@ public PathRelationship out( Identifier... relationships )
return new PathRelationship( this, Direction.OUT, Arrays.asList( relationships ) ); return new PathRelationship( this, Direction.OUT, Arrays.asList( relationships ) );
} }


/**
* Declare a new outgoing relationship from this node.
* <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. * Declare a new outgoing relationship from this node.
* <p/> * <p/>
Expand Down Expand Up @@ -196,6 +221,27 @@ public PathRelationship in( Identifier... relationships )
return new PathRelationship( this, Direction.IN, Arrays.asList( relationships ) ); return new PathRelationship( this, Direction.IN, Arrays.asList( 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. * Declare a new relationship on this node.
* <p/> * <p/>
Expand Down Expand Up @@ -241,6 +287,27 @@ public PathRelationship both( Identifier... relationships )
return new PathRelationship( this, Direction.BOTH, Arrays.asList( 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 @Override
public void asString( StringBuilder builder ) public void asString( StringBuilder builder )
{ {
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/neo4j/cypherdsl/CypherCookbookTest.java
Expand Up @@ -21,6 +21,7 @@
package org.neo4j.cypherdsl; package org.neo4j.cypherdsl;


import org.junit.Test; import org.junit.Test;
import org.neo4j.cypherdsl.grammar.Start;
import org.neo4j.cypherdsl.query.Query; import org.neo4j.cypherdsl.query.Query;


import static org.junit.Assert.*; import static org.junit.Assert.*;
Expand All @@ -44,13 +45,13 @@ public void test5_1_1()
node( "hyperEdge" ).out( "hasRole" ).node( "role" ) ).toQuery(); 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", 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" ) ). where( identifier( "group" ).string( "name" ).eq( "Group2" ) ).
returns( identifier( "role" ).string( "name" ) ). returns( identifier( "role" ).string( "name" ) ).
toString() ); 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", 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" ) ). returns( identifier( "role" ).property( "name" ), identifier( "group" ).property( "name" ) ).
orderBy( order( identifier( "role" ).string( "name" ), ASCENDING ) ). orderBy( order( identifier( "role" ).string( "name" ), ASCENDING ) ).
toString() ); toString() );
Expand Down

0 comments on commit 2f5ccf5

Please sign in to comment.