Skip to content

Commit

Permalink
Fixed foreach grammar to allow for several statements per foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
rickardoberg committed Jun 25, 2012
1 parent c4974ae commit bf6571d
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 105 deletions.
27 changes: 15 additions & 12 deletions src/main/java/org/neo4j/cypherdsl/CypherQuery.java
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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 ----------------------------------------------------
/**
Expand Down Expand Up @@ -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 --------------------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/neo4j/cypherdsl/ForEach.java
Expand Up @@ -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);
}
84 changes: 0 additions & 84 deletions src/main/java/org/neo4j/cypherdsl/ForEachExpression.java

This file was deleted.

38 changes: 38 additions & 0 deletions src/main/java/org/neo4j/cypherdsl/ForEachStatements.java
@@ -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 );
}
54 changes: 49 additions & 5 deletions src/main/java/org/neo4j/cypherdsl/query/ForEachClause.java
@@ -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.
Expand All @@ -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
Expand All @@ -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( ')' );
}
}
78 changes: 78 additions & 0 deletions src/main/java/org/neo4j/cypherdsl/query/ForEachStatement.java
@@ -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;
}
}
5 changes: 4 additions & 1 deletion src/test/java/org/neo4j/cypherdsl/CypherReferenceTest.java
Expand Up @@ -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
Expand Down

0 comments on commit bf6571d

Please sign in to comment.