Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions console/src/main/java/com/arcadedb/console/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Console(final boolean interactive) throws IOException {

terminal = TerminalBuilder.builder().system(system).streams(System.in, System.out).jansi(true).build();
Completer completer = new StringsCompleter("align database", "begin", "rollback", "commit", "check database", "close", "connect", "create database",
"create user", "drop database", "drop user", "export", "import", "help", "info types", "load", "exit", "quit", "set", "match", "select", "insert into",
"create user", "drop database", "drop user", "export", "import", "help", "info types", "list databases", "load", "exit", "quit", "set", "match", "select", "insert into",
"update", "delete", "pwd");

lineReader = LineReaderBuilder.builder().terminal(terminal).parser(parser).variable("history-file", ".history").history(new DefaultHistory())
Expand Down Expand Up @@ -187,6 +187,8 @@ else if (lineLowerCase.startsWith("close"))
executeClose();
else if (lineLowerCase.startsWith("commit"))
executeCommit();
else if (lineLowerCase.startsWith("list databases"))
executeList(line);
else if (lineLowerCase.startsWith("connect"))
executeConnect(line);
else if (lineLowerCase.startsWith("create database"))
Expand Down Expand Up @@ -312,6 +314,29 @@ private void executeClose() {
}
}

private void executeList(final String line) {
final String url = line.substring("list databases".length()).trim();

final String[] urlParts = url.split(" ");

outputLine("Databases:");
if (urlParts[0].startsWith(REMOTE_PREFIX)) {
final RemoteDatabase holdRemoteDatabase = remoteDatabase;
connectToRemoteServer(url, false);
Comment thread
lvca marked this conversation as resolved.
for (Object f : remoteDatabase.databases()) {
outputLine(f.toString());
}
remoteDatabase = holdRemoteDatabase;

} else {
File file = new File(databaseDirectory);
for (String f : file.list()) {
outputLine(f);
}
}
flushOutput();
}

private void executeConnect(final String line) {
final String url = line.substring("connect".length()).trim();

Expand All @@ -321,7 +346,7 @@ private void executeConnect(final String line) {
outputLine("Database already connected, to connect to a different database close the current one first");
else if (!urlParts[0].isEmpty()) {
if (urlParts[0].startsWith(REMOTE_PREFIX)) {
connectToRemoteServer(url);
connectToRemoteServer(url, true);

outputLine("Connected");
flushOutput();
Expand All @@ -346,7 +371,7 @@ private void executeCreateDatabase(final String line) {
outputLine("Database already connected, to connect to a different database close the current one first");
else if (!url.isEmpty()) {
if (url.startsWith(REMOTE_PREFIX)) {
connectToRemoteServer(url);
connectToRemoteServer(url, true);
remoteDatabase.create();

outputLine("Database created");
Expand Down Expand Up @@ -411,7 +436,7 @@ private void executeDropDatabase(final String line) {
outputLine("A database is open, close the database first");
else if (!url.isEmpty()) {
if (url.startsWith(REMOTE_PREFIX)) {
connectToRemoteServer(url);
connectToRemoteServer(url, true);
remoteDatabase.drop();

outputLine("Database dropped");
Expand Down Expand Up @@ -650,6 +675,7 @@ private void executeHelp() {
outputLine("help|? -> ask for this help");
outputLine("info types -> prints available types");
outputLine("info transaction -> prints current transaction");
outputLine("list databases |remote:<url> <user> <pw> -> lists databases");
outputLine("load <path> -> runs local script");
outputLine("rollback -> rolls back current transaction");
outputLine("set language = sql|sqlscript|cypher|gremlin|mongo -> sets console query language");
Expand All @@ -662,15 +688,15 @@ private void checkDatabaseIsOpen() {
throw new ArcadeDBException("No active database. Open a database first");
}

private void connectToRemoteServer(final String url) {
private void connectToRemoteServer(final String url, final Boolean needsDatabase) {
final String conn = url.startsWith(REMOTE_PREFIX + "//") ? url.substring((REMOTE_PREFIX + "//").length()) : url.substring(REMOTE_PREFIX.length());

final String[] serverUserPassword = conn.split(" ");
final String[] serverUserPassword = conn.trim().split(" ");
if (serverUserPassword.length != 3)
throw new ConsoleException("URL username and password are missing");

final String[] serverParts = serverUserPassword[0].split("/");
if (serverParts.length != 2)
if ((needsDatabase && serverParts.length != 2) || (!needsDatabase && serverParts.length != 1))
throw new ConsoleException("Remote URL '" + url + "' not valid");

String remoteServer;
Expand All @@ -685,7 +711,15 @@ private void connectToRemoteServer(final String url) {
remotePort = Integer.parseInt(serverParts[0].substring(portPos + 1));
}

remoteDatabase = new RemoteDatabase(remoteServer, remotePort, serverParts[1], serverUserPassword[1], serverUserPassword[2]);
String serverDatabaseName;

if (needsDatabase) {
serverDatabaseName = serverParts[1];
} else {
serverDatabaseName = "";
}

remoteDatabase = new RemoteDatabase(remoteServer, remotePort, serverDatabaseName, serverUserPassword[1], serverUserPassword[2]);
}

private void flushOutput() {
Expand Down
5 changes: 5 additions & 0 deletions console/src/test/java/com/arcadedb/console/ConsoleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void drop() {
FileUtils.deleteRecursively(new File("target/databases"));
}

@Test
public void testList() throws IOException {
Assertions.assertTrue(console.parse("list databases;", false));
}

@Test
public void testConnect() throws IOException {
Assertions.assertTrue(console.parse("connect " + DB_NAME + ";info types", false));
Expand Down
7 changes: 7 additions & 0 deletions network/src/main/java/com/arcadedb/remote/RemoteDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ public void create() {
databaseCommand("create", "SQL", null, null, true, null);
}

public List databases() {
return (List) serverCommand("GET","databases", "SQL", null, null, true, true, (connection, response) -> {

return response.getJSONArray("result").toList();
});
}

public boolean exists() {
return (boolean) databaseCommand("exists", "SQL", null, null, true, (connection, response) -> {

Expand Down