Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 78da61f

Browse files
committed
Support database selection with 2.x driver
- Add :use command for selecting active database - Add -d, --database command line parameter - Upgrade driver dependency to 2.0.0-alpha01
1 parent a38a6b1 commit 78da61f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+491
-319
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ ext {
7272
argparse4jVersion = '0.7.0'
7373
junitVersion = '4.12'
7474
evaluatorVersion = '3.5.4'
75-
neo4jJavaDriverVersion = '1.7.0'
75+
neo4jJavaDriverVersion = '2.0.0-alpha01'
7676
findbugsVersion = '3.0.0'
7777
jansiVersion = '1.13'
7878
jlineVersion = '2.14.6'

cypher-shell/src/integration-test/java/org/neo4j/shell/MainIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void connectInteractivelyPromptsOnWrongAuthentication() throws Exception
4040
cliArgs.getPort(),
4141
cliArgs.getUsername(),
4242
cliArgs.getPassword(),
43-
cliArgs.getEncryption());
43+
cliArgs.getEncryption(),
44+
cliArgs.getDatabase());
4445

4546
CypherShell shell = new CypherShell(logger, prettyConfig);
4647

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellFailureIntegrationTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
import org.junit.Rule;
66
import org.junit.Test;
77
import org.junit.rules.ExpectedException;
8-
import org.neo4j.driver.v1.exceptions.AuthenticationException;
8+
9+
import org.neo4j.driver.exceptions.AuthenticationException;
910
import org.neo4j.shell.ConnectionConfig;
1011
import org.neo4j.shell.CypherShell;
1112
import org.neo4j.shell.StringLinePrinter;
1213
import org.neo4j.shell.cli.Format;
1314
import org.neo4j.shell.exception.CommandException;
1415
import org.neo4j.shell.prettyprint.PrettyConfig;
1516

17+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
18+
1619
public class CypherShellFailureIntegrationTest {
1720
@Rule
1821
public final ExpectedException thrown = ExpectedException.none();
@@ -28,9 +31,20 @@ public void setUp() throws Exception {
2831

2932
@Test
3033
public void cypherWithNoPasswordShouldReturnValidError() throws CommandException {
31-
thrown.expect( AuthenticationException.class );
34+
thrown.expect(AuthenticationException.class);
3235
thrown.expectMessage("The client is unauthorized due to authentication failure.");
3336

34-
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "", true));
37+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "", true, ABSENT_DB_NAME));
38+
}
39+
40+
@Test
41+
public void switchingDatabaseInOpenTransactionShouldFail() throws CommandException {
42+
thrown.expect(CommandException.class);
43+
thrown.expectMessage("There is an open transaction.");
44+
45+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "", true, ABSENT_DB_NAME));
46+
47+
shell.beginTransaction();
48+
shell.setActiveDatabase("another_database");
3549
}
3650
}

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellPlainIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static org.hamcrest.CoreMatchers.containsString;
1717
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
1819
import static org.neo4j.shell.prettyprint.OutputFormatter.NEWLINE;
1920

2021
public class CypherShellPlainIntegrationTest {
@@ -28,7 +29,7 @@ public class CypherShellPlainIntegrationTest {
2829
public void setUp() throws Exception {
2930
linePrinter.clear();
3031
shell = new CypherShell(linePrinter, new PrettyConfig(Format.PLAIN, true, 1000));
31-
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true));
32+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true, ABSENT_DB_NAME));
3233
}
3334

3435
@After

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellVerboseIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.junit.Assert.assertEquals;
1818
import static org.junit.Assert.assertTrue;
1919
import static org.junit.Assume.assumeTrue;
20+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
2021
import static org.neo4j.shell.Versions.majorVersion;
2122
import static org.neo4j.shell.Versions.minorVersion;
2223

@@ -38,7 +39,7 @@ public void setUp() throws Exception {
3839
commitCommand = new Commit(shell);
3940
beginCommand = new Begin(shell);
4041

41-
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true));
42+
shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true, ABSENT_DB_NAME));
4243
}
4344

4445
@After
@@ -72,7 +73,7 @@ public void connectTwiceThrows() throws CommandException {
7273
thrown.expect(CommandException.class);
7374
thrown.expectMessage("Already connected");
7475

75-
ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true);
76+
ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true, ABSENT_DB_NAME);
7677
assertTrue("Shell should already be connected", shell.isConnected());
7778
shell.connect(config);
7879
}

cypher-shell/src/main/java/org/neo4j/shell/ConnectionConfig.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.neo4j.shell;
22

3-
import org.neo4j.driver.v1.Config;
3+
import org.neo4j.driver.Config;
44

55
import javax.annotation.Nonnull;
66

@@ -11,15 +11,18 @@ public class ConnectionConfig {
1111
private final Config.EncryptionLevel encryption;
1212
private String username;
1313
private String password;
14+
private String database;
1415

1516
public ConnectionConfig(@Nonnull String scheme, @Nonnull String host, int port,
16-
@Nonnull String username, @Nonnull String password, boolean encryption) {
17+
@Nonnull String username, @Nonnull String password, boolean encryption,
18+
@Nonnull String database) {
1719
this.host = host;
1820
this.port = port;
1921
this.username = fallbackToEnvVariable(username, "NEO4J_USERNAME");
2022
this.password = fallbackToEnvVariable(password, "NEO4J_PASSWORD");
2123
this.encryption = encryption ? Config.EncryptionLevel.REQUIRED : Config.EncryptionLevel.NONE;
2224
this.scheme = scheme;
25+
this.database = database;
2326
}
2427

2528
/**
@@ -68,6 +71,11 @@ public Config.EncryptionLevel encryption() {
6871
return encryption;
6972
}
7073

74+
@Nonnull
75+
public String database() {
76+
return database;
77+
}
78+
7179
public void setUsername(@Nonnull String username) {
7280
this.username = username;
7381
}

cypher-shell/src/main/java/org/neo4j/shell/CypherShell.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* A possibly interactive shell for evaluating cypher statements.
3030
*/
31-
public class CypherShell implements StatementExecuter, Connector, TransactionHandler, ParameterMap {
31+
public class CypherShell implements StatementExecuter, Connector, TransactionHandler, ParameterMap, DatabaseManager {
3232
// Final space to catch newline
3333
protected static final Pattern cmdNamePattern = Pattern.compile("^\\s*(?<name>[^\\s]+)\\b(?<args>.*)\\s*$");
3434
protected final Map<String, ParamValue> queryParams = new HashMap<>();
@@ -199,4 +199,15 @@ protected void addRuntimeHookToResetShell() {
199199
Runtime.getRuntime().addShutdownHook(new Thread(this::reset));
200200
}
201201

202+
@Override
203+
public void setActiveDatabase(String databaseName) throws CommandException
204+
{
205+
boltStateHandler.setActiveDatabase(databaseName);
206+
}
207+
208+
@Override
209+
public String getActiveDatabase()
210+
{
211+
return boltStateHandler.getActiveDatabase();
212+
}
202213
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.neo4j.shell;
2+
3+
import org.neo4j.shell.exception.CommandException;
4+
5+
/**
6+
* An object capable of tracking the active database.
7+
*/
8+
public interface DatabaseManager
9+
{
10+
void setActiveDatabase(String databaseName) throws CommandException;
11+
12+
String getActiveDatabase();
13+
}

cypher-shell/src/main/java/org/neo4j/shell/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.neo4j.shell;
22

33
import jline.console.ConsoleReader;
4-
import org.neo4j.driver.v1.exceptions.AuthenticationException;
4+
import org.neo4j.driver.exceptions.AuthenticationException;
55
import org.neo4j.shell.build.Build;
66
import org.neo4j.shell.cli.CliArgHelper;
77
import org.neo4j.shell.cli.CliArgs;
@@ -67,7 +67,8 @@ void startShell(@Nonnull CliArgs cliArgs) {
6767
cliArgs.getPort(),
6868
cliArgs.getUsername(),
6969
cliArgs.getPassword(),
70-
cliArgs.getEncryption());
70+
cliArgs.getEncryption(),
71+
cliArgs.getDatabase());
7172

7273
try {
7374
CypherShell shell = new CypherShell(logger, prettyConfig);

cypher-shell/src/main/java/org/neo4j/shell/ShellRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ static ShellRunner getShellRunner(@Nonnull CliArgs cliArgs,
5151
} else if (shouldBeInteractive(cliArgs)) {
5252
UserMessagesHandler userMessagesHandler =
5353
new UserMessagesHandler(connectionConfig, cypherShell.getServerVersion());
54-
return new InteractiveShellRunner(cypherShell, cypherShell, logger, new ShellStatementParser(),
55-
System.in, FileHistorian.getDefaultHistoryFile(), userMessagesHandler);
54+
return new InteractiveShellRunner(cypherShell, cypherShell, cypherShell, logger, new ShellStatementParser(),
55+
System.in, FileHistorian.getDefaultHistoryFile(), userMessagesHandler, connectionConfig);
5656
} else {
5757
return new NonInteractiveShellRunner(cliArgs.getFailBehavior(), cypherShell, logger,
5858
new ShellStatementParser(), System.in);

0 commit comments

Comments
 (0)