Permalink
Browse files

Fixed foreach grammar to allow for several statements per foreach

  • Loading branch information...
1 parent c4974ae commit bf6571deb33274ed11dc457d42699b5a0d3a9d06 @rickardoberg rickardoberg committed Jun 25, 2012
@@ -19,15 +19,17 @@
*/
package org.neo4j.cypherdsl;
-
import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
import org.neo4j.cypherdsl.query.AbstractPath;
import org.neo4j.cypherdsl.query.BinaryPredicateExpression;
import org.neo4j.cypherdsl.query.BooleanExpression;
import org.neo4j.cypherdsl.query.CreateClause;
import org.neo4j.cypherdsl.query.DeleteClause;
import org.neo4j.cypherdsl.query.Expression;
import org.neo4j.cypherdsl.query.ForEachClause;
+import org.neo4j.cypherdsl.query.ForEachStatement;
import org.neo4j.cypherdsl.query.FunctionExpression;
import org.neo4j.cypherdsl.query.Has;
import org.neo4j.cypherdsl.query.Identifier;
@@ -48,6 +50,7 @@
import org.neo4j.cypherdsl.query.Property;
import org.neo4j.cypherdsl.query.PropertyValue;
import org.neo4j.cypherdsl.query.Query;
+import org.neo4j.cypherdsl.query.Regexp;
import org.neo4j.cypherdsl.query.RelateClause;
import org.neo4j.cypherdsl.query.ReturnClause;
import org.neo4j.cypherdsl.query.ReturnExpression;
@@ -59,16 +62,11 @@
import org.neo4j.cypherdsl.query.StringProperty;
import org.neo4j.cypherdsl.query.WhereClause;
import org.neo4j.cypherdsl.query.WhereExpression;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import org.neo4j.cypherdsl.query.WithClause;
-import static org.neo4j.cypherdsl.query.FunctionExpression.Extract;
+import static org.neo4j.cypherdsl.query.FunctionExpression.*;
import static org.neo4j.cypherdsl.query.Query.*;
-import org.neo4j.cypherdsl.query.Regexp;
-import org.neo4j.cypherdsl.query.WithClause;
-
/**
* DSL for creating Cypher queries. Once created you can serialize to a string,
* or retrieve the internal Query model for further processing.
@@ -1139,6 +1137,11 @@ public static OrderByExpression order( Expression expression, Order order )
return orderBy;
}
+ // For each -----------------------------------------------------
+ public static ForEachStatements in(Identifier id, Expression in)
+ {
+ return new ForEachClause( id, in );
+ }
// Functions ----------------------------------------------------
/**
@@ -1695,12 +1698,12 @@ public UpdateNext relate( AbstractPath<?>... expressions )
}
// For each -----------------------------------------------------
+
@Override
- public ForEachExpression forEach( Identifier id, Expression in )
+ public UpdateNext forEach( ForEachStatement statement )
{
- ForEachClause clause = new ForEachClause( id, in );
- query.add(clause);
- return new ForEachExpression(clause, this);
+ query.add( statement.getClause() );
+ return this;
}
// Start --------------------------------------------------------
@@ -20,13 +20,12 @@
package org.neo4j.cypherdsl;
-import org.neo4j.cypherdsl.query.Expression;
-import org.neo4j.cypherdsl.query.Identifier;
+import org.neo4j.cypherdsl.query.ForEachStatement;
/**
* TODO
*/
public interface ForEach
{
- ForEachExpression forEach(Identifier id, Expression in);
+ UpdateNext forEach(ForEachStatement statement);
}
@@ -1,84 +0,0 @@
-/**
- * Copyright (c) 2002-2012 "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;
-
-import java.util.Arrays;
-import org.neo4j.cypherdsl.query.AbstractPath;
-import org.neo4j.cypherdsl.query.CreateClause;
-import org.neo4j.cypherdsl.query.DeleteClause;
-import org.neo4j.cypherdsl.query.Expression;
-import org.neo4j.cypherdsl.query.ForEachClause;
-import org.neo4j.cypherdsl.query.Identifier;
-import org.neo4j.cypherdsl.query.RelateClause;
-import org.neo4j.cypherdsl.query.SetClause;
-import org.neo4j.cypherdsl.query.SetProperty;
-
-/**
- * TODO
- */
-public class ForEachExpression
- implements Create, Set, Delete, Relate, ForEach
-{
- private UpdateNext updateNext;
- private ForEachClause clause;
-
- public ForEachExpression( ForEachClause clause, UpdateNext updateNext)
- {
- this.clause = clause;
- this.updateNext = updateNext;
- }
-
- @Override
- public UpdateNext create( AbstractPath<?>... paths )
- {
- clause.execute( new CreateClause( Arrays.asList( paths ) ) );
- return updateNext;
- }
-
- @Override
- public UpdateNext set( SetProperty... propertyValues )
- {
- clause.execute( new SetClause( Arrays.asList( propertyValues ) ) );
- return updateNext;
- }
-
- @Override
- public UpdateNext delete( Expression... expressions )
- {
- clause.execute( new DeleteClause( Arrays.asList( expressions ) ) );
- return updateNext;
- }
-
- @Override
- public UpdateNext relate( AbstractPath<?>... expressions )
- {
- clause.execute( new RelateClause( Arrays.asList( expressions ) ) );
- return updateNext;
- }
-
- @Override
- public ForEachExpression forEach( Identifier id, Expression in )
- {
- ForEachClause nextForEach = new ForEachClause( id, in );
- clause.execute( nextForEach );
- return new ForEachExpression(nextForEach, updateNext);
- }
-}
@@ -0,0 +1,38 @@
+/**
+ * Copyright (c) 2002-2011 "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;
+
+import org.neo4j.cypherdsl.query.AbstractPath;
+import org.neo4j.cypherdsl.query.Expression;
+import org.neo4j.cypherdsl.query.ForEachStatement;
+import org.neo4j.cypherdsl.query.SetProperty;
+
+/**
+ * TODO
+ */
+public interface ForEachStatements
+{
+ ForEachStatement create(AbstractPath<?>... paths);
+ ForEachStatement set( SetProperty... propertyValues );
+ ForEachStatement delete( Expression... expressions );
+ ForEachStatement relate( AbstractPath<?>... expressions );
+ ForEachStatement forEach( ForEachStatement statement );
+}
@@ -1,5 +1,5 @@
/**
- * Copyright (c) 2002-2012 "Neo Technology,"
+ * Copyright (c) 2002-2011 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
@@ -20,25 +20,61 @@
package org.neo4j.cypherdsl.query;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.neo4j.cypherdsl.ForEachStatements;
+
/**
* TODO
*/
public class ForEachClause
extends Clause
+ implements AsString, ForEachStatements
{
private final Identifier id;
private final Expression in;
- private Clause clause;
+ private final List<AsString> forEachStatements = new ArrayList<AsString>( );
public ForEachClause( Identifier id, Expression in )
{
this.id = id;
this.in = in;
}
- public void execute(Clause clause)
+ public ForEachStatement create(AbstractPath<?>... paths)
+ {
+ return new ForEachStatement(add( new CreateClause( Arrays.asList(paths) ) ) );
+ }
+
+ @Override
+ public ForEachStatement set( SetProperty... setProperties )
+ {
+ return new ForEachStatement(add( new SetClause( Arrays.asList(setProperties) ) ) );
+ }
+
+ @Override
+ public ForEachStatement delete( Expression... expressions )
+ {
+ return new ForEachStatement(add( new DeleteClause( Arrays.asList(expressions) ) ) );
+ }
+
+ @Override
+ public ForEachStatement relate( AbstractPath<?>... expressions )
{
- this.clause = clause;
+ return new ForEachStatement(add( new RelateClause( Arrays.asList(expressions) ) ) );
+ }
+
+ @Override
+ public ForEachStatement forEach( ForEachStatement statement )
+ {
+ return new ForEachStatement(add( statement ) );
+ }
+
+ ForEachClause add(AsString clause)
+ {
+ forEachStatements.add( clause );
+ return this;
}
@Override
@@ -49,7 +85,15 @@ public void asString( StringBuilder builder )
builder.append( " in " );
in.asString( builder );
builder.append( ":" );
- clause.asString( builder );
+
+ String comma = "";
+ for( AsString forEachStatement : forEachStatements )
+ {
+ builder.append( comma );
+ forEachStatement.asString( builder );
+ comma = ",";
+ }
+
builder.append( ')' );
}
}
@@ -0,0 +1,78 @@
+/**
+ * Copyright (c) 2002-2011 "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;
+
+import java.util.Arrays;
+import org.neo4j.cypherdsl.ForEachStatements;
+
+/**
+ * TODO
+ */
+public class ForEachStatement
+ implements ForEachStatements, AsString
+{
+ private ForEachClause forEachClause;
+
+ public ForEachStatement( ForEachClause forEachClause )
+ {
+ this.forEachClause = forEachClause;
+ }
+
+ public ForEachStatement create(AbstractPath<?>... paths)
+ {
+ return new ForEachStatement( forEachClause.add(new CreateClause( Arrays.asList( paths ) )));
+ }
+
+ @Override
+ public ForEachStatement set( SetProperty... setProperties )
+ {
+ return new ForEachStatement( forEachClause.add(new SetClause( Arrays.asList( setProperties ) )));
+ }
+
+ @Override
+ public ForEachStatement delete( Expression... expressions )
+ {
+ return new ForEachStatement( forEachClause.add(new DeleteClause( Arrays.asList( expressions ) )));
+ }
+
+ @Override
+ public ForEachStatement relate( AbstractPath<?>... expressions )
+ {
+ return new ForEachStatement( forEachClause.add(new RelateClause( Arrays.asList( expressions ) )));
+ }
+
+ @Override
+ public ForEachStatement forEach( ForEachStatement statement )
+ {
+ return new ForEachStatement( forEachClause.add(statement.getClause()));
+ }
+
+ @Override
+ public void asString( StringBuilder builder )
+ {
+ forEachClause.asString( builder );
+ }
+
+ public Clause getClause()
+ {
+ return forEachClause;
+ }
+}
@@ -934,7 +934,10 @@ public void test16_21_5()
public void test16_22_1()
{
assertEquals( CYPHER+"START begin=node(2),end=node(1) MATCH p=(begin)-->(end) FOREACH(n in nodes(p): SET n.marked=true)",
- start( node( "begin", 2 ), node( "end", 1 ) ).match( path( "p" ).from( "begin" ).out( ).to("end" )).forEach(identifier("n"), nodes( identifier( "p" ) )).set(property( identifier( "n" ).property( "marked" ), literal( true )) ).toString());
+ start( node( "begin", 2 ), node( "end", 1 ) ).
+ match( path( "p" ).from( "begin" ).out( ).to("end" )).
+ forEach(in( identifier("n"), nodes( identifier( "p" ) )).
+ set(property( identifier( "n" ).property( "marked" ), literal( true )) )).toString());
}
// Cookbook

0 comments on commit bf6571d

Please sign in to comment.