5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Mike Lively <m@digitalsandwich.com>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.2.0
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49 require_once 'PHPUnit/Extensions/Database/DB/IMetaData.php';
51 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54 * Provides a basic constructor for all meta data classes and a factory for
55 * generating the appropriate meta data class.
59 * @author Mike Lively <m@digitalsandwich.com>
60 * @copyright 2009 Mike Lively <m@digitalsandwich.com>
61 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
62 * @version Release: 3.3.17
63 * @link http://www.phpunit.de/
64 * @since Class available since Release 3.2.0
66 abstract class PHPUnit_Extensions_Database_DB_MetaData implements PHPUnit_Extensions_Database_DB_IMetaData
68 protected static $metaDataClassMap = array(
69 'pgsql' => 'PHPUnit_Extensions_Database_DB_MetaData_PgSQL',
70 'mysql' => 'PHPUnit_Extensions_Database_DB_MetaData_MySQL',
71 'oci' => 'PHPUnit_Extensions_Database_DB_MetaData_Oci',
72 'sqlite' => 'PHPUnit_Extensions_Database_DB_MetaData_Sqlite'
76 * The PDO connection used to retreive database meta data
83 * The default schema name for the meta data object.
90 * The character used to quote schema objects.
92 protected $schemaObjectQuoteChar = '"';
95 * The command used to perform a TRUNCATE operation.
97 protected $truncateCommand = 'TRUNCATE';
100 * Creates a new database meta data object using the given pdo connection
104 * @param string $schema
106 public final function __construct(PDO $pdo, $schema)
109 $this->schema = $schema;
113 * Creates a meta data object based on the driver of given $pdo object and
117 * @param string $schema
118 * @return PHPUnit_Extensions_Database_DB_MetaData
120 public static function createMetaData(PDO $pdo, $schema)
122 $driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
123 if (isset(self::$metaDataClassMap[$driverName])) {
124 $className = self::$metaDataClassMap[$driverName];
126 if ($className instanceof ReflectionClass) {
127 return $className->newInstance($pdo, $schema);
129 return self::registerClassWithDriver($className, $driverName)->newInstance($pdo, $schema);
132 throw new Exception("Could not find a meta data driver for {$driverName} pdo driver.");
137 * Validates and registers the given $className with the given $pdoDriver.
138 * It should be noted that this function will not attempt to include /
139 * require the file. The $pdoDriver can be determined by the value of the
140 * PDO::ATTR_DRIVER_NAME attribute for a pdo object.
142 * A reflection of the $className is returned.
144 * @param string $className
145 * @param string $pdoDriver
146 * @return ReflectionClass
148 public static function registerClassWithDriver($className, $pdoDriver)
150 if (!class_exists($className)) {
151 throw new Exception("Specified class for {$pdoDriver} driver ({$className}) does not exist.");
154 $reflection = new ReflectionClass($className);
155 if ($reflection->isSubclassOf('PHPUnit_Extensions_Database_DB_MetaData')) {
156 return self::$metaDataClassMap[$pdoDriver] = $reflection;
158 throw new Exception("Specified class for {$pdoDriver} driver ({$className}) does not extend PHPUnit_Extensions_Database_DB_MetaData.");
163 * Returns the schema for the connection.
167 public function getSchema()
169 return $this->schema;
173 * Returns a quoted schema object. (table name, column name, etc)
175 * @param string $object
178 public function quoteSchemaObject($object)
180 return $this->schemaObjectQuoteChar.
181 str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar.$this->schemaObjectQuoteChar, $object).
182 $this->schemaObjectQuoteChar;
186 * Returns the command for the database to truncate a table.
190 public function getTruncateCommand()
192 return $this->truncateCommand;
196 * Returns true if the rdbms allows cascading
200 public function allowsCascading()
207 * I am not sure why these requires can't go above the class, but when they do
208 * the classes can't find the PHPUnit_Extensions_Database_DB_MetaData
211 require_once 'PHPUnit/Extensions/Database/DB/MetaData/Sqlite.php';
212 require_once 'PHPUnit/Extensions/Database/DB/MetaData/InformationSchema.php';
213 require_once 'PHPUnit/Extensions/Database/DB/MetaData/MySQL.php';
214 require_once 'PHPUnit/Extensions/Database/DB/MetaData/PgSQL.php';