Generic.php 3.9 KB

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