]> git.mxchange.org Git - friendica.git/blob - src/Database/DBStructure.php
Merge pull request #8272 from MrPetovan/bug/8254-regex-url-img
[friendica.git] / src / Database / DBStructure.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 Exception;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Logger;
27 use Friendica\DI;
28 use Friendica\Util\DateTimeFormat;
29
30 require_once __DIR__ . '/../../include/dba.php';
31
32 /**
33  * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
34  */
35 class DBStructure
36 {
37         const UPDATE_NOT_CHECKED = 0; // Database check wasn't executed before
38         const UPDATE_SUCCESSFUL  = 1; // Database check was successful
39         const UPDATE_FAILED      = 2; // Database check failed
40
41         const RENAME_COLUMN      = 0;
42         const RENAME_PRIMARY_KEY = 1;
43
44         /**
45          * Database structure definition loaded from config/dbstructure.config.php
46          *
47          * @var array
48          */
49         private static $definition = [];
50
51         /**
52          * Converts all tables from MyISAM to InnoDB
53          */
54         public static function convertToInnoDB()
55         {
56                 $tables = DBA::selectToArray(
57                         ['information_schema' => 'tables'],
58                         ['table_name'],
59                         ['engine' => 'MyISAM', 'table_schema' => DBA::databaseName()]
60                 );
61
62                 if (!DBA::isResult($tables)) {
63                         echo DI::l10n()->t('There are no tables on MyISAM.') . "\n";
64                         return;
65                 }
66
67                 foreach ($tables AS $table) {
68                         $sql = "ALTER TABLE " . DBA::quoteIdentifier($table['table_name']) . " engine=InnoDB;";
69                         echo $sql . "\n";
70
71                         $result = DBA::e($sql);
72                         if (!DBA::isResult($result)) {
73                                 self::printUpdateError($sql);
74                         }
75                 }
76         }
77
78         /**
79          * Print out database error messages
80          *
81          * @param string $message Message to be added to the error message
82          *
83          * @return string Error message
84          */
85         private static function printUpdateError($message)
86         {
87                 echo DI::l10n()->t("\nError %d occurred during database update:\n%s\n",
88                         DBA::errorNo(), DBA::errorMessage());
89
90                 return DI::l10n()->t('Errors encountered performing database changes: ') . $message . EOL;
91         }
92
93         public static function printStructure($basePath)
94         {
95                 $database = self::definition($basePath, false);
96
97                 echo "-- ------------------------------------------\n";
98                 echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n";
99                 echo "-- DB_UPDATE_VERSION " . DB_UPDATE_VERSION . "\n";
100                 echo "-- ------------------------------------------\n\n\n";
101                 foreach ($database AS $name => $structure) {
102                         echo "--\n";
103                         echo "-- TABLE $name\n";
104                         echo "--\n";
105                         self::createTable($name, $structure, true, false);
106
107                         echo "\n";
108                 }
109         }
110
111         /**
112          * Loads the database structure definition from the config/dbstructure.config.php file.
113          * On first pass, defines DB_UPDATE_VERSION constant.
114          *
115          * @see static/dbstructure.config.php
116          * @param boolean $with_addons_structure Whether to tack on addons additional tables
117          * @param string  $basePath              The base path of this application
118          * @return array
119          * @throws Exception
120          */
121         public static function definition($basePath, $with_addons_structure = true)
122         {
123                 if (!self::$definition) {
124
125                         $filename = $basePath . '/static/dbstructure.config.php';
126
127                         if (!is_readable($filename)) {
128                                 throw new Exception('Missing database structure config file static/dbstructure.config.php');
129                         }
130
131                         $definition = require $filename;
132
133                         if (!$definition) {
134                                 throw new Exception('Corrupted database structure config file static/dbstructure.config.php');
135                         }
136
137                         self::$definition = $definition;
138                 } else {
139                         $definition = self::$definition;
140                 }
141
142                 if ($with_addons_structure) {
143                         Hook::callAll('dbstructure_definition', $definition);
144                 }
145
146                 return $definition;
147         }
148
149         private static function createTable($name, $structure, $verbose, $action)
150         {
151                 $r = true;
152
153                 $engine = "";
154                 $comment = "";
155                 $sql_rows = [];
156                 $primary_keys = [];
157                 foreach ($structure["fields"] AS $fieldname => $field) {
158                         $sql_rows[] = "`" . DBA::escape($fieldname) . "` " . self::FieldCommand($field);
159                         if (!empty($field['primary'])) {
160                                 $primary_keys[] = $fieldname;
161                         }
162                 }
163
164                 if (!empty($structure["indexes"])) {
165                         foreach ($structure["indexes"] AS $indexname => $fieldnames) {
166                                 $sql_index = self::createIndex($indexname, $fieldnames, "");
167                                 if (!is_null($sql_index)) {
168                                         $sql_rows[] = $sql_index;
169                                 }
170                         }
171                 }
172
173                 if (isset($structure["engine"])) {
174                         $engine = " ENGINE=" . $structure["engine"];
175                 }
176
177                 if (isset($structure["comment"])) {
178                         $comment = " COMMENT='" . DBA::escape($structure["comment"]) . "'";
179                 }
180
181                 $sql = implode(",\n\t", $sql_rows);
182
183                 $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", DBA::escape($name)) . $sql .
184                         "\n)" . $engine . " DEFAULT COLLATE utf8mb4_general_ci" . $comment;
185                 if ($verbose) {
186                         echo $sql . ";\n";
187                 }
188
189                 if ($action) {
190                         $r = DBA::e($sql);
191                 }
192
193                 return $r;
194         }
195
196         private static function FieldCommand($parameters, $create = true)
197         {
198                 $fieldstruct = $parameters["type"];
199
200                 if (isset($parameters["Collation"])) {
201                         $fieldstruct .= " COLLATE " . $parameters["Collation"];
202                 }
203
204                 if (isset($parameters["not null"])) {
205                         $fieldstruct .= " NOT NULL";
206                 }
207
208                 if (isset($parameters["default"])) {
209                         if (strpos(strtolower($parameters["type"]), "int") !== false) {
210                                 $fieldstruct .= " DEFAULT " . $parameters["default"];
211                         } else {
212                                 $fieldstruct .= " DEFAULT '" . $parameters["default"] . "'";
213                         }
214                 }
215                 if (isset($parameters["extra"])) {
216                         $fieldstruct .= " " . $parameters["extra"];
217                 }
218
219                 if (isset($parameters["comment"])) {
220                         $fieldstruct .= " COMMENT '" . DBA::escape($parameters["comment"]) . "'";
221                 }
222
223                 /*if (($parameters["primary"] != "") && $create)
224                         $fieldstruct .= " PRIMARY KEY";*/
225
226                 return ($fieldstruct);
227         }
228
229         private static function createIndex($indexname, $fieldnames, $method = "ADD")
230         {
231                 $method = strtoupper(trim($method));
232                 if ($method != "" && $method != "ADD") {
233                         throw new Exception("Invalid parameter 'method' in self::createIndex(): '$method'");
234                 }
235
236                 if (in_array($fieldnames[0], ["UNIQUE", "FULLTEXT"])) {
237                         $index_type = array_shift($fieldnames);
238                         $method .= " " . $index_type;
239                 }
240
241                 $names = "";
242                 foreach ($fieldnames AS $fieldname) {
243                         if ($names != "") {
244                                 $names .= ",";
245                         }
246
247                         if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
248                                 $names .= "`" . DBA::escape($matches[1]) . "`(" . intval($matches[2]) . ")";
249                         } else {
250                                 $names .= "`" . DBA::escape($fieldname) . "`";
251                         }
252                 }
253
254                 if ($indexname == "PRIMARY") {
255                         return sprintf("%s PRIMARY KEY(%s)", $method, $names);
256                 }
257
258
259                 $sql = sprintf("%s INDEX `%s` (%s)", $method, DBA::escape($indexname), $names);
260                 return ($sql);
261         }
262
263         /**
264          * Updates DB structure and returns eventual errors messages
265          *
266          * @param string $basePath   The base path of this application
267          * @param bool   $verbose
268          * @param bool   $action     Whether to actually apply the update
269          * @param bool   $install    Is this the initial update during the installation?
270          * @param array  $tables     An array of the database tables
271          * @param array  $definition An array of the definition tables
272          * @return string Empty string if the update is successful, error messages otherwise
273          * @throws Exception
274          */
275         public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null)
276         {
277                 if ($action && !$install) {
278                         DI::config()->set('system', 'maintenance', 1);
279                         DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: Database update', DateTimeFormat::utcNow() . ' ' . date('e')));
280                 }
281
282                 $errors = '';
283
284                 Logger::log('updating structure', Logger::DEBUG);
285
286                 // Get the current structure
287                 $database = [];
288
289                 if (is_null($tables)) {
290                         $tables = q("SHOW TABLES");
291                 }
292
293                 if (DBA::isResult($tables)) {
294                         foreach ($tables AS $table) {
295                                 $table = current($table);
296
297                                 Logger::log(sprintf('updating structure for table %s ...', $table), Logger::DEBUG);
298                                 $database[$table] = self::tableStructure($table);
299                         }
300                 }
301
302                 // Get the definition
303                 if (is_null($definition)) {
304                         $definition = self::definition($basePath);
305                 }
306
307                 // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
308                 if ((version_compare(DBA::serverInfo(), '5.7.4') >= 0) &&
309                         !(strpos(DBA::serverInfo(), 'MariaDB') !== false)) {
310                         $ignore = '';
311                 } else {
312                         $ignore = ' IGNORE';
313                 }
314
315                 // Compare it
316                 foreach ($definition AS $name => $structure) {
317                         $is_new_table = false;
318                         $group_by = "";
319                         $sql3 = "";
320                         $is_unique = false;
321                         $temp_name = $name;
322                         if (!isset($database[$name])) {
323                                 $r = self::createTable($name, $structure, $verbose, $action);
324                                 if (!DBA::isResult($r)) {
325                                         $errors .= self::printUpdateError($name);
326                                 }
327                                 $is_new_table = true;
328                         } else {
329                                 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
330                                         if (isset($database[$name]["indexes"][$indexname])) {
331                                                 $current_index_definition = implode(",", $database[$name]["indexes"][$indexname]);
332                                         } else {
333                                                 $current_index_definition = "__NOT_SET__";
334                                         }
335                                         $new_index_definition = implode(",", $fieldnames);
336                                         if ($current_index_definition != $new_index_definition) {
337                                                 if ($fieldnames[0] == "UNIQUE") {
338                                                         $is_unique = true;
339                                                         if ($ignore == "") {
340                                                                 $temp_name = "temp-" . $name;
341                                                         }
342                                                 }
343                                         }
344                                 }
345
346                                 /*
347                                  * Drop the index if it isn't present in the definition
348                                  * or the definition differ from current status
349                                  * and index name doesn't start with "local_"
350                                  */
351                                 foreach ($database[$name]["indexes"] as $indexname => $fieldnames) {
352                                         $current_index_definition = implode(",", $fieldnames);
353                                         if (isset($structure["indexes"][$indexname])) {
354                                                 $new_index_definition = implode(",", $structure["indexes"][$indexname]);
355                                         } else {
356                                                 $new_index_definition = "__NOT_SET__";
357                                         }
358                                         if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
359                                                 $sql2 = self::dropIndex($indexname);
360                                                 if ($sql3 == "") {
361                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
362                                                 } else {
363                                                         $sql3 .= ", " . $sql2;
364                                                 }
365                                         }
366                                 }
367                                 // Compare the field structure field by field
368                                 foreach ($structure["fields"] AS $fieldname => $parameters) {
369                                         if (!isset($database[$name]["fields"][$fieldname])) {
370                                                 $sql2 = self::addTableField($fieldname, $parameters);
371                                                 if ($sql3 == "") {
372                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
373                                                 } else {
374                                                         $sql3 .= ", " . $sql2;
375                                                 }
376                                         } else {
377                                                 // Compare the field definition
378                                                 $field_definition = $database[$name]["fields"][$fieldname];
379
380                                                 // Remove the relation data that is used for the referential integrity
381                                                 unset($parameters['relation']);
382
383                                                 // We change the collation after the indexes had been changed.
384                                                 // This is done to avoid index length problems.
385                                                 // So here we always ensure that there is no need to change it.
386                                                 unset($parameters['Collation']);
387                                                 unset($field_definition['Collation']);
388
389                                                 // Only update the comment when it is defined
390                                                 if (!isset($parameters['comment'])) {
391                                                         $parameters['comment'] = "";
392                                                 }
393
394                                                 $current_field_definition = DBA::cleanQuery(implode(",", $field_definition));
395                                                 $new_field_definition = DBA::cleanQuery(implode(",", $parameters));
396                                                 if ($current_field_definition != $new_field_definition) {
397                                                         $sql2 = self::modifyTableField($fieldname, $parameters);
398                                                         if ($sql3 == "") {
399                                                                 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
400                                                         } else {
401                                                                 $sql3 .= ", " . $sql2;
402                                                         }
403                                                 }
404                                         }
405                                 }
406                         }
407
408                         /*
409                          * Create the index if the index don't exists in database
410                          * or the definition differ from the current status.
411                          * Don't create keys if table is new
412                          */
413                         if (!$is_new_table) {
414                                 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
415                                         if (isset($database[$name]["indexes"][$indexname])) {
416                                                 $current_index_definition = implode(",", $database[$name]["indexes"][$indexname]);
417                                         } else {
418                                                 $current_index_definition = "__NOT_SET__";
419                                         }
420                                         $new_index_definition = implode(",", $fieldnames);
421                                         if ($current_index_definition != $new_index_definition) {
422                                                 $sql2 = self::createIndex($indexname, $fieldnames);
423
424                                                 // Fetch the "group by" fields for unique indexes
425                                                 $group_by = self::groupBy($fieldnames);
426                                                 if ($sql2 != "") {
427                                                         if ($sql3 == "") {
428                                                                 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
429                                                         } else {
430                                                                 $sql3 .= ", " . $sql2;
431                                                         }
432                                                 }
433                                         }
434                                 }
435
436                                 if (isset($database[$name]["table_status"]["Comment"])) {
437                                         $structurecomment = $structure["comment"] ?? '';
438                                         if ($database[$name]["table_status"]["Comment"] != $structurecomment) {
439                                                 $sql2 = "COMMENT = '" . DBA::escape($structurecomment) . "'";
440
441                                                 if ($sql3 == "") {
442                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
443                                                 } else {
444                                                         $sql3 .= ", " . $sql2;
445                                                 }
446                                         }
447                                 }
448
449                                 if (isset($database[$name]["table_status"]["Engine"]) && isset($structure['engine'])) {
450                                         if ($database[$name]["table_status"]["Engine"] != $structure['engine']) {
451                                                 $sql2 = "ENGINE = '" . DBA::escape($structure['engine']) . "'";
452
453                                                 if ($sql3 == "") {
454                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
455                                                 } else {
456                                                         $sql3 .= ", " . $sql2;
457                                                 }
458                                         }
459                                 }
460
461                                 if (isset($database[$name]["table_status"]["Collation"])) {
462                                         if ($database[$name]["table_status"]["Collation"] != 'utf8mb4_general_ci') {
463                                                 $sql2 = "DEFAULT COLLATE utf8mb4_general_ci";
464
465                                                 if ($sql3 == "") {
466                                                         $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
467                                                 } else {
468                                                         $sql3 .= ", " . $sql2;
469                                                 }
470                                         }
471                                 }
472
473                                 if ($sql3 != "") {
474                                         $sql3 .= "; ";
475                                 }
476
477                                 // Now have a look at the field collations
478                                 // Compare the field structure field by field
479                                 foreach ($structure["fields"] AS $fieldname => $parameters) {
480                                         // Compare the field definition
481                                         $field_definition = ($database[$name]["fields"][$fieldname] ?? '') ?: ['Collation' => ''];
482
483                                         // Define the default collation if not given
484                                         if (!isset($parameters['Collation']) && !empty($field_definition['Collation'])) {
485                                                 $parameters['Collation'] = 'utf8mb4_general_ci';
486                                         } else {
487                                                 $parameters['Collation'] = null;
488                                         }
489
490                                         if ($field_definition['Collation'] != $parameters['Collation']) {
491                                                 $sql2 = self::modifyTableField($fieldname, $parameters);
492                                                 if (($sql3 == "") || (substr($sql3, -2, 2) == "; ")) {
493                                                         $sql3 .= "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
494                                                 } else {
495                                                         $sql3 .= ", " . $sql2;
496                                                 }
497                                         }
498                                 }
499                         }
500
501                         if ($sql3 != "") {
502                                 if (substr($sql3, -2, 2) != "; ") {
503                                         $sql3 .= ";";
504                                 }
505
506                                 $field_list = '';
507                                 if ($is_unique && $ignore == '') {
508                                         foreach ($database[$name]["fields"] AS $fieldname => $parameters) {
509                                                 $field_list .= 'ANY_VALUE(`' . $fieldname . '`),';
510                                         }
511                                         $field_list = rtrim($field_list, ',');
512                                 }
513
514                                 if ($verbose) {
515                                         // Ensure index conversion to unique removes duplicates
516                                         if ($is_unique && ($temp_name != $name)) {
517                                                 if ($ignore != "") {
518                                                         echo "SET session old_alter_table=1;\n";
519                                                 } else {
520                                                         echo "DROP TABLE IF EXISTS `" . $temp_name . "`;\n";
521                                                         echo "CREATE TABLE `" . $temp_name . "` LIKE `" . $name . "`;\n";
522                                                 }
523                                         }
524
525                                         echo $sql3 . "\n";
526
527                                         if ($is_unique && ($temp_name != $name)) {
528                                                 if ($ignore != "") {
529                                                         echo "SET session old_alter_table=0;\n";
530                                                 } else {
531                                                         echo "INSERT INTO `" . $temp_name . "` SELECT " . DBA::anyValueFallback($field_list) . " FROM `" . $name . "`" . $group_by . ";\n";
532                                                         echo "DROP TABLE `" . $name . "`;\n";
533                                                         echo "RENAME TABLE `" . $temp_name . "` TO `" . $name . "`;\n";
534                                                 }
535                                         }
536                                 }
537
538                                 if ($action) {
539                                         if (!$install) {
540                                                 DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: updating %s table.', DateTimeFormat::utcNow() . ' ' . date('e'), $name));
541                                         }
542
543                                         // Ensure index conversion to unique removes duplicates
544                                         if ($is_unique && ($temp_name != $name)) {
545                                                 if ($ignore != "") {
546                                                         DBA::e("SET session old_alter_table=1;");
547                                                 } else {
548                                                         $r = DBA::e("DROP TABLE IF EXISTS `" . $temp_name . "`;");
549                                                         if (!DBA::isResult($r)) {
550                                                                 $errors .= self::printUpdateError($sql3);
551                                                                 return $errors;
552                                                         }
553
554                                                         $r = DBA::e("CREATE TABLE `" . $temp_name . "` LIKE `" . $name . "`;");
555                                                         if (!DBA::isResult($r)) {
556                                                                 $errors .= self::printUpdateError($sql3);
557                                                                 return $errors;
558                                                         }
559                                                 }
560                                         }
561
562                                         $r = DBA::e($sql3);
563                                         if (!DBA::isResult($r)) {
564                                                 $errors .= self::printUpdateError($sql3);
565                                         }
566                                         if ($is_unique && ($temp_name != $name)) {
567                                                 if ($ignore != "") {
568                                                         DBA::e("SET session old_alter_table=0;");
569                                                 } else {
570                                                         $r = DBA::e("INSERT INTO `" . $temp_name . "` SELECT " . $field_list . " FROM `" . $name . "`" . $group_by . ";");
571                                                         if (!DBA::isResult($r)) {
572                                                                 $errors .= self::printUpdateError($sql3);
573                                                                 return $errors;
574                                                         }
575                                                         $r = DBA::e("DROP TABLE `" . $name . "`;");
576                                                         if (!DBA::isResult($r)) {
577                                                                 $errors .= self::printUpdateError($sql3);
578                                                                 return $errors;
579                                                         }
580                                                         $r = DBA::e("RENAME TABLE `" . $temp_name . "` TO `" . $name . "`;");
581                                                         if (!DBA::isResult($r)) {
582                                                                 $errors .= self::printUpdateError($sql3);
583                                                                 return $errors;
584                                                         }
585                                                 }
586                                         }
587                                 }
588                         }
589                 }
590
591                 if ($action && !$install) {
592                         DI::config()->set('system', 'maintenance', 0);
593                         DI::config()->set('system', 'maintenance_reason', '');
594
595                         if ($errors) {
596                                 DI::config()->set('system', 'dbupdate', self::UPDATE_FAILED);
597                         } else {
598                                 DI::config()->set('system', 'dbupdate', self::UPDATE_SUCCESSFUL);
599                         }
600                 }
601
602                 return $errors;
603         }
604
605         private static function tableStructure($table)
606         {
607                 $structures = q("DESCRIBE `%s`", $table);
608
609                 $full_columns = q("SHOW FULL COLUMNS FROM `%s`", $table);
610
611                 $indexes = q("SHOW INDEX FROM `%s`", $table);
612
613                 $table_status = q("SHOW TABLE STATUS WHERE `name` = '%s'", $table);
614
615                 if (DBA::isResult($table_status)) {
616                         $table_status = $table_status[0];
617                 } else {
618                         $table_status = [];
619                 }
620
621                 $fielddata = [];
622                 $indexdata = [];
623
624                 if (DBA::isResult($indexes)) {
625                         foreach ($indexes AS $index) {
626                                 if ($index["Key_name"] != "PRIMARY" && $index["Non_unique"] == "0" && !isset($indexdata[$index["Key_name"]])) {
627                                         $indexdata[$index["Key_name"]] = ["UNIQUE"];
628                                 }
629
630                                 if ($index["Index_type"] == "FULLTEXT" && !isset($indexdata[$index["Key_name"]])) {
631                                         $indexdata[$index["Key_name"]] = ["FULLTEXT"];
632                                 }
633
634                                 $column = $index["Column_name"];
635
636                                 if ($index["Sub_part"] != "") {
637                                         $column .= "(" . $index["Sub_part"] . ")";
638                                 }
639
640                                 $indexdata[$index["Key_name"]][] = $column;
641                         }
642                 }
643                 if (DBA::isResult($structures)) {
644                         foreach ($structures AS $field) {
645                                 // Replace the default size values so that we don't have to define them
646                                 $search = ['tinyint(1)', 'tinyint(3) unsigned', 'tinyint(4)', 'smallint(5) unsigned', 'smallint(6)', 'mediumint(8) unsigned', 'mediumint(9)', 'bigint(20)', 'int(10) unsigned', 'int(11)'];
647                                 $replace = ['boolean', 'tinyint unsigned', 'tinyint', 'smallint unsigned', 'smallint', 'mediumint unsigned', 'mediumint', 'bigint', 'int unsigned', 'int'];
648                                 $field["Type"] = str_replace($search, $replace, $field["Type"]);
649
650                                 $fielddata[$field["Field"]]["type"] = $field["Type"];
651                                 if ($field["Null"] == "NO") {
652                                         $fielddata[$field["Field"]]["not null"] = true;
653                                 }
654
655                                 if (isset($field["Default"])) {
656                                         $fielddata[$field["Field"]]["default"] = $field["Default"];
657                                 }
658
659                                 if ($field["Extra"] != "") {
660                                         $fielddata[$field["Field"]]["extra"] = $field["Extra"];
661                                 }
662
663                                 if ($field["Key"] == "PRI") {
664                                         $fielddata[$field["Field"]]["primary"] = true;
665                                 }
666                         }
667                 }
668                 if (DBA::isResult($full_columns)) {
669                         foreach ($full_columns AS $column) {
670                                 $fielddata[$column["Field"]]["Collation"] = $column["Collation"];
671                                 $fielddata[$column["Field"]]["comment"] = $column["Comment"];
672                         }
673                 }
674
675                 return ["fields" => $fielddata, "indexes" => $indexdata, "table_status" => $table_status];
676         }
677
678         private static function dropIndex($indexname)
679         {
680                 $sql = sprintf("DROP INDEX `%s`", DBA::escape($indexname));
681                 return ($sql);
682         }
683
684         private static function addTableField($fieldname, $parameters)
685         {
686                 $sql = sprintf("ADD `%s` %s", DBA::escape($fieldname), self::FieldCommand($parameters));
687                 return ($sql);
688         }
689
690         private static function modifyTableField($fieldname, $parameters)
691         {
692                 $sql = sprintf("MODIFY `%s` %s", DBA::escape($fieldname), self::FieldCommand($parameters, false));
693                 return ($sql);
694         }
695
696         /**
697          * Constructs a GROUP BY clause from a UNIQUE index definition.
698          *
699          * @param array $fieldnames
700          * @return string
701          */
702         private static function groupBy(array $fieldnames)
703         {
704                 if ($fieldnames[0] != "UNIQUE") {
705                         return "";
706                 }
707
708                 array_shift($fieldnames);
709
710                 $names = "";
711                 foreach ($fieldnames AS $fieldname) {
712                         if ($names != "") {
713                                 $names .= ",";
714                         }
715
716                         if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
717                                 $names .= "`" . DBA::escape($matches[1]) . "`";
718                         } else {
719                                 $names .= "`" . DBA::escape($fieldname) . "`";
720                         }
721                 }
722
723                 $sql = sprintf(" GROUP BY %s", $names);
724                 return $sql;
725         }
726
727         /**
728          * Renames columns or the primary key of a table
729          *
730          * @todo You cannot rename a primary key if "auto increment" is set
731          *
732          * @param string $table            Table name
733          * @param array  $columns          Columns Syntax for Rename: [ $old1 => [ $new1, $type1 ], $old2 => [ $new2, $type2 ], ... ]
734          *                                 Syntax for Primary Key: [ $col1, $col2, ...]
735          * @param int    $type             The type of renaming (Default is Column)
736          *
737          * @return boolean Was the renaming successful?
738          * @throws Exception
739          */
740         public static function rename($table, $columns, $type = self::RENAME_COLUMN)
741         {
742                 if (empty($table) || empty($columns)) {
743                         return false;
744                 }
745
746                 if (!is_array($columns)) {
747                         return false;
748                 }
749
750                 $table = DBA::escape($table);
751
752                 $sql = "ALTER TABLE `" . $table . "`";
753                 switch ($type) {
754                         case self::RENAME_COLUMN:
755                                 if (!self::existsColumn($table, array_keys($columns))) {
756                                         return false;
757                                 }
758                                 $sql .= implode(',', array_map(
759                                         function ($to, $from) {
760                                                 return " CHANGE `" . $from . "` `" . $to[0] . "` " . $to[1];
761                                         },
762                                         $columns,
763                                         array_keys($columns)
764                                 ));
765                                 break;
766                         case self::RENAME_PRIMARY_KEY:
767                                 if (!self::existsColumn($table, $columns)) {
768                                         return false;
769                                 }
770                                 $sql .= " DROP PRIMARY KEY, ADD PRIMARY KEY(`" . implode('`, `', $columns) . "`)";
771                                 break;
772                         default:
773                                 return false;
774                 }
775
776                 $sql .= ";";
777
778                 $stmt = DBA::p($sql);
779
780                 if (is_bool($stmt)) {
781                         $retval = $stmt;
782                 } else {
783                         $retval = true;
784                 }
785
786                 DBA::close($stmt);
787
788                 return $retval;
789         }
790
791         /**
792          *    Check if the columns of the table exists
793          *
794          * @param string $table   Table name
795          * @param array  $columns Columns to check ( Syntax: [ $col1, $col2, .. ] )
796          *
797          * @return boolean Does the table exist?
798          * @throws Exception
799          */
800         public static function existsColumn($table, $columns = [])
801         {
802                 if (empty($table)) {
803                         return false;
804                 }
805
806                 if (is_null($columns) || empty($columns)) {
807                         return self::existsTable($table);
808                 }
809
810                 $table = DBA::escape($table);
811
812                 foreach ($columns AS $column) {
813                         $sql = "SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "';";
814
815                         $stmt = DBA::p($sql);
816
817                         if (is_bool($stmt)) {
818                                 $retval = $stmt;
819                         } else {
820                                 $retval = (DBA::numRows($stmt) > 0);
821                         }
822
823                         DBA::close($stmt);
824
825                         if (!$retval) {
826                                 return false;
827                         }
828                 }
829
830                 return true;
831         }
832
833         /**
834          *    Check if a table exists
835          *
836          * @param string|array $table Table name
837          *
838          * @return boolean Does the table exist?
839          * @throws Exception
840          */
841         public static function existsTable($table)
842         {
843                 if (empty($table)) {
844                         return false;
845                 }
846
847                 if (is_array($table)) {
848                         $condition = ['table_schema' => key($table), 'table_name' => current($table)];
849                 } else {
850                         $condition = ['table_schema' => DBA::databaseName(), 'table_name' => $table];
851                 }
852
853                 $result = DBA::exists(['information_schema' => 'tables'], $condition);
854
855                 return $result;
856         }
857
858         /**
859          * Returns the columns of a table
860          *
861          * @param string $table Table name
862          *
863          * @return array An array of the table columns
864          * @throws Exception
865          */
866         public static function getColumns($table)
867         {
868                 $stmtColumns = DBA::p("SHOW COLUMNS FROM `" . $table . "`");
869                 return DBA::toArray($stmtColumns);
870         }
871 }