Skip to content

Commit

Permalink
Merge pull request #16 from WatchTowerHQ/main
Browse files Browse the repository at this point in the history
table limit range support
  • Loading branch information
back-2-95 authored Nov 8, 2022
2 parents 9c69f7a + aeca7c0 commit 6842f6d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 34 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
82 changes: 49 additions & 33 deletions src/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
);
}
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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
);
}
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
11 changes: 10 additions & 1 deletion tests/MysqldumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6842f6d

Please sign in to comment.