Skip to content

Commit

Permalink
Added conditions on table level. Now you can define which rows are to…
Browse files Browse the repository at this point in the history
… select. #4
  • Loading branch information
dritter committed Dec 4, 2015
1 parent 61f3032 commit 7125abe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ Example:
</slimdump>
```

### Conditions

You may want to select only some rows. In that case you can define a condition on a table.

Example:
```xml
<?xml version="1.0" ?>
<slimdump>
<!-- Dump all users whose usernames begin with foo -->
<table name="user" dump="full" condition="`username` LIKE 'foo%'" />
</slimdump>
```

### Dump modes

The following modes are supported for the `dump` attribute:
Expand Down
16 changes: 16 additions & 0 deletions src/Webfactory/Slimdump/Config/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Table
{
private $selector;
private $dump;
/** @var \SimpleXMLElement */
private $config;

private $columns = array();

Expand All @@ -26,6 +28,8 @@ class Table
*/
public function __construct(\SimpleXMLElement $config)
{
$this->config = $config;

$attr = $config->attributes();
$this->selector = (string) $attr->name;

Expand Down Expand Up @@ -118,6 +122,18 @@ public function getStringForInsertStatement($columnName, $value, $isBlobColumn,
}
}

/**
* @return string - The WHERE condition.
*/
public function getCondition()
{
$condition = (string)$this->config->attributes()->condition;

if (trim($condition) !== '') {
return ' WHERE ' . $condition;
}
}

/**
* @param string $columnName
*
Expand Down
2 changes: 2 additions & 0 deletions src/Webfactory/Slimdump/Database/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public function dumpData($table, Table $tableConfig, Connection $db)
}
$s .= " FROM `$table`";

$s .= $tableConfig->getCondition();

$this->output->writeln("-- BEGIN DATA $table");

$bufferSize = 0;
Expand Down
26 changes: 26 additions & 0 deletions test/Webfactory/Slimdump/Config/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,30 @@ public function testInvalidConfiguration()
new Table($xmlElement);
}

public function testSelectCondition()
{
$xml = '<?xml version="1.0" ?>
<table name="dicht*" dump="full" condition="`first_name` LIKE \'foo%\'" />';

$xmlElement = new \SimpleXMLElement($xml);

$table = new Table($xmlElement);

$this->assertTrue($table->isDataDumpRequired());
$this->assertEquals(' WHERE `first_name` LIKE \'foo%\'', $table->getCondition());
}

public function testSelectConditionWhenConditionIsEmpty()
{
$xml = '<?xml version="1.0" ?>
<table name="dicht*" dump="full" condition=" " />';

$xmlElement = new \SimpleXMLElement($xml);

$table = new Table($xmlElement);

$this->assertTrue($table->isDataDumpRequired());
$this->assertEquals('', $table->getCondition());
}

}

0 comments on commit 7125abe

Please sign in to comment.