Skip to content

Commit c1af684

Browse files
Ashley Hindleifsnop
authored andcommitted
Add ability to add table specific 'where' conditions
1 parent ef52f5e commit c1af684

File tree

5 files changed

+108
-3
lines changed

5 files changed

+108
-3
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ $dumper->setTransformColumnValueHook(function ($tableName, $colName, $colValue)
104104
$dumper->start('storage/work/dump.sql');
105105
```
106106

107+
## Table specific export conditions
108+
You can register table specific 'where' clauses to limit data on a per table basis. These override the default `where` dump setting:
109+
110+
```php
111+
$dumper = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
112+
113+
$dumper->setTableWheres(array(
114+
'users' => 'date_registered > NOW() - INTERVAL 3 MONTH AND deleted=0',
115+
'logs' => 'date_logged > NOW() - INTERVAL 1 DAY',
116+
'posts' => 'isLive=1'
117+
));
118+
```
119+
107120
## Constructor and default parameters
108121
```php
109122
/**

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"php": ">=5.3.0"
1818
},
1919
"require-dev": {
20-
"squizlabs/php_codesniffer": "1.*"
20+
"squizlabs/php_codesniffer": "1.*",
21+
"phpunit/phpunit": "4.8.36"
2122
},
2223
"autoload": {
2324
"psr-4": {"Ifsnop\\": "src/Ifsnop/"}

phpunit.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
beStrictAboutCoversAnnotation="true"
7+
beStrictAboutOutputDuringTests="true"
8+
beStrictAboutTestsThatDoNotTestAnything="true"
9+
beStrictAboutTodoAnnotatedTests="true"
10+
colors="true"
11+
verbose="true">
12+
<testsuite name="default">
13+
<directory suffix="Test.php">unit-tests</directory>
14+
</testsuite>
15+
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">src</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/Ifsnop/Mysqldump/Mysqldump.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ class Mysqldump
9898
*/
9999
private $dsnArray = array();
100100

101+
/**
102+
* Keyed on table name, with the value as the conditions
103+
* e.g. - 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH'
104+
*
105+
* @var array
106+
*/
107+
private $tableWheres = array();
108+
101109
/**
102110
* Constructor of Mysqldump. Note that in the case of an SQLite database
103111
* connection, the filename must be in the $db parameter.
@@ -223,6 +231,33 @@ public static function array_replace_recursive($array1, $array2)
223231
return $array1;
224232
}
225233

234+
/**
235+
* Keyed by table name, with the value as the conditions:
236+
* e.g. 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH AND deleted=0'
237+
*
238+
* @param array $tableWheres
239+
*/
240+
public function setTableWheres(array $tableWheres)
241+
{
242+
$this->tableWheres = $tableWheres;
243+
}
244+
245+
/**
246+
* @param $tableName
247+
*
248+
* @return bool|mixed
249+
*/
250+
public function getTableWhere($tableName)
251+
{
252+
if (!empty($this->tableWheres[$tableName])) {
253+
return $this->tableWheres[$tableName];
254+
} elseif ($this->dumpSettings['where']) {
255+
return $this->dumpSettings['where'];
256+
}
257+
258+
return false;
259+
}
260+
226261
/**
227262
* Parse DSN string and extract dbname value
228263
* Several examples of a DSN string
@@ -977,9 +1012,13 @@ private function listValues($tableName)
9771012

9781013
$stmt = "SELECT ".implode(",", $colStmt)." FROM `$tableName`";
9791014

980-
if ($this->dumpSettings['where']) {
981-
$stmt .= " WHERE {$this->dumpSettings['where']}";
1015+
// Table specific conditions override the default 'where'
1016+
$condition = $this->getTableWhere($tableName);
1017+
1018+
if ($condition) {
1019+
$stmt .= " WHERE {$condition}";
9821020
}
1021+
9831022
$resultSet = $this->dbHandler->query($stmt);
9841023
$resultSet->setFetchMode(PDO::FETCH_ASSOC);
9851024

unit-tests/MysqldumpTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Ifsnop\Mysqldump\Mysqldump;
4+
5+
class MysqldumpTest extends PHPUnit_Framework_TestCase
6+
{
7+
8+
/** @test */
9+
public function tableSpecificWhereConditionsWork()
10+
{
11+
$dump = new Mysqldump('mysql:host=localhost;dbname=test', 'testing', 'testing', array(
12+
'where' => 'defaultWhere'
13+
));
14+
15+
$dump->setTableWheres(array(
16+
'users' => 'date_registered > NOW() - INTERVAL 3 MONTH AND is_deleted=0',
17+
'logs' => 'date_registered > NOW() - INTERVAL 1 DAY',
18+
'posts' => 'active=1'
19+
));
20+
21+
$this->assertEquals(
22+
'date_registered > NOW() - INTERVAL 3 MONTH AND is_deleted=0',
23+
$dump->getTableWhere('users')
24+
);
25+
26+
$this->assertEquals(
27+
'defaultWhere',
28+
$dump->getTableWhere('non_overriden_table')
29+
);
30+
}
31+
}

0 commit comments

Comments
 (0)