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';
50 require_once 'PHPUnit/Extensions/Database/DataSet/QueryTable.php';
51 require_once 'PHPUnit/Extensions/Database/DB/IDatabaseConnection.php';
52 require_once 'PHPUnit/Extensions/Database/DB/MetaData.php';
53 require_once 'PHPUnit/Extensions/Database/DB/ResultSetTable.php';
54 require_once 'PHPUnit/Extensions/Database/DB/DataSet.php';
55 require_once 'PHPUnit/Extensions/Database/DB/FilteredDataSet.php';
57 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
60 * Provides a basic interface for communicating with a database.
64 * @author Mike Lively <m@digitalsandwich.com>
65 * @copyright 2009 Mike Lively <m@digitalsandwich.com>
66 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
67 * @version Release: 3.3.17
68 * @link http://www.phpunit.de/
69 * @since Class available since Release 3.2.0
71 class PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection implements PHPUnit_Extensions_Database_DB_IDatabaseConnection
76 protected $connection;
84 * The metadata object used to retrieve table meta data from the database.
86 * @var PHPUnit_Extensions_Database_DB_IMetaData
91 * Creates a new database connection
93 * @param PDO $connection
94 * @param string $schema - The name of the database schema you will be testing against.
96 public function __construct(PDO $connection, $schema)
98 $this->connection = $connection;
99 $this->metaData = PHPUnit_Extensions_Database_DB_MetaData::createMetaData($connection, $schema);
100 $this->schema = $schema;
102 $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
106 * Close this connection.
108 public function close()
110 unset($this->connection);
114 * Returns a database metadata object that can be used to retrieve table
115 * meta data from the database.
117 * @return PHPUnit_Extensions_Database_DB_IMetaData
119 public function getMetaData()
121 return $this->metaData;
125 * Returns the schema for the connection.
129 public function getSchema()
131 return $this->schema;
135 * Creates a dataset containing the specified table names. If no table
136 * names are specified then it will created a dataset over the entire
139 * @param array $tableNames
140 * @return PHPUnit_Extensions_Database_DataSet_IDataSet
141 * @todo Implement the filtered data set.
143 public function createDataSet(Array $tableNames = NULL)
145 if (empty($tableNames)) {
146 return new PHPUnit_Extensions_Database_DB_DataSet($this);
148 return new PHPUnit_Extensions_Database_DB_FilteredDataSet($this, $tableNames);
153 * Creates a table with the result of the specified SQL statement.
155 * @param string $resultName
157 * @return PHPUnit_Extensions_Database_DB_Table
159 public function createQueryTable($resultName, $sql)
161 return new PHPUnit_Extensions_Database_DataSet_QueryTable($resultName, $sql, $this);
165 * Returns this connection database configuration
167 * @return PHPUnit_Extensions_Database_Database_DatabaseConfig
169 public function getConfig()
175 * Returns a PDO Connection
179 public function getConnection()
181 return $this->connection;
185 * Returns the number of rows in the given table. You can specify an
186 * optional where clause to return a subset of the table.
188 * @param string $tableName
189 * @param string $whereClause
192 public function getRowCount($tableName, $whereClause = NULL)
194 $query = "SELECT COUNT(*) FROM {$tableName}";
196 if (isset($whereClause)) {
197 $query .= " WHERE {$whereClause}";
202 * Returns a quoted schema object. (table name, column name, etc)
204 * @param string $object
207 public function quoteSchemaObject($object)
209 return $this->getMetaData()->quoteSchemaObject($object);
213 * Returns the command used to truncate a table.
217 public function getTruncateCommand()
219 return $this->getMetaData()->getTruncateCommand();
223 * Returns true if the connection allows cascading
227 public function allowsCascading()
229 return $this->getMetaData()->allowsCascading();