You are here

protected function MySQLiSource::_getTableCreateSQL in Backup and Migrate 8.4

Get the sql for the structure of the given table.

Parameters

array $table:

Return value

string

1 call to MySQLiSource::_getTableCreateSQL()
MySQLiSource::exportToFile in lib/backup_migrate_core/src/Source/MySQLiSource.php
Export this source to the given temp file. This should be the main back up function for this source.

File

lib/backup_migrate_core/src/Source/MySQLiSource.php, line 325

Class

MySQLiSource
Class MySQLiSource.

Namespace

BackupMigrate\Core\Source

Code

protected function _getTableCreateSQL($table) {
  $out = "";

  // If this is a view.
  if (empty($table['engine'])) {

    // Switch SQL mode to for a simpler version of the create view syntax.
    $sql_mode = $this
      ->_fetchValue("SELECT @@SESSION.sql_mode");

    // @TODO: Setting the sql_mode does not seem to work.
    $this
      ->query("SET sql_mode = 'ANSI'");
    $create = $this
      ->_fetchAssoc("SHOW CREATE VIEW `" . $table['name'] . "`");
    if ($create) {

      // Lowercase the keys for consistency.
      $create = array_change_key_case($create);
      $out .= "DROP VIEW IF EXISTS `" . $table['name'] . "`;\n";
      $out .= "SET sql_mode = 'ANSI';\n";
      $out .= strtr($create['create view'], "\n", " ") . ";\n";
      $out .= "SET sql_mode = '{$sql_mode}';\n";
    }

    // Set the SQL_mode back to the original value.
    $this
      ->query("SET SQL_mode = '{$sql_mode}'");
  }
  else {
    $create = $this
      ->_fetchAssoc("SHOW CREATE TABLE `" . $table['name'] . "`");
    if ($create) {

      // Lowercase the keys for consistency.
      $create = array_change_key_case($create);
      $out .= "DROP TABLE IF EXISTS `" . $table['name'] . "`;\n";

      // Remove newlines.
      $out .= strtr($create['create table'], [
        "\n" => ' ',
      ]);
      if ($table['auto_increment']) {
        $out .= " AUTO_INCREMENT=" . $table['auto_increment'];
      }
      $out .= ";\n";
    }
  }
  return $out;
}