1 backup.mysql.inc | protected BackupMySql::dumpTableDataSqlToFile(BackupFile $file, $table) |
Get the SQL to insert the data for a given table.
File
- core/
includes/ backup/ backup.mysql.inc, line 320 - Contains the BackupMySQL class.
Class
- BackupMySql
- Creates and restores backups from a MySQL database source.
Code
protected function dumpTableDataSqlToFile(BackupFile $file, $table) {
$rows_per_query = $this->settings['data_rows_per_query'];
$rows_per_line = $this->settings['data_rows_per_line'];
$bytes_per_line = $this->settings['data_bytes_per_line'];
if ($this->settings['verbose']) {
$this->log('Table: %table', array('%table' => $table['name']), Backup::LOG_INFO);
}
// Escape backslashes, PHP code, special chars.
$search = array('\\', "'", "\x00", "\x0a", "\x0d", "\x1a");
$replace = array('\\\\', "''", '\0', '\n', '\r', '\Z');
$lines = 0;
$from = 0;
$args = array('fetch' => PDO::FETCH_ASSOC);
while ($data = $this->query("SELECT * FROM `" . $table['name'] . "`", array(), $args, $from, $rows_per_query)) {
if ($data->rowCount() == 0) {
break;
}
$rows = $bytes = 0;
foreach ($data as $row) {
$from++;
// DB Escape the values.
$items = array();
foreach ($row as $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;
}
// Otherwise add a comma to end the previous entry.
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++;
}
}
if ($this->settings['verbose']) {
$peak_memory_usage = format_size(memory_get_peak_usage(TRUE), LANGUAGE_SYSTEM);
$this->log('Peak memory usage: %size', array('%size' => $peak_memory_usage), Backup::LOG_INFO);
}
return $lines;
}