2020-04-23 08:19:44 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2023-01-01 15:36:24 +01:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-04-23 08:19:44 +02:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Database;
|
|
|
|
|
|
|
|
use Friendica\DI;
|
2022-07-12 23:21:16 +02:00
|
|
|
use Friendica\Util\Writer\ViewDefinitionSqlWriter;
|
2020-04-23 08:19:44 +02:00
|
|
|
|
2020-04-24 10:48:34 +02:00
|
|
|
class View
|
2020-04-23 08:19:44 +02:00
|
|
|
{
|
2022-06-20 01:08:52 +02:00
|
|
|
/**
|
|
|
|
* Creates a view
|
|
|
|
*
|
|
|
|
* @param bool $verbose Whether to show SQL statements
|
|
|
|
* @param bool $action Whether to execute SQL statements
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-23 08:19:44 +02:00
|
|
|
public static function create(bool $verbose, bool $action)
|
|
|
|
{
|
2021-03-04 01:02:34 +01:00
|
|
|
// Delete previously used views that aren't used anymore
|
2022-07-12 23:21:16 +02:00
|
|
|
foreach (['post-view', 'post-thread-view'] as $view) {
|
2021-03-04 01:02:34 +01:00
|
|
|
if (self::isView($view)) {
|
|
|
|
$sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($view));
|
|
|
|
if (!empty($sql) && $verbose) {
|
|
|
|
echo $sql . ";\n";
|
|
|
|
}
|
2022-07-12 23:21:16 +02:00
|
|
|
|
2021-03-04 01:02:34 +01:00
|
|
|
if (!empty($sql) && $action) {
|
|
|
|
DBA::e($sql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 22:05:32 +01:00
|
|
|
// just for Create purpose, reload the view definition with addons to explicit get the whole definition
|
|
|
|
$definition = DI::viewDefinition()->load(true)->getAll();
|
2020-04-23 08:19:44 +02:00
|
|
|
|
|
|
|
foreach ($definition as $name => $structure) {
|
2022-07-12 23:21:16 +02:00
|
|
|
if (self::isView($name)) {
|
|
|
|
DBA::e(sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name)));
|
|
|
|
} elseif (self::isTable($name)) {
|
|
|
|
DBA::e(sprintf("DROP TABLE IF EXISTS `%s`", DBA::escape($name)));
|
2020-04-23 09:02:18 +02:00
|
|
|
}
|
2022-07-12 23:21:16 +02:00
|
|
|
DBA::e(ViewDefinitionSqlWriter::createView($name, $structure));
|
2020-04-23 08:19:44 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-29 20:06:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given table/view is a view
|
|
|
|
*
|
|
|
|
* @param string $view
|
|
|
|
* @return boolean "true" if it's a view
|
|
|
|
*/
|
2022-06-20 01:08:52 +02:00
|
|
|
private static function isView(string $view): bool
|
2020-11-29 20:06:43 +01:00
|
|
|
{
|
2022-06-21 12:09:51 +02:00
|
|
|
$status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['TABLE_TYPE'],
|
2020-11-29 20:06:43 +01:00
|
|
|
['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $view]);
|
|
|
|
|
|
|
|
if (empty($status['TABLE_TYPE'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status['TABLE_TYPE'] == 'VIEW';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-20 15:01:46 +01:00
|
|
|
* Check if the given table/view is a table
|
2020-11-29 20:06:43 +01:00
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return boolean "true" if it's a table
|
|
|
|
*/
|
2022-06-20 01:08:52 +02:00
|
|
|
private static function isTable(string $table): bool
|
2020-11-29 20:06:43 +01:00
|
|
|
{
|
2022-06-21 12:09:51 +02:00
|
|
|
$status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['TABLE_TYPE'],
|
2020-11-29 20:06:43 +01:00
|
|
|
['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $table]);
|
|
|
|
|
2020-11-29 21:59:24 +01:00
|
|
|
if (empty($status['TABLE_TYPE'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-29 20:06:43 +01:00
|
|
|
|
2020-11-29 21:59:24 +01:00
|
|
|
return $status['TABLE_TYPE'] == 'BASE TABLE';
|
2020-11-29 20:06:43 +01:00
|
|
|
}
|
2020-04-23 08:19:44 +02:00
|
|
|
}
|