-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatioDatabase.java
More file actions
101 lines (87 loc) · 3.63 KB
/
SpatioDatabase.java
File metadata and controls
101 lines (87 loc) · 3.63 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
99
100
101
package io.spatio;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.*;
public class SpatioDatabase {
private static final String DB_URL = "jdbc:sqlite:spatio.db";
private static SpatioDatabase INSTANCE;
private final Connection connection;
public SpatioDatabase() {
try (Connection connection = DriverManager.getConnection(DB_URL)) {
Path dir = Paths.get(System.getProperty("user.home"), ".spatio");
Files.createDirectories(dir);
Path dbFile = dir.resolve("spatio.db");
String url = "jdbc:sqlite:" + dbFile.toAbsolutePath();
this.connection = DriverManager.getConnection(url);
this.connection.setAutoCommit(true);
Statement stmt = connection.createStatement();
stmt.execute("""
CREATE TABLE IF NOT EXISTS certificates (
hash TEXT PRIMARY KEY,
latitude REAL,
longitude REAL,
timestamp TEXT,
signature TEXT
);
""");
} catch (SQLException e) {
throw new RuntimeException("Erreur lors de l'initialisation de la base SQLite", e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static synchronized SpatioDatabase getInstance() {
if (INSTANCE == null) {
INSTANCE = new SpatioDatabase();
}
return INSTANCE;
}
public Connection getConnection() {
return connection;
}
public void insertCertificate(SpatioCertificate cert) {
try (Connection conn = DriverManager.getConnection(DB_URL)) {
String sql = "INSERT OR REPLACE INTO certificates (hash, latitude, longitude, timestamp, signature) VALUES (?, ?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, cert.hash());
stmt.setDouble(2, cert.latitude());
stmt.setDouble(3, cert.longitude());
stmt.setString(4, cert.timestamp().toString());
stmt.setString(5, cert.signature());
stmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Erreur insertion certificat", e);
}
}
public int getCount() {
try (Connection conn = DriverManager.getConnection(DB_URL)) {
String sql = "SELECT COUNT(*) AS nb FROM certificates";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
rs.next();
return rs.getInt("nb");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public SpatioCertificate getCertificateByHash(String hash) {
try (Connection conn = DriverManager.getConnection(DB_URL)) {
String sql = "SELECT * FROM certificates WHERE hash = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, hash);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
double lat = rs.getDouble("latitude");
double lon = rs.getDouble("longitude");
String timestamp = rs.getString("timestamp");
String signature = rs.getString("signature");
return new SpatioCertificate(lat, lon, java.time.Instant.parse(timestamp), hash, signature);
}
return null;
} catch (SQLException e) {
throw new RuntimeException("Erreur lecture certificat", e);
}
}
}