Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Implement watch methods for PDO driver #435

Merged
merged 3 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"ocramius/lazy-property": "^1.0",
"symfony/phpunit-bridge": "^5.2"
},
"suggest": {
"ext-pdo": "Required to pdo backend."
},
"scripts": {
"post-install-cmd": [
"@phpunit-setup"
Expand Down
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
76 changes: 75 additions & 1 deletion src/Db/PdoRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +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->tableWatches = sprintf('"%s"', $tableWatch);
$this->initSchema();
}

Expand Down Expand Up @@ -187,6 +192,13 @@ public function initSchema(): void
"main_pmu" INTEGER NOT NULL
)
', $this->table));
$this->pdo->exec(sprintf('
CREATE TABLE IF NOT EXISTS %s (
"id" CHAR(24) PRIMARY KEY,
"removed" TEXT NULL,
"name" TEXT NOT NULL
)
', $this->tableWatches));
}

public function saveProfile(array $data): void
Expand Down Expand Up @@ -228,4 +240,66 @@ public function saveProfile(array $data): void
', $this->table));
$stmt->execute($data);
}

public function saveWatch(array $data): bool
{
$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))
);
}
}
38 changes: 35 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,55 @@ 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