Generic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Test
  17. * @subpackage PHPUnit
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Abstract
  24. */
  25. require_once "Zend/Db/Adapter/Abstract.php";
  26. /**
  27. * @see PHPUnit_Extensions_Database_DB_IMetaData
  28. */
  29. require_once "PHPUnit/Extensions/Database/DB/IMetaData.php";
  30. /**
  31. * Generic Metadata accessor for the Zend_Db adapters
  32. *
  33. * @uses PHPUnit_Extensions_Database_DB_IMetaData
  34. * @category Zend
  35. * @package Zend_Test
  36. * @subpackage PHPUnit
  37. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Test_PHPUnit_Db_Metadata_Generic implements PHPUnit_Extensions_Database_DB_IMetaData
  41. {
  42. /**
  43. * Zend_Db Connection
  44. *
  45. * @var Zend_Db_Adapter_Abstract
  46. */
  47. protected $_connection;
  48. /**
  49. * Schemaname
  50. *
  51. * @var string
  52. */
  53. protected $_schema;
  54. /**
  55. * Cached Table metadata
  56. *
  57. * @var array
  58. */
  59. protected $_tableMetadata = array();
  60. /**
  61. * Creates a new database meta data object using the given pdo connection
  62. * and schema name.
  63. *
  64. * @param PDO $pdo
  65. * @param string $schema
  66. */
  67. public final function __construct(Zend_Db_Adapter_Abstract $db, $schema)
  68. {
  69. $this->_connection = $db;
  70. $this->_schema = $schema;
  71. }
  72. /**
  73. * List Tables
  74. *
  75. * @return array
  76. */
  77. public function getTableNames()
  78. {
  79. return $this->_connection->listTables();
  80. }
  81. /**
  82. * Get Table information
  83. *
  84. * @param string $tableName
  85. * @return array
  86. */
  87. protected function getTableDescription($tableName)
  88. {
  89. if(!isset($this->_tableMetadata[$tableName])) {
  90. $this->_tableMetadata[$tableName] = $this->_connection->describeTable($tableName);
  91. }
  92. return $this->_tableMetadata[$tableName];
  93. }
  94. /**
  95. * Returns an array containing the names of all the columns in the
  96. * $tableName table,
  97. *
  98. * @param string $tableName
  99. * @return array
  100. */
  101. public function getTableColumns($tableName)
  102. {
  103. $tableMeta = $this->getTableDescription($tableName);
  104. $columns = array_keys($tableMeta);
  105. return $columns;
  106. }
  107. /**
  108. * Returns an array containing the names of all the primary key columns in
  109. * the $tableName table.
  110. *
  111. * @param string $tableName
  112. * @return array
  113. */
  114. public function getTablePrimaryKeys($tableName)
  115. {
  116. $tableMeta = $this->getTableDescription($tableName);
  117. $primaryColumnNames = array();
  118. foreach($tableMeta AS $column) {
  119. if($column['PRIMARY'] == true) {
  120. $primaryColumnNames[] = $column['COLUMN_NAME'];
  121. }
  122. }
  123. return $primaryColumnNames;
  124. }
  125. /**
  126. * Returns the name of the default schema.
  127. *
  128. * @return string
  129. */
  130. public function getSchema()
  131. {
  132. return $this->_schema;
  133. }
  134. /**
  135. * Returns a quoted schema object. (table name, column name, etc)
  136. *
  137. * @param string $object
  138. * @return string
  139. */
  140. public function quoteSchemaObject($object)
  141. {
  142. return $this->_connection->quoteIdentifier($object);
  143. }
  144. /**
  145. * Returns true if the rdbms allows cascading
  146. *
  147. * @return bool
  148. */
  149. public function allowsCascading()
  150. {
  151. return false;
  152. }
  153. /**
  154. * Disables primary keys if rdbms does not allow setting them otherwise
  155. *
  156. * @param string $tableName
  157. */
  158. public function disablePrimaryKeys($tableName)
  159. {
  160. // Implemented only to match new DBUnit interface
  161. }
  162. /**
  163. * Reenables primary keys after they have been disabled
  164. *
  165. * @param string $tableName
  166. */
  167. public function enablePrimaryKeys($tableName)
  168. {
  169. // Implemented only to match new DBUnit interface
  170. }
  171. }