diff --git a/README.md b/README.md index df368c0d..f45a35d8 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,15 @@ $dumper->setTableLimits([ 'posts' => 10 ]); ``` +You can also specify the limits as an array where the first value is the number of rows and the second is the offset +```php +$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password'); + +$dumper->setTableLimits([ + 'users' => [20, 10], //MySql query equivalent "... LIMIT 20 OFFSET 10" +]); +``` ## Dump Settings Dump settings can be changed from default values with 4th argument for Mysqldump constructor: diff --git a/src/Mysqldump.php b/src/Mysqldump.php index 3fa32ddb..22ede6c5 100644 --- a/src/Mysqldump.php +++ b/src/Mysqldump.php @@ -75,7 +75,8 @@ public function __construct( ?string $pass = null, array $settings = [], array $pdoOptions = [] - ) { + ) + { $this->dsn = $this->parseDsn($dsn); $this->user = $user; $this->pass = $pass; @@ -249,20 +250,20 @@ private function getDumpFileHeader(): string { // Some info about software, source and time $header = sprintf( - "-- mysqldump-php https://github.com/druidfi/mysqldump-php". PHP_EOL. - "--". PHP_EOL. - "-- Host: %s\tDatabase: %s". PHP_EOL. - "-- ------------------------------------------------------". PHP_EOL, + "-- mysqldump-php https://github.com/druidfi/mysqldump-php" . PHP_EOL . + "--" . PHP_EOL . + "-- Host: %s\tDatabase: %s" . PHP_EOL . + "-- ------------------------------------------------------" . PHP_EOL, $this->host, $this->dbName ); if (!empty($version = $this->db->getVersion())) { - $header .= "-- Server version \t". $version . PHP_EOL; + $header .= "-- Server version \t" . $version . PHP_EOL; } if (!$this->settings->skipDumpDate()) { - $header .= "-- Date: ".date('r'). PHP_EOL . PHP_EOL; + $header .= "-- Date: " . date('r') . PHP_EOL . PHP_EOL; } return $header; @@ -276,7 +277,7 @@ private function getDumpFileFooter(): string $footer = '-- Dump completed'; if (!$this->settings->skipDumpDate()) { - $footer .= ' on: '.date('r'); + $footer .= ' on: ' . date('r'); } $footer .= PHP_EOL; @@ -510,9 +511,9 @@ private function getTableStructure(string $tableName) if (!$this->settings->skipComments()) { $ret = sprintf( - "--".PHP_EOL. - "-- Table structure for table `%s`".PHP_EOL. - "--".PHP_EOL.PHP_EOL, + "--" . PHP_EOL . + "-- Table structure for table `%s`" . PHP_EOL . + "--" . PHP_EOL . PHP_EOL, $tableName ); } @@ -550,7 +551,7 @@ private function getTableColumnTypes(string $tableName): array foreach ($columns as $col) { $types = $this->db->parseColumnType($col); $columnTypes[$col['Field']] = [ - 'is_numeric'=> $types['is_numeric'], + 'is_numeric' => $types['is_numeric'], 'is_blob' => $types['is_blob'], 'type' => $types['type'], 'type_sql' => $col['Type'], @@ -610,7 +611,7 @@ private function createStandInTable(string $viewName): string $ret = implode(PHP_EOL . ',', $ret); return sprintf( - "CREATE TABLE IF NOT EXISTS `%s` (".PHP_EOL."%s".PHP_EOL.");".PHP_EOL, + "CREATE TABLE IF NOT EXISTS `%s` (" . PHP_EOL . "%s" . PHP_EOL . ");" . PHP_EOL, $viewName, $ret ); @@ -623,9 +624,9 @@ private function getViewStructureView(string $viewName) { if (!$this->settings->skipComments()) { $ret = sprintf( - "--". PHP_EOL. - "-- View structure for view `%s`". PHP_EOL. - "--". PHP_EOL . PHP_EOL, + "--" . PHP_EOL . + "-- View structure for view `%s`" . PHP_EOL . + "--" . PHP_EOL . PHP_EOL, $viewName ); @@ -672,9 +673,9 @@ private function getTriggerStructure(string $triggerName) private function getProcedureStructure(string $procedureName) { if (!$this->settings->skipComments()) { - $ret = "--".PHP_EOL. - "-- Dumping routines for database '".$this->dbName."'".PHP_EOL. - "--".PHP_EOL.PHP_EOL; + $ret = "--" . PHP_EOL . + "-- Dumping routines for database '" . $this->dbName . "'" . PHP_EOL . + "--" . PHP_EOL . PHP_EOL; $this->write($ret); } @@ -695,9 +696,9 @@ private function getProcedureStructure(string $procedureName) private function getFunctionStructure(string $functionName) { if (!$this->settings->skipComments()) { - $ret = "--".PHP_EOL. - "-- Dumping routines for database '".$this->dbName."'".PHP_EOL. - "--".PHP_EOL.PHP_EOL; + $ret = "--" . PHP_EOL . + "-- Dumping routines for database '" . $this->dbName . "'" . PHP_EOL . + "--" . PHP_EOL . PHP_EOL; $this->write($ret); } @@ -719,9 +720,9 @@ private function getFunctionStructure(string $functionName) private function getEventStructure(string $eventName) { if (!$this->settings->skipComments()) { - $ret = "--".PHP_EOL. - "-- Dumping events for database '".$this->dbName."'".PHP_EOL. - "--".PHP_EOL.PHP_EOL; + $ret = "--" . PHP_EOL . + "-- Dumping events for database '" . $this->dbName . "'" . PHP_EOL . + "--" . PHP_EOL . PHP_EOL; $this->write($ret); } @@ -801,7 +802,7 @@ private function listValues(string $tableName) $colNames = $this->getColumnNames($tableName); } - $stmt = "SELECT ".implode(",", $colStmt)." FROM `$tableName`"; + $stmt = "SELECT " . implode(",", $colStmt) . " FROM `$tableName`"; // Table specific conditions override the default 'where' $condition = $this->getTableWhere($tableName); @@ -811,7 +812,9 @@ private function listValues(string $tableName) } if ($limit = $this->getTableLimit($tableName)) { - $stmt .= sprintf(' LIMIT %d', $limit); + $stmt .= is_numeric($limit) ? + sprintf(' LIMIT %d', $limit) : + sprintf(' LIMIT %s', $limit); } $resultSet = $this->conn->query($stmt); @@ -873,9 +876,9 @@ private function prepareListValues(string $tableName) { if (!$this->settings->skipComments()) { $this->write( - "--".PHP_EOL. - "-- Dumping data for table `$tableName`".PHP_EOL. - "--".PHP_EOL.PHP_EOL + "--" . PHP_EOL . + "-- Dumping data for table `$tableName`" . PHP_EOL . + "--" . PHP_EOL . PHP_EOL ); } @@ -936,8 +939,8 @@ private function endListValues(string $tableName, int $count = 0) if (!$this->settings->skipComments()) { $this->write( - "-- Dumped table `".$tableName."` with $count row(s)".PHP_EOL. - '--'.PHP_EOL.PHP_EOL + "-- Dumped table `" . $tableName . "` with $count row(s)" . PHP_EOL . + '--' . PHP_EOL . PHP_EOL ); } } @@ -1037,7 +1040,20 @@ public function getTableLimit(string $tableName) return false; } - return is_numeric($this->tableLimits[$tableName]) ? $this->tableLimits[$tableName] : false; + $limit = false; + + if (is_numeric($this->tableLimits[$tableName])) { + $limit = $this->tableLimits[$tableName]; + } + + if (is_array($this->tableLimits[$tableName]) && + count($this->tableLimits[$tableName]) === 2 && + is_numeric(implode('', $this->tableLimits[$tableName])) + ) { + $limit = implode(',', $this->tableLimits[$tableName]); + } + + return $limit; } /** diff --git a/tests/MysqldumpTest.php b/tests/MysqldumpTest.php index a8a29103..6a643ac9 100644 --- a/tests/MysqldumpTest.php +++ b/tests/MysqldumpTest.php @@ -97,13 +97,22 @@ public function testTableSpecificLimitsWork() $dump->setTableLimits([ 'users' => 200, 'logs' => 500, - 'table_with_invalid_limit' => '41923, 42992' + 'table_with_invalid_limit' => '41923, 42992', + 'table_with_range_limit' => [100, 300], + 'table_with_range_limit2' => [1, 1], + 'table_with_invalid_range_limit' => [100], + 'table_with_invalid_range_limit2' => [100, 300, 400], + ]); $this->assertEquals(200, $dump->getTableLimit('users')); $this->assertEquals(500, $dump->getTableLimit('logs')); $this->assertFalse($dump->getTableLimit('table_with_invalid_limit')); $this->assertFalse($dump->getTableLimit('table_name_with_no_limit')); + $this->assertEquals('100,300', $dump->getTableLimit('table_with_range_limit')); + $this->assertFalse($dump->getTableLimit('table_with_invalid_range_limit')); + $this->assertFalse($dump->getTableLimit('table_with_invalid_range_limit2')); + $this->assertFalse($dump->getTableLimit('table_with_invalid_range_limit3')); } private function getPrivate(Mysqldump $dump, $var)