diff --git a/composer.json b/composer.json index 4cff9a616..f64ecde0f 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/config/config.default.php b/config/config.default.php index 0423d2b88..e0dbb5347 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -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. diff --git a/src/Db/PdoRepository.php b/src/Db/PdoRepository.php index 86a8f2c01..d9611bd2c 100644 --- a/src/Db/PdoRepository.php +++ b/src/Db/PdoRepository.php @@ -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(); } @@ -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 @@ -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)) + ); + } } diff --git a/src/Searcher/PdoSearcher.php b/src/Searcher/PdoSearcher.php index 9f0cdb91b..ccb867f9e 100644 --- a/src/Searcher/PdoSearcher.php +++ b/src/Searcher/PdoSearcher.php @@ -6,6 +6,7 @@ use XHGui\Exception\NotImplementedException; use XHGui\Options\SearchOptions; use XHGui\Profile; +use XHGui\Util; class PdoSearcher implements SearcherInterface { @@ -157,17 +158,46 @@ 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; } /** @@ -175,6 +205,8 @@ public function getAllWatches() */ public function truncateWatches() { + $this->db->truncateWatches(); + return $this; } diff --git a/src/ServiceProvider/PdoStorageProvider.php b/src/ServiceProvider/PdoStorageProvider.php index 5bf22f181..232d0b60a 100644 --- a/src/ServiceProvider/PdoStorageProvider.php +++ b/src/ServiceProvider/PdoStorageProvider.php @@ -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) {