2
0

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