Repository navigation
Expand file tree
/
Copy pathDatabase.java
More file actions
63 lines (53 loc) · 1.77 KB
/
Copy pathDatabase.java
File metadata and controls
63 lines (53 loc) · 1.77 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Database {
private Map<String, Table> tables;
public Database() {
this.tables = new HashMap<>();
}
public Table createTable(String tableName,
List<String> fieldNames,
List<String> fieldTypes,
List<String> primaryKeys) {
return createTable(tableName, fieldNames, fieldTypes, primaryKeys, new ArrayList<>());
}
public Table createTable(String tableName,
List<String> fieldNames,
List<String> fieldTypes,
List<String> primaryKeys,
List<String> secondaryKeys) {
if (tables.containsKey(tableName)) {
return null;
}
Table table = new Table(tableName, fieldNames, fieldTypes, primaryKeys, secondaryKeys);
tables.put(tableName, table);
return table;
}
public boolean hasTable(String tableName) {
return tables.containsKey(tableName);
}
public Table selectTable(String tableName) {
return tables.get(tableName);
}
public boolean deleteTable(String tableName) {
if (!tables.containsKey(tableName)) {
return false;
}
tables.remove(tableName);
return true;
}
public Set<String> getTableNames() {
return new HashSet<>(tables.keySet());
}
public void listTables() {
if (tables.isEmpty()) {
System.out.println("No tables in database.");
} else {
System.out.println("Tables: " + tables.keySet());
}
}
}