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