Permalink
Browse files

Added Neo4j start() helper method

Added first cut at Projection support
  • Loading branch information...
Rickard Öberg
Rickard Öberg committed Nov 4, 2011
1 parent 57a3e90 commit e1e37c29aeb59f3bfc040527238f2e86a596c37d
@@ -0,0 +1,9 @@
+<component name="CopyrightManager">
+ <copyright>
+ <option name="notice" value="Copyright (c) 2002-2011 &quot;Neo Technology,&quot;&#10;Network Engine for Objects in Lund AB [http://neotechnology.com]&#10;&#10;This file is part of Neo4j.&#10;&#10;Neo4j is free software: you can redistribute it and/or modify&#10;it under the terms of the GNU General Public License as published by&#10;the Free Software Foundation, either version 3 of the License, or&#10;(at your option) any later version.&#10;&#10;This program is distributed in the hope that it will be useful,&#10;but WITHOUT ANY WARRANTY; without even the implied warranty of&#10;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the&#10;GNU General Public License for more details.&#10;&#10;You should have received a copy of the GNU General Public License&#10;along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;." />
+ <option name="keyword" value="Copyright" />
+ <option name="allowReplaceKeyword" value="" />
+ <option name="myName" value="Neo4j" />
+ <option name="myLocal" value="true" />
+ </copyright>
+</component>
View
12 pom.xml
@@ -66,24 +66,24 @@
<version>2.2.3</version>
<scope>provided</scope>
</dependency>
-
<dependency>
<groupId>org.neo4j</groupId>
- <artifactId>neo4j-cypher</artifactId>
+ <artifactId>neo4j-kernel</artifactId>
<version>1.5-SNAPSHOT</version>
- <scope>test</scope>
+ <type>jar</type>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
- <artifactId>neo4j-kernel</artifactId>
+ <artifactId>neo4j-cypher</artifactId>
<version>1.5-SNAPSHOT</version>
- <type>test-jar</type>
- <scope>test</scope>
+ <optional>true</optional>
+
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>1.5-SNAPSHOT</version>
+ <type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
@@ -84,7 +84,7 @@ public Match starts( StartExpression... startExpression )
return new Grammar();
}
- protected StartExpression.StartNodes node( String name, int... id )
+ protected StartExpression.StartNodes node( String name, long... id )
{
return StartExpression.node( name, id );
}
@@ -109,7 +109,7 @@ public Match starts( StartExpression... startExpression )
return StartExpression.query( name, indexName, query );
}
- protected StartExpression.StartRelationships relationship( String name, int... id )
+ protected StartExpression.StartRelationships relationship( String name, long... id )
{
return StartExpression.relationship( name, id );
}
@@ -73,7 +73,7 @@ public static Identifier identifier( String name )
return parameters;
}
- public static Literal[] literals( int[] values )
+ public static Literal[] literals( long[] values )
{
Literal[] literals = new Literal[values.length];
for( int i = 0; i < values.length; i++ )
@@ -30,11 +30,11 @@
extends Expression
implements AsString, Serializable,Cloneable
{
- public static StartNodes node( String name, int... id )
+ public static StartNodes node( String name, long... id )
{
checkEmpty( name, "Name" );
- for( int i : id )
+ for( long i : id )
{
if (i < 0)
throw new IllegalArgumentException( "Id may not be below zero" );
@@ -91,11 +91,11 @@ public static StartNodesQuery query( String name, String indexName, String query
return startNodesQuery;
}
- public static StartRelationships relationship( String name, int... id )
+ public static StartRelationships relationship( String name, long... id )
{
checkEmpty( name, "Name" );
- for( int i : id )
+ for( long i : id )
{
if (i < 0)
throw new IllegalArgumentException( "Id may not be below zero" );
@@ -0,0 +1,57 @@
+/**
+ * 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.neo4j;
+
+import org.neo4j.cypherdsl.query.Query;
+import org.neo4j.cypherdsl.query.StartExpression;
+import org.neo4j.graphdb.Node;
+
+import static org.neo4j.cypherdsl.query.Query.checkEmpty;
+
+/**
+ * TODO
+ */
+public abstract class StartExpressionNeo
+ extends StartExpression
+{
+ public static StartNodes node( String name, Node... nodes )
+ {
+ checkEmpty( name, "Name" );
+
+ for( Node node : nodes )
+ {
+ Query.checkNull( node, "Node" );
+ }
+
+ long[] ids = new long[nodes.length];
+ for( int i = 0; i < nodes.length; i++ )
+ {
+ Node node = nodes[ i ];
+ ids[i] = node.getId();
+ }
+
+ StartExpressionNeo.StartNodes startNodes = new StartExpressionNeo.StartNodes();
+ startNodes.name = name;
+ startNodes.nodes = literals( ids );
+ return startNodes;
+ }
+
+}
@@ -0,0 +1,79 @@
+/**
+ * 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.querydsl;
+
+import com.mysema.query.types.Expression;
+import com.mysema.query.types.Projections;
+import com.mysema.query.types.QBean;
+import com.mysema.query.types.path.PathBuilder;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.neo4j.cypher.javacompat.ExecutionEngine;
+import org.neo4j.cypher.javacompat.ExecutionResult;
+import org.neo4j.cypherdsl.Execute;
+
+/**
+ * TODO
+ */
+public class Projection
+{
+ private ExecutionEngine engine;
+
+ public Projection( ExecutionEngine engine )
+ {
+ this.engine = engine;
+ }
+
+ public <T> Iterable<T> iterable(Execute query, Class<T> targetClass)
+ {
+ return iterable( query, Collections.<String,Object>emptyMap(), targetClass );
+ }
+
+ public <T> Iterable<T> iterable(Execute query, Map<String, Object> parameters, Class<T> targetClass)
+ {
+ PathBuilder<T> friend = new PathBuilder<T>( targetClass, "entity" );
+ Field[] fields = targetClass.getFields();
+ Expression[] fieldExpressions = new Expression[fields.length];
+ for( int i = 0; i < fields.length; i++ )
+ {
+ fieldExpressions[i] = friend.getString( fields[ i ].getName() );
+ }
+
+ QBean<T> bean = Projections.fields( targetClass, fieldExpressions );
+ ExecutionResult result = engine.execute( query.toString() , parameters );
+ List<T> friends = new ArrayList<T>( );
+ for( Map<String, Object> stringObjectMap : result )
+ {
+ Object[] args = new Object[stringObjectMap.size()];
+ int idx = 0;
+ for( Object object : stringObjectMap.values() )
+ {
+ args[idx++] = object;
+ }
+ friends.add( bean.newInstance( args ) );
+ }
+
+ return friends;
+ }
+}
@@ -24,19 +24,21 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.neo4j.cypher.commands.Query;
-import org.neo4j.cypher.javacompat.CypherParser;
import org.neo4j.cypher.javacompat.ExecutionEngine;
+import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.cypherdsl.query.StartExpression;
+import org.neo4j.cypherdsl.querydsl.Projection;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.test.GraphDescription;
import org.neo4j.test.GraphHolder;
import org.neo4j.test.ImpermanentGraphDatabase;
import org.neo4j.test.TestData;
+import static org.neo4j.cypherdsl.CypherQuery.*;
import static org.neo4j.cypherdsl.query.MatchExpression.*;
import static org.neo4j.cypherdsl.query.ReturnExpression.*;
+import static org.neo4j.cypherdsl.query.neo4j.StartExpressionNeo.*;
/**
* Set up a query using the CypherQuery builder, and then use it to execute a query to a test database.
@@ -50,7 +52,6 @@
private ImpermanentGraphDatabase graphdb;
private ExecutionEngine engine;
- private CypherParser parser;
@Test
@GraphDescription.Graph( value = {
@@ -62,15 +63,40 @@ public void testCypherExecution()
{
data.get();
- String query = CypherQuery.start( StartExpression.lookup( "john", "node_auto_index", "name", "John" ) )
+ String query = start( StartExpression.lookup( "john", "node_auto_index", "name", "John" ) )
.match( path().from( "john" ).out( "friend" )
.link().out( "friend" ).to( "fof" ))
- .returns( properties( "john.name", "fof.name" ) )
+ .returns( properties( "john.name", "fof.name", "john" ) )
.toString();
System.out.println(query);
- Query parsedQuery = parser.parse( query );
- System.out.println( engine.execute( parsedQuery ).toString() );
+ ExecutionResult result = engine.execute( query );
+ Node john = null;
+ for( Map<String, Object> stringObjectMap : result )
+ {
+ john = ((Node)stringObjectMap.get( "john" ));
+ }
+ System.out.println( result.toString() );
+
+ {
+ Execute q = start( node( "john", john ) )
+ .match( path().from( "john" ).out( "friend" )
+ .link().out( "friend" ).to( "fof" ) )
+ .returns( properties( "john.name", "fof.name" ) );
+
+ System.out.println(q);
+ System.out.println( engine.execute( q.toString() ).toString() );
+ }
+
+ {
+ Projection projection = new Projection(engine);
+
+ Iterable<Friend> friends = projection.iterable( start( node( "john", john ) )
+ .match( path().from( "john" ).out( "friend" )
+ .link().out( "friend" ).to( "fof" ) )
+ .returns( properties( "john.name", "fof.name" ) ), Friend.class );
+ System.out.println( friends );
+ }
}
@Before
@@ -80,7 +106,6 @@ public void setup()
graphdb = new ImpermanentGraphDatabase();
graphdb.cleanContent( false );
- parser = new CypherParser();
engine = new ExecutionEngine( graphdb );
}
@@ -89,4 +114,16 @@ public GraphDatabaseService graphdb()
{
return graphdb;
}
+
+ public static class Friend
+ {
+ public String name;
+ public String friend;
+
+ @Override
+ public String toString()
+ {
+ return name+" is friend with "+friend;
+ }
+ }
}

0 comments on commit e1e37c2

Please sign in to comment.