From 7e6a9bd893eee66dfb1119bd291fa993e0688706 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 29 Nov 2020 19:06:43 +0000 Subject: [PATCH 1/2] Ensure that a view is a view and not a table --- src/Database/View.php | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Database/View.php b/src/Database/View.php index e1335d9df6..bfdcdff7ca 100644 --- a/src/Database/View.php +++ b/src/Database/View.php @@ -111,7 +111,11 @@ class View } } - $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name)); + if (self::isView($name)) { + $sql = sprintf("DROP VIEW `%s`", DBA::escape($name)); + } elseif (self::isTable($name)) { + $sql = sprintf("DROP TABLE `%s`", DBA::escape($name)); + } if ($verbose) { echo $sql . ";\n"; @@ -134,4 +138,40 @@ class View return $r; } + + /** + * Check if the given table/view is a view + * + * @param string $view + * @return boolean "true" if it's a view + */ + private static function isView(string $view) + { + $status = DBA::selectFirst(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_TYPE'], + ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $view]); + + if (empty($status['TABLE_TYPE'])) { + return false; + } + + return $status['TABLE_TYPE'] == 'VIEW'; + } + + /** + * Check if the given table/view is a view + * + * @param string $table + * @return boolean "true" if it's a table + */ + private static function isTable(string $table) + { + $status = DBA::selectFirst(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_TYPE'], + ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $table]); + + if (empty($status['TABLE_TYPE'])) { + return false; + } + + return $status['TABLE_TYPE'] == 'BASE TABLE'; + } } From eb48a3606142ada0781239b3aecf23a40e4b62e6 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 29 Nov 2020 21:59:24 +0100 Subject: [PATCH 2/2] Update src/Database/View.php Co-authored-by: Hypolite Petovan --- src/Database/View.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Database/View.php b/src/Database/View.php index bfdcdff7ca..51d051786d 100644 --- a/src/Database/View.php +++ b/src/Database/View.php @@ -168,10 +168,10 @@ class View $status = DBA::selectFirst(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_TYPE'], ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $table]); - if (empty($status['TABLE_TYPE'])) { - return false; - } + if (empty($status['TABLE_TYPE'])) { + return false; + } - return $status['TABLE_TYPE'] == 'BASE TABLE'; + return $status['TABLE_TYPE'] == 'BASE TABLE'; } }