From c2396e575470ab4d2dec506bc4134c6706dc75d1 Mon Sep 17 00:00:00 2001 From: blackcoder87 Date: Tue, 3 Dec 2024 15:43:17 +0100 Subject: [PATCH] Add ifForeignKeyConstraintExists and getDatabaseName --- application/libraries/Ilch/Database/Mysql.php | 35 +++++++++++++++++-- tests/libraries/ilch/Database/MysqlTest.php | 12 +++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/application/libraries/Ilch/Database/Mysql.php b/application/libraries/Ilch/Database/Mysql.php index dce8a012e..588d7a0b6 100644 --- a/application/libraries/Ilch/Database/Mysql.php +++ b/application/libraries/Ilch/Database/Mysql.php @@ -20,12 +20,17 @@ class Mysql /** * @var string|null */ - protected $prefix = null; + protected ?string $prefix = null; /** * @var \mysqli|null */ - protected $conn = null; + protected ?\mysqli $conn = null; + + /** + * @var string|null + */ + protected ?string $dbName = null; /** * Close database connection. @@ -79,9 +84,20 @@ public function setDatabase(string $db): bool return false; } + $this->dbName = $db; return @$this->conn->select_db($db); } + /** + * Get name of currently used database. + * + * @return string + */ + public function getDatabaseName(): string + { + return $this->dbName; + } + /** * Connects to database. * @@ -213,6 +229,21 @@ public function ifColumnExists(string $table, string $column): bool return \mysqli_num_rows($result) > 0; } + /** + * Check if Foreign Key Contraint exists. + * + * @param string $table table without prefix + * @param string $constraintName constraint name + * @return bool + * @throws Exception + * @since 2.2.7 + */ + public function ifForeignKeyConstraintExists(string $table, string $constraintName): bool + { + $table = \str_replace('[prefix]_', '', $table); + return $this->queryCell("SELECT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='" . $this->getDatabaseName() . "' AND table_name='" . '[prefix]_' . $table . "' AND constraint_name='" . $constraintName . "');"); + } + /** * Create Select Statement Query Builder * diff --git a/tests/libraries/ilch/Database/MysqlTest.php b/tests/libraries/ilch/Database/MysqlTest.php index 9e2f243c0..873f30e36 100644 --- a/tests/libraries/ilch/Database/MysqlTest.php +++ b/tests/libraries/ilch/Database/MysqlTest.php @@ -90,4 +90,16 @@ public function testInsertWithEmptyValue() ->fetchCell(); self::assertEquals(1, $result, 'The db entry has not being inserted with an empty string.'); } + + /** + * Tests if ifForeignKeyConstraintExists works as expected. + * + * @return void + */ + public function testIfForeignKeyConstraintExists() + { + self::assertTrue($this->db->ifForeignKeyConstraintExists('[prefix]_menu_items', 'FK_[prefix]_menu_items_[prefix]_menu'), 'Returning true for an existing foreign key constraint failed.'); + self::assertTrue($this->db->ifForeignKeyConstraintExists('menu_items', 'FK_menu_items_menu'), 'Returning true for an existing foreign key constraint failed. No prefixes.'); + self::assertFalse($this->db->ifForeignKeyConstraintExists('[prefix]_menu_items', 'wrongConstraintName'), 'Returning false for a clearly wrong contraint name failed.'); + } }