/** * Tests whether a select query with order by is build correctly. * * @test */ public function testOrderBy() { $table = new Table('`Customer`'); // Table name must be always quoted $tableConfig = new TableConfiguration('Customer'); $tableConfig->setOrderBy('id DESC'); $query = $this->createSelectQueryBuilder($tableConfig, $table)->getSQL(); $this->assertEquals('SELECT * FROM `Customer` t ORDER BY id DESC', $query); }
/** * Find dependencies on other tables in the given foreign keys and add those dependencies as filters to the * table configuration. * * @param ForeignKeyConstraint[] $foreignKeys * @param TableConfiguration $tableConfig * @param DumperConfigurationInterface $config * * @return bool */ private function findDependencies(array $foreignKeys, TableConfiguration $tableConfig, DumperConfigurationInterface $config) { foreach ($foreignKeys as $foreignKey) { $referencedTable = $foreignKey->getForeignTableName(); $referencedTableConfig = $config->getTableConfig($referencedTable); $hasDependency = $referencedTableConfig->getLimit() > 0 || !empty($referencedTableConfig->getFilters()) || $referencedTableConfig->getQuery() != null; if ($hasDependency) { $tableConfig->addFilter(new DataDependentFilter($foreignKey->getColumns()[0], $referencedTable, $foreignKey->getForeignColumns()[0])); } } }
/** * Tests whether a ignored table will be detected correctly. * * @test */ public function testIgnoredTables() { $ignoredTableConfig = new TableConfiguration('ignored'); $ignoredTableConfig->setIgnoreTable(); $config = new SqlDumperConfiguration(); $config->addTableConfig($ignoredTableConfig); $tableFilter = new TableFilter($config); $notIgnoredTable = new Table('not_ignored'); $ignoredTable = new Table('ignored'); $this->assertTrue($tableFilter->isTableNotIgnored($notIgnoredTable)); // not ignored, as not configured $this->assertFalse($tableFilter->isTableNotIgnored($ignoredTable)); // not ignored, as not configured $this->assertEquals(array($notIgnoredTable), $tableFilter->filterIgnoredTables(array($notIgnoredTable, $ignoredTable))); }
/** * Init the array that is used to harvest values for dependency resolving. * * @param TableConfiguration $tableConfig */ private function initValueHarvesting(TableConfiguration $tableConfig) { $this->harvestedValues[$tableConfig->getName()] = array(); $harvestColumns = $tableConfig->getColumnsToHarvest(); foreach ($harvestColumns as $harvestColumn) { $this->harvestedValues[$tableConfig->getName()][$harvestColumn] = array(); } }
/** * Add a new table configuration. * * @param TableConfiguration $tableConfig * * @return TableConfiguration */ public function addTableConfig(TableConfiguration $tableConfig) { return $this->tableConfigs[$tableConfig->getName()] = $tableConfig; }
/** * Validates and modifies the data dependent filter to act like an IN-filter. * * @param DataDependentFilter $filter * @param TableConfiguration $tableConfig * @param array $harvestedValues */ private function handleDataDependentFilter(DataDependentFilter $filter, TableConfiguration $tableConfig, array $harvestedValues) { $tableName = $tableConfig->getName(); $referencedTable = $filter->getReferencedTable(); $referencedColumn = $filter->getReferencedColumn(); // Ensure the dependent table has been dumped before the current table if (!isset($harvestedValues[$referencedTable])) { throw new InvalidArgumentException(sprintf('The table %s has not been dumped before %s', $referencedTable, $tableName)); } // Ensure the necessary column was included in the dump if (!isset($harvestedValues[$referencedTable][$referencedColumn])) { throw new InvalidArgumentException(sprintf('The column %s of table %s has not been dumped.', $referencedTable, $tableName)); } $filter->setValue($harvestedValues[$referencedTable][$referencedColumn]); }