diff --git a/README.md b/README.md index ab4b0f6..d04f976 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# MiniDB +

MiniDB

MiniDB is a type of [key-value](https://en.wikipedia.org/wiki/Key%E2%80%93value_database) like [XML Document Database](https://en.wikipedia.org/wiki/XML_database). @@ -12,16 +12,22 @@ This is my college Project Submission for the following course - Course Code: IS4C04 - Course Instructor: `Mr. Suhaas KP`, Assistant Professor, Department of Information Science and Engineering, `The National Institute Of Engineering`, Mysuru 570008 +### Made with +- OpenJDK 16.0.1 +- Visual Studio Code +- A bit Powered by [Github-Copilot-preview](https://copilot.github.com/) + ## Usage Use the following commands to perform [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operations on this NoSQL Database. - `list` to list all the created databases - `new {name}` For creating a new database. (`{}` denotes that name is a variable) -- `use {name}` to select the database from the existing ones for CRUD operations +- `use {name}` to select the database from the existing ones for CRUD operations. +- `delete {name}` to delete a database. - `schema {}` for declaring schema of the database. (NOTE: schema must be declared first to add any data) -- `add {}` for adding data to the database. the data seperated with `,` must follow the schema. -- `read` to read and show the data into the console. +- `add {}` for adding data to the database. the data is seperated with `,` and must follow the schema. +- `read` to read and show the data of the database. ### Example usage @@ -41,4 +47,5 @@ schema id,name,legs add 1,cat,4 add 2,fish,0 ``` -Note: The first element is taken as the id, which is used as a pointer to data. So, it's a good practice to name it as `id` itself. \ No newline at end of file + +Note: The first element is taken as the id, which is used as a pointer to data. So, it's a good practice to name it as `id` itself. diff --git a/src/cli.java b/src/cli.java index 38d25af..d887f60 100644 --- a/src/cli.java +++ b/src/cli.java @@ -117,11 +117,16 @@ private static void cliInputs(String input) { break; } - case "update/delete": { + case "update": { break; } + case "delete": { + registry.deleteDatabase(inputCmds[1]); + break; + } + default: { System.out.println("UNKNOWN COMMAND: " + inputCmds[0] + "\nType `info commands` for commands list"); break; diff --git a/src/minidb/xmlParser/RegistryFile.java b/src/minidb/xmlParser/RegistryFile.java index 55526a4..2c43199 100644 --- a/src/minidb/xmlParser/RegistryFile.java +++ b/src/minidb/xmlParser/RegistryFile.java @@ -1,5 +1,6 @@ package minidb.xmlParser; +import java.io.File; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Objects; @@ -112,6 +113,7 @@ public void listAllDatabases() { public int checkDatabase(String name) { int x = -1; NodeList list = this.doc.getElementsByTagName("name"); + for (int i = 0; i < list.getLength(); i++) { Node dbNode = list.item(i); String dbName = dbNode.getTextContent(); @@ -149,6 +151,22 @@ public String getDatabasePath(String name, boolean create) { } public void deleteDatabase(String name) { + int dbid = checkDatabase(name); + + if (dbid != -1) { + String dbPath = getDatabasePath(name, false); + + NodeList list = this.doc.getElementsByTagName("database"); + Element dbEntry = (Element) list.item(dbid); + dbEntry.getParentNode().removeChild(dbEntry); + this.updateFile(); + File f = new File(dbPath); + f.delete(); + print("Database deleted"); + + } else { + print("Database does not exist"); + } } }