Repository navigation
Expand file tree
/
Copy pathTableCliCmd.java
More file actions
159 lines (149 loc) · 6.42 KB
/
Copy pathTableCliCmd.java
File metadata and controls
159 lines (149 loc) · 6.42 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.io.IOException;
import java.util.*;
public class TableCliCmd extends CliCmd {
private static Table currentTable = null;
public TableCliCmd() {
super("table");
}
@Override
public boolean createHandler() throws IOException {
Database db = DatabaseCliCmd.getCurrentDatabase();
if (db == null) {
System.out.println("Error: no database selected.");
return false;
}
if (args.isEmpty()) {
System.out.println("Error: table create requires a table name.");
return false;
}
String tableName = args.get(0);
if (args.size() < 2) {
System.out.println("Error: table create requires at least one field. Use: table create <name> field:TYPE ...");
return false;
}
List<String> fieldArgs = args.subList(1, args.size());
List<String> fieldNames = new ArrayList<>();
List<String> fieldTypes = new ArrayList<>();
List<String> primaryKeys = new ArrayList<>();
List<String> secondaryKeys = new ArrayList<>();
for (int i = 1; i < args.size(); i++){
String fieldArg = args.get(i);
String[] parts = fieldArg.split(":");
if (parts.length != 2 && parts.length != 3) {
System.out.println("Error: invalid field format '" + fieldArg + "'. Use fieldname:TYPE or fieldname:TYPE:KEYTYPE");
return false;
}
String fieldName = parts[0];
String fieldType = parts[1].toUpperCase();
String keyType = "NONE";
if (parts.length == 3) {
keyType = parts[2].toUpperCase();
}
if (!keyType.equals("PRIMARY") && !keyType.equals("SECONDARY") && !keyType.equals("NONE")) {
System.out.println("Error: invalid key type '" + keyType + "' for field '" + fieldName + "'. Use PRIMARY, SECONDARY, or NONE.");
return false;
}
fieldNames.add(fieldName);
fieldTypes.add(fieldType);
if (keyType.equals("PRIMARY")) {
primaryKeys.add(fieldName);
} else if (keyType.equals("SECONDARY")) {
secondaryKeys.add(fieldName);
}
}
Table table = db.createTable(tableName, fieldNames, fieldTypes, primaryKeys, secondaryKeys);
if (table == null) {
System.out.println("Error: table '" + tableName + "' already exists.");
return false;
}
DatabaseCliCmd.getCurrentPersistenceLog().logTableCreate(tableName, fieldArgs);
System.out.println("Created table '" + tableName + "' with fields: " + fieldNames
+ " types: " + fieldTypes
+ " primary keys: " + primaryKeys
+ " secondary keys: " + secondaryKeys);
return true;
}
@Override
public boolean selectHandler() throws IOException {
Database db = DatabaseCliCmd.getCurrentDatabase();
if (db == null) {
System.out.println("Error: no database selected.");
return false;
}
if (args.isEmpty()) {
System.out.println("Error: table select requires a table name.");
return false;
}
String tableName = args.get(0);
Table table = db.selectTable(tableName);
if (table == null) {
System.out.println("Error: table '" + tableName + "' not found.");
return false;
}
currentTable = table;
DatabaseCliCmd.getCurrentPersistenceLog().logTableSelect(tableName);
System.out.println("Selected table '" + tableName + "'.");
return true;
}
@Override
public boolean deleteHandler() throws IOException {
Database db = DatabaseCliCmd.getCurrentDatabase();
if (db == null) {
System.out.println("Error: no database selected.");
return false;
}
if (args.isEmpty()) {
System.out.println("Error: table delete requires a table name.");
return false;
}
String tableName = args.get(0);
boolean deleted = db.deleteTable(tableName);
if (!deleted) {
System.out.println("Error: table '" + tableName + "' does not exist.");
return false;
}
if (currentTable != null && currentTable.getTableName().equals(tableName)) {
currentTable = null;
}
DatabaseCliCmd.getCurrentPersistenceLog().logTableDelete(tableName);
System.out.println("Dropped table '" + tableName + "'.");
return true;
}
@Override
public boolean listHandler() {
Database db = DatabaseCliCmd.getCurrentDatabase();
if (db == null) {
System.out.println("Error: no database selected.");
return false;
}
db.listTables();
return true;
}
@Override
public boolean helpHandler() {
System.out.println("=== Table Commands ===");
System.out.println("table create <name> <field:TYPE[:KEYTYPE]> [...] - Create a table with fields");
System.out.println("table select <name> - Select a table for row operations");
System.out.println("table delete <name> - Delete a table");
System.out.println("table list - List all tables in current database");
System.out.println();
System.out.println("Supported types: LONG, STRING");
System.out.println("Supported key types: PRIMARY, SECONDARY, NONE (default if omitted: NONE)");
System.out.println(" PRIMARY - must be unique across rows; can be looked up/updated/deleted by");
System.out.println(" SECONDARY - duplicates allowed; can still be looked up/updated/deleted by");
System.out.println(" NONE - a plain field, cannot be used to look up a row");
System.out.println();
System.out.println("Examples:");
System.out.println(" table create products id:LONG:PRIMARY sku:STRING:SECONDARY name:STRING price:LONG");
System.out.println(" table select products");
System.out.println(" table list");
System.out.println(" table delete products");
return true;
}
public static Table getCurrentTable() {
return currentTable;
}
public static void clearCurrentTable() {
currentTable = null;
}
}