Generic.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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-2014 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. * Generic Metadata accessor for the Zend_Db adapters
  28. *
  29. * @uses PHPUnit_Extensions_Database_DB_IMetaData
  30. * @category Zend
  31. * @package Zend_Test
  32. * @subpackage PHPUnit
  33. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Test_PHPUnit_Db_Metadata_Generic implements PHPUnit_Extensions_Database_DB_IMetaData
  37. {
  38. /**
  39. * Zend_Db Connection
  40. *
  41. * @var Zend_Db_Adapter_Abstract
  42. */
  43. protected $_connection;
  44. /**
  45. * Schemaname
  46. *
  47. * @var string
  48. */
  49. protected $_schema;
  50. /**
  51. * Cached Table metadata
  52. *
  53. * @var array
  54. */
  55. protected $_tableMetadata = array();
  56. /**
  57. * Creates a new database meta data object using the given pdo connection
  58. * and schema name.
  59. *
  60. * @param Zend_Db_Adapter_Abstract $db
  61. * @param string $schema
  62. */
  63. public final function __construct(Zend_Db_Adapter_Abstract $db, $schema)
  64. {
  65. $this->_connection = $db;
  66. $this->_schema = $schema;
  67. }
  68. /**
  69. * List Tables
  70. *
  71. * @return array
  72. */
  73. public function getTableNames()
  74. {
  75. return $this->_connection->listTables();
  76. }
  77. /**
  78. * Get Table information
  79. *
  80. * @param string $tableName
  81. * @return array
  82. */
  83. protected function getTableDescription($tableName)
  84. {
  85. if(!isset($this->_tableMetadata[$tableName])) {
  86. $this->_tableMetadata[$tableName] = $this->_connection->describeTable($tableName);
  87. }
  88. return $this->_tableMetadata[$tableName];
  89. }
  90. /**
  91. * Returns an array containing the names of all the columns in the
  92. * $tableName table,
  93. *
  94. * @param string $tableName
  95. * @return array
  96. */
  97. public function getTableColumns($tableName)
  98. {
  99. $tableMeta = $this->getTableDescription($tableName);
  100. $columns = array_keys($tableMeta);
  101. return $columns;
  102. }
  103. /**
  104. * Returns an array containing the names of all the primary key columns in
  105. * the $tableName table.
  106. *
  107. * @param string $tableName
  108. * @return array
  109. */
  110. public function getTablePrimaryKeys($tableName)
  111. {
  112. $tableMeta = $this->getTableDescription($tableName);
  113. $primaryColumnNames = array();
  114. foreach($tableMeta AS $column) {
  115. if($column['PRIMARY'] == true) {
  116. $primaryColumnNames[] = $column['COLUMN_NAME'];
  117. }
  118. }
  119. return $primaryColumnNames;
  120. }
  121. /**
  122. * Returns the name of the default schema.
  123. *
  124. * @return string
  125. */
  126. public function getSchema()
  127. {
  128. return $this->_schema;
  129. }
  130. /**
  131. * Returns a quoted schema object. (table name, column name, etc)
  132. *
  133. * @param string $object
  134. * @return string
  135. */
  136. public function quoteSchemaObject($object)
  137. {
  138. return $this->_connection->quoteIdentifier($object);
  139. }
  140. /**
  141. * Returns true if the rdbms allows cascading
  142. *
  143. * @return bool
  144. */
  145. public function allowsCascading()
  146. {
  147. return false;
  148. }
  149. /**
  150. * Disables primary keys if rdbms does not allow setting them otherwise
  151. *
  152. * @param string $tableName
  153. */
  154. public function disablePrimaryKeys($tableName)
  155. {
  156. // Implemented only to match new DBUnit interface
  157. }
  158. /**
  159. * Reenables primary keys after they have been disabled
  160. *
  161. * @param string $tableName
  162. */
  163. public function enablePrimaryKeys($tableName)
  164. {
  165. // Implemented only to match new DBUnit interface
  166. }
  167. }