Repository navigation
Expand file tree
/
Copy pathRowCliCmd.java
More file actions
233 lines (222 loc) · 9.69 KB
/
Copy pathRowCliCmd.java
File metadata and controls
233 lines (222 loc) · 9.69 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.io.IOException;
import java.util.*;
public class RowCliCmd extends CliCmd {
public RowCliCmd() {
super("row");
}
@Override
public boolean createHandler() throws IOException {
Table table = TableCliCmd.getCurrentTable();
if (table == null) {
System.out.println("Error: no table selected.");
return false;
}
if (args.size() < 2) {
System.out.println("Error: row create requires field-value pairs. Use: row create field1 value1 field2 value2 ...");
return false;
}
if (args.size() % 2 != 0) {
System.out.println("Error: must provide an even number of arguments (field-value pairs).");
return false;
}
Row row = new Row();
for (int i = 0; i < args.size(); i += 2){
String fieldName = args.get(i);
String fieldValue = args.get(i + 1);
String fieldType = table.getFieldType(fieldName);
if (fieldType == null) {
System.out.println("Error: field '" + fieldName + "' does not exist.");
return false;
}
if (fieldType.equals("LONG")) {
Long value = Long.parseLong(fieldValue);
row.setLong(fieldName, value);
} else if (fieldType.equals("STRING")) {
row.setString(fieldName, fieldValue);
}
}
String duplicateKey = table.findDuplicatePrimaryKey(row);
if (duplicateKey != null) {
System.out.println("Error: duplicate value for primary key '" + duplicateKey + "': " + row.get(duplicateKey));
return false;
}
List<String> missingFields = table.getMissingFields(row);
for (String missingField : missingFields) {
System.out.println("Warning: row is missing field '" + missingField + "'");
}
table.insert(row);
DatabaseCliCmd.getCurrentPersistenceLog().logRowCreate(args);
System.out.println("Inserted row: " + row);
return true;
}
@Override
public boolean getHandler() {
Table table = TableCliCmd.getCurrentTable();
if (table == null) {
System.out.println("Error: no table selected.");
return false;
}
if (args.size() < 2) {
System.out.println("Error: row get requires a key field and key value. Use: row get keyField keyValue");
return false;
}
String keyField = args.get(0);
String keyValue = args.get(1);
String fieldType = table.getFieldType(keyField);
if (fieldType == null) {
System.out.println("Error: field '" + keyField + "' does not exist.");
return false;
}
if (!table.isKeyField(keyField)) {
System.out.println("Error: '" + keyField + "' is not a primary or secondary key.");
return false;
}
long startTime = System.nanoTime();
Row found = null;
if (fieldType.equals("LONG")) {
Long keyValueLong = Long.parseLong(keyValue);
found = table.getByLongKey(keyField, keyValueLong);
} else if (fieldType.equals("STRING")) {
found = table.getByStringKey(keyField, keyValue);
}
long nanoseconds = System.nanoTime() - startTime;
String seconds = formatSeconds(nanoseconds);
if (found == null) {
System.out.println("Error: no row found where " + keyField + " = " + keyValue
+ " (searched " + table.size() + " rows in " + seconds + " seconds)");
return false;
}
System.out.println("Found: " + found + " (in " + seconds + " seconds)");
return true;
}
private String formatSeconds(long nanoseconds) {
double seconds = nanoseconds / 1000000000.0;
return String.format(Locale.ROOT, "%.6f", seconds);
}
@Override
public boolean selectHandler() {
return getHandler();
}
@Override
public boolean updateHandler() throws IOException {
Table table = TableCliCmd.getCurrentTable();
if (table == null) {
System.out.println("Error: no table selected.");
return false;
}
if (args.size() < 4) {
System.out.println("Error: row update requires keyField keyValue fieldToUpdate newValue. Use: row update keyField keyValue fieldToUpdate newValue");
return false;
}
String keyField = args.get(0);
String keyValue = args.get(1);
String fieldToUpdate = args.get(2);
String newValue = args.get(3);
String keyFieldType = table.getFieldType(keyField);
if (keyFieldType == null) {
System.out.println("Error: key field '" + keyField + "' does not exist.");
return false;
}
String updateFieldType = table.getFieldType(fieldToUpdate);
if (updateFieldType == null) {
System.out.println("Error: field '" + fieldToUpdate + "' does not exist.");
return false;
}
if (!table.isKeyField(keyField)) {
System.out.println("Error: '" + keyField + "' is not a primary or secondary key.");
return false;
}
boolean updated = false;
if (keyFieldType.equals("LONG")) {
Long keyValueLong = Long.parseLong(keyValue);
if (updateFieldType.equals("LONG")) {
Long newValueLong = Long.parseLong(newValue);
updated = table.updateField(keyField, keyValueLong, fieldToUpdate, newValueLong);
} else if (updateFieldType.equals("STRING")) {
updated = table.updateField(keyField, keyValueLong, fieldToUpdate, newValue);
}
} else if (keyFieldType.equals("STRING")) {
if (updateFieldType.equals("LONG")) {
Long newValueLong = Long.parseLong(newValue);
updated = table.updateField(keyField, keyValue, fieldToUpdate, newValueLong);
} else if (updateFieldType.equals("STRING")) {
updated = table.updateField(keyField, keyValue, fieldToUpdate, newValue);
}
}
if (updated) {
DatabaseCliCmd.getCurrentPersistenceLog().logRowUpdate(args);
System.out.println("Updated '" + fieldToUpdate + "' on row where " + keyField + " = " + keyValue);
} else {
System.out.println("Error: no row found where " + keyField + " = " + keyValue);
}
return updated;
}
@Override
public boolean deleteHandler() throws IOException {
Table table = TableCliCmd.getCurrentTable();
if (table == null) {
System.out.println("Error: no table selected.");
return false;
}
if (args.size() < 2) {
System.out.println("Error: row delete requires a key field and key value. Use: row delete keyField keyValue");
return false;
}
String keyField = args.get(0);
String keyValue = args.get(1);
String fieldType = table.getFieldType(keyField);
if (fieldType == null) {
System.out.println("Error: field '" + keyField + "' does not exist.");
return false;
}
if (!table.isKeyField(keyField)) {
System.out.println("Error: '" + keyField + "' is not a primary or secondary key.");
return false;
}
int deletedCount = 0;
if (fieldType.equals("LONG")) {
Long keyValueLong = Long.parseLong(keyValue);
deletedCount = table.deleteByLongKey(keyField, keyValueLong);
} else if (fieldType.equals("STRING")) {
deletedCount = table.deleteByStringKey(keyField, keyValue);
}
if (deletedCount > 0) {
DatabaseCliCmd.getCurrentPersistenceLog().logRowDelete(args);
System.out.println("Deleted " + deletedCount + " row(s) where " + keyField + " = " + keyValue);
} else {
System.out.println("Error: no row found where " + keyField + " = " + keyValue);
}
return deletedCount > 0;
}
@Override
public boolean listHandler() {
Table table = TableCliCmd.getCurrentTable();
if (table == null) {
System.out.println("Error: no table selected.");
return false;
}
table.printAll();
return true;
}
@Override
public boolean helpHandler() {
System.out.println("=== Row Commands ===");
System.out.println("row create <field> <value> [<field> <value> ...] - Insert a row with field-value pairs");
System.out.println("row get <keyField> <keyValue> - Get a row by a PRIMARY or SECONDARY key field");
System.out.println("row update <keyField> <keyValue> <field> <value> - Update a field in a row, looked up by key field");
System.out.println("row delete <keyField> <keyValue> - Delete a row by a PRIMARY or SECONDARY key field");
System.out.println("row list - List all rows in current table");
System.out.println();
System.out.println("Note: keyField must be declared PRIMARY or SECONDARY on the table (see 'table help').");
System.out.println("Inserting a duplicate value for a PRIMARY key field is rejected.");
System.out.println();
System.out.println("Examples:");
System.out.println(" row create id 1 name Alice price 100");
System.out.println(" row create id 2 name Bob price 200");
System.out.println(" row get id 1");
System.out.println(" row update id 1 price 150");
System.out.println(" row list");
System.out.println(" row delete id 2");
return true;
}
}