Skip to content

Commit

Permalink
Implement watch: saveWatch, getAllWatches, getAllWatches, truncateWat…
Browse files Browse the repository at this point in the history
…ches

Co-authored-by: Elan Ruusamäe <glen@delfi.ee>
  • Loading branch information
fengqi and glensc committed Nov 6, 2021
1 parent fce62b9 commit 5bb1f24
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'user' => getenv('XHGUI_PDO_USER') ?: null,
'pass' => getenv('XHGUI_PDO_PASS') ?: null,
'table' => getenv('XHGUI_PDO_TABLE') ?: 'results',
'tableWatch' => getenv('XHGUI_PDO_TABLE_WATCHES') ?: 'watches',
],

// Database options for MongoDB.
Expand Down
81 changes: 79 additions & 2 deletions src/Db/PdoRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ class PdoRepository
/** @var string */
private $table;

/** @var string */
private $tableWatches;

/**
* @param PDO $pdo An open database connection
* @param string $table Table name where Xhgui profiles are stored
* @param string $tableWatch Table name where Xhgui watch functions are stored
*/
public function __construct(PDO $pdo, string $table)
public function __construct(PDO $pdo, string $table, string $tableWatch)
{
$this->pdo = $pdo;
$this->table = sprintf('"%s"', $table);
$this->initSchema();
$this->tableWatches = sprintf('"%s"', $tableWatch);
}

public function getLatest(): array
Expand Down Expand Up @@ -191,6 +195,7 @@ public function initSchema(): void

public function saveProfile(array $data): void
{
$this->initSchema();
$stmt = $this->pdo->prepare(sprintf('
INSERT INTO %s (
"id",
Expand Down Expand Up @@ -228,4 +233,76 @@ public function saveProfile(array $data): void
', $this->table));
$stmt->execute($data);
}

public function saveWatch(array $data) :bool
{
$this->initWatchesSchema();
$stmt = $this->pdo->prepare(sprintf('
INSERT INTO %s (
"id",
"removed",
"name"
) VALUES (
:_id,
:removed,
:name
)
', $this->tableWatches));
return $stmt->execute($data);
}

public function removeWatch(string $id): bool
{
$stmt = $this->pdo->prepare(sprintf('
DELETE FROM %s
WHERE id = :id
', $this->tableWatches));

return $stmt->execute(['id' => $id]);
}

public function updateWatch(array $data): bool
{
$stmt = $this->pdo->prepare(sprintf('
UPDATE %s SET
"removed" = :removed,
"name" = :name
WHERE
"id" = :_id
', $this->tableWatches));
return $stmt->execute($data);
}

public function getAllWatches(): Generator
{
$query = sprintf('
SELECT
"id",
"removed",
"name"
FROM %s
', $this->tableWatches);
$stmt = $this->pdo->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
yield $row;
}
}

public function truncateWatches()
{
return is_int(
$this->pdo->exec(sprintf('DELETE FROM %s', $this->tableWatches))
);
}

private function initWatchesSchema()
{
$this->pdo->exec(sprintf('
CREATE TABLE IF NOT EXISTS %s (
"id" CHAR(24) PRIMARY KEY,
"removed" TEXT NULL,
"name" TEXT NOT NULL
)
', $this->tableWatches));
}
}
36 changes: 33 additions & 3 deletions src/Searcher/PdoSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use XHGui\Exception\NotImplementedException;
use XHGui\Options\SearchOptions;
use XHGui\Profile;
use XHGui\Util;

class PdoSearcher implements SearcherInterface
{
Expand Down Expand Up @@ -157,24 +158,53 @@ public function truncate()
/**
* {@inheritdoc}
*/
public function saveWatch(array $data)
public function saveWatch(array $data): bool
{
if (empty($data['name'])) {
return false;
}

if (!empty($data['removed']) && isset($data['_id'])) {
$this->db->removeWatch($data['_id']);

return true;
}

if (empty($data['_id'])) {
$data['_id'] = Util::generateId();
$data['removed'] = 0;
$this->db->saveWatch($data);

return true;
}

$this->db->updateWatch($data);
return true;
}

/**
* {@inheritdoc}
*/
public function getAllWatches()
public function getAllWatches(): array
{
return [];
$results = [];
foreach ($this->db->getAllWatches() as $row) {
$results[] = [
'_id' => $row['id'],
'removed' => $row['removed'],
'name' => $row['name']
];
}

return $results;
}

/**
* {@inheritdoc}
*/
public function truncateWatches()
{
$this->db->truncateWatches();
return $this;
}

Expand Down
6 changes: 5 additions & 1 deletion src/ServiceProvider/PdoStorageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function register(Container $app): void
};

$app[PdoRepository::class] = static function ($app) {
return new PdoRepository($app['pdo'], $app['config']['pdo']['table']);
return new PdoRepository(
$app['pdo'],
$app['config']['pdo']['table'],
$app['config']['pdo']['tableWatch']
);
};

$app['searcher.pdo'] = static function ($app) {
Expand Down

0 comments on commit 5bb1f24

Please sign in to comment.