Save to database #13

Merged
haylan merged 42 commits from save-to-database into main 2026-07-12 21:51:57 +00:00
Showing only changes of commit e7f14dfd35 - Show all commits
+20
View File
@@ -13,6 +13,9 @@ class ContributionStore
private PDO $pdo;
private const int DAY_IN_SECONDS = 86400;
/**
* Opens (creating if needed) the SQLite store at $dbPath and ensures the schema exists.
*/
public function __construct(
private readonly string $dbPath = "%kernel.project_dir%/var/data/contributions.db",
private readonly ?int $retentionDays = null,
@@ -38,6 +41,9 @@ class ContributionStore
');
}
/**
* Inserts a contribution count, overwriting any existing count for the same provider/date.
*/
public function add(string $provider, int $unixtime, int $count): void
{
$stmt = $this->pdo->prepare('
@@ -48,6 +54,9 @@ class ContributionStore
$stmt->execute([$provider, $unixtime, $count]);
}
/**
* Deletes the contribution row for the given provider/date, if any.
*/
public function remove(string $provider, int $unixtime): void
{
$stmt = $this->pdo->prepare('
@@ -58,6 +67,8 @@ class ContributionStore
}
/**
* Upserts a batch of date => count pairs for a provider via repeated add() calls.
*
* @param array<int, int> $dateCounts unix timestamp => count
*/
public function merge(string $provider, array $dateCounts): void
@@ -67,6 +78,9 @@ class ContributionStore
}
}
/**
* Returns the most recent stored unix timestamp for a provider, or null if it has no rows.
*/
public function latestDate(string $provider): ?int
{
$stmt = $this->pdo->prepare('SELECT MAX(date) FROM contributions WHERE provider = ?');
@@ -76,6 +90,9 @@ class ContributionStore
return $result !== null ? (int) $result : null;
}
/**
* Returns all stored contributions for a provider, optionally limited to the last $sinceDays days.
*/
public function all(string $provider, ?int $sinceDays = null): ContributionCollection
{
$sql = 'SELECT provider, date, count FROM contributions WHERE provider = ?';
@@ -97,6 +114,9 @@ class ContributionStore
return new ContributionCollection(...$contributions);
}
/**
* Deletes rows older than the configured retention window; a no-op if retention is unset or 0.
*/
public function prune(): void
{
if ($this->retentionDays === null || $this->retentionDays === 0) {