Multidb.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 = (int) (
  76. isset($params['isDefaultTableAdapter']) && $params['isDefaultTableAdapter']
  77. || isset($params['default']) && $params['default']
  78. );
  79. unset(
  80. $params['adapter'],
  81. $params['default'],
  82. $params['isDefaultTableAdapter']
  83. );
  84. $this->_dbs[$id] = Zend_Db::factory($adapter, $params);
  85. if ($default) {
  86. $this->_setDefault($this->_dbs[$id]);
  87. }
  88. }
  89. return $this;
  90. }
  91. /**
  92. * Determine if the given db(identifier) is the default db.
  93. *
  94. * @param string|Zend_Db_Adapter_Abstract $db The db to determine whether it's set as default
  95. * @return boolean True if the given parameter is configured as default. False otherwise
  96. */
  97. public function isDefault($db)
  98. {
  99. if(!$db instanceof Zend_Db_Adapter_Abstract) {
  100. $db = $this->getDb($db);
  101. }
  102. return $db === $this->_defaultDb;
  103. }
  104. /**
  105. * Retrieve the specified database connection
  106. *
  107. * @param null|string|Zend_Db_Adapter_Abstract $db The adapter to retrieve.
  108. * Null to retrieve the default connection
  109. * @return Zend_Db_Adapter_Abstract
  110. * @throws Zend_Application_Resource_Exception if the given parameter could not be found
  111. */
  112. public function getDb($db = null)
  113. {
  114. if ($db === null) {
  115. return $this->getDefaultDb();
  116. }
  117. if (isset($this->_dbs[$db])) {
  118. return $this->_dbs[$db];
  119. }
  120. throw new Zend_Application_Resource_Exception(
  121. 'A DB adapter was tried to retrieve, but was not configured'
  122. );
  123. }
  124. /**
  125. * Get the default db connection
  126. *
  127. * @param boolean $justPickOne If true, a random (the first one in the stack)
  128. * connection is returned if no default was set.
  129. * If false, null is returned if no default was set.
  130. * @return null|Zend_Db_Adapter_Abstract
  131. */
  132. public function getDefaultDb($justPickOne = true)
  133. {
  134. if ($this->_defaultDb !== null) {
  135. return $this->_defaultDb;
  136. }
  137. if ($justPickOne) {
  138. return reset($this->_dbs); // Return first db in db pool
  139. }
  140. return null;
  141. }
  142. /**
  143. * Set the default db adapter
  144. *
  145. * @var Zend_Db_Adapter_Abstract $adapter Adapter to set as default
  146. */
  147. protected function _setDefault(Zend_Db_Adapter_Abstract $adapter)
  148. {
  149. Zend_Db_Table::setDefaultAdapter($adapter);
  150. $this->_defaultDb = $adapter;
  151. }
  152. }