X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FDatabase%2FView.php;h=9af4de0fce79de994e64133f6398e89d1276b7e3;hb=e8becc153856debb927d7afac17bae5c80a0abb7;hp=c7f55ee7526c7c9c3dc4a43a08bd0636b1436633;hpb=51bc5279a07397e28b35fbeefb27ea6041f9b6cd;p=friendica.git diff --git a/src/Database/View.php b/src/Database/View.php index c7f55ee752..9af4de0fce 100644 --- a/src/Database/View.php +++ b/src/Database/View.php @@ -1,6 +1,6 @@ $structure) { @@ -88,7 +102,7 @@ class View { $database = self::definition($basePath, false); - foreach ($database AS $name => $structure) { + foreach ($database as $name => $structure) { echo "--\n"; echo "-- VIEW $name\n"; echo "--\n"; @@ -103,7 +117,7 @@ class View $r = true; $sql_rows = []; - foreach ($structure["fields"] AS $fieldname => $origin) { + foreach ($structure["fields"] as $fieldname => $origin) { if (is_string($origin)) { $sql_rows[] = $origin . " AS `" . DBA::escape($fieldname) . "`"; } elseif (is_array($origin) && (sizeof($origin) == 2)) { @@ -111,13 +125,17 @@ class View } } - $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name)); + if (self::isView($name)) { + $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name)); + } elseif (self::isTable($name)) { + $sql = sprintf("DROP TABLE IF EXISTS `%s`", DBA::escape($name)); + } - if ($verbose) { + if (!empty($sql) && $verbose) { echo $sql . ";\n"; } - if ($action) { + if (!empty($sql) && $action) { DBA::e($sql); } @@ -134,4 +152,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 table + * + * @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'; + } }