Multidb.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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-2010 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.db1.adapter = "pdo_mysql"
  32. * resources.multidb.db1.host = "localhost"
  33. * resources.multidb.db1.username = "webuser"
  34. * resources.multidb.db1.password = "XXXX"
  35. * resources.multidb.db1.dbname = "db1"
  36. * resources.multidb.db1.default = true
  37. *
  38. * resources.multidb.db2.adapter = "pdo_pgsql"
  39. * resources.multidb.db2.host = "example.com"
  40. * resources.multidb.db2.username = "dba"
  41. * resources.multidb.db2.password = "notthatpublic"
  42. * resources.multidb.db2.dbname = "db2"
  43. * </pre>
  44. *
  45. * @category Zend
  46. * @package Zend_Application
  47. * @subpackage Resource
  48. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  49. * @license http://framework.zend.com/license/new-bsd New BSD License
  50. */
  51. class Zend_Application_Resource_Multidb extends Zend_Application_Resource_ResourceAbstract
  52. {
  53. /**
  54. * Associative array containing all configured db's
  55. *
  56. * @var array
  57. */
  58. protected $_dbs = array();
  59. /**
  60. * An instance of the default db, if set
  61. *
  62. * @var null|Zend_Db_Adapter_Abstract
  63. */
  64. protected $_defaultDb;
  65. /**
  66. * Initialize the Database Connections (instances of Zend_Db_Table_Abstract)
  67. *
  68. * @return Zend_Application_Resource_Multidb
  69. */
  70. public function init()
  71. {
  72. $options = $this->getOptions();
  73. foreach ($options as $id => $params) {
  74. $adapter = $params['adapter'];
  75. $default = isset($params['default'])?(int)$params['default']:false;
  76. unset($params['adapter'], $params['default']);
  77. $this->_dbs[$id] = Zend_Db::factory($adapter, $params);
  78. if ($default
  79. // For consistency with the Db Resource Plugin
  80. || (isset($params['isDefaultTableAdapter'])
  81. && $params['isDefaultTableAdapter'] == true)
  82. ) {
  83. $this->_setDefault($this->_dbs[$id]);
  84. }
  85. }
  86. return $this;
  87. }
  88. /**
  89. * Determine if the given db(identifier) is the default db.
  90. *
  91. * @param string|Zend_Db_Adapter_Abstract $db The db to determine whether it's set as default
  92. * @return boolean True if the given parameter is configured as default. False otherwise
  93. */
  94. public function isDefault($db)
  95. {
  96. if(!$db instanceof Zend_Db_Adapter_Abstract) {
  97. $db = $this->getDb($db);
  98. }
  99. return $db === $this->_defaultDb;
  100. }
  101. /**
  102. * Retrieve the specified database connection
  103. *
  104. * @param null|string|Zend_Db_Adapter_Abstract $db The adapter to retrieve.
  105. * Null to retrieve the default connection
  106. * @return Zend_Db_Adapter_Abstract
  107. * @throws Zend_Application_Resource_Exception if the given parameter could not be found
  108. */
  109. public function getDb($db = null)
  110. {
  111. if ($db === null) {
  112. return $this->getDefaultDb();
  113. }
  114. if (isset($this->_dbs[$db])) {
  115. return $this->_dbs[$db];
  116. }
  117. throw new Zend_Application_Resource_Exception(
  118. 'A DB adapter was tried to retrieve, but was not configured'
  119. );
  120. }
  121. /**
  122. * Get the default db connection
  123. *
  124. * @param boolean $justPickOne If true, a random (the first one in the stack)
  125. * connection is returned if no default was set.
  126. * If false, null is returned if no default was set.
  127. * @return null|Zend_Db_Adapter_Abstract
  128. */
  129. public function getDefaultDb($justPickOne = true)
  130. {
  131. if ($this->_defaultDb !== null) {
  132. return $this->_defaultDb;
  133. }
  134. if ($justPickOne) {
  135. return reset($this->_dbs); // Return first db in db pool
  136. }
  137. return null;
  138. }
  139. /**
  140. * Set the default db adapter
  141. *
  142. * @var Zend_Db_Adapter_Abstract $adapter Adapter to set as default
  143. */
  144. protected function _setDefault(Zend_Db_Adapter_Abstract $adapter)
  145. {
  146. Zend_Db_Table::setDefaultAdapter($adapter);
  147. $this->_defaultDb = $adapter;
  148. }
  149. }