]> git.mxchange.org Git - friendica.git/blob - src/Database/View.php
spelling: successful
[friendica.git] / src / Database / View.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Database;
23
24 use Friendica\DI;
25 use Friendica\Util\Writer\ViewDefinitionSqlWriter;
26
27 class View
28 {
29         /**
30          * Creates a view
31          *
32          * @param bool $verbose Whether to show SQL statements
33          * @param bool $action Whether to execute SQL statements
34          * @return void
35          */
36         public static function create(bool $verbose, bool $action)
37         {
38                 // Delete previously used views that aren't used anymore
39                 foreach (['post-view', 'post-thread-view'] as $view) {
40                         if (self::isView($view)) {
41                                 $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($view));
42                                 if (!empty($sql) && $verbose) {
43                                         echo $sql . ";\n";
44                                 }
45
46                                 if (!empty($sql) && $action) {
47                                         DBA::e($sql);
48                                 }
49                         }
50                 }
51
52                 // just for Create purpose, reload the view definition with addons to explicit get the whole definition
53                 $definition = DI::viewDefinition()->load(true)->getAll();
54
55                 foreach ($definition as $name => $structure) {
56                         if (self::isView($name)) {
57                                 DBA::e(sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name)));
58                         } elseif (self::isTable($name)) {
59                                 DBA::e(sprintf("DROP TABLE IF EXISTS `%s`", DBA::escape($name)));
60                         }
61                         DBA::e(ViewDefinitionSqlWriter::createView($name, $structure));
62                 }
63         }
64
65         /**
66          * Check if the given table/view is a view
67          *
68          * @param string $view
69          * @return boolean "true" if it's a view
70          */
71         private static function isView(string $view): bool
72         {
73                 $status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['TABLE_TYPE'],
74                         ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $view]);
75
76                 if (empty($status['TABLE_TYPE'])) {
77                         return false;
78                 }
79
80                 return $status['TABLE_TYPE'] == 'VIEW';
81         }
82
83         /**
84          * Check if the given table/view is a table
85          *
86          * @param string $table
87          * @return boolean "true" if it's a table
88          */
89         private static function isTable(string $table): bool
90         {
91                 $status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['TABLE_TYPE'],
92                         ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_NAME' => $table]);
93
94                 if (empty($status['TABLE_TYPE'])) {
95                         return false;
96                 }
97
98                 return $status['TABLE_TYPE'] == 'BASE TABLE';
99         }
100 }