<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Friendica\Core\Hook;
use Friendica\DI;
-require_once __DIR__ . '/../../include/dba.php';
-
class View
{
/**
- * view definition loaded from config/dbview.config.php
+ * view definition loaded from static/dbview.config.php
*
* @var array
*/
private static $definition = [];
/**
- * Loads the database structure definition from the config/dbview.config.php file.
+ * Loads the database structure definition from the static/dbview.config.php file.
* On first pass, defines DB_UPDATE_VERSION constant.
*
* @see static/dbview.config.php
public static function create(bool $verbose, bool $action)
{
+ // Delete previously used views that aren't used anymore
+ foreach(['post-view', 'post-thread-view'] as $view) {
+ if (self::isView($view)) {
+ $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($view));
+ if (!empty($sql) && $verbose) {
+ echo $sql . ";\n";
+ }
+
+ if (!empty($sql) && $action) {
+ DBA::e($sql);
+ }
+ }
+ }
+
$definition = self::definition();
foreach ($definition as $name => $structure) {
{
$database = self::definition($basePath, false);
- foreach ($database AS $name => $structure) {
+ foreach ($database as $name => $structure) {
echo "--\n";
echo "-- VIEW $name\n";
echo "--\n";
$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)) {
}
}
- $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);
}
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';
+ }
}