Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ifForeignKeyConstraintExists and getDatabaseName #1134

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 33 additions & 2 deletions application/libraries/Ilch/Database/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
*
Expand Down
12 changes: 12 additions & 0 deletions tests/libraries/ilch/Database/MysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
blackcoder87 marked this conversation as resolved.
Show resolved Hide resolved
self::assertFalse($this->db->ifForeignKeyConstraintExists('[prefix]_menu_items', 'wrongConstraintName'), 'Returning false for a clearly wrong contraint name failed.');
}
}
Loading