Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

4 changes: 2 additions & 2 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ abstract public function create(string $name): bool;
* Optionally check if collection exists in database
*
* @param string $database database name
* @param string $collection (optional) collection name
* @param string|null $collection (optional) collection name
*
* @return bool
*/
abstract public function exists(string $database, ?string $collection): bool;
abstract public function exists(string $database, ?string $collection = null): bool;

/**
* List Databases
Expand Down
6 changes: 5 additions & 1 deletion src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public function create(string $name): bool
{
$name = $this->filter($name);

$sql = "CREATE DATABASE IF NOT EXISTS `{$name}` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;";
if ($this->exists($name)) {
return true;
}

$sql = "CREATE DATABASE `{$name}` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;";

$sql = $this->trigger(Database::EVENT_DATABASE_CREATE, $sql);

Expand Down
6 changes: 5 additions & 1 deletion src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public function create(string $name): bool
{
$name = $this->filter($name);

$sql = "CREATE SCHEMA IF NOT EXISTS \"{$name}\"";
if ($this->exists($name)) {
return true;
}

$sql = "CREATE SCHEMA \"{$name}\"";
$sql = $this->trigger(Database::EVENT_DATABASE_CREATE, $sql);

return $this->getPDO()
Expand Down
35 changes: 10 additions & 25 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,45 +50,30 @@ public function ping(): bool
* @return bool
* @throws Exception
*/
public function exists(string $database, ?string $collection): bool
public function exists(string $database, ?string $collection = null): bool
{
$database = $this->filter($database);

if (!\is_null($collection)) {
$collection = $this->filter($collection);

$select = 'TABLE_NAME';
$from = 'INFORMATION_SCHEMA.TABLES';
$where = 'TABLE_SCHEMA = :schema AND TABLE_NAME = :table';
$match = "{$this->getNamespace()}_{$collection}";
} else {
$select = 'SCHEMA_NAME';
$from = 'INFORMATION_SCHEMA.SCHEMATA';
$where = 'SCHEMA_NAME = :schema';
$match = $database;
}

$stmt = $this->getPDO()
->prepare("SELECT {$select}
FROM {$from}
WHERE {$where};");

$stmt->bindValue(':schema', $database, PDO::PARAM_STR);

if (!\is_null($collection)) {
$stmt = $this->getPDO()->prepare("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = :schema AND TABLE_NAME = :table");
$stmt->bindValue(':schema', $database, PDO::PARAM_STR);
$stmt->bindValue(':table', "{$this->getNamespace()}_{$collection}", PDO::PARAM_STR);
} else {
$stmt = $this->getPDO()->prepare("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = :schema");
$stmt->bindValue(':schema', $database, PDO::PARAM_STR);
}

$stmt->execute();

$document = $stmt->fetchAll();
$stmt->closeCursor();
if (!empty($document)) {
$document = $document[0];

if (empty($document)) {
return false;
}

return (($document[$select] ?? '') === $match) || // case insensitive check
(($document[strtolower($select)] ?? '') === $match);
return true;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ class SQLite extends MariaDB
* Optionally check if collection exists in Database
*
* @param string $database
* @param string $collection
* @param string|null $collection
* @return bool
* @throws Exception
* @throws PDOException
* @throws DatabaseException
*/
public function exists(string $database, ?string $collection): bool
public function exists(string $database, ?string $collection = null): bool
{
$database = $this->filter($database);

Expand Down