Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeja committed Mar 17, 2024
2 parents a5b9f22 + 60e0871 commit 6a6e1b8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
Empty file.
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: test

on: [push]

concurrency: production_environment

jobs:
test:
runs-on: ubuntu-latest

name: Check code style

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Install PHP dependencies (composer)
run: composer install

- name: Check code style
run: composer phpcs
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# 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');
```

# Contributing
We welcome contributions! Please see our contribution guidelines for details on how to submit pull requests, report issues, or suggest improvements.

# License
This project is licensed under the General Public License (GPLv3). See the LICENSE file for details.
1 change: 0 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<rule ref="Squiz.Commenting.DocCommentAlignment"/>
<rule ref="Squiz.Commenting.EmptyCatchComment"/>
<rule ref="Squiz.Commenting.InlineComment"/>
<rule ref="Squiz.Commenting.LongConditionClosingComment"/>
<rule ref="Squiz.Commenting.PostStatementComment"/>
<rule ref="Squiz.Commenting.VariableComment"/>
<rule ref="Squiz.Formatting.OperatorBracket"/>
Expand Down
69 changes: 49 additions & 20 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 @@ -81,7 +82,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 @@ -92,7 +93,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 @@ -101,8 +102,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 @@ -125,13 +126,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 @@ -186,26 +191,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'])) {
if (in_array($table['Database'], ['mysql', 'sys', 'performance_schema', 'information_schema']) === false) {
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 @@ -226,17 +231,41 @@ private function alter()

}


/**
* Create admin user for a catalog
*
* @param string $catalog 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 $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 = 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("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]);
}


}

0 comments on commit 6a6e1b8

Please sign in to comment.