Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,34 @@ tables:
if_false_converters:
- 'employee'

filters:
filters: # filters are applied with AND operator
- [in, 'id', [1, 2, 3]] # only select rows which id is 1, 2 or 3
- [like, 'name', 'Markus %'] # only select rows which column "name" starts with Markus
- [isNotNull, 'street']
- [or, [gte, id, 1], [lte, id, 3] ] # ( id >= 1 ) OR ( id <= 3)

dependencies: # Used to declare dependencies not constrained by a foreign keys.
- column: "reference_id"
referenced_table: "referenceTable"
referenced_column: "id"

bar:
dependencies: # This will apply the dependency based on the logical condition
- column: "reference_id"
referenced_table: "referenceTableA"
referenced_column: "id"
condition: [eq, 'reference_table', 'referenceTableA']
- column: "reference_id"
referenced_table: "referenceTableB"
referenced_column: "id"
condition: [eq, 'reference_table', 'referenceTableB']

beer:
dependencies: # This will apply all the dependencies dynamically for tables using a column to refer to the dependency
- column: "reference_id"
column_as_referenced_table: "reference_table"
referenced_column: "id"

foo_custom:
query: >
SELECT *, bar.value1 AS xyz
Expand Down
4 changes: 2 additions & 2 deletions src/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

/** @var DumperInterface $dumper */
$class = $config->getFullQualifiedDumperClassName();
$dumper = new $class();
$dumper->dump($context);
$dumper = new $class($context);
$dumper->dump();
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Configuration/DumperConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Digilist\SnakeDumper\Configuration;

use Digilist\SnakeDumper\Dumper\DataLoaderInterface;

interface DumperConfigurationInterface
{

Expand All @@ -14,4 +16,10 @@ public function getDumper();
* @return OutputConfiguration
*/
public function getOutputConfig();

/**
* @param DataLoaderInterface $dataLoader
* @return mixed
*/
public function hydrateConfig(DataLoaderInterface $dataLoader);
}
16 changes: 14 additions & 2 deletions src/Configuration/SnakeConfigurationTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,22 @@ public function getConfigTreeBuilder()
->arrayNode('filters')
->prototype('variable')->end()
->end()
->arrayNode('dependencies')
->arrayPrototype()
->children()
->scalarNode('column')->end()
->scalarNode('column_as_referenced_table')->end()
->scalarNode('referenced_table')->end()
->scalarNode('referenced_column')->end()
->arrayNode('condition')
->prototype('variable')->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
->end();

return $treeBuilder;
}
Expand Down
23 changes: 12 additions & 11 deletions src/Configuration/SqlDumperConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Digilist\SnakeDumper\Configuration;

use Digilist\SnakeDumper\Configuration\Table\Filter\DataDependentFilter;
use Digilist\SnakeDumper\Configuration\Table\TableConfiguration;
use Digilist\SnakeDumper\Configuration\Table\TableDependencyConstraint;
use Digilist\SnakeDumper\Dumper\DataLoaderInterface;

class SqlDumperConfiguration extends AbstractConfiguration implements DumperConfigurationInterface
{
Expand Down Expand Up @@ -133,18 +134,13 @@ public function parseDependencies()
// a table depends on another table if a dependent filter is defined
foreach ($this->tableConfigs as $table) {
// if the dependent table is not configured, create a configuration
/** @var TableDependencyConstraint $dependency */
foreach ($table->getDependentTables() as $dependency) {
if (!array_key_exists($dependency, $this->tableConfigs)) {
$this->tableConfigs[$dependency] = new TableConfiguration($dependency, array());
}
}

// find dependent filters and add harvest columns
foreach ($table->getFilters() as $filter) {
if ($filter instanceof DataDependentFilter) {
// the dependent table needs to collect values of that column
$this->tableConfigs[$filter->getReferencedTable()]->addHarvestColumn($filter->getReferencedColumn());
$dependentTable = $dependency->getReferencedTable();
if (!array_key_exists($dependentTable, $this->tableConfigs)) {
$this->tableConfigs[$dependentTable] = new TableConfiguration($dependentTable, array());
}
$this->tableConfigs[$dependentTable]->addHarvestColumn($dependency->getReferencedColumn());
}
}
}
Expand All @@ -165,7 +161,12 @@ protected function parseConfig(array $dumperConfig)
foreach ($dumperConfig['tables'] as $name => $tableConfig) {
$this->tableConfigs[$name] = new TableConfiguration($name, $tableConfig);
}
}

public function hydrateConfig(DataLoaderInterface $dataLoader) {
foreach ($this->tableConfigs as $tableConfiguration) {
$tableConfiguration->hydrateConfig($dataLoader);
}
$this->parseDependencies();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Digilist\SnakeDumper\Configuration\Table\Filter;

use Digilist\SnakeDumper\Configuration\AbstractConfiguration;
use InvalidArgumentException;

class DefaultFilter implements FilterInterface
class ColumnFilter implements FilterColumnInterface
{

const OPERATOR_EQ = 'eq';
Expand Down Expand Up @@ -65,15 +64,15 @@ class DefaultFilter implements FilterInterface
private $value;

/**
* DefaultFilter constructor.
* ColumnFilter constructor.
*
* @param string $columnName
* @param string $operator
* @param string $value
*/
public function __construct($columnName, $operator, $value = null)
{
if (!in_array($operator, self::$validOperators)) {
if (!self::isValidOperator($operator)) {
throw new InvalidArgumentException('Invalid filter operator: ' . $operator);
}

Expand All @@ -82,6 +81,11 @@ public function __construct($columnName, $operator, $value = null)
$this->value = $value;
}

public static function isValidOperator($operator)
{
return in_array($operator, self::$validOperators);
}

/**
* @return string
*/
Expand Down Expand Up @@ -135,4 +139,12 @@ public function setValue($value)
{
return $this->value = $value;
}

/**
* @return array
*/
public function getFilters()
{
return [$this->columnName, $this->value];
}
}
69 changes: 69 additions & 0 deletions src/Configuration/Table/Filter/CompositeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php


namespace Digilist\SnakeDumper\Configuration\Table\Filter;


use InvalidArgumentException;

class CompositeFilter implements FilterInterface
{
const OPERATOR_OR = 'or';
const OPERATOR_AND = 'and';

private static $validOperators = [
self::OPERATOR_OR,
self::OPERATOR_AND,
];

private static $dbalOperators = [
self::OPERATOR_OR => 'orX',
self::OPERATOR_AND => 'andX'
];

/**
* @var array
*/
private $filters;

/**
* @var string
*/
private $operator;


public function __construct($operator, array $filters = [])
{
if (!self::isValidOperator($operator)) {
throw new InvalidArgumentException('Invalid filter operator: ' . $operator);
}

$this->operator = $operator;
$this->filters = $filters;
}

/**
* @param $operator
* @return bool
*/
public static function isValidOperator($operator)
{
return in_array($operator, self::$validOperators);
}

/**
* @return string
*/
public function getOperator()
{
return self::$dbalOperators[$this->operator];
}

/**
* @return array
*/
public function getFilters()
{
return $this->filters;
}
}
2 changes: 1 addition & 1 deletion src/Configuration/Table/Filter/DataDependentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Digilist\SnakeDumper\Configuration\Table\Filter;

class DataDependentFilter extends DefaultFilter
class DataDependentFilter extends ColumnFilter
{

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Configuration/Table/Filter/FilterColumnInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Digilist\SnakeDumper\Configuration\Table\Filter;

interface FilterColumnInterface extends FilterInterface
{

/**
* @return string
*/
public function getColumnName();


/**
* @return string
*/
public function getValue();
}
26 changes: 26 additions & 0 deletions src/Configuration/Table/Filter/FilterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


namespace Digilist\SnakeDumper\Configuration\Table\Filter;


use InvalidArgumentException;

class FilterFactory
{
public static function buildFilter(array $filter) {
$operator = $filter[0];

if (ColumnFilter::isValidOperator($operator)) {
$columnName = $filter[1];
$value = isset($filter[2]) ? $filter[2] : null;
return new ColumnFilter($columnName, $operator, $value);
}

if (CompositeFilter::isValidOperator($operator)) {
$subFilters = array_map([__CLASS__, 'buildFilter'], array_slice($filter,1));
return new CompositeFilter($operator, $subFilters);
}
throw new InvalidArgumentException('Invalid filter operator: ' . $operator);
}
}
9 changes: 0 additions & 9 deletions src/Configuration/Table/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@
interface FilterInterface
{

/**
* @return string
*/
public function getColumnName();

/**
* @return string
*/
public function getOperator();

/**
* @return string
*/
public function getValue();
}
Loading