Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into tennant_examble
Browse files Browse the repository at this point in the history
  • Loading branch information
alegerber committed Mar 18, 2024
2 parents de80c57 + c179b1d commit 8337217
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
8 changes: 4 additions & 4 deletions implementations-dummy-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
67 changes: 40 additions & 27 deletions src/Catalog.php → src/CatalogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package Mariadb\CatalogsPHP
*/
class Catalog
class CatalogManager
{
/**
* The connection to the MariaDB server.
Expand All @@ -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');
}
}

Expand All @@ -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',
Expand Down Expand Up @@ -80,25 +80,30 @@ 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
);
}

// Check support for catalogs.
if ($this->isCatalogSupported() === false) {
throw new CatalogManagerException('The MariaDB server does not support catalogs.');
}
}


/**
* 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) {
throw new Exception('Catalog name already exists.');
if (in_array($catalogName, array_keys($this->list())) === true) {
throw new CatalogManagerException('Catalog name already exists.');
}

$rootPrivileges = $this->connection->query("SELECT * FROM mysql.global_priv WHERE User='{$this->dbUser}' AND Host='%';");
Expand All @@ -110,9 +115,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');
Expand Down Expand Up @@ -144,18 +149,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);
Expand Down Expand Up @@ -184,25 +189,25 @@ 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.
* @throws CatalogManagerException 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');
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');
}
}

Expand All @@ -212,9 +217,9 @@ 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());
throw new CatalogManagerException('Error dropping catalog: ' . $e->getMessage());
}

return true;
Expand All @@ -237,25 +242,25 @@ 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
*
* @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
Expand All @@ -269,4 +274,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';
}
}
2 changes: 1 addition & 1 deletion src/Exception.php → src/CatalogManagerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @package Mariadb\CatalogsPHP
*/
class Exception extends \Exception
class CatalogManagerException extends \Exception
{
/**
* Constructs the Exception.
Expand Down
6 changes: 3 additions & 3 deletions tests/CatalogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mariadb\CatalogsPHP\Tests;

use Mariadb\CatalogsPHP\Catalog;
use Mariadb\CatalogsPHP\CatalogManager;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -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;

Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 8337217

Please sign in to comment.