X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FDatabase%2FView.php;h=a31151c2117a3d8d63a848372f2515d3164a4ac2;hb=277cd5143361ebc2024cc810740d50be3a93a4cb;hp=e1335d9df65f049307877085451e832b23cab5d7;hpb=1b0a8ec5c0e46e2ef45004b0c1f65498c5b16b36;p=friendica.git diff --git a/src/Database/View.php b/src/Database/View.php index e1335d9df6..a31151c211 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'; + } }