-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDuplicationDatabase.java
More file actions
98 lines (81 loc) · 3.3 KB
/
Copy pathDuplicationDatabase.java
File metadata and controls
98 lines (81 loc) · 3.3 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
package modelset.process;
import java.io.File;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.annotation.Nonnull;
import mar.analysis.duplicates.DuplicateFinder.DuplicationGroup;
import mar.validation.IFileInfo;
/**
* A wrapper to handle the duplication database.
*
* @author jesus
*
*/
public class DuplicationDatabase implements AutoCloseable {
private Connection connection;
@Nonnull
public DuplicationDatabase(File file) {
String url = getConnectionString(file);
try {
Connection conn = DriverManager.getConnection(url);
if (conn != null) {
if (! file.exists()) {
DatabaseMetaData meta = conn.getMetaData();
System.out.println("The driver name is " + meta.getDriverName());
System.out.println("A new database has been created.");
}
String duplication_run = "CREATE TABLE IF NOT EXISTS duplication_run (\n"
+ " id varchar(255) NOT NULL,\n"
+ " t0 float NOT NULL,\n"
+ " t1 float NOT NULL\n"
+ ");";
String duplicates = "CREATE TABLE IF NOT EXISTS duplicates (\n"
+ " model_id varchar(255) NOT NULL,\n"
+ " group_id varchar (255) NOT NULL,\n"
+ " duplication_run varchar (255) NOT NULL\n"
+ ");";
Statement stmt = conn.createStatement();
stmt.execute(duplication_run);
stmt = conn.createStatement();
stmt.execute(duplicates);
}
this.connection = conn;
this.connection.setAutoCommit(false);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void close() throws SQLException {
this.connection.commit();
this.connection.close();
}
@Nonnull
public static String getConnectionString(File file) {
return "jdbc:sqlite:" + file.getAbsolutePath();
}
public void addDuplicationRun(String id, double t0, double t1) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO duplication_run(id, t0, t1) VALUES (?, ?, ?)");
preparedStatement.setString(1, id);
preparedStatement.setDouble(2, t0);
preparedStatement.setDouble(3, t1);
preparedStatement.execute();
preparedStatement.close();
}
public void addGroup(String runId, DuplicationGroup<IFileInfo> duplicationGroup) throws SQLException {
try(PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO duplicates(model_id, group_id, duplication_run) VALUES (?, ?, ?)")) {
String groupId = duplicationGroup.getRepresentative().getModelId();
for (IFileInfo f : duplicationGroup) {
String id = f.getModelId();
preparedStatement.setString(1, id);
preparedStatement.setString(2, groupId);
preparedStatement.setString(3, runId);
preparedStatement.execute();
}
}
}
}