Skip to content

Commit

Permalink
working delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
U-C-S committed Jul 22, 2021
1 parent 38554ec commit 71400fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MiniDB
<h1 align=center>MiniDB</h1>

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).

Expand All @@ -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

Expand All @@ -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.

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.
7 changes: 6 additions & 1 deletion src/cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/minidb/xmlParser/RegistryFile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package minidb.xmlParser;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit 71400fa

Please sign in to comment.