Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,27 @@ public function restore($path)
throw new Exception("Failed reading file {$path}. Check access permissions.");
}

$gzfile = false;
# .gz files start with 0x1F 0x8B byte sequence
if(bin2hex(fread($handle, 2)) === '1f8b') {
fclose($handle);
$handle = gzopen($path , 'rb');
$gzfile = true;
} else {
rewind($handle);
}

if(!$this->dbHandler){
$this->connect();
}

$buffer = '';
while ( !feof($handle) ) {
$line = trim(fgets($handle));
if($gzfile) {
$line = trim(gzgets($handle));
} else {
$line = trim(fgets($handle));
}

if (substr($line, 0, 2) == '--' || !$line) {
continue; // skip comments
Expand All @@ -314,7 +328,11 @@ public function restore($path)
}
}

fclose($handle);
if($gzfile) {
gzclose($handle);
} else {
fclose($handle);
}
}

/**
Expand Down Expand Up @@ -1145,7 +1163,6 @@ private function listValues($tableName)
$this->prepareListValues($tableName);

$onlyOnce = true;
$lineSize = 0;

// colStmt is used to form a query to obtain row values
$colStmt = $this->getColumnStmt($tableName);
Expand Down Expand Up @@ -1175,35 +1192,33 @@ private function listValues($tableName)
$ignore = $this->dumpSettings['insert-ignore'] ? ' IGNORE' : '';

$count = 0;
$line = '';
foreach ($resultSet as $row) {
$count++;
$vals = $this->prepareColumnValues($tableName, $row);
if ($onlyOnce || !$this->dumpSettings['extended-insert']) {
if ($this->dumpSettings['complete-insert']) {
$lineSize += $this->compressManager->write(
"INSERT$ignore INTO `$tableName` (".
$line .= "INSERT$ignore INTO `$tableName` (".
implode(", ", $colNames).
") VALUES (".implode(",", $vals).")"
);
") VALUES (".implode(",", $vals).")";
} else {
$lineSize += $this->compressManager->write(
"INSERT$ignore INTO `$tableName` VALUES (".implode(",", $vals).")"
);
$line .= "INSERT$ignore INTO `$tableName` VALUES (".implode(",", $vals).")";
}
$onlyOnce = false;
} else {
$lineSize += $this->compressManager->write(",(".implode(",", $vals).")");
$line .= ",(".implode(",", $vals).")";
}
if (($lineSize > $this->dumpSettings['net_buffer_length']) ||
if ((strlen($line) > $this->dumpSettings['net_buffer_length']) ||
!$this->dumpSettings['extended-insert']) {
$onlyOnce = true;
$lineSize = $this->compressManager->write(";".PHP_EOL);
$this->compressManager->write($line . ";".PHP_EOL);
$line = '';
}
}
$resultSet->closeCursor();

if (!$onlyOnce) {
$this->compressManager->write(";".PHP_EOL);
if ('' !== $line) {
$this->compressManager->write($line. ";".PHP_EOL);
}

$this->endListValues($tableName, $count);
Expand Down
1 change: 1 addition & 0 deletions tests/create_users.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mysql -u root -e "GRANT ALL PRIVILEGES ON test009.* TO 'travis'@'%' WITH GRANT O
mysql -u root -e "GRANT ALL PRIVILEGES ON test010.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -u root -e "GRANT ALL PRIVILEGES ON test011.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -u root -e "GRANT ALL PRIVILEGES ON test012.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -u root -e "GRANT ALL PRIVILEGES ON test014.* TO 'travis'@'%' WITH GRANT OPTION;"
mysql -u root -e "GRANT SUPER,LOCK TABLES ON *.* TO 'travis'@'%';"
mysql -u root -e "GRANT SELECT ON mysql.proc to 'travis'@'%';"
mysql -u root -e "FLUSH PRIVILEGES;"
13 changes: 13 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,17 @@
));
$dump->start("mysqldump-php_test013.sql");

print "starting mysql-php_test014.sql" . PHP_EOL;
$dump = new IMysqldump\Mysqldump(
"mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test014",
"travis",
"",
array(
"insert-ignore" => true,
"extended-insert" => true,
));
$timer=microtime(true);
$dump->start("mysqldump-php_test014.sql");
print round(microtime(true) - $timer,3) . " seconds" . PHP_EOL;

exit(0);
3 changes: 3 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ mysqldump -utravis test001 \
> mysqldump_test013.sql
errCode=$?; ret[((index++))]=$errCode

#speed test
gzip -dc test014.src.sql.gz | mysql -utravis

php test.php || { echo "ERROR running test.php" && exit -1; }
errCode=$?; ret[((index++))]=$errCode

Expand Down
Binary file added tests/test014.src.sql.gz
Binary file not shown.