From c661c48ad746a914329a903831bf39c0f9fdba60 Mon Sep 17 00:00:00 2001 From: Florian Blaser Date: Mon, 18 Mar 2024 10:15:35 +0100 Subject: [PATCH 1/4] :recycle: unify parameter names --- src/Catalog.php | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Catalog.php b/src/Catalog.php index 910751c..3473b7c 100644 --- a/src/Catalog.php +++ b/src/Catalog.php @@ -90,14 +90,14 @@ public function __construct( /** * Create a new catalog * - * @param string $catName The new Catalog name. + * @param string $catalogName The new Catalog name. * * @return int */ - public function create(string $catName): int + public function create(string $catalogName): int { // Check if the Catalog name is valid. - if (in_array($catName, array_keys($this->list())) === true) { + if (in_array($catalogName, array_keys($this->list())) === true) { throw new Exception('Catalog name already exists.'); } @@ -110,9 +110,9 @@ public function create(string $catName): int __DIR__ . '/create_catalog_sql/maria_add_gis_sp.sql', __DIR__ . '/create_catalog_sql/mysql_sys_schema.sql', ]; - $this->checkCatalogName($catName); - $this->connection->exec('CREATE CATALOG IF NOT EXISTS ' . $catName); - $this->connection->exec('USE CATALOG ' . $catName); + $this->checkCatalogName($catalogName); + $this->connection->exec('CREATE CATALOG IF NOT EXISTS ' . $catalogName); + $this->connection->exec('USE CATALOG ' . $catalogName); $this->connection->exec('CREATE DATABASE IF NOT EXISTS mysql'); $this->connection->exec('USE mysql'); @@ -144,18 +144,18 @@ public function create(string $catName): int } } - return $this->getPort($catName); + return $this->getPort($catalogName); } /** * Get the port of a catalog. * - * @param string $catName The catalog name. + * @param string $catalogName The catalog name. * * @return int */ - public function getPort(string $catName): int + public function getPort(string $catalogName): int { // TODO: wait for the functionality to be implemented in the server. return ($this->dbPort ?? 0); @@ -184,19 +184,19 @@ public function list(): array /** * Drop a catalog. * - * @param string $catName The catalog name. + * @param string $catalogName The catalog name. * * @return void * * @throws PDOException If a PDO error occurs during the catalog drop attempt. * @throws Exception If a general error occurs during catalog drop. */ - public function drop(string $catName): bool + public function drop(string $catalogName): bool { try { // Enter the catalog. - $this->checkCatalogName($catName); - $this->connection->exec('USE CATALOG ' . $catName); + $this->checkCatalogName($catalogName); + $this->connection->exec('USE CATALOG ' . $catalogName); // Check if there are any tables besides mysql, sys, performance_schema and information_schema. $tables = $this->connection->query('SHOW DATABASES'); @@ -212,7 +212,7 @@ public function drop(string $catName): bool $this->connection->exec('DROP DATABASE IF EXISTS performance_schema'); // Drop the catalog. - $this->connection->exec('DROP CATALOG ' . $catName); + $this->connection->exec('DROP CATALOG ' . $catalogName); } catch (\PDOException $e) { throw new Exception('Error dropping catalog: ' . $e->getMessage()); } @@ -237,7 +237,7 @@ private function alter() /** * Create admin user for a catalog * - * @param string $catalog The catalog name + * @param string $catalogName The catalog name * @param string $userName The user name * @param string $password The user password * @param string $authHost The database host @@ -245,17 +245,17 @@ private function alter() * @return void */ public function createAdminUserForCatalog( - string $catalog, + string $catalogName, string $userName, string $password, string $authHost = 'localhost' ): void { - $this->checkCatalogName($catalog); - $this->connection->exec("USE CATALOG {$catalog}"); + $this->checkCatalogName($catalogName); + $this->connection->exec("USE CATALOG {$catalogName}"); $this->connection->exec("USE mysql"); $this->connection = new \PDO( - "mysql:host={$this->dbHost};port={$this->dbPort};dbname={$catalog}.mysql", + "mysql:host={$this->dbHost};port={$this->dbPort};dbname={$catalogName}.mysql", $this->dbUser, $this->dbPass, $this->dbOptions From a20fc2d4620801be77b26c1474c1b2123a5e1ec5 Mon Sep 17 00:00:00 2001 From: Florian Blaser Date: Mon, 18 Mar 2024 10:22:18 +0100 Subject: [PATCH 2/4] :recycle: Rename the classes to CatalogManager aand CatalogManagerException --- implementations-dummy-code.php | 8 ++++---- src/{Catalog.php => CatalogManager.php} | 16 ++++++++-------- ...Exception.php => CatalogManagerException.php} | 2 +- tests/CatalogTest.php | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) rename src/{Catalog.php => CatalogManager.php} (93%) rename src/{Exception.php => CatalogManagerException.php} (96%) diff --git a/implementations-dummy-code.php b/implementations-dummy-code.php index 561fed1..8e2c328 100644 --- a/implementations-dummy-code.php +++ b/implementations-dummy-code.php @@ -2,14 +2,14 @@ require ('vendor/autoload.php'); -use Mariadb\CatalogsPHP\Catalog; +use Mariadb\CatalogsPHP\CatalogManager; /** * Use case 1: Create a new catalog and connect it to a wp install. */ -$cat = new Catalog('127.0.0.1', 3306, 'root', 'rootpassword'); -$catPort = $cat->create('wp_1'); -$cat->createAdminUserForCatalog('wp_1', 'admin', 'adminpassword', '%'); +$cat = new CatalogManager('127.0.0.1', 3306, 'root', 'rootpassword'); +$catPort = $cat->create('wp_2'); +$cat->createAdminUserForCatalog('wp_2', 'admin', 'adminpassword', '%'); // Using PDO, Create a DB and user in the collection using the $catPort. // Set DB_NAME with the "name:$catPort" diff --git a/src/Catalog.php b/src/CatalogManager.php similarity index 93% rename from src/Catalog.php rename to src/CatalogManager.php index 3473b7c..ad9a5a3 100644 --- a/src/Catalog.php +++ b/src/CatalogManager.php @@ -9,7 +9,7 @@ * * @package Mariadb\CatalogsPHP */ -class Catalog +class CatalogManager { /** * The connection to the MariaDB server. @@ -23,7 +23,7 @@ class Catalog private function checkCatalogName($catalogName): void { if (preg_match('/[^a-zA-Z0-9_]/', $catalogName) === 1) { - throw new Exception('Invalid catalog name'); + throw new CatalogManagerException('Invalid catalog name'); } } @@ -48,7 +48,7 @@ private function checkCatalogName($catalogName): void * @param \PDO|null $pdo Optional. An existing PDO connection to use. Default is null. * * @throws PDOException If a PDO error occurs during the connection attempt. - * @throws Exception If a general error occurs during instantiation. + * @throws CatalogManagerException If a general error occurs during instantiation. */ public function __construct( protected string $dbHost = 'localhost', @@ -80,7 +80,7 @@ public function __construct( $version = $versionQuery->fetchColumn(); if (version_compare($version, self::MINIMAL_MARIA_VERSION, '<') === true) { - throw new Exception( + throw new CatalogManagerException( 'The MariaDB version is too low. The minimal version is ' . self::MINIMAL_MARIA_VERSION ); } @@ -98,7 +98,7 @@ public function create(string $catalogName): int { // Check if the Catalog name is valid. if (in_array($catalogName, array_keys($this->list())) === true) { - throw new Exception('Catalog name already exists.'); + throw new CatalogManagerException('Catalog name already exists.'); } $rootPrivileges = $this->connection->query("SELECT * FROM mysql.global_priv WHERE User='{$this->dbUser}' AND Host='%';"); @@ -189,7 +189,7 @@ public function list(): array * @return void * * @throws PDOException If a PDO error occurs during the catalog drop attempt. - * @throws Exception If a general error occurs during catalog drop. + * @throws CatalogManagerException If a general error occurs during catalog drop. */ public function drop(string $catalogName): bool { @@ -202,7 +202,7 @@ public function drop(string $catalogName): bool $tables = $this->connection->query('SHOW DATABASES'); foreach ($tables as $table) { if (in_array($table['Database'], ['mysql', 'sys', 'performance_schema', 'information_schema']) === false) { - throw new Exception('Catalog is not empty'); + throw new CatalogManagerException('Catalog is not empty'); } } @@ -214,7 +214,7 @@ public function drop(string $catalogName): bool // Drop the catalog. $this->connection->exec('DROP CATALOG ' . $catalogName); } catch (\PDOException $e) { - throw new Exception('Error dropping catalog: ' . $e->getMessage()); + throw new CatalogManagerException('Error dropping catalog: ' . $e->getMessage()); } return true; diff --git a/src/Exception.php b/src/CatalogManagerException.php similarity index 96% rename from src/Exception.php rename to src/CatalogManagerException.php index b56f340..4d9a0f2 100644 --- a/src/Exception.php +++ b/src/CatalogManagerException.php @@ -12,7 +12,7 @@ * * @package Mariadb\CatalogsPHP */ -class Exception extends \Exception +class CatalogManagerException extends \Exception { /** * Constructs the Exception. diff --git a/tests/CatalogTest.php b/tests/CatalogTest.php index f66a398..8633c7e 100644 --- a/tests/CatalogTest.php +++ b/tests/CatalogTest.php @@ -2,7 +2,7 @@ namespace Mariadb\CatalogsPHP\Tests; -use Mariadb\CatalogsPHP\Catalog; +use Mariadb\CatalogsPHP\CatalogManager; use PHPUnit\Framework\TestCase; /** @@ -13,7 +13,7 @@ class CatalogTest extends TestCase { /** - * @var Catalog $catalog The Catalog instance to test + * @var CatalogManager $catalog The Catalog instance to test */ private $catalog; @@ -31,7 +31,7 @@ protected function setUp(): void $this->pdoMock = $this->createMock(\PDO::class); // Inject the PDO mock into your Catalog class - $this->catalog = new Catalog('localhost', 3306, 'root', '', null, $this->pdoMock); + $this->catalog = new CatalogManager('localhost', 3306, 'root', '', null, $this->pdoMock); } /** From 740dba792db6b0c529e937eebedf747733394090 Mon Sep 17 00:00:00 2001 From: Wesley Stessens Date: Mon, 18 Mar 2024 10:34:10 +0100 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20Check=20if=20catalog=20extensio?= =?UTF-8?q?n=20is=20supported?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Catalog.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Catalog.php b/src/Catalog.php index 4084e7a..d4f5850 100644 --- a/src/Catalog.php +++ b/src/Catalog.php @@ -75,6 +75,11 @@ public function __construct( 'The MariaDB version is too low. The minimal version is ' . self::MINIMAL_MARIA_VERSION ); } + + // Check support for catalogs. + if ($this->isCatalogSupported() === false) { + throw new Exception('The MariaDB server does not support catalogs.'); + } } @@ -257,4 +262,12 @@ public function createAdminUserForCatalog( "GRANT ALL PRIVILEGES ON `%`.* TO ?@? IDENTIFIED BY ? WITH GRANT OPTION;" )->execute([$userName, $authHost, $password]); } + + public function isCatalogSupported(): bool + { + $query = $this->connection->query("SHOW GLOBAL VARIABLES LIKE 'CATALOGS';"); + $enabled = $query->fetchObject()?->Value ?? 'OFF'; + + return strtoupper($enabled) === 'ON'; + } } From c179b1d2ea27734b99dc041a58059f9de3c56ae5 Mon Sep 17 00:00:00 2001 From: Wesley Stessens Date: Mon, 18 Mar 2024 10:35:57 +0100 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20Fix=20renamed=20Exception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CatalogManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CatalogManager.php b/src/CatalogManager.php index 0877943..1fdddfa 100644 --- a/src/CatalogManager.php +++ b/src/CatalogManager.php @@ -87,7 +87,7 @@ public function __construct( // Check support for catalogs. if ($this->isCatalogSupported() === false) { - throw new Exception('The MariaDB server does not support catalogs.'); + throw new CatalogManagerException('The MariaDB server does not support catalogs.'); } }