diff --git a/console/src/main/java/com/arcadedb/console/Console.java b/console/src/main/java/com/arcadedb/console/Console.java index 2947590f9e..ed663c04f0 100644 --- a/console/src/main/java/com/arcadedb/console/Console.java +++ b/console/src/main/java/com/arcadedb/console/Console.java @@ -321,83 +321,76 @@ private void executeClose() { private void executeListDatabases(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)) { + if (url.startsWith(REMOTE_PREFIX)) { final RemoteDatabase holdRemoteDatabase = remoteDatabase; + connectToRemoteServer(url, false); for (Object f : remoteDatabase.databases()) { outputLine(f.toString()); } + remoteDatabase = holdRemoteDatabase; } else { - File file = new File(databaseDirectory); - for (String f : file.list()) { + for (String f : new File(databaseDirectory).list()) { outputLine(f); } } + flushOutput(); } private void executeConnect(final String line) { final String url = line.substring("connect".length()).trim(); - final String[] urlParts = url.split(" "); + checkDatabaseIsConnected(); + checkUrlIsEmpty(url); - if (localDatabase != null || remoteDatabase != null) - 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, true); + if (url.startsWith(REMOTE_PREFIX)) { + connectToRemoteServer(url, true); + + } else { + final String[] urlParts = url.split(" "); - outputLine("Connected"); - flushOutput(); + final String localUrl = parseLocalUrl(urlParts[0]); - } else { - PaginatedFile.MODE mode = PaginatedFile.MODE.READ_WRITE; - if (urlParts.length > 1) - mode = PaginatedFile.MODE.valueOf(urlParts[1].toUpperCase()); + checkDatabaseIsLocked(localUrl); - final String databaseUrl = databaseDirectory + urlParts[0]; + PaginatedFile.MODE mode = PaginatedFile.MODE.READ_WRITE; + if (urlParts.length > 1) + mode = PaginatedFile.MODE.valueOf(urlParts[1].toUpperCase()); - final File lockFile = new File(databaseUrl + "/database.lck"); + databaseFactory = new DatabaseFactory(localUrl); + localDatabase = (DatabaseInternal) databaseFactory.setAutoTransaction(true).open(mode); + } - if (!lockFile.exists()) { - databaseFactory = new DatabaseFactory(databaseUrl); - localDatabase = (DatabaseInternal) databaseFactory.setAutoTransaction(true).open(mode); - } else { - outputLine("Database appears locked by server."); - } - } - } else - throw new ConsoleException("URL missing"); + outputLine("Database connected"); + flushOutput(); } private void executeCreateDatabase(final String line) { String url = line.substring("create database".length()).trim(); - if (localDatabase != null || remoteDatabase != null) - 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, true); - remoteDatabase.create(); - outputLine("Database created"); - flushOutput(); + checkDatabaseIsConnected(); + checkUrlIsEmpty(url); - } else { - if (url.startsWith("file://")) - url = url.substring("file://".length()); + if (url.startsWith(REMOTE_PREFIX)) { + connectToRemoteServer(url, true); + remoteDatabase.create(); - url = databaseDirectory + url; + } else { + final String localUrl = parseLocalUrl(url); - databaseFactory = new DatabaseFactory(url); - localDatabase = (DatabaseInternal) databaseFactory.setAutoTransaction(true).create(); - } - } else - throw new ConsoleException("URL missing"); + if(new File(localUrl).exists()) + throw new ConsoleException("Database already exists"); + + databaseFactory = new DatabaseFactory(localUrl); + localDatabase = (DatabaseInternal) databaseFactory.setAutoTransaction(true).create(); + } + + outputLine("Database created"); + flushOutput(); } private void executeCreateUser(final String line) { @@ -442,26 +435,29 @@ private void executeCreateUser(final String line) { private void executeDropDatabase(final String line) { final String url = line.substring("drop database".length()).trim(); - if (localDatabase != null || remoteDatabase != null) - outputLine("A database is open, close the database first"); - else if (!url.isEmpty()) { - if (url.startsWith(REMOTE_PREFIX)) { - connectToRemoteServer(url, true); - remoteDatabase.drop(); - - outputLine("Database dropped"); - flushOutput(); - - } else { - databaseFactory = new DatabaseFactory(url); - localDatabase = (DatabaseInternal) databaseFactory.setAutoTransaction(true).open(); - localDatabase.drop(); - } - } else - throw new ConsoleException("URL missing"); + + checkDatabaseIsConnected(); + checkUrlIsEmpty(url); + + if (url.startsWith(REMOTE_PREFIX)) { + connectToRemoteServer(url, true); + remoteDatabase.drop(); + + } else { + final String localUrl = parseLocalUrl(url); + + checkDatabaseIsLocked(localUrl); + + databaseFactory = new DatabaseFactory(localUrl); + localDatabase = (DatabaseInternal) databaseFactory.setAutoTransaction(true).open(); + localDatabase.drop(); + } remoteDatabase = null; localDatabase = null; + + outputLine("Database dropped"); + flushOutput(); } private void executeDropUser(final String line) { @@ -675,9 +671,9 @@ private void executeHelp() { outputLine("HELP:"); outputLine("begin -> begins a new transaction"); outputLine("check database -> check database integrity"); - outputLine("close ||remote: -> closes the database"); outputLine("commit -> commits current transaction"); outputLine("connect |remote: -> connects to a database"); + outputLine("close -> disconnects a database"); outputLine("create database |remote: -> creates a new database"); outputLine("create user identified by [grant connect to *] -> creates a user"); outputLine("drop database |remote: -> deletes a database"); @@ -695,7 +691,27 @@ private void executeHelp() { private void checkDatabaseIsOpen() { if (localDatabase == null && remoteDatabase == null) - throw new ArcadeDBException("No active database. Open a database first"); + throw new ConsoleException("No active database. Open a database first"); + } + + private void checkDatabaseIsConnected() { + if (localDatabase != null || remoteDatabase != null) + throw new ConsoleException("Database already connected, close current first"); + } + + private void checkDatabaseIsLocked(final String url) { + + if (new File(url + "/database.lck").exists()) + throw new ConsoleException("Database appears locked by server"); + } + + private void checkUrlIsEmpty(final String url) { + if(url.isEmpty()) + throw new ConsoleException("URL missing"); + } + + private String parseLocalUrl(final String url) { + return databaseDirectory + url.replaceFirst("file://",""); } private void connectToRemoteServer(final String url, final Boolean needsDatabase) { diff --git a/console/src/test/java/com/arcadedb/console/ConsoleTest.java b/console/src/test/java/com/arcadedb/console/ConsoleTest.java index 466bde21da..694a8bb9c6 100644 --- a/console/src/test/java/com/arcadedb/console/ConsoleTest.java +++ b/console/src/test/java/com/arcadedb/console/ConsoleTest.java @@ -44,7 +44,7 @@ public class ConsoleTest { public void populate() throws IOException { FileUtils.deleteRecursively(new File("./target/databases")); console = new Console(false).setRootPath("./target"); - Assertions.assertTrue(console.parse("create database " + DB_NAME, false)); + Assertions.assertTrue(console.parse("create database " + DB_NAME + "; close", false)); } @AfterEach @@ -76,7 +76,7 @@ public void testSetVerbose() throws IOException { @Test public void testSetLanguage() throws IOException { - console.parse("set language = sql; select 1", false); + console.parse("connect " + DB_NAME + ";set language = sql; select 1", false); } @Test