You are here

function backup_migrate_destination_db_mysql::_dump_table_data_sql_to_file in Backup and Migrate 8.2

Same name and namespace in other branches
  1. 8.3 includes/destinations.db.mysql.inc \backup_migrate_destination_db_mysql::_dump_table_data_sql_to_file()
  2. 6.2 includes/destinations.db.mysql.inc \backup_migrate_destination_db_mysql::_dump_table_data_sql_to_file()
  3. 7.3 includes/destinations.db.mysql.inc \backup_migrate_destination_db_mysql::_dump_table_data_sql_to_file()
  4. 7.2 includes/destinations.db.mysql.inc \backup_migrate_destination_db_mysql::_dump_table_data_sql_to_file()

Get the sql to insert the data for a given table

1 call to backup_migrate_destination_db_mysql::_dump_table_data_sql_to_file()
backup_migrate_destination_db_mysql::_backup_db_to_file in includes/destinations.db.mysql.inc
Backup the databases to a file.

File

includes/destinations.db.mysql.inc, line 307
Functions to handle the direct to database destination.

Class

backup_migrate_destination_db_mysql
A destination type for saving to a database server.

Code

function _dump_table_data_sql_to_file($file, $table) {
  $rows_per_line = variable_get('backup_migrate_data_rows_per_line', 30);
  $bytes_per_line = variable_get('backup_migrate_data_bytes_per_line', 2000);
  $lines = 0;
  $data = $this
    ->query("SELECT * FROM `" . $table['name'] . "`", array(), array(
    'fetch' => PDO::FETCH_ASSOC,
  ));
  $rows = $bytes = 0;

  // Escape backslashes, PHP code, special chars
  $search = array(
    '\\',
    "'",
    "\0",
    "\n",
    "\r",
    "\32",
  );
  $replace = array(
    '\\\\',
    "''",
    '\\0',
    '\\n',
    '\\r',
    '\\Z',
  );
  $line = array();
  foreach ($data as $row) {

    // DB Escape the values.
    $items = array();
    foreach ($row as $key => $value) {
      $items[] = is_null($value) ? "null" : "'" . str_replace($search, $replace, $value) . "'";
    }

    // If there is a row to be added.
    if ($items) {

      // Start a new line if we need to.
      if ($rows == 0) {
        $file
          ->write("INSERT INTO `" . $table['name'] . "` VALUES ");
        $bytes = $rows = 0;
      }
      else {
        $file
          ->write(",");
      }

      // Write the data itself.
      $sql = implode(',', $items);
      $file
        ->write('(' . $sql . ')');
      $bytes += strlen($sql);
      $rows++;

      // Finish the last line if we've added enough items
      if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) {
        $file
          ->write(";\n");
        $lines++;
        $bytes = $rows = 0;
      }
    }
  }

  // Finish any unfinished insert statements.
  if ($rows > 0) {
    $file
      ->write(";\n");
    $lines++;
  }
  return $lines;
}