Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for labels

  • Loading branch information...
commit 40be8f9c173eee4bf9bef9addf0003d5a4fa5ac8 1 parent ce18cb0
@DavideD DavideD authored
View
21 src/main/java/org/neo4j/cypherdsl/CypherQuery.java
@@ -46,6 +46,7 @@
import org.neo4j.cypherdsl.query.Filter;
import org.neo4j.cypherdsl.query.FunctionExpression;
import org.neo4j.cypherdsl.query.IterablePredicateExpression;
+import org.neo4j.cypherdsl.query.LabelValue;
import org.neo4j.cypherdsl.query.NamedPath;
import org.neo4j.cypherdsl.query.Operator;
import org.neo4j.cypherdsl.query.OrderByExpression;
@@ -87,7 +88,7 @@ public static StartNext start( StartExpression... startExpressions )
* @param paths
* @return
*/
- public static UpdateNext match( PathExpression... paths )
+ public static StartNext match( PathExpression... paths )
{
CypherQuery query = new CypherQuery();
return query.matches(paths);
@@ -184,6 +185,18 @@ public static Parameter param( String name )
}
/**
+ * Declare a label.
+ *
+ * @param label literal value
+ * @return Label instance
+ */
+ public static LabelValue label( String label )
+ {
+ checkNull( label, "Label" );
+ return new LabelValue( label );
+ }
+
+ /**
* Declare a literal string value, such as "Foo".
*
* @param value literal value
@@ -507,7 +520,7 @@ protected UpdateNext merges( PathExpression... paths )
* @param paths
* @return
*/
- protected UpdateNext matches( PathExpression... paths )
+ protected StartNext matches( PathExpression... paths )
{
query.add( new MatchClause( Arrays.asList( paths ) ) );
@@ -875,7 +888,7 @@ protected UpdateNext matches( PathExpression... paths )
*/
public static Path node()
{
- return new Path( null, null, null );
+ return new Path( null, null, null, null );
}
/**
@@ -907,7 +920,7 @@ public static Path node( String id )
*/
public static Path node( Expression expression )
{
- return new Path( expression, null, null );
+ return new Path( expression, null, null, null );
}
/**
View
36 src/main/java/org/neo4j/cypherdsl/Path.java
@@ -34,6 +34,8 @@
import org.neo4j.cypherdsl.expression.Expression;
import org.neo4j.cypherdsl.expression.PathExpression;
import org.neo4j.cypherdsl.query.AbstractExpression;
+import org.neo4j.cypherdsl.query.LabelValue;
+import org.neo4j.cypherdsl.query.LabelValues;
import org.neo4j.cypherdsl.query.PropertyValue;
import org.neo4j.cypherdsl.query.PropertyValues;
@@ -46,13 +48,30 @@
{
private final Expression node;
private final Expression nodePropertyValues;
+ private final Expression nodeLabels;
private final PathRelationship relationship;
- Path( Expression node, PathRelationship relationship, Expression nodePropertyValues )
+ Path( Expression node, PathRelationship relationship, Expression nodePropertyValues, Expression labels )
{
this.node = node;
this.relationship = relationship;
this.nodePropertyValues = nodePropertyValues;
+ this.nodeLabels = labels;
+ }
+
+ public Path labels( LabelValue... labels )
+ {
+ return new Path( node, relationship, nodePropertyValues, new LabelValues( asList( labels ) ) );
+ }
+
+ public Path labels( Iterable<LabelValue> labels )
+ {
+ return new Path( node, relationship, nodePropertyValues, new LabelValues( labels ) );
+ }
+
+ public Path label( String label )
+ {
+ return new Path( node, relationship, nodePropertyValues, new LabelValue( label ) );
}
/**
@@ -71,7 +90,7 @@
*/
public Path values( PropertyValue... propertyValues )
{
- return new Path( node, relationship, new PropertyValues( asList( propertyValues ) ) );
+ return new Path( node, relationship, new PropertyValues( asList( propertyValues ) ), nodeLabels );
}
/**
@@ -90,7 +109,7 @@ public Path values( PropertyValue... propertyValues )
*/
public Path values( Iterable<PropertyValue> propertyValues )
{
- return new Path( node, relationship, new PropertyValues( propertyValues ) );
+ return new Path( node, relationship, new PropertyValues( propertyValues ), nodeLabels );
}
/**
@@ -108,7 +127,7 @@ public Path values( Iterable<PropertyValue> propertyValues )
*/
public Path values( Parameter propertyValues )
{
- return new Path( node, relationship, propertyValues );
+ return new Path( node, relationship, propertyValues, nodeLabels );
}
/**
@@ -322,7 +341,10 @@ public void asString( StringBuilder builder )
if ( node != null )
{
node.asString( builder );
-
+ if ( nodeLabels != null )
+ {
+ nodeLabels.asString( builder );
+ }
if ( nodePropertyValues != null )
{
builder.append( ' ' );
@@ -331,6 +353,10 @@ public void asString( StringBuilder builder )
}
else
{
+ if ( nodeLabels != null )
+ {
+ nodeLabels.asString( builder );
+ }
if ( nodePropertyValues != null )
{
nodePropertyValues.asString( builder );
View
4 src/main/java/org/neo4j/cypherdsl/PathRelationship.java
@@ -173,7 +173,7 @@ public PathRelationship hops( Integer minHops, Integer maxHops )
*/
public Path node()
{
- return new Path( null, this, null );
+ return new Path( null, this, null, null );
}
/**
@@ -207,7 +207,7 @@ public Path node( String id )
*/
public Path node( Expression id )
{
- return new Path( id, this, null );
+ return new Path( id, this, null, null );
}
@Override
View
28 src/main/java/org/neo4j/cypherdsl/expression/LabelExpression.java
@@ -0,0 +1,28 @@
+/**
+ * Copyright (c) 2002-2014 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.neo4j.cypherdsl.expression;
+
+/**
+ * Expression that evaluates to a single label
+ */
+public interface LabelExpression
+ extends ScalarExpression
+{
+}
View
39 src/main/java/org/neo4j/cypherdsl/query/LabelValue.java
@@ -0,0 +1,39 @@
+/**
+ * Copyright (c) 2002-2014 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.neo4j.cypherdsl.query;
+
+/**
+ * Represents matching a label to a value
+ */
+public class LabelValue extends AbstractExpression {
+
+ private final String label;
+
+ public LabelValue(String label) {
+ this.label = label;
+ }
+
+ @Override
+ public void asString(StringBuilder builder) {
+ builder.append(":");
+ builder.append(label);
+ }
+
+}
View
44 src/main/java/org/neo4j/cypherdsl/query/LabelValues.java
@@ -0,0 +1,44 @@
+/**
+ * Copyright (c) 2002-2014 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.neo4j.cypherdsl.query;
+
+/**
+ * Represents a collection of Label
+ */
+public class LabelValues
+ extends AbstractExpression
+{
+ private final Iterable<LabelValue> labels;
+
+ public LabelValues( Iterable<LabelValue> labels )
+ {
+ this.labels = labels;
+ }
+
+ @Override
+ public void asString( StringBuilder builder )
+ {
+ for ( LabelValue label : labels )
+ {
+ label.asString( builder );
+ }
+ }
+}
View
11 src/test/java/org/neo4j/cypherdsl/CypherQueryTest2.java
@@ -68,4 +68,15 @@ public void testAndOrPrecedence()
returns( identifier( "n" ) );
}}.toString() );
}
+
+ @Test
+ public void testLabel()
+ {
+ assertQueryEquals( CYPHER + "CREATE (n:Person:Swedish)",
+ new CypherQuery()
+ {{
+ creates( node("n").labels( label("Person"), label("Swedish") ) );
+ }}.toString() );
+ }
+
}
View
78 src/test/java/org/neo4j/cypherdsl/CypherReferenceTest.java
@@ -46,6 +46,7 @@
import static org.neo4j.cypherdsl.CypherQuery.isNotNull;
import static org.neo4j.cypherdsl.CypherQuery.isNull;
import static org.neo4j.cypherdsl.CypherQuery.last;
+import static org.neo4j.cypherdsl.CypherQuery.label;
import static org.neo4j.cypherdsl.CypherQuery.length;
import static org.neo4j.cypherdsl.CypherQuery.literal;
import static org.neo4j.cypherdsl.CypherQuery.lookup;
@@ -1205,4 +1206,81 @@ public void test16_23_4_4()
// returns( sign( -17 ), sign( 0.1 ) ).
// toString() );
// }
+
+ @Test
+ public void test10_1_2_Match_with_labels()
+ {
+ assertQueryEquals( CYPHER + "MATCH (charlie:Person {name:\"Charlie Sheen\"})--(movie:Movie) RETURN movie",
+ match( node( "charlie" ).label( "Person" ).values( value("name", "Charlie Sheen") ).
+ both().node( "movie" ).label( "Movie" ) ).
+ returns( identifier( "movie" ) ).
+ toString() );
+ }
+
+ @Test
+ public void test10_1_4_Match_with_properties_on_a_variable_length_path()
+ {
+ assertQueryEquals( CYPHER + "MATCH (charlie:Person {name:\"Charlie Sheen\"}),(martin:Person {name:\"Martin Sheen\"}) " +
+ "CREATE (charlie)-[:X {blocked:false}]->(:Unblocked)<-[:X {blocked:false}]-(martin) " +
+ "CREATE (charlie)-[:X {blocked:true}]->(:Blocked)<-[:X {blocked:false}]-(martin)",
+ match( node( "charlie" ).label( "Person" ).values( value("name", "Charlie Sheen") ),
+ node( "martin" ).label( "Person" ).values( value("name", "Martin Sheen") ) ).
+ create( node( "charlie" ).
+ out( "X" ).values( value( "blocked", false ) ).
+ node().label( "Unblocked" ).
+ in( "X").values( value( "blocked", false) ).
+ node( "martin" ) ).
+ create( node( "charlie" ).
+ out( "X" ).values( value( "blocked", true ) ).
+ node().label( "Blocked" ).
+ in( "X" ).values( value( "blocked", false ) ).
+ node( "martin" ) ).
+ toString() );
+ }
+
+ @Test
+ public void test11_1_1_Create_a_node_with_a_label()
+ {
+ assertQueryEquals( CYPHER + "CREATE (n:Person)",
+ create( node( "n" ).label( "Person" ) ).toString() );
+ }
+
+ @Test
+ public void test11_1_1_Create_a_node_with_multiple_labels()
+ {
+ assertQueryEquals( CYPHER + "CREATE (n:Person:Swedish)",
+ create( node( "n" ).labels( label("Person"), label("Swedish") ) ).toString() );
+ }
+
+ @Test
+ public void test11_1_1_Create_node_and_add_labels_and_properties()
+ {
+ assertQueryEquals( CYPHER + "CREATE (n:Person {name:\"Andres\",title:\"Developer\"})",
+ create( node( "n" ).label("Person" ).values( value( "name", "Andres" ), value( "title", "Developer" ) ) ).toString() );
+ }
+
+ @Test
+ public void test10_1_2_Get_all_nodes_with_a_label()
+ {
+ assertQueryEquals( CYPHER + "MATCH (movie:Movie) RETURN movie",
+ match( node( "movie" ).label( "Movie" ) ).
+ returns( identifier( "movie" ) ).
+ toString() );
+ }
+
+ @Test
+ public void test11_1_2_Create_a_relationship_between_two_nodes()
+ {
+ assertQueryEquals( CYPHER + "MATCH (a:Person),(b:Person) " +
+ "WHERE a.name=\"Node A\" and b.name=\"Node B\" " +
+ "CREATE (a)-[r:RELTYPE]->(b) " +
+ "RETURN r",
+ match( node( "a" ).label( "Person" ), node( "b" ).label( "Person" ) ).
+ where( identifier( "a" ).string( "name" ).eq( "Node A" ).
+ and( identifier( "b" ).string( "name" ).eq( "Node B" ) ) ).
+ create( node( "a" ).out( "RELTYPE" ).as( "r" ).node( "b" ) ).
+ returns( identifier( "r" ) ).
+ toString() );
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.