Permalink
Browse files

intermediate

  • Loading branch information...
systay committed May 31, 2012
1 parent 46cfa2a commit a3857e825393a881cb4d87563ad5aca113cdf67f
@@ -35,7 +35,7 @@ private Result( Continuation continuation, Object returnValue )
public static Result INPUT_COMPLETE( Object returnValue )
{
- return new Result( Continuation.INPUT_COMPLETE, null );
+ return new Result( Continuation.INPUT_COMPLETE, returnValue );
}
public static Result EXIT = new Result( Continuation.EXIT, null );
@@ -42,7 +42,7 @@ public void setInterpreter(SessionInterpreter interpreter)
this.interpreter = interpreter;
}
- public Object interpret( String line, Output out ) throws Exception
+ public Result interpret( String line, Output out ) throws Exception
{
return interpreter.execute( this, line, out );
}
@@ -19,15 +19,30 @@
*/
package org.neo4j.shell.impl;
-import org.neo4j.helpers.Service;
-import org.neo4j.shell.*;
-import org.neo4j.shell.apps.Alias;
-
import java.io.Serializable;
import java.rmi.RemoteException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
import java.util.regex.Pattern;
+import org.neo4j.helpers.Service;
+import org.neo4j.shell.App;
+import org.neo4j.shell.AppCommandParser;
+import org.neo4j.shell.AppShellServer;
+import org.neo4j.shell.Continuation;
+import org.neo4j.shell.Output;
+import org.neo4j.shell.Response;
+import org.neo4j.shell.Result;
+import org.neo4j.shell.Session;
+import org.neo4j.shell.ShellException;
+import org.neo4j.shell.TabCompletion;
+import org.neo4j.shell.TextUtil;
+import org.neo4j.shell.apps.Alias;
+
/**
* A common implementation of an {@link AppShellServer}. The server can be given
* one or more java packages f.ex. "org.neo4j.shell.apps" where some
@@ -99,7 +114,6 @@ public Response interpretLine( Serializable clientId, String line, Output out )
throws ShellException
{
Session session = getClientSession( clientId );
- session.setInterpreter( this );
if ( line == null || line.trim().length() == 0 )
return new Response( getPrompt( session ), Continuation.INPUT_COMPLETE );
@@ -114,7 +128,7 @@ public Response interpretLine( Serializable clientId, String line, Output out )
}
}
- public Result execute(Session session, String line, Output out) throws Exception
+ public Result execute( Session session, String line, Output out ) throws Exception
{
Result commandResult = null;
for ( String command : line.split( Pattern.quote( "&&" ) ) )
@@ -152,6 +166,14 @@ protected String replaceAlias( String line, Session session )
return apps.keySet().toArray( new String[apps.size()] );
}
+ @Override
+ protected Session newSession( Serializable id, Map<String, Serializable> initialSession )
+ {
+ Session session = super.newSession( id, initialSession );
+ session.setInterpreter( this );
+ return session;
+ }
+
@Override
public TabCompletion tabComplete( Serializable clientID, String partOfLine )
throws ShellException, RemoteException
@@ -99,7 +99,7 @@ public Welcome welcome( Map<String, Serializable> initialSession ) throws Remote
}
}
- private Session newSession( Serializable id, Map<String, Serializable> initialSession )
+ protected Session newSession( Serializable id, Map<String, Serializable> initialSession )
{
Session session = new Session( id );
initialPopulateSession( session );
@@ -73,6 +73,6 @@ protected Result exec( AppCommandParser parser, Session session, Output out ) th
{
out.println( result.toString() );
}
- return Result.INPUT_COMPLETE;
+ return Result.INPUT_COMPLETE( result );
}
}
@@ -0,0 +1,72 @@
+package org.neo4j.shell.kernel.apps;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.neo4j.cypher.javacompat.ExecutionResult;
+import org.neo4j.shell.AppCommandParser;
+import org.neo4j.shell.Continuation;
+import org.neo4j.shell.Output;
+import org.neo4j.shell.Result;
+import org.neo4j.shell.Session;
+
+public class Let extends ReadOnlyGraphDatabaseApp
+{
+ Pattern p = Pattern.compile( "^([a-zA-Z0-9_])*[ \\\\t]*=[ \\\\t]*(.*)" );
+
+ @Override
+ protected Result exec( AppCommandParser parser, Session session, Output out ) throws Exception
+ {
+ String lineWithoutApp = parser.getLineWithoutApp();
+ Matcher m = p.matcher( lineWithoutApp );
+ if ( !m.matches() )
+ {
+ out.println( "LET xyz = START...RETURN x;" );
+ return Result.INPUT_COMPLETE;
+ }
+
+ String variable = m.group( 1 ).trim();
+ String cmd = m.group( 2 ).trim();
+
+ Result result = session.interpret( cmd, out );
+
+ if ( result.getContinuation() == Continuation.INPUT_COMPLETE )
+ {
+ Map<String, Object> params = getOrCreateCypherParams( session );
+ Object returnValue = result.getReturnValue();
+ if( returnValue == null)
+ {
+ params.remove( variable );
+ }
+ else if ( returnValue instanceof ExecutionResult )
+ {
+ ExecutionResult executionResult = (ExecutionResult) returnValue;
+ if(executionResult.columns().size()>2)
+ {
+ out.println( "Can only assign a single column Cypher result to a variable" );
+ return Result.INPUT_COMPLETE;
+ }
+ } else {
+ params.put( variable, returnValue );
+ }
+ }
+
+ return result;
+ }
+
+ private Map<String,Object> getOrCreateCypherParams( Session session )
+ {
+ final String CYPHER_PARAMS_KEY = "CYPHER_PARAMS";
+ if(session.get( CYPHER_PARAMS_KEY ) == null) {
+ Map<String,Object> params = new HashMap<String, Object>( );
+ session.set( CYPHER_PARAMS_KEY, params );
+ return params;
+ }
+ else
+ {
+ return (Map<String, Object>) session.get( CYPHER_PARAMS_KEY );
+ }
+ }
+}
@@ -61,6 +61,6 @@ protected Result exec( AppCommandParser parser, Session session, Output out ) th
{
out.println( "Node " + getDisplayName( getServer(), session, node, false ) + " created" );
}
- return Result.INPUT_COMPLETE;
+ return Result.INPUT_COMPLETE( node );
}
}
@@ -107,7 +107,7 @@ protected Result exec(AppCommandParser parser, Session session, Output out)
}
if ( parser.options().containsKey( "cd" ) ) cdTo( session, node );
- return Result.INPUT_COMPLETE;
+ return Result.INPUT_COMPLETE( relationship );
}
private Node getOrCreateNode( AppCommandParser parser, Session session, boolean createNode, boolean suppliedNode ) throws ShellException
@@ -66,12 +66,12 @@ protected Result exec( AppCommandParser parser, Session session, Output out )
{
ExecutionResult result = engine.execute( queryWithoutSemicolon, getParameters( session ) );
out.println( result.toString() );
+ return Result.INPUT_COMPLETE( result );
}
catch ( CypherException e )
{
throw ShellException.wrapCause( e );
}
- return Result.INPUT_COMPLETE;
}
else
{
@@ -8,6 +8,7 @@ org.neo4j.shell.kernel.apps.Cd
org.neo4j.shell.kernel.apps.Commit
org.neo4j.shell.kernel.apps.Create
org.neo4j.shell.kernel.apps.Cypher
+org.neo4j.shell.kernel.apps.Let
org.neo4j.shell.kernel.apps.Ls
org.neo4j.shell.kernel.apps.Mknode
org.neo4j.shell.kernel.apps.Mkrel
@@ -401,4 +401,10 @@ public void canSetInitialSessionVariables() throws Exception
}
executeCommand( client, "env", allStrings );
}
+
+ @Test
+ public void testLet() throws Exception
+ {
+ executeCommand( "let x = start n=node(0) return n,n;", "WUT" );
+ }
}

0 comments on commit a3857e8

Please sign in to comment.