Skip to content

Commit

Permalink
Merge pull request #188 from guvra/feat-transform-hook-optimizations
Browse files Browse the repository at this point in the history
Add a hook on table rows, optimize table modifications using hooks
  • Loading branch information
ifsnop committed Feb 12, 2020
2 parents 0931ad6 + 9e1394c commit 7533cf0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,25 @@ Plain old PHP:
Refer to the [wiki](https://github.com/ifsnop/mysqldump-php/wiki/Full-usage-example) for some examples and a comparision between mysqldump and mysqldump-php dumps.

## Changing values when exporting

You can register a callable that will be used to transform values during the export. An example use-case for this is removing sensitive data from database dumps:

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

$dumper->setTransformColumnValueHook(function ($tableName, $colName, $colValue) {
if ($colName === 'social_security_number') {
return (string) rand(1000000, 9999999);
$dumper->setTransformTableRowHook(function ($tableName, array $row) {
if ($tableName === 'customers') {
$row['social_security_number'] = (string) rand(1000000, 9999999);
}

return $colValue;
return $row;
});

$dumper->start('storage/work/dump.sql');
```

## Getting information about the dump

You can register a callable that will be used to report on the progress of the dump

```php
Expand All @@ -115,6 +118,7 @@ $dumper->setInfoHook(function($object, $info) {
```

## Table specific export conditions

You can register table specific 'where' clauses to limit data on a per table basis. These override the default `where` dump setting:

```php
Expand All @@ -128,6 +132,7 @@ $dumper->setTableWheres(array(
```

## Table specific export limits

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

```php
Expand All @@ -141,6 +146,7 @@ $dumper->setTableLimits(array(
```

## Constructor and default parameters

```php
/**
* Constructor of Mysqldump. Note that in the case of an SQLite database
Expand Down
49 changes: 24 additions & 25 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Mysqldump
private $pdoSettings = array();
private $version;
private $tableColumnTypes = array();
private $transformTableRowCallable;
private $transformColumnValueCallable;
private $infoCallable;

Expand Down Expand Up @@ -1017,12 +1018,20 @@ private function getEventStructure($eventName)
*
* @return array
*/
private function prepareColumnValues($tableName, $row)
private function prepareColumnValues($tableName, array $row)
{
$ret = array();
$columnTypes = $this->tableColumnTypes[$tableName];

if ($this->transformTableRowCallable) {
$row = call_user_func($this->transformTableRowCallable, $tableName, $row);
}

foreach ($row as $colName => $colValue) {
$colValue = $this->hookTransformColumnValue($tableName, $colName, $colValue, $row);
if ($this->transformColumnValueCallable) {
$colValue = call_user_func($this->transformColumnValueCallable, $tableName, $colName, $colValue, $row);
}

$ret[] = $this->escape($colValue, $columnTypes[$colName]);
}

Expand Down Expand Up @@ -1055,51 +1064,41 @@ private function escape($colValue, $colType)
}

/**
* Set a callable that will will be used to transform column values.
* Set a callable that will be used to transform table rows
*
* @param callable $callable
*
* @return void
*/
public function setTransformColumnValueHook($callable)
public function setTransformTableRowHook($callable)
{
$this->transformColumnValueCallable = $callable;
$this->transformTableRowCallable = $callable;
}

/**
* Set a callable that will be used to report dump information
* Set a callable that will be used to transform column values
*
* @param callable $callable
*
* @return void
*
* @deprecated Use setTransformTableRowHook instead for better performance
*/
public function setInfoHook($callable)
public function setTransformColumnValueHook($callable)
{
$this->infoCallable = $callable;
$this->transformColumnValueCallable = $callable;
}

/**
* Give extending classes an opportunity to transform column values
* Set a callable that will be used to report dump information
*
* @param string $tableName Name of table which contains rows
* @param string $colName Name of the column in question
* @param string $colValue Value of the column in question
* @param array $row Full row
* @param callable $callable
*
* @return string
* @return void
*/
protected function hookTransformColumnValue($tableName, $colName, $colValue, $row)
public function setInfoHook($callable)
{
if (!$this->transformColumnValueCallable) {
return $colValue;
}

return call_user_func_array($this->transformColumnValueCallable, array(
$tableName,
$colName,
$colValue,
$row
));
$this->infoCallable = $callable;
}

/**
Expand Down

0 comments on commit 7533cf0

Please sign in to comment.