Skip to content

Commit

Permalink
Fixed code styles
Browse files Browse the repository at this point in the history
  • Loading branch information
janw-me committed Mar 17, 2024
1 parent 9f4a7f8 commit 32d9403
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# catalogs-php
A PHP framework to talk to Catalogs. Part of the [CloudFest 2024 Hackathon](https://hackathon.cloudfest.com/project/integrating-mariadb-catalogs-with-php-platforms/).

# Useage
# Installation

Include the catalog-php with composer:

composer require mariadb/catalogs-php
```bash
composer require mariadb/catalogs-php:dev-main
```

Include autoloader in your project.

```php
<?php require_once('vendor/autoload.php');
```

2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 25 additions & 16 deletions src/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Catalog
// This is too low, because this is a beta version we are developing for.
public const MINIMAL_MARIA_VERSION = '11.0.2';


/**
* Class constructor.
*
Expand Down Expand Up @@ -76,7 +77,7 @@ public function __construct(
/**
* Create a new catalog
*
* @param string $catName The new Catalog name.
* @param string $catName The new Catalog name.
*
* @return int
*/
Expand All @@ -87,7 +88,7 @@ public function create(string $catName): int
throw new Exception('Catalog name already exists.');
}

$root_privileges = $this->connection->query("SELECT * FROM mysql.global_priv WHERE User='{$this->dbUser}' AND Host='%';");
$rootPrivileges = $this->connection->query("SELECT * FROM mysql.global_priv WHERE User='{$this->dbUser}' AND Host='%';");

$scripts = [
'src/create_catalog_sql/mysql_system_tables.sql',
Expand All @@ -96,8 +97,8 @@ public function create(string $catName): int
'src/create_catalog_sql/maria_add_gis_sp.sql',
'src/create_catalog_sql/mysql_sys_schema.sql',
];
$this->connection->exec('CREATE CATALOG IF NOT EXISTS ' .$catName);
$this->connection->exec('USE CATALOG ' . $catName);
$this->connection->exec('CREATE CATALOG IF NOT EXISTS '.$catName);
$this->connection->exec('USE CATALOG '.$catName);

$this->connection->exec('CREATE DATABASE IF NOT EXISTS mysql');
$this->connection->exec('USE mysql');
Expand All @@ -120,13 +121,17 @@ public function create(string $catName): int
$this->connection->exec($content);
}

if ($root_privileges->rowCount() > 0) {
foreach ($root_privileges as $privilege) {
$this->connection->exec("INSERT INTO mysql.global_priv VALUES ('{$privilege['Host']}', '{$privilege['User']}', '{$privilege['Priv']}');");
if ($rootPrivileges->rowCount() > 0) {
foreach ($rootPrivileges as $privilege) {
$host = $privilege['Host'];
$user = $privilege['User'];
$priv = $privilege['Priv'];
$this->connection->exec("INSERT INTO mysql.global_priv VALUES ('{$host}', '{$user}', '{$priv}');");
}
}

return $this->getPort($catName);

}


Expand Down Expand Up @@ -181,26 +186,26 @@ public function drop(string $catName): bool
);

try {
// enter the catalog
$this->connection->exec('USE CATALOG ' . $catName);
// Enter the catalog.
$this->connection->exec('USE CATALOG '.$catName);

// check if there are any tables besides mysql, sys, performance_schema and information_schema
// 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'])) {
throw new \Exception('Catalog is not empty');
}
}

// drop mysql, sys and performance_schema
// Drop mysql, sys and performance_schema.
$this->connection->exec('DROP DATABASE IF EXISTS mysql');
$this->connection->exec('DROP DATABASE IF EXISTS sys');
$this->connection->exec('DROP DATABASE IF EXISTS performance_schema');

// drop the catalog
$this->connection->exec('DROP CATALOG ' . $catName);
// Drop the catalog.
$this->connection->exec('DROP CATALOG '.$catName);
} catch (\PDOException $e) {
throw new \Exception('Error dropping catalog: ' . $e->getMessage());
throw new \Exception('Error dropping catalog: '.$e->getMessage());
}

return true;
Expand All @@ -221,17 +226,21 @@ private function alter()

}


/**
* @return void
*/
public function createAdminUserForCatalog(string $catalog, string $userName, string $password, string $authHost = 'localhost'): void
public function createAdminUserForCatalog(string $catalog, string $userName, string $password, string $authHost='localhost'): void
{
$this->connection->exec("USE CATALOG {$catalog}");
$this->connection->exec("USE mysql");

$this->connection = new \PDO("mysql:host={$this->dbHost};port={$this->dbPort};dbname={$catalog}.mysql", $this->dbUser, $this->dbPass, $this->dbOptions);

$this->connection->prepare("CREATE USER ?@? IDENTIFIED BY ?;")->execute([$userName, $authHost, $password]);
$this->connection->prepare("GRANT ALL PRIVILEGES ON `%`.* TO ?@? IDENTIFIED BY ? WITH GRANT OPTION;")->execute([$userName, $authHost,$password]);
$this->connection->prepare("GRANT ALL PRIVILEGES ON `%`.* TO ?@? IDENTIFIED BY ? WITH GRANT OPTION;")->execute([$userName, $authHost, $password]);

}


}

0 comments on commit 32d9403

Please sign in to comment.