|
| 1 | +class SQL { |
| 2 | + |
| 3 | + private final Map<String, Map<Integer, List<String>>> database; |
| 4 | + private final Map<String, Integer> tableIdMap; |
| 5 | + |
| 6 | + public SQL(List<String> tableNames, List<Integer> columns) { |
| 7 | + this.database = new HashMap<>(); |
| 8 | + this.tableIdMap = new HashMap<>(); |
| 9 | + for (String name : tableNames) { |
| 10 | + database.put(name, new HashMap<>()); |
| 11 | + tableIdMap.put(name, 1); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + public void insertRow(String tableName, List<String> row) { |
| 16 | + // Select the table |
| 17 | + Map<Integer, List<String>> table = this.database.get(tableName); |
| 18 | + // Insert row in the table |
| 19 | + table.put(tableIdMap.get(tableName), row); |
| 20 | + // Increment rowId for the table |
| 21 | + tableIdMap.put(tableName, tableIdMap.get(tableName) + 1); |
| 22 | + } |
| 23 | + |
| 24 | + public void deleteRow(String tableName, int rowId) { |
| 25 | + // Select the table |
| 26 | + Map<Integer, List<String>> table = this.database.get(tableName); |
| 27 | + // Delete the row against the rowId |
| 28 | + table.remove(rowId); |
| 29 | + } |
| 30 | + |
| 31 | + public String selectCell(String tableName, int rowId, int columnId) { |
| 32 | + // Select the table |
| 33 | + Map<Integer, List<String>> table = this.database.get(tableName); |
| 34 | + // Select row |
| 35 | + List<String> row = table.get(rowId); |
| 36 | + // Select column |
| 37 | + return row.get(columnId - 1); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Your SQL object will be instantiated and called as such: |
| 43 | + * SQL obj = new SQL(names, columns); |
| 44 | + * obj.insertRow(name,row); |
| 45 | + * obj.deleteRow(name,rowId); |
| 46 | + * String param_3 = obj.selectCell(name,rowId,columnId); |
| 47 | + */ |
0 commit comments