SHA256
105 lines
3.5 KiB
Java
105 lines
3.5 KiB
Java
package shine.db.dao;
|
|
|
|
import shine.db.DbController;
|
|
import shine.db.entities.SyncServerEntry;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* DAO локальной таблицы серверов-партнёров для будущей межсерверной синхронизации.
|
|
*/
|
|
public final class SyncServersDAO {
|
|
|
|
private static volatile SyncServersDAO instance;
|
|
private final DbController db = DbController.getInstance();
|
|
|
|
private SyncServersDAO() {}
|
|
|
|
public static SyncServersDAO getInstance() {
|
|
if (instance == null) {
|
|
synchronized (SyncServersDAO.class) {
|
|
if (instance == null) instance = new SyncServersDAO();
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public List<SyncServerEntry> listAll() throws SQLException {
|
|
try (Connection c = db.getConnection()) {
|
|
return listAll(c);
|
|
}
|
|
}
|
|
|
|
public List<SyncServerEntry> listAll(Connection c) throws SQLException {
|
|
String sql = """
|
|
SELECT login, server_address, updated_at_ms
|
|
FROM sync_servers
|
|
ORDER BY login
|
|
""";
|
|
List<SyncServerEntry> result = new ArrayList<>();
|
|
try (PreparedStatement ps = c.prepareStatement(sql);
|
|
ResultSet rs = ps.executeQuery()) {
|
|
while (rs.next()) {
|
|
result.add(mapRow(rs));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Полностью заменяет список партнёров актуальным снимком из Solana PDA.
|
|
*/
|
|
public void replaceAll(List<SyncServerEntry> entries) throws SQLException {
|
|
try (Connection c = db.getConnection()) {
|
|
replaceAll(c, entries);
|
|
}
|
|
}
|
|
|
|
public void replaceAll(Connection c, List<SyncServerEntry> entries) throws SQLException {
|
|
boolean oldAutoCommit = c.getAutoCommit();
|
|
c.setAutoCommit(false);
|
|
try (Statement st = c.createStatement()) {
|
|
st.executeUpdate("DELETE FROM sync_servers");
|
|
String sql = """
|
|
INSERT INTO sync_servers (
|
|
login, server_address, updated_at_ms
|
|
) VALUES (?, ?, ?)
|
|
""";
|
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
|
for (SyncServerEntry entry : entries) {
|
|
ps.setString(1, entry.getLogin());
|
|
ps.setString(2, safe(entry.getServerAddress()));
|
|
ps.setLong(3, entry.getUpdatedAtMs());
|
|
ps.addBatch();
|
|
}
|
|
ps.executeBatch();
|
|
}
|
|
c.commit();
|
|
} catch (Exception e) {
|
|
try { c.rollback(); } catch (Exception ignored) {}
|
|
if (e instanceof SQLException sqlEx) throw sqlEx;
|
|
throw new SQLException("Не удалось обновить таблицу sync_servers", e);
|
|
} finally {
|
|
try { c.setAutoCommit(oldAutoCommit); } catch (Exception ignored) {}
|
|
}
|
|
}
|
|
|
|
private SyncServerEntry mapRow(ResultSet rs) throws SQLException {
|
|
SyncServerEntry entry = new SyncServerEntry();
|
|
entry.setLogin(rs.getString("login"));
|
|
entry.setServerAddress(rs.getString("server_address"));
|
|
entry.setUpdatedAtMs(rs.getLong("updated_at_ms"));
|
|
return entry;
|
|
}
|
|
|
|
private static String safe(String value) {
|
|
return value == null ? "" : value;
|
|
}
|
|
}
|