
org.neo4j.shell.kernel.apps.cypher.Start Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-shell Show documentation
Show all versions of neo4j-shell Show documentation
A generic command shell with a client and server part.
/*
* Copyright (c) 2002-2016 "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 .
*/
package org.neo4j.shell.kernel.apps.cypher;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.text.MessageFormat;
import java.util.Map;
import org.neo4j.graphdb.Result;
import org.neo4j.helpers.Service;
import org.neo4j.kernel.GraphDatabaseQueryService;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.security.AccessMode;
import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.kernel.impl.coreapi.PropertyContainerLocker;
import org.neo4j.kernel.impl.query.Neo4jTransactionalContext;
import org.neo4j.kernel.impl.query.QueryExecutionEngine;
import org.neo4j.kernel.impl.query.QueryExecutionKernelException;
import org.neo4j.kernel.impl.query.QuerySession;
import org.neo4j.kernel.impl.query.TransactionalContext;
import org.neo4j.shell.App;
import org.neo4j.shell.AppCommandParser;
import org.neo4j.shell.Continuation;
import org.neo4j.shell.Output;
import org.neo4j.shell.OutputAsWriter;
import org.neo4j.shell.Session;
import org.neo4j.shell.ShellException;
import org.neo4j.shell.kernel.apps.NodeOrRelationship;
import org.neo4j.shell.kernel.apps.TransactionProvidingApp;
@Service.Implementation( App.class )
public class Start extends TransactionProvidingApp
{
private QueryExecutionEngine engine;
@Override
public String getDescription()
{
String className = this.getClass().getSimpleName().toUpperCase();
return MessageFormat.format( "Executes a Cypher query. Usage: {0} ;\nExample: MATCH " +
"(me)-[:KNOWS]->(you) RETURN you.name;\nwhere '{'self'}' will be replaced with the current location in " +
"the graph.Please, note that the query must end with a semicolon. Other parameters are\ntaken from " +
"shell variables, see ''help export''.", className );
}
@Override
public Continuation execute( AppCommandParser parser, Session session, Output out ) throws Exception
{
String query = parser.getLine().trim();
if ( isComplete( query ) )
{
if ( getEngine().isPeriodicCommit( query ) )
{
return exec( parser, session, out );
}
else
{
return super.execute( parser, session, out );
}
}
else
{
return Continuation.INPUT_INCOMPLETE;
}
}
@Override
protected Continuation exec( AppCommandParser parser, Session session, Output out )
throws ShellException, RemoteException
{
String query = parser.getLine().trim();
if ( isComplete( query ) )
{
final long startTime = System.currentTimeMillis();
try
{
Result result = getResult( trimQuery( query ), session );
handleResult( out, result, startTime );
}
catch ( QueryExecutionKernelException e )
{
handleException( out, e, startTime );
return Continuation.EXCEPTION_CAUGHT;
}
return Continuation.INPUT_COMPLETE;
}
else
{
return Continuation.INPUT_INCOMPLETE;
}
}
private Result getResult( String query, Session session )
throws ShellException, RemoteException, QueryExecutionKernelException
{
return getEngine().executeQuery( query, getParameters( session ), shellSession( session ) );
}
private String trimQuery( String query )
{
return query.substring( 0, query.lastIndexOf( ";" ) );
}
protected void handleResult( Output out, Result result, long startTime )
throws RemoteException, ShellException
{
printResult( out, result, startTime );
}
private void printResult( Output out, Result result, long startTime ) throws RemoteException
{
result.writeAsStringTo( new PrintWriter( new OutputAsWriter( out ) ) );
out.println( (now() - startTime) + " ms" );
if ( result.getQueryExecutionType().requestedExecutionPlanDescription() )
{
out.println();
out.println( result.getExecutionPlanDescription().toString() );
}
}
private void handleException( Output out, QueryExecutionKernelException exception, long startTime )
throws RemoteException
{
out.println( (now() - startTime) + " ms" );
out.println();
out.println( "WARNING: " + exception.getMessage() );
}
private Map getParameters( Session session ) throws ShellException
{
try
{
NodeOrRelationship self = getCurrent( session );
session.set( "self", self.isNode() ? self.asNode() : self.asRelationship() );
}
catch ( ShellException e )
{ // OK, current didn't exist
}
return session.asMap();
}
private boolean isComplete( String query )
{
return query.endsWith( ";" );
}
protected QueryExecutionEngine getEngine()
{
if ( this.engine == null )
{
this.engine = getServer().getDb().getDependencyResolver().resolveDependency( QueryExecutionEngine.class );
}
return this.engine;
}
protected long now()
{
return System.currentTimeMillis();
}
private QuerySession shellSession( Session session )
{
GraphDatabaseQueryService graph = this.engine.queryService();
InternalTransaction transaction = graph.beginTransaction( KernelTransaction.Type.implicit, AccessMode.Static.FULL );
Statement statement =
graph.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class ).get();
Neo4jTransactionalContext context =
new Neo4jTransactionalContext( graph, transaction, statement, new PropertyContainerLocker() );
return new ShellQuerySession( session, context );
}
private static class ShellQuerySession extends QuerySession
{
private final Session session;
ShellQuerySession( Session session, TransactionalContext transactionalContext )
{
super( transactionalContext );
this.session = session;
}
@Override
public String toString()
{
return String.format( "shell-session\tshell\t%s", session.getId() );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy