-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseController.java
137 lines (115 loc) · 3.68 KB
/
DatabaseController.java
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
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.nio.charset.*;
public class DatabaseController {
private String pool;
public DatabaseController(String pool) {
this.pool = pool;
}
public Database newDatabase(String name) throws DatabaseException {
if (Files.exists(Paths.get(".", pool, name)))
throw new DatabaseException("既に存在するデータベースです");
try {
Files.createDirectories(Paths.get(".", pool, name));
} catch (IOException ioe) {
throw new DatabaseException("DBP用ファイル作成に失敗");
}
return new Database(name);
}
public Database openDatabase(String name) throws DatabaseException {
if (!Files.exists(Paths.get(".", pool, name)))
throw new DatabaseException("存在しないデータベースです");
Database db = new Database(name);
try {
// テーブル読込
Files.newDirectoryStream(Paths.get(".", pool, name)).forEach(path -> {
try {
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
// カラム読込
String[] clms = lines.get(0).split("::");
Table tbl = new Table(path.getFileName().toString(), clms.length);
Arrays.stream(clms).forEach(clm -> {
try {
tbl.addColumn(clm);
} catch (DatabaseException de) {
throw new RuntimeException();
}
});
// レコード読込
for (int i = 1; i < lines.size(); i++) {
Record rec = tbl.getRecordInstance();
Arrays.stream(lines.get(i).split("::")).forEach(field -> {
try {
rec.addField(field);
} catch (DatabaseException de) {
throw new RuntimeException();
}
});
tbl.addRecord(rec);
}
db.addTable(tbl);
} catch (Exception e) {
throw new RuntimeException();
}
});
} catch (Exception e) {
throw new DatabaseException("DBP用ファイルの読込に失敗");
}
return db;
}
public void saveDatabase(Database db) throws DatabaseException {
try {
db.getTables().stream().forEach(tbl -> {
try {
BufferedWriter bw = Files.newBufferedWriter(Paths.get(".", pool, db.getName(), tbl.getName()), StandardCharsets.UTF_8);
for (int i = 0; i < tbl.getSize(); i++) {
String clm = tbl.getColumns().get(i);
bw.write(clm, 0, clm.length());
if (i != tbl.getSize() - 1)
bw.write("::", 0, 2);
}
bw.newLine();
for (int i = 0; i < tbl.getRecords().size(); i++) {
Record rec = tbl.getRecords().get(i);
for (int j = 0; j < rec.getFields().size(); j++) {
String field = rec.getFields().get(j);
bw.write(field, 0, field.length());
if (j != rec.getFields().size() - 1)
bw.write("::", 0, 2);
}
bw.newLine();
}
bw.close();
} catch (Exception e) {
throw new RuntimeException();
}
});
} catch (Exception e) {
throw new DatabaseException("DBP用ファイルへの書出に失敗");
}
}
public void removeDatabase(String dbName) throws DatabaseException {
Path root = Paths.get(".", pool, dbName);
try {
Files.walk(root, FileVisitOption.FOLLOW_LINKS)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
root.toFile().delete();
} catch (IOException ioe) {
throw new DatabaseException("DBP用ファイル及ディレクトリの削除に失敗");
}
}
public List<String> getDatabaseNames() throws DatabaseException {
List<String> dbNames = new ArrayList<>();
try {
Files.newDirectoryStream(Paths.get(".", pool)).forEach(path -> {
dbNames.add(path.getFileName().toString());
});
} catch (IOException ioe) {
throw new DatabaseException("DBP用ファイルの一覧取得に失敗");
}
return dbNames;
}
}