Refactor database operations for resolved DNS records

This commit is contained in:
netkas 2025-01-15 13:55:52 -05:00
parent 3adb62cfc7
commit 4a143a8a97
2 changed files with 27 additions and 27 deletions

View file

@ -1,7 +1,7 @@
create table resolved_dns_records create table resolved_dns_records
( (
domain varchar(512) not null comment 'The domain name' domain varchar(512) not null comment 'Unique Index for the server domain'
primary key comment 'Unique Index for the server domain', primary key,
rpc_endpoint text not null comment 'The endpoint of the RPC server', rpc_endpoint text not null comment 'The endpoint of the RPC server',
public_key text not null comment 'The Public Key of the server', public_key text not null comment 'The Public Key of the server',
expires bigint not null comment 'The Unix Timestamp for when the server''s keypair expires', expires bigint not null comment 'The Unix Timestamp for when the server''s keypair expires',

View file

@ -139,21 +139,28 @@
$endpoint = $dnsRecord->getRpcEndpoint(); $endpoint = $dnsRecord->getRpcEndpoint();
$publicKey = $dnsRecord->getPublicSigningKey(); $publicKey = $dnsRecord->getPublicSigningKey();
if($domain === null || $endpoint === null || $publicKey === null)
{
throw new DatabaseOperationException('Failed to add a resolved server to the database: Invalid parameters');
}
if(self::resolvedServerExists($domain)) if(self::resolvedServerExists($domain))
{ {
$statement = Database::getConnection()->prepare("UPDATE resolved_dns_records SET rpc_endpoint=?, public_key=?, expires=?, updated=? WHERE domain=?"); try
$statement->bindParam(1, $endpoint);
$statement->bindParam(2, $publicKey);
$expires = (new DateTime())->setTimestamp($dnsRecord->getExpires());
$statement->bindParam(3, $expires);
$updated = new DateTime();
$statement->bindParam(4, $updated);
$statement->bindParam(5, $domain);
$statement->execute();
if($statement->rowCount() === 0)
{ {
throw new DatabaseOperationException('Failed to update a resolved server in the database'); $statement = Database::getConnection()->prepare("UPDATE resolved_dns_records SET rpc_endpoint=:rpc_endpoint, public_key=:public_key, expires=:expires, updated=:updated WHERE domain=:domain");
$statement->bindParam(':rpc_endpoint', $endpoint);
$statement->bindParam(':public_key', $publicKey);
$expires = (new DateTime())->setTimestamp($dnsRecord->getExpires())->format('Y-m-d H:i:s');
$statement->bindParam(':expires', $expires);
$updated = (new DateTime())->format('Y-m-d H:i:s');
$statement->bindParam(':updated', $updated);
$statement->bindParam(':domain', $domain);
$statement->execute();
}
catch(PDOException $e)
{
throw new DatabaseOperationException('Failed to update a resolved server in the database', $e);
} }
return; return;
@ -161,20 +168,13 @@
try try
{ {
$statement = Database::getConnection()->prepare("INSERT INTO resolved_dns_records (domain, rpc_endpoint, public_key, expires, updated) VALUES (?, ?, ?, ?, ?)"); $statement = Database::getConnection()->prepare("INSERT INTO resolved_dns_records (domain, rpc_endpoint, public_key, expires) VALUES (:domain, :rpc_endpoint, :public_key, :expires)");
$statement->bindParam(1, $domain); $statement->bindParam(':domain', $domain);
$statement->bindParam(2, $endpoint); $statement->bindParam(':rpc_endpoint', $endpoint);
$statement->bindParam(3, $publicKey); $statement->bindParam(':public_key', $publicKey);
$expires = (new DateTime())->setTimestamp($dnsRecord->getExpires()); $expires = (new DateTime())->setTimestamp($dnsRecord->getExpires())->format('Y-m-d H:i:s');
$statement->bindParam(4, $expires); $statement->bindParam(':expires', $expires);
$updated = new DateTime();
$statement->bindParam(5, $updated);
$statement->execute(); $statement->execute();
if($statement->rowCount() === 0)
{
throw new DatabaseOperationException('Failed to add a resolved server to the database');
}
} }
catch(PDOException $e) catch(PDOException $e)
{ {