Multidb.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_Application
  17. * @subpackage Resource
  18. * @copyright Copyright (c) 2005-2015 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. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  23. require_once 'Zend/Db/Table.php';
  24. /**
  25. */
  26. /**
  27. * Cache Manager resource
  28. *
  29. * Example configuration:
  30. * <pre>
  31. * resources.multidb.defaultMetadataCache = "database"
  32. *
  33. * resources.multidb.db1.adapter = "pdo_mysql"
  34. * resources.multidb.db1.host = "localhost"
  35. * resources.multidb.db1.username = "webuser"
  36. * resources.multidb.db1.password = "XXXX"
  37. * resources.multidb.db1.dbname = "db1"
  38. * resources.multidb.db1.default = true
  39. *
  40. * resources.multidb.db2.adapter = "pdo_pgsql"
  41. * resources.multidb.db2.host = "example.com"
  42. * resources.multidb.db2.username = "dba"
  43. * resources.multidb.db2.password = "notthatpublic"
  44. * resources.multidb.db2.dbname = "db2"
  45. * </pre>
  46. *
  47. * @category Zend
  48. * @package Zend_Application
  49. * @subpackage Resource
  50. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. */
  53. class Zend_Application_Resource_Multidb extends Zend_Application_Resource_ResourceAbstract
  54. {
  55. /**
  56. * Associative array containing all configured db's
  57. *
  58. * @var array
  59. */
  60. protected $_dbs = array();
  61. /**
  62. * An instance of the default db, if set
  63. *
  64. * @var null|Zend_Db_Adapter_Abstract
  65. */
  66. protected $_defaultDb;
  67. /**
  68. * Initialize the Database Connections (instances of Zend_Db_Table_Abstract)
  69. *
  70. * @return Zend_Application_Resource_Multidb
  71. */
  72. public function init()
  73. {
  74. $options = $this->getOptions();
  75. if (isset($options['defaultMetadataCache'])) {
  76. $this->_setDefaultMetadataCache($options['defaultMetadataCache']);
  77. unset($options['defaultMetadataCache']);
  78. }
  79. foreach ($options as $id => $params) {
  80. $adapter = $params['adapter'];
  81. $default = (int) (
  82. isset($params['isDefaultTableAdapter']) && $params['isDefaultTableAdapter']
  83. || isset($params['default']) && $params['default']
  84. );
  85. unset(
  86. $params['adapter'],
  87. $params['default'],
  88. $params['isDefaultTableAdapter']
  89. );
  90. $this->_dbs[$id] = Zend_Db::factory($adapter, $params);
  91. if ($default) {
  92. $this->_setDefault($this->_dbs[$id]);
  93. }
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Determine if the given db(identifier) is the default db.
  99. *
  100. * @param string|Zend_Db_Adapter_Abstract $db The db to determine whether it's set as default
  101. * @return boolean True if the given parameter is configured as default. False otherwise
  102. */
  103. public function isDefault($db)
  104. {
  105. if (!$db instanceof Zend_Db_Adapter_Abstract) {
  106. $db = $this->getDb($db);
  107. }
  108. return $db === $this->_defaultDb;
  109. }
  110. /**
  111. * Retrieve the specified database connection
  112. *
  113. * @param null|string|Zend_Db_Adapter_Abstract $db The adapter to retrieve.
  114. * Null to retrieve the default connection
  115. * @return Zend_Db_Adapter_Abstract
  116. * @throws Zend_Application_Resource_Exception if the given parameter could not be found
  117. */
  118. public function getDb($db = null)
  119. {
  120. if ($db === null) {
  121. return $this->getDefaultDb();
  122. }
  123. if (isset($this->_dbs[$db])) {
  124. return $this->_dbs[$db];
  125. }
  126. throw new Zend_Application_Resource_Exception(
  127. 'A DB adapter was tried to retrieve, but was not configured'
  128. );
  129. }
  130. /**
  131. * Get the default db connection
  132. *
  133. * @param boolean $justPickOne If true, a random (the first one in the stack)
  134. * connection is returned if no default was set.
  135. * If false, null is returned if no default was set.
  136. * @return null|Zend_Db_Adapter_Abstract
  137. */
  138. public function getDefaultDb($justPickOne = true)
  139. {
  140. if ($this->_defaultDb !== null) {
  141. return $this->_defaultDb;
  142. }
  143. if ($justPickOne) {
  144. return reset($this->_dbs); // Return first db in db pool
  145. }
  146. return null;
  147. }
  148. /**
  149. * Set the default db adapter
  150. *
  151. * @var Zend_Db_Adapter_Abstract $adapter Adapter to set as default
  152. */
  153. protected function _setDefault(Zend_Db_Adapter_Abstract $adapter)
  154. {
  155. Zend_Db_Table::setDefaultAdapter($adapter);
  156. $this->_defaultDb = $adapter;
  157. }
  158. /**
  159. * Set the default metadata cache
  160. *
  161. * @param string|Zend_Cache_Core $cache
  162. * @return Zend_Application_Resource_Multidb
  163. */
  164. protected function _setDefaultMetadataCache($cache)
  165. {
  166. $metadataCache = null;
  167. if (is_string($cache)) {
  168. $bootstrap = $this->getBootstrap();
  169. if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper &&
  170. $bootstrap->hasPluginResource('CacheManager')
  171. ) {
  172. $cacheManager = $bootstrap->bootstrap('CacheManager')
  173. ->getResource('CacheManager');
  174. if (null !== $cacheManager && $cacheManager->hasCache($cache)) {
  175. $metadataCache = $cacheManager->getCache($cache);
  176. }
  177. }
  178. } else if ($cache instanceof Zend_Cache_Core) {
  179. $metadataCache = $cache;
  180. }
  181. if ($metadataCache instanceof Zend_Cache_Core) {
  182. Zend_Db_Table::setDefaultMetadataCache($metadataCache);
  183. }
  184. return $this;
  185. }
  186. }