You are here

protected function MySQLiSource::_getTableCreateSql in Backup and Migrate 5.0.x

Get the sql for the structure of the given table.

Parameters

array $table:

Return value

string

1 call to MySQLiSource::_getTableCreateSql()
MySQLiSource::exportToFile in src/Core/Source/MySQLiSource.php
Export this source to the given temp file.

File

src/Core/Source/MySQLiSource.php, line 336

Class

MySQLiSource
@package Drupal\backup_migrate\Core\Source

Namespace

Drupal\backup_migrate\Core\Source

Code

protected function _getTableCreateSql(array $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;
}