-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathDesign SQL.java
More file actions
47 lines (41 loc) · 1.54 KB
/
Design SQL.java
File metadata and controls
47 lines (41 loc) · 1.54 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
class SQL {
private final Map<String, Map<Integer, List<String>>> database;
private final Map<String, Integer> tableIdMap;
public SQL(List<String> tableNames, List<Integer> columns) {
this.database = new HashMap<>();
this.tableIdMap = new HashMap<>();
for (String name : tableNames) {
database.put(name, new HashMap<>());
tableIdMap.put(name, 1);
}
}
public void insertRow(String tableName, List<String> row) {
// Select the table
Map<Integer, List<String>> table = this.database.get(tableName);
// Insert row in the table
table.put(tableIdMap.get(tableName), row);
// Increment rowId for the table
tableIdMap.put(tableName, tableIdMap.get(tableName) + 1);
}
public void deleteRow(String tableName, int rowId) {
// Select the table
Map<Integer, List<String>> table = this.database.get(tableName);
// Delete the row against the rowId
table.remove(rowId);
}
public String selectCell(String tableName, int rowId, int columnId) {
// Select the table
Map<Integer, List<String>> table = this.database.get(tableName);
// Select row
List<String> row = table.get(rowId);
// Select column
return row.get(columnId - 1);
}
}
/**
* Your SQL object will be instantiated and called as such:
* SQL obj = new SQL(names, columns);
* obj.insertRow(name,row);
* obj.deleteRow(name,rowId);
* String param_3 = obj.selectCell(name,rowId,columnId);
*/