SHA256
134 lines
5.1 KiB
Java
134 lines
5.1 KiB
Java
package shine.db.dao;
|
|
|
|
import shine.db.DbController;
|
|
import shine.db.KeyEncodingUtil;
|
|
import shine.db.entities.SolanaUserPdaCurrentEntry;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
|
|
/**
|
|
* Доступ к актуальной PostgreSQL-таблице пользователей, поддерживаемой sync-модулем.
|
|
*/
|
|
public final class SolanaUserPdaCurrentDAO {
|
|
|
|
private static volatile SolanaUserPdaCurrentDAO instance;
|
|
|
|
private final DbController db = DbController.getInstance();
|
|
|
|
private SolanaUserPdaCurrentDAO() {}
|
|
|
|
public static SolanaUserPdaCurrentDAO getInstance() {
|
|
if (instance == null) {
|
|
synchronized (SolanaUserPdaCurrentDAO.class) {
|
|
if (instance == null) {
|
|
instance = new SolanaUserPdaCurrentDAO();
|
|
}
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public SolanaUserPdaCurrentEntry getByBlockchainName(String blockchainName) throws SQLException {
|
|
try (Connection c = db.getConnection()) {
|
|
return getByBlockchainName(c, blockchainName);
|
|
}
|
|
}
|
|
|
|
|
|
public SolanaUserPdaCurrentEntry getByLogin(String login) throws SQLException {
|
|
String sql = """
|
|
SELECT
|
|
login,
|
|
blockchain_name,
|
|
blockchain_key,
|
|
paid_limit_bytes
|
|
FROM solana_user_pda_current
|
|
WHERE LOWER(login) = LOWER(?)
|
|
LIMIT 1
|
|
""";
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, login);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
if (!rs.next()) return null;
|
|
return mapRow(rs);
|
|
}
|
|
}
|
|
}
|
|
|
|
public SolanaUserPdaCurrentEntry getByBlockchainName(Connection c, String blockchainName) throws SQLException {
|
|
String sql = """
|
|
SELECT
|
|
login,
|
|
blockchain_name,
|
|
blockchain_key,
|
|
paid_limit_bytes
|
|
FROM solana_user_pda_current
|
|
WHERE blockchain_name = ?
|
|
LIMIT 1
|
|
""";
|
|
|
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, blockchainName);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
if (rs.next()) return mapRow(rs);
|
|
}
|
|
}
|
|
// Old forks remain valid separate blockchains even after root rotation.
|
|
String forkSql = """
|
|
SELECT u.login,
|
|
u.login || '-' || LPAD(((f.value ->> 'forkIndex')::integer + 1)::text, 3, '0') AS blockchain_name,
|
|
f.value ->> 'blockchainKey' AS blockchain_key,
|
|
(f.value ->> 'paidLimitBytes')::bigint AS paid_limit_bytes
|
|
FROM solana_user_pda_current u
|
|
CROSS JOIN LATERAL jsonb_array_elements(
|
|
COALESCE(NULLIF(u.blockchain_forks_json, ''), '[]')::jsonb
|
|
) AS f(value)
|
|
WHERE LOWER(u.login || '-' || LPAD(((f.value ->> 'forkIndex')::integer + 1)::text, 3, '0')) = LOWER(?)
|
|
LIMIT 1
|
|
""";
|
|
try (PreparedStatement ps = c.prepareStatement(forkSql)) {
|
|
ps.setString(1, blockchainName);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
return rs.next() ? mapRow(rs) : null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Resolve a user/fork by the 32-byte Ed25519 blockchain key used as ANS-104 owner. */
|
|
public SolanaUserPdaCurrentEntry getByBlockchainKey(byte[] owner32) throws SQLException {
|
|
if (owner32 == null || owner32.length != 32) return null;
|
|
String wanted = KeyEncodingUtil.encodeBase58(owner32);
|
|
String sql = """
|
|
SELECT
|
|
u.login,
|
|
u.login || '-' || LPAD(((fork_item.value ->> 'forkIndex')::integer + 1)::text, 3, '0') AS blockchain_name,
|
|
fork_item.value ->> 'blockchainKey' AS blockchain_key,
|
|
(fork_item.value ->> 'paidLimitBytes')::bigint AS paid_limit_bytes
|
|
FROM solana_user_pda_current u
|
|
CROSS JOIN LATERAL jsonb_array_elements(COALESCE(NULLIF(u.blockchain_forks_json, ''), '[]')::jsonb) AS fork_item(value)
|
|
WHERE fork_item.value ->> 'blockchainKey' = ?
|
|
AND ((fork_item.value ->> 'forkIndex')::integer + 1) BETWEEN 1 AND 999
|
|
LIMIT 1
|
|
""";
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, wanted);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
if (!rs.next()) return null;
|
|
return mapRow(rs);
|
|
}
|
|
}
|
|
}
|
|
|
|
private SolanaUserPdaCurrentEntry mapRow(ResultSet rs) throws SQLException {
|
|
SolanaUserPdaCurrentEntry entry = new SolanaUserPdaCurrentEntry();
|
|
entry.setLogin(rs.getString("login"));
|
|
entry.setBlockchainName(rs.getString("blockchain_name"));
|
|
entry.setBlockchainKey(KeyEncodingUtil.base58KeyToBase64_32(rs.getString("blockchain_key")));
|
|
entry.setPaidLimitBytes(rs.getLong("paid_limit_bytes"));
|
|
return entry;
|
|
}
|
|
}
|