Repository navigation
Expand file tree
/
Copy pathDatabaseCliCmd.java
More file actions
116 lines (106 loc) · 3.96 KB
/
Copy pathDatabaseCliCmd.java
File metadata and controls
116 lines (106 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class DatabaseCliCmd extends CliCmd {
private static Map<String, Database> databases = new HashMap<>();
private static Map<String, DatabasePersistenceLog> persistenceLogs = new HashMap<>();
private static Database currentDatabase = null;
private static DatabasePersistenceLog currentPersistenceLog = null;
public DatabaseCliCmd() {
super("db");
}
@Override
public boolean createHandler() throws IOException {
if (args.isEmpty()) {
System.out.println("Error: db create requires a database name.");
return false;
}
String dbName = args.get(0);
if (databases.containsKey(dbName)) {
System.out.println("Error: database '" + dbName + "' already exists.");
return false;
}
Database db = new Database();
databases.put(dbName, db);
DatabasePersistenceLog log = new DatabasePersistenceLog(dbName);
persistenceLogs.put(dbName, log);
log.logDbCreate();
System.out.println("Created database '" + dbName + "'.");
return true;
}
@Override
public boolean selectHandler() {
if (args.isEmpty()) {
System.out.println("Error: db select requires a database name.");
return false;
}
String dbName = args.get(0);
if (!databases.containsKey(dbName)) {
System.out.println("Error: database '" + dbName + "' not found.");
return false;
}
Database selected = databases.get(dbName);
if (currentDatabase != selected) {
TableCliCmd.clearCurrentTable();
}
currentDatabase = selected;
currentPersistenceLog = persistenceLogs.get(dbName);
System.out.println("Selected database '" + dbName + "'.");
return true;
}
@Override
public boolean deleteHandler() throws IOException {
if (args.isEmpty()) {
System.out.println("Error: db delete requires a database name.");
return false;
}
String dbName = args.get(0);
if (!databases.containsKey(dbName)) {
System.out.println("Error: database '" + dbName + "' not found.");
return false;
}
Database removed = databases.remove(dbName);
DatabasePersistenceLog removedLog = persistenceLogs.remove(dbName);
if (removedLog != null) {
Files.deleteIfExists(Path.of(removedLog.getFilePath()));
}
if (currentDatabase == removed) {
currentDatabase = null;
currentPersistenceLog = null;
TableCliCmd.clearCurrentTable();
}
System.out.println("Deleted database '" + dbName + "'.");
return true;
}
@Override
public boolean listHandler() {
if (databases.isEmpty()) {
System.out.println("No databases.");
} else {
System.out.println("Databases: " + databases.keySet());
}
return true;
}
@Override
public boolean helpHandler() {
System.out.println("=== Database Commands ===");
System.out.println("db create <name> - Create a new database");
System.out.println("db select <name> - Select a database for table operations");
System.out.println("db delete <name> - Delete a database");
System.out.println("db list - List all databases");
System.out.println();
System.out.println("Examples:");
System.out.println(" db create mystore");
System.out.println(" db select mystore");
System.out.println(" db list");
System.out.println(" db delete mystore");
return true;
}
public static Database getCurrentDatabase() {
return currentDatabase;
}
public static DatabasePersistenceLog getCurrentPersistenceLog() {
return currentPersistenceLog;
}
}