protected function MySQLiSource::_dumpTableSqlToFile in Backup and Migrate 5.0.x
Get the sql to insert the data for a given table.
1 call to MySQLiSource::_dumpTableSqlToFile()
- MySQLiSource::exportToFile in src/
Core/ Source/ MySQLiSource.php - Export this source to the given temp file.
File
- src/
Core/ Source/ MySQLiSource.php, line 381
Class
- MySQLiSource
- @package Drupal\backup_migrate\Core\Source
Namespace
Drupal\backup_migrate\Core\SourceCode
protected function _dumpTableSqlToFile(BackupFileWritableInterface $file, $table) {
// If this is a view, do not export any data.
if (empty($table['engine'])) {
return 0;
}
// Otherwise export the table data.
$rows_per_line = 30;
// $this->confGet('rows_per_line');
// variable_get('backup_migrate_data_rows_per_line', 30);
$bytes_per_line = 2000;
// $this->confGet('bytes_per_line');
// variable_get('backup_migrate_data_bytes_per_line', 2000);
$lines = 0;
$result = $this
->query("SELECT * FROM `" . $table['name'] . "`");
$rows = $bytes = 0;
// Escape backslashes, PHP code, special chars.
$search = [
'\\',
"'",
"\0",
"\n",
"\r",
"\32",
];
$replace = [
'\\\\',
"''",
'\\0',
'\\n',
'\\r',
'\\Z',
];
while ($result && ($row = $result
->fetch_assoc())) {
// DB Escape the values.
$items = [];
foreach ($row as $key => $value) {
$items[] = is_null($value) ? "null" : "'" . str_replace($search, $replace, $value) . "'";
// @todo escape binary data.
}
// 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;
}