Page MenuHomeDevCentral

D2511.id6337.diff
No OneTemporary

D2511.id6337.diff

diff --git a/src/DatabaseEngine.php b/src/DatabaseEngine.php
--- a/src/DatabaseEngine.php
+++ b/src/DatabaseEngine.php
@@ -30,6 +30,13 @@
protected abstract function getExceptionContext (): array;
+ /**
+ * Determines if the specified table or view exists.
+ */
+ public abstract function isExistingTable (
+ string $database, string $table
+ ) : bool;
+
///
/// Engine mechanics
///
diff --git a/src/Engines/MySQLiEngine.php b/src/Engines/MySQLiEngine.php
--- a/src/Engines/MySQLiEngine.php
+++ b/src/Engines/MySQLiEngine.php
@@ -16,6 +16,8 @@
class MySQLiEngine extends DatabaseEngine {
+ use WithMySQL;
+
/**
* The connection identifier
*/
diff --git a/src/Engines/WithMySQL.php b/src/Engines/WithMySQL.php
new file mode 100644
--- /dev/null
+++ b/src/Engines/WithMySQL.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Keruald\Database\Engines;
+
+trait WithMySQL {
+
+ public function isExistingTable (string $database, string $table) : bool {
+ $escapedTable = $this->escape($table);
+ $sql = "SHOW TABLE STATUS
+ FROM `$database`
+ WHERE Name = '$escapedTable'";
+
+ return $this->queryScalar($sql) === $table;
+ }
+
+ public function isView (string $database, string $view) : bool {
+ $escapedView = $this->escape($view);
+ $escapedDatabase = $this->escape($database);
+
+ $sql = <<<EOF
+SELECT TABLE_TYPE
+FROM information_schema.tables
+WHERE TABLE_SCHEMA = "$escapedDatabase" AND TABLE_NAME = "$escapedView"
+EOF;
+
+ return $this->queryScalar($sql) === "VIEW";
+ }
+
+}
diff --git a/tests/Engines/MySQLiEngineTest.php b/tests/Engines/MySQLiEngineTest.php
--- a/tests/Engines/MySQLiEngineTest.php
+++ b/tests/Engines/MySQLiEngineTest.php
@@ -13,8 +13,10 @@
private MySQLiEngine $db;
+ const DB_NAME = "test_keruald_db";
+
protected function setUp (): void {
- $this->db = new MySQLiEngine('localhost', '', '', 'test_keruald_db');
+ $this->db = new MySQLiEngine('localhost', '', '', self::DB_NAME);
}
public function testLoad () {
@@ -22,7 +24,7 @@
'host' => 'localhost',
'username' => '',
'password' => '',
- 'database' => 'test_keruald_db',
+ 'database' => self::DB_NAME,
]);
$this->assertInstanceOf("mysqli", $instance->getUnderlyingDriver());
@@ -35,7 +37,7 @@
'host' => 'localhost',
'username' => 'notexisting',
'password' => 'notexistingeither',
- 'database' => 'test_keruald_db',
+ 'database' => self::DB_NAME,
]);
}
@@ -175,4 +177,32 @@
$this->assertEquals($expected, $this->db->error());
}
+ public function testIsView () {
+ $this->assertTrue($this->db->isView(self::DB_NAME, "ships_count"));
+ }
+
+ public function testIsViewWhenItIsTable () {
+ $this->assertFalse($this->db->isView(self::DB_NAME, "ships"));
+ }
+
+ public function testIsViewWhenNotExisting () {
+ $this->assertFalse($this->db->isView(self::DB_NAME, "notexisting"));
+ }
+
+ public function testIsExisting () {
+ $this->assertTrue($this->db->isExistingTable(self::DB_NAME, "ships"));
+ }
+
+ public function testIsExistingWithView () {
+ $this->assertTrue($this->db->isExistingTable(
+ self::DB_NAME, "ships_count")
+ );
+ }
+
+ public function testIsExistingWhenNotExisting () {
+ $this->assertFalse($this->db->isExistingTable(
+ self::DB_NAME, "notexisting")
+ );
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 18:37 (18 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2258095
Default Alt Text
D2511.id6337.diff (3 KB)

Event Timeline