Permalink
Browse files

Fix queries in PostgreSQL Metadata and make schema optional in all dr…

…ivers.

The metadata queries for PostgreSQL were incorrect. They were using
the schema parameter as the table name (catalog in pgsql) which caused
the queries to fail to produce any work. Furthermore the schema was
hardcoded to 'public' negating the effect of the schema parameter.

In addition to fixing the queries I made the schema parameter to a
database connection optional. It now defaults to an empty string.
In MySQL, SQlite, and Oracle this parameter is not used. PostgreSQL
is likely the only RDBMS to use it. In the PostgreSQL metadata class
if the schema is empty the default 'public' one is used. This is
consistent with the database engine itself where tables created without
a schema default to public.
  • Loading branch information...
1 parent f8a8261 commit 17cf7df1389af9f161188a25f2f274b7e7befe62 @paulyg paulyg committed with Apr 9, 2010
@@ -75,11 +75,6 @@ class PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection implements PHPUni
protected $connection;
/**
- * @var string
- */
- protected $schema;
-
- /**
* The metadata object used to retrieve table meta data from the database.
*
* @var PHPUnit_Extensions_Database_DB_IMetaData
@@ -92,12 +87,10 @@ class PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection implements PHPUni
* @param PDO $connection
* @param string $schema - The name of the database schema you will be testing against.
*/
- public function __construct(PDO $connection, $schema)
+ public function __construct(PDO $connection, $schema = '')
{
$this->connection = $connection;
$this->metaData = PHPUnit_Extensions_Database_DB_MetaData::createMetaData($connection, $schema);
- $this->schema = $schema;
-
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
@@ -127,7 +120,7 @@ public function getMetaData()
*/
public function getSchema()
{
- return $this->schema;
+ return $this->getMetaData()->getSchema();
}
/**
@@ -102,7 +102,7 @@
* @param PDO $pdo
* @param string $schema
*/
- public final function __construct(PDO $pdo, $schema)
+ public final function __construct(PDO $pdo, $schema = '')
{
$this->pdo = $pdo;
$this->schema = $schema;
@@ -116,7 +116,7 @@
* @param string $schema
* @return PHPUnit_Extensions_Database_DB_MetaData
*/
- public static function createMetaData(PDO $pdo, $schema)
+ public static function createMetaData(PDO $pdo, $schema = '')
{
$driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
if (isset(self::$metaDataClassMap[$driverName])) {
@@ -50,7 +50,7 @@
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
/**
- * Provides functionality to retrieve meta data from a database with information_schema support.
+ * Provides functionality to retrieve meta data from a MySQL database.
*
* @category Testing
* @package PHPUnit
@@ -65,10 +65,6 @@ class PHPUnit_Extensions_Database_DB_MetaData_MySQL extends PHPUnit_Extensions_D
{
protected $schemaObjectQuoteChar = '`';
- protected $columns = array();
-
- protected $keys = array();
-
/**
* Returns an array containing the names of all the tables in the database.
*
@@ -50,7 +50,7 @@
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
/**
- * Provides functionality to retrieve meta data from a postgres database.
+ * Provides functionality to retrieve meta data from a PostgreSQL database.
*
* @category Testing
* @package PHPUnit
@@ -61,7 +61,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.2.0
*/
-class PHPUnit_Extensions_Database_DB_MetaData_PgSQL extends PHPUnit_Extensions_Database_DB_MetaData_InformationSchema
+class PHPUnit_Extensions_Database_DB_MetaData_PgSQL extends PHPUnit_Extensions_Database_DB_MetaData
{
/**
@@ -77,8 +77,7 @@ public function getTableNames()
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE='BASE TABLE' AND
- TABLE_CATALOG = ? AND
- TABLE_SCHEMA = 'public'
+ TABLE_SCHEMA = ?
ORDER BY TABLE_NAME
";
@@ -93,8 +92,40 @@ public function getTableNames()
return $tableNames;
}
- /**
- * Loads column info from a sqlite database.
+ /**
+ * Returns an array containing the names of all the columns in the
+ * $tableName table,
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTableColumns($tableName)
+ {
+ if (!isset($this->columns[$tableName])) {
+ $this->loadColumnInfo($tableName);
+ }
+
+ return $this->columns[$tableName];
+ }
+
+ /**
+ * Returns an array containing the names of all the primary key columns in
+ * the $tableName table.
+ *
+ * @param string $tableName
+ * @return array
+ */
+ public function getTablePrimaryKeys($tableName)
+ {
+ if (!isset($this->keys[$tableName])) {
+ $this->loadColumnInfo($tableName);
+ }
+
+ return $this->keys[$tableName];
+ }
+
+ /**
+ * Loads column info from a database table.
*
* @param string $tableName
*/
@@ -105,13 +136,11 @@ protected function loadColumnInfo($tableName)
$columnQuery = "
SELECT DISTINCT
- COLUMN_NAME,
- ORDINAL_POSITION
+ COLUMN_NAME, ORDINAL_POSITION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = ? AND
- TABLE_SCHEMA = 'public' AND
- TABLE_CATALOG = ?
+ TABLE_SCHEMA = ?
ORDER BY ORDINAL_POSITION
";
@@ -127,17 +156,13 @@ protected function loadColumnInfo($tableName)
KCU.COLUMN_NAME,
KCU.ORDINAL_POSITION
FROM
- INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE as KCU
+ LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC
+ ON TC.TABLE_NAME = KCU.TABLE_NAME
WHERE
- TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND
- TC.TABLE_NAME = KCU.TABLE_NAME AND
- TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA AND
- TC.TABLE_CATALOG = KCU.TABLE_CATALOG AND
TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.TABLE_NAME = ? AND
- TC.TABLE_SCHEMA = 'public' AND
- TC.TABLE_CATALOG = ?
+ TC.TABLE_SCHEMA = ?
ORDER BY
KCU.ORDINAL_POSITION ASC
";
@@ -150,6 +175,20 @@ protected function loadColumnInfo($tableName)
}
}
+ /**
+ * Returns the schema for the connection.
+ *
+ * @return string
+ */
+ public function getSchema()
+ {
+ if (empty($this->schema)) {
+ return 'public';
+ } else {
+ return $this->schema;
+ }
+ }
+
/**
* Returns true if the rdbms allows cascading
*
@@ -50,7 +50,7 @@
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
/**
- * Provides functionality to retrieve meta data from a sqlite database.
+ * Provides functionality to retrieve meta data from an Sqlite database.
*
* @category Testing
* @package PHPUnit
@@ -153,7 +153,7 @@ protected function newDatabaseTester()
* @param string $schema
* @return PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
*/
- protected function createDefaultDBConnection(PDO $connection, $schema)
+ protected function createDefaultDBConnection(PDO $connection, $schema = '')
{
return new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($connection, $schema);
}

0 comments on commit 17cf7df

Please sign in to comment.