diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index 0a2f2b7e..77571444 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -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 @@ -314,7 +328,11 @@ public function restore($path) } } - fclose($handle); + if($gzfile) { + gzclose($handle); + } else { + fclose($handle); + } } /** @@ -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); @@ -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); diff --git a/tests/create_users.sh b/tests/create_users.sh index 124955ee..7dccbbcb 100755 --- a/tests/create_users.sh +++ b/tests/create_users.sh @@ -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;" diff --git a/tests/test.php b/tests/test.php index 49a6f4ff..f806b29b 100644 --- a/tests/test.php +++ b/tests/test.php @@ -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); diff --git a/tests/test.sh b/tests/test.sh index 7ad22257..f829f7d3 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -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 diff --git a/tests/test014.src.sql.gz b/tests/test014.src.sql.gz new file mode 100644 index 00000000..69c8b341 Binary files /dev/null and b/tests/test014.src.sql.gz differ