Skip to content

Commit

Permalink
Add table specific LIMITs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashley Hindle authored and ifsnop committed Feb 17, 2019
1 parent c1af684 commit 9d131c2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ $dumper->setTableWheres(array(
));
```

## Table specific export limits
You can register table specific 'limits' to limit the returned rows on a per table basis:

```php
$dumper = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');

$dumper->setTableLimits(array(
'users' => 300,
'logs' => 50,
'posts' => 10
));
```

## Constructor and default parameters
```php
/**
Expand Down
37 changes: 37 additions & 0 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Mysqldump
* @var array
*/
private $tableWheres = array();
private $tableLimits = array();

/**
* Constructor of Mysqldump. Note that in the case of an SQLite database
Expand Down Expand Up @@ -258,6 +259,36 @@ public function getTableWhere($tableName)
return false;
}

/**
* Keyed by table name, with the value as the numeric limit:
* e.g. 'users' => 3000
*
* @param array $tableLimits
*/
public function setTableLimits(array $tableLimits)
{
$this->tableLimits = $tableLimits;
}

/**
* Returns the LIMIT for the table. Must be numeric to be returned.
* @param $tableName
* @return bool
*/
public function getTableLimit($tableName)
{
if (empty($this->tableLimits[$tableName])) {
return false;
}

$limit = $this->tableLimits[$tableName];
if (!is_numeric($limit)) {
return false;
}

return $limit;
}

/**
* Parse DSN string and extract dbname value
* Several examples of a DSN string
Expand Down Expand Up @@ -1019,6 +1050,12 @@ private function listValues($tableName)
$stmt .= " WHERE {$condition}";
}

$limit = $this->getTableLimit($tableName);

if ($limit) {
$stmt .= " LIMIT {$limit}";
}

$resultSet = $this->dbHandler->query($stmt);
$resultSet->setFetchMode(PDO::FETCH_ASSOC);

Expand Down
17 changes: 17 additions & 0 deletions unit-tests/MysqldumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ public function tableSpecificWhereConditionsWork()
$dump->getTableWhere('non_overriden_table')
);
}

/** @test */
public function tableSpecificLimitsWork()
{
$dump = new Mysqldump('mysql:host=localhost;dbname=test', 'testing', 'testing');

$dump->setTableLimits(array(
'users' => 200,
'logs' => 500,
'table_with_invalid_limit' => '41923, 42992'
));

$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'));
}
}

0 comments on commit 9d131c2

Please sign in to comment.