]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
DBStructure enhancements (#5437)
[friendica.git] / src / Database / DBStructure.php
index 55188d2b39c19a42e3666721d2e4d0f6caf04221..c6c8c156626012b2e2844888567ce6cf8c4cb8d7 100644 (file)
@@ -684,6 +684,141 @@ class DBStructure
                return $sql;
        }
 
+       /**
+        *      Check if a table exists
+        *
+        * @param string $table Table name
+        *
+        * @return boolean Does the table exist?
+        */
+       public static function existsTable($table)
+       {
+               if (empty($table)) {
+                       return false;
+               }
+
+               $table = DBA::escape($table);
+
+               $sql = "SHOW TABLES LIKE '" . $table . "';";
+
+               $stmt = DBA::p($sql);
+
+               if (is_bool($stmt)) {
+                       $retval = $stmt;
+               } else {
+                       $retval = (DBA::num_rows($stmt) > 0);
+               }
+
+               DBA::close($stmt);
+
+               return $retval;
+       }
+
+       /**
+        *      Check if the columns of the table exists
+        *
+        * @param string $table   Table name
+        * @param array  $columns Columns to check ( Syntax: [ $col1, $col2, .. ] )
+        *
+        * @return boolean Does the table exist?
+        */
+       public static function existsColumn($table, $columns = []) {
+               if (empty($table)) {
+                       return false;
+               }
+
+               if (is_null($columns) || empty($columns)) {
+                       return self::existsTable($table);
+               }
+
+               $table = DBA::escape($table);
+
+               foreach ($columns AS $column) {
+                       $sql = "SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "';";
+
+                       $stmt = DBA::p($sql);
+
+                       if (is_bool($stmt)) {
+                               $retval = $stmt;
+                       } else {
+                               $retval = (DBA::num_rows($stmt) > 0);
+                       }
+
+                       DBA::close($stmt);
+
+                       if (!$retval) {
+                               return false;
+                       }
+               }
+
+               return true;
+       }
+
+       const RENAME_COLUMN = 0;
+       const RENAME_PRIMARY_KEY = 1;
+
+       /**
+        * Renames columns or the primary key of a table
+        * @todo You cannot rename a primary key if "auto increment" is set
+        *
+        * @param string $table    Table name
+        * @param array  $columns  Columns Syntax for Rename: [ $old1 => [ $new1, $type1 ], $old2 => [ $new2, $type2 ], ... ] )
+        *                                 Syntax for Primary Key: [ $col1, $col2, ...] )
+        * @param int    $type     The type of renaming (Default is Column)
+        *
+        * @return boolean Was the renaming successful?
+        *
+        */
+       public static function rename($table, $columns, $type = self::RENAME_COLUMN) {
+               if (empty($table) || empty($columns)) {
+                       return false;
+               }
+
+               if (!is_array($columns)) {
+                       return false;
+               }
+
+               $table = DBA::escape($table);
+
+               $sql = "ALTER TABLE `" . $table . "`";
+               switch ($type) {
+                       case self::RENAME_COLUMN:
+                               if (!self::existsColumn($table, array_keys($columns))) {
+                                       return false;
+                               }
+                               $sql .= implode(',', array_map(
+                                       function ($to, $from) {
+                                               return " CHANGE `" . $from . "` `" . $to[0] . "` " . $to[1];
+                                       },
+                                       $columns,
+                                       array_keys($columns)
+                               ));
+                               break;
+                       case self::RENAME_PRIMARY_KEY:
+                               if (!self::existsColumn($table, $columns)) {
+                                       return false;
+                               }
+                               $sql .= " DROP PRIMARY KEY, ADD PRIMARY KEY(`" . implode('`, `', $columns) . "`)";
+                               break;
+                       default:
+                               return false;
+               }
+
+               $sql .= ";";
+
+               $stmt = DBA::p($sql);
+
+               if (is_bool($stmt)) {
+                       $retval = $stmt;
+               } else {
+                       $retval = true;
+               }
+
+               DBA::close($stmt);
+
+               return $retval;
+       }
+
        public static function definition() {
                $database = [];