1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31:
32: class Mage_Core_Model_Resource
33: {
34: const AUTO_UPDATE_CACHE_KEY = 'DB_AUTOUPDATE';
35: const AUTO_UPDATE_ONCE = 0;
36: const AUTO_UPDATE_NEVER = -1;
37: const AUTO_UPDATE_ALWAYS = 1;
38:
39: const DEFAULT_READ_RESOURCE = 'core_read';
40: const DEFAULT_WRITE_RESOURCE = 'core_write';
41: const DEFAULT_SETUP_RESOURCE = 'core_setup';
42:
43: 44: 45: 46: 47:
48: protected $_connectionTypes = array();
49:
50: 51: 52: 53: 54:
55: protected $_connections = array();
56:
57: 58: 59: 60: 61:
62: protected $_skippedConnections = array();
63:
64: 65: 66: 67: 68:
69: protected $_entities = array();
70:
71: 72: 73: 74: 75:
76: protected $_mappedTableNames;
77:
78: 79: 80: 81: 82: 83:
84: public function getConnection($name)
85: {
86: if (isset($this->_connections[$name])) {
87: $connection = $this->_connections[$name];
88: if (isset($this->_skippedConnections[$name]) && !Mage::app()->getIsCacheLocked()) {
89: $connection->setCacheAdapter(Mage::app()->getCache());
90: unset($this->_skippedConnections[$name]);
91: }
92: return $connection;
93: }
94: $connConfig = Mage::getConfig()->getResourceConnectionConfig($name);
95:
96: if (!$connConfig) {
97: $this->_connections[$name] = $this->_getDefaultConnection($name);
98: return $this->_connections[$name];
99: }
100: if (!$connConfig->is('active', 1)) {
101: return false;
102: }
103:
104: $origName = $connConfig->getParent()->getName();
105: if (isset($this->_connections[$origName])) {
106: $this->_connections[$name] = $this->_connections[$origName];
107: return $this->_connections[$origName];
108: }
109:
110: $connection = $this->_newConnection((string)$connConfig->type, $connConfig);
111: if ($connection) {
112: if (Mage::app()->getIsCacheLocked()) {
113: $this->_skippedConnections[$name] = true;
114: } else {
115: $connection->setCacheAdapter(Mage::app()->getCache());
116: }
117: }
118:
119: $this->_connections[$name] = $connection;
120: if ($origName !== $name) {
121: $this->_connections[$origName] = $connection;
122: }
123:
124: return $connection;
125: }
126:
127: 128: 129: 130: 131: 132:
133: protected function _getConnectionAdapterClassName($type)
134: {
135: $config = Mage::getConfig()->getResourceTypeConfig($type);
136: if (!empty($config->adapter)) {
137: return (string)$config->adapter;
138: }
139: return false;
140: }
141:
142: 143: 144: 145: 146: 147: 148:
149: protected function _newConnection($type, $config)
150: {
151: if ($config instanceof Mage_Core_Model_Config_Element) {
152: $config = $config->asArray();
153: }
154: if (!is_array($config)) {
155: return false;
156: }
157:
158: $connection = false;
159:
160: $className = $this->_getConnectionAdapterClassName($type);
161: if ($className) {
162:
163: $config['profiler'] = isset($config['profiler']) && $config['profiler'] != 'false';
164:
165: $connection = new $className($config);
166: if ($connection instanceof Varien_Db_Adapter_Interface) {
167:
168: if (!empty($config['initStatements'])) {
169: $connection->query($config['initStatements']);
170: }
171: } else {
172: $connection = false;
173: }
174: }
175:
176:
177: if (!$connection) {
178: $typeInstance = $this->getConnectionTypeInstance($type);
179: $connection = $typeInstance->getConnection($config);
180: if (!$connection instanceof Varien_Db_Adapter_Interface) {
181: $connection = false;
182: }
183: }
184:
185: return $connection;
186: }
187:
188: 189: 190: 191: 192: 193:
194: protected function _getDefaultConnection($requiredConnectionName)
195: {
196: if (strpos($requiredConnectionName, 'read') !== false) {
197: return $this->getConnection(self::DEFAULT_READ_RESOURCE);
198: }
199: return $this->getConnection(self::DEFAULT_WRITE_RESOURCE);
200: }
201:
202: 203: 204: 205: 206: 207: 208: 209:
210: public function getConnectionTypeInstance($type)
211: {
212: if (!isset($this->_connectionTypes[$type])) {
213: $config = Mage::getConfig()->getResourceTypeConfig($type);
214: $typeClass = $config->getClassName();
215: $this->_connectionTypes[$type] = new $typeClass();
216: }
217: return $this->_connectionTypes[$type];
218: }
219:
220: 221: 222: 223: 224: 225: 226:
227: public function getEntity($model, $entity)
228: {
229: $modelsNode = Mage::getConfig()->getNode()->global->models;
230: $entityConfig = $modelsNode->$model->entities->{$entity};
231:
232: 233: 234: 235: 236:
237: if (isset($modelsNode->$model->deprecatedNode)) {
238: $deprecatedNode = $modelsNode->$model->deprecatedNode;
239: if (isset($modelsNode->$deprecatedNode->entities->$entity)) {
240: $entityConfig = $modelsNode->$deprecatedNode->entities->$entity;
241: }
242: }
243:
244: return $entityConfig;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public function getTableName($modelEntity)
254: {
255: $tableSuffix = null;
256: if (is_array($modelEntity)) {
257: list($modelEntity, $tableSuffix) = $modelEntity;
258: }
259:
260: $parts = explode('/', $modelEntity);
261: if (isset($parts[1])) {
262: list($model, $entity) = $parts;
263: $entityConfig = false;
264: if (!empty(Mage::getConfig()->getNode()->global->models->{$model}->resourceModel)) {
265: $resourceModel = (string)Mage::getConfig()->getNode()->global->models->{$model}->resourceModel;
266: $entityConfig = $this->getEntity($resourceModel, $entity);
267: }
268:
269: if ($entityConfig && !empty($entityConfig->table)) {
270: $tableName = (string)$entityConfig->table;
271: } else {
272: Mage::throwException(Mage::helper('core')->__('Can\'t retrieve entity config: %s', $modelEntity));
273: }
274: } else {
275: $tableName = $modelEntity;
276: }
277:
278: Mage::dispatchEvent('resource_get_tablename', array(
279: 'resource' => $this,
280: 'model_entity' => $modelEntity,
281: 'table_name' => $tableName,
282: 'table_suffix' => $tableSuffix
283: ));
284:
285: $mappedTableName = $this->getMappedTableName($tableName);
286: if ($mappedTableName) {
287: $tableName = $mappedTableName;
288: } else {
289: $tablePrefix = (string)Mage::getConfig()->getTablePrefix();
290: $tableName = $tablePrefix . $tableName;
291: }
292:
293: if (!is_null($tableSuffix)) {
294: $tableName .= '_' . $tableSuffix;
295: }
296: return $this->getConnection(self::DEFAULT_READ_RESOURCE)->getTableName($tableName);
297: }
298:
299: 300: 301: 302: 303: 304: 305:
306: public function setMappedTableName($tableName, $mappedName)
307: {
308: $this->_mappedTableNames[$tableName] = $mappedName;
309: return $this;
310: }
311:
312: 313: 314: 315: 316: 317:
318: public function getMappedTableName($tableName)
319: {
320: if (isset($this->_mappedTableNames[$tableName])) {
321: return $this->_mappedTableNames[$tableName];
322: } else {
323: return false;
324: }
325: }
326:
327: 328: 329: 330: 331: 332:
333: public function cleanDbRow(&$row)
334: {
335: $zeroDate = $this->getConnection(self::DEFAULT_READ_RESOURCE)->getSuggestedZeroDate();
336: if (!empty($row) && is_array($row)) {
337: foreach ($row as $key=>&$value) {
338: if (is_string($value) && $value === $zeroDate) {
339: $value = '';
340: }
341: }
342: }
343: return $this;
344: }
345:
346: 347: 348: 349: 350: 351: 352: 353:
354: public function createConnection($name, $type, $config)
355: {
356: if (!isset($this->_connections[$name])) {
357: $connection = $this->_newConnection($type, $config);
358:
359: $this->_connections[$name] = $connection;
360: }
361: return $this->_connections[$name];
362: }
363:
364: public function checkDbConnection()
365: {
366: if (!$this->getConnection('core_read')) {
367:
368: }
369: }
370:
371: public function getAutoUpdate()
372: {
373: return self::AUTO_UPDATE_ALWAYS;
374:
375: }
376:
377: public function setAutoUpdate($value)
378: {
379:
380: return $this;
381: }
382: 383: 384: 385: 386: 387: 388: 389:
390: public function getIdxName($tableName, $fields, $indexType = Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX)
391: {
392: return $this->getConnection(self::DEFAULT_READ_RESOURCE)
393: ->getIndexName($this->getTableName($tableName), $fields, $indexType);
394: }
395:
396: 397: 398: 399: 400: 401: 402: 403: 404:
405: public function getFkName($priTableName, $priColumnName, $refTableName, $refColumnName)
406: {
407: return $this->getConnection(self::DEFAULT_READ_RESOURCE)
408: ->getForeignKeyName($this->getTableName($priTableName), $priColumnName,
409: $this->getTableName($refTableName), $refColumnName);
410: }
411: }
412: