Permalink
Browse files

- Merge [4494:4495] and [4591:4593].

  • Loading branch information...
sebastianbergmann committed Feb 1, 2009
1 parent f9e3332 commit d5b1e3ac06d054d3710620244ad131f255510c0c
@@ -99,24 +99,37 @@ public function __construct(PHPUnit_Extensions_Database_DB_IDatabaseConnection $
* @param PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData
* @return unknown
*/
- public static function buildTableSelect(PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData)
+ public static function buildTableSelect(PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData, PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection = NULL)
{
if ($tableMetaData->getTableName() == '') {
$e = new Exception("Empty Table Name");
echo $e->getTraceAsString();
throw $e;
}
- $columnList = implode(', ', $tableMetaData->getColumns());
+ $columns = $tableMetaData->getColumns();
+ if ($databaseConnection) {
+ $columns = array_map(array($databaseConnection, 'quoteSchemaObject'), $columns);
+ }
+ $columnList = implode(', ', $columns);
+
+ if ($databaseConnection) {
+ $tableName = $databaseConnection->quoteSchemaObject($tableMetaData->getTableName());
+ } else {
+ $tableName = $tableMetaData->getTableName();
+ }
$primaryKeys = $tableMetaData->getPrimaryKeys();
+ if ($databaseConnection) {
+ $primaryKeys = array_map(array($databaseConnection, 'quoteSchemaObject'), $primaryKeys);
+ }
if (count($primaryKeys)) {
$orderBy = 'ORDER BY ' . implode(' ASC, ', $primaryKeys) . ' ASC';
} else {
$orderBy = '';
}
- return "SELECT {$columnList} FROM {$tableMetaData->getTableName()} {$orderBy}";
+ return "SELECT {$columnList} FROM {$tableName} {$orderBy}";
}
/**
@@ -190,7 +190,7 @@ public function getConnection()
*/
public function getRowCount($tableName, $whereClause = NULL)
{
- $query = "SELECT COUNT(*) FROM {$tableName}";
+ $query = "SELECT COUNT(*) FROM ".$this->quoteSchemaObject($tableName);
if (isset($whereClause)) {
$query .= " WHERE {$whereClause}";
@@ -177,9 +177,39 @@ public function getSchema()
*/
public function quoteSchemaObject($object)
{
- return $this->schemaObjectQuoteChar.
- str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar.$this->schemaObjectQuoteChar, $object).
- $this->schemaObjectQuoteChar;
+ $parts = explode('.', $object);
+ $quotedParts = array();
+
+ foreach ($parts as $part) {
+ $quotedParts[] = $this->schemaObjectQuoteChar .
+ str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar.$this->schemaObjectQuoteChar, $part).
+ $this->schemaObjectQuoteChar;
+ }
+
+ return implode('.', $quotedParts);
+ }
+
+ /**
+ * Seperates the schema and the table from a fully qualified table name.
+ *
+ * Returns an associative array containing the 'schema' and the 'table'.
+ *
+ * @param string $fullTableName
+ * @return array
+ */
+ public function splitTableName($fullTableName)
+ {
+ if (($dot = strpos($fullTableName, '.')) !== FALSE) {
+ return array(
+ 'schema' => substr($fullTableName, 0, $dot),
+ 'table' => substr($fullTableName, $dot + 1)
+ );
+ } else {
+ return array(
+ 'schema' => NULL,
+ 'table' => $fullTableName
+ );
+ }
}
/**
@@ -98,7 +98,7 @@ public function getTableNames()
*/
public function getTableColumns($tableName)
{
- $query = 'SHOW COLUMNS FROM `' . $tableName . '`';
+ $query = 'SHOW COLUMNS FROM ' . $this->quoteSchemaObject($tableName);
$statement = $this->pdo->prepare($query);
$statement->execute();
@@ -119,7 +119,7 @@ public function getTableColumns($tableName)
*/
public function getTablePrimaryKeys($tableName)
{
- $query = 'SHOW INDEX FROM `' . $tableName . '`';
+ $query = 'SHOW INDEX FROM ' . $this->quoteSchemaObject($tableName);
$statement = $this->pdo->prepare($query);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
@@ -132,9 +132,18 @@ protected function loadColumnInfo($tableName)
$this->columns[$tableName] = array();
$this->keys[$tableName] = array();
+ $tableParts = $this->splitTableName($tableName);
+
+ if (!empty($tableParts['schema']))
+ {
+ $ownerQuery = " AND OWNER = '{$schema}'";
+ $conOwnerQuery = " AND a.owner = '{$schema}'";
+ }
+
$query = "SELECT DISTINCT COLUMN_NAME
FROM USER_TAB_COLUMNS
- WHERE TABLE_NAME='".$tableName."'
+ WHERE TABLE_NAME='".$tableParts['table']."'
+ $ownerQuery
ORDER BY COLUMN_NAME";
$result = $this->pdo->query($query);
@@ -147,7 +156,8 @@ protected function loadColumnInfo($tableName)
FROM all_constraints a, all_cons_columns b
WHERE a.constraint_type='P'
AND a.constraint_name=b.constraint_name
- AND a.table_name = '".$tableName."' ";
+ $conOwnerQuery
+ AND a.table_name = '".$tableParts['table']."' ";
$result = $this->pdo->query($keyQuery);
@@ -76,7 +76,7 @@ public function __construct(PHPUnit_Extensions_Database_DataSet_ITableMetaData $
{
$this->setTableMetaData($tableMetaData);
- $pdoStatement = $databaseConnection->getConnection()->prepare(PHPUnit_Extensions_Database_DB_DataSet::buildTableSelect($tableMetaData));
+ $pdoStatement = $databaseConnection->getConnection()->prepare(PHPUnit_Extensions_Database_DB_DataSet::buildTableSelect($tableMetaData, $databaseConnection));
$pdoStatement->execute();
$this->data = $pdoStatement->fetchAll(PDO::FETCH_ASSOC);
}
@@ -78,13 +78,25 @@ class PHPUnit_Extensions_Database_DataSet_DataSetFilter extends PHPUnit_Extensio
* The tables to exclude from the data set.
* @var Array
*/
- protected $excludeTables;
+ protected $excludeTables = array();
+
+ /**
+ * The tables to exclude from the data set.
+ * @var Array
+ */
+ protected $includeTables = array();
/**
* The columns to exclude from the data set.
* @var Array
*/
- protected $excludeColumns;
+ protected $excludeColumns = array();
+
+ /**
+ * The columns to exclude from the data set.
+ * @var Array
+ */
+ protected $includeColumns = array();
/**
* Creates a new filtered data set.
@@ -95,22 +107,24 @@ class PHPUnit_Extensions_Database_DataSet_DataSetFilter extends PHPUnit_Extensio
* to the special string '*'.
*
* @param PHPUnit_Extensions_Database_DataSet_IDataSet $originalDataSet
- * @param Array $excludeTables
+ * @param Array $excludeTables @deprecated use set* methods instead.
*/
- public function __construct(PHPUnit_Extensions_Database_DataSet_IDataSet $originalDataSet, Array $excludeTables)
+ public function __construct(PHPUnit_Extensions_Database_DataSet_IDataSet $originalDataSet, Array $excludeTables = array())
{
$this->originalDataSet = $originalDataSet;
- $this->excludeTables = $excludeTables;
- foreach ($this->excludeTables as $tableName => $values) {
+ $tables = array();
+ foreach ($excludeTables as $tableName => $values) {
if (is_array($values)) {
- $this->excludeColumns[$tableName] = $values;
+ $this->setExcludeColumnsForTable($tableName, $values);
} elseif ($values == '*') {
- $this->excludeTables[] = $tableName;
+ $tables[] = $tableName;
} else {
- $this->excludeColumns[$tableName] = (array)$values;
+ $this->setExcludeColumnsForTable($tableName, (array)$values);
}
}
+
+ $this->addExcludeTables($tables);
}
/**
@@ -129,16 +143,66 @@ protected function createIterator($reverse = FALSE)
/* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
$tableName = $table->getTableMetaData()->getTableName();
- if (in_array($tableName, $this->excludeTables)) {
+ if ((!in_array($tableName, $this->includeTables) && !empty($this->includeTables)) ||
+ in_array($tableName, $this->excludeTables)
+ ) {
continue;
- } elseif (array_key_exists($tableName, $this->excludeColumns)) {
- $new_tables[] = new PHPUnit_Extensions_Database_DataSet_TableFilter($table, $this->excludeColumns[$tableName]);
+ } elseif (!empty($this->excludeColumns[$tableName]) || !empty($this->includeColumns[$tableName])) {
+ $new_table = new PHPUnit_Extensions_Database_DataSet_TableFilter($table);
+
+ if (!empty($this->includeColumns[$tableName])) {
+ $new_table->addIncludeColumns($this->includeColumns[$tableName]);
+ }
+
+ if (!empty($this->excludeColumns[$tableName])) {
+ $new_table->addExcludeColumns($this->excludeColumns[$tableName]);
+ }
+
+ $new_tables[] = $new_table;
} else {
$new_tables[] = $table;
}
}
return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($new_tables);
}
+
+ /**
+ * Adds tables to be included in the data set.
+ * @param array $tables
+ */
+ public function addIncludeTables(Array $tables)
+ {
+ $this->includeTables = array_unique(array_merge($this->includeTables, $tables));
+ }
+
+ /**
+ * Adds tables to be included in the data set.
+ * @param array $tables
+ */
+ public function addExcludeTables(Array $tables)
+ {
+ $this->excludeTables = array_unique(array_merge($this->excludeTables, $tables));
+ }
+
+ /**
+ * Adds columns to include in the data set for the given table.
+ * @param string $table
+ * @param Array $columns
+ */
+ public function setIncludeColumnsForTable($table, Array $columns)
+ {
+ $this->includeColumns[$table] = $columns;
+ }
+
+ /**
+ * Adds columns to include in the data set for the given table.
+ * @param string $table
+ * @param Array $columns
+ */
+ public function setExcludeColumnsForTable($table, Array $columns)
+ {
+ $this->excludeColumns[$table] = $columns;
+ }
}
?>
@@ -73,10 +73,17 @@ class PHPUnit_Extensions_Database_DataSet_TableFilter extends PHPUnit_Extensions
*/
protected $originalTable;
- public function __construct(PHPUnit_Extensions_Database_DataSet_ITable $originalTable, Array $excludeColumns)
+ /**
+ * Creates a new table filter using the original table
+ *
+ * @param $originalTable PHPUnit_Extensions_Database_DataSet_ITable
+ * @param $excludeColumns Array @deprecated, use the set* methods instead.
+ */
+ public function __construct(PHPUnit_Extensions_Database_DataSet_ITable $originalTable, Array $excludeColumns = array())
{
$this->originalTable = $originalTable;
- $this->setTableMetaData(new PHPUnit_Extensions_Database_DataSet_TableMetaDataFilter($originalTable->getTableMetaData(), $excludeColumns));
+ $this->setTableMetaData(new PHPUnit_Extensions_Database_DataSet_TableMetaDataFilter($originalTable->getTableMetaData()));
+ $this->addExcludeColumns($excludeColumns);
}
/**
@@ -103,5 +110,39 @@ public function getValue($row, $column)
throw new InvalidArgumentException("The given row ({$row}) and column ({$column}) do not exist in table {$this->getTableMetaData()->getTableName()}");
}
}
+
+ /**
+ * Sets the columns to include in the table.
+ * @param Array $includeColumns
+ */
+ public function addIncludeColumns(Array $includeColumns)
+ {
+ $this->tableMetaData->addIncludeColumns($includeColumns);
+ }
+
+ /**
+ * Clears the included columns.
+ */
+ public function clearIncludeColumns()
+ {
+ $this->tableMetaData->clearIncludeColumns();
+ }
+
+ /**
+ * Sets the columns to exclude from the table.
+ * @param Array $excludeColumns
+ */
+ public function addExcludeColumns(Array $excludeColumns)
+ {
+ $this->tableMetaData->addExcludeColumns($excludeColumns);
+ }
+
+ /**
+ * Clears the included columns.
+ */
+ public function clearExcludeColumns()
+ {
+ $this->tableMetaData->clearExcludeColumns();
+ }
}
?>
Oops, something went wrong.

0 comments on commit d5b1e3a

Please sign in to comment.