You are here

database_mysql_dump.inc in Demonstration site (Sandbox / Snapshot) 5

Same filename and directory in other branches
  1. 6 database_mysql_dump.inc
  2. 7 database_mysql_dump.inc

File

database_mysql_dump.inc
View source
<?php

// Some older mysql client libs are missing this constant.
if (!defined('MYSQLI_BINARY_FLAG')) {
  define('MYSQLI_BINARY_FLAG', 128);
}

/**
 * Dump active database.
 */
function demo_dump_db($filename, $exclude = array()) {

  // Make sure we have permission to save our backup file.
  $directory = dirname($filename);
  if (!file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
    return FALSE;
  }
  if ($fp = fopen($filename, 'wb')) {
    $header = "-- Demo.module database dump (version " . DEMO_DUMP_VERSION . ")\n";
    $header .= "-- http://drupal.org/project/demo\n";
    $header .= "--\n";
    $header .= "-- Database: " . _demo_get_database() . "\n";
    $header .= "-- Date: " . format_date(time(), 'large') . "\n";

    // Temporarily disable foreign key checks for the time of import.
    $header .= "SET FOREIGN_KEY_CHECKS = 0;\n";
    fwrite($fp, $header);
    foreach (demo_enum_tables() as $table) {

      // Always export structure to allow creating a new site
      // from a database dump
      fwrite($fp, _demo_dump_table_structure($table));
      if (!in_array($table, $exclude)) {
        fwrite($fp, _demo_dump_table_data($table));
      }
    }

    // Re-enable foreign key checks.
    fwrite($fp, "\nSET FOREIGN_KEY_CHECKS = 1;\n");
    fclose($fp);
    return TRUE;
  }
  return FALSE;
}

/**
 * Returns the name of the active database.
 */
function _demo_get_database() {
  $database = array_keys(db_fetch_array(db_query('SHOW TABLES')));
  $database = preg_replace('/^Tables_in_/', '', $database[0]);
  return $database;
}

/**
 * Dump table structure.
 */
function _demo_dump_table_structure($table) {
  $output = "\n";
  $output .= "--\n";
  $output .= "-- Table structure for table '{$table}'\n";
  $output .= "--\n\n";
  $data = db_fetch_array(db_query("SHOW CREATE TABLE %s", $table));
  $output .= preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $data['Create Table']) . ";\n";
  return $output;
}

/**
 * Dump table data.
 *
 * This code has largely been stolen from the phpMyAdmin project.
 */
function _demo_dump_table_data($table) {
  $output = "\n";
  $output .= "--\n";
  $output .= "-- Dumping data for table '{$table}'\n";
  $output .= "--\n\n";

  // Dump table data
  $result = db_query("SELECT * FROM %s", $table);

  // Get table fields.
  if ($fields = _demo_get_fields($result)) {

    // Disable indices to speed up import.
    $output .= "/*!40000 ALTER TABLE {$table} DISABLE KEYS */;\n";

    // Escape backslashes, PHP code, special chars
    $search = array(
      '\\',
      "'",
      "\0",
      "\n",
      "\r",
      "\32",
    );
    $replace = array(
      '\\\\',
      "''",
      '\\0',
      '\\n',
      '\\r',
      '\\Z',
    );
    $insert_cmd = "INSERT INTO `{$table}` VALUES\n";
    $insert_buffer = '';
    $current_row = 0;
    $query_size = 0;
    while ($row = db_fetch_array($result)) {
      $current_row++;
      $values = array();
      $field = 0;
      foreach ($row as $value) {

        // NULL
        if (!isset($value) || is_null($value)) {
          $values[] = 'NULL';
        }
        else {
          if ($fields[$field]->numeric && !$fields[$field]->timestamp && !$fields[$field]->blob) {
            $values[] = $value;
          }
          else {
            if ($fields[$field]->binary && $fields[$field]->blob) {

              // Empty blobs need to be different, but '0' is also empty :-(
              if (empty($value) && $value != '0') {
                $values[] = "''";
              }
              else {
                $values[] = '0x' . bin2hex($value);
              }
            }
            else {
              $values[] = "'" . str_replace($search, $replace, $value) . "'";
            }
          }
        }
        $field++;
      }
      if ($current_row == 1) {
        $insert_buffer = $insert_cmd . '(' . implode(', ', $values) . ')';
      }
      else {
        $insert_buffer = '(' . implode(', ', $values) . ')';
        if ($query_size + strlen($insert_buffer) > 50000) {
          $output .= ";\n";
          $current_row = 1;
          $query_size = 0;
          $insert_buffer = $insert_cmd . $insert_buffer;
        }
      }
      $query_size += strlen($insert_buffer);
      $output .= ($current_row == 1 ? '' : ",\n") . $insert_buffer;
    }
    if ($current_row > 0) {
      $output .= ";\n";
    }

    // Enable indices again.
    $output .= "/*!40000 ALTER TABLE {$table} ENABLE KEYS */;\n";
  }
  return $output;
}

/**
 * Return table fields and their properties.
 */
function _demo_get_fields($result) {
  $fields = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
      $num_fields = mysql_num_fields($result);
      for ($i = 0; $i < $num_fields; $i++) {
        $meta = mysql_fetch_field($result, $i);

        // Enhance field definition with custom properties.
        $meta->timestamp = (int) ($meta->type == 'timestamp');
        $flags = mysql_field_flags($result, $i);
        $meta->binary = (int) (stristr($flags, 'binary') !== FALSE);
        $fields[] = $meta;
      }
      break;
    case 'mysqli':
      while ($meta = mysqli_fetch_field($result)) {

        // Enhance the field definition for mysql-extension compatibilty.
        $meta->numeric = (int) (bool) ($meta->flags & MYSQLI_NUM_FLAG);
        $meta->blob = (int) (bool) ($meta->flags & MYSQLI_BLOB_FLAG);

        // Add custom properties.
        $meta->timestamp = (int) ($meta->type == MYSQLI_TYPE_TIMESTAMP);
        $meta->binary = (int) (bool) ($meta->flags & MYSQLI_BINARY_FLAG);
        $fields[] = $meta;
      }
      break;
  }
  return $fields;
}

Functions

Namesort descending Description
demo_dump_db Dump active database.
_demo_dump_table_data Dump table data.
_demo_dump_table_structure Dump table structure.
_demo_get_database Returns the name of the active database.
_demo_get_fields Return table fields and their properties.