1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
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@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Core Resource Resource Model
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Core_Model_Resource_Resource extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Database versions
39: *
40: * @var array
41: */
42: protected static $_versions = null;
43:
44: /**
45: * Resource data versions cache array
46: *
47: * @var array
48: */
49: protected static $_dataVersions = null;
50:
51: /**
52: * Define main table
53: *
54: */
55: protected function _construct()
56: {
57: $this->_init('core/resource', 'store_id');
58: }
59:
60: /**
61: * Fill static versions arrays.
62: * This routine fetches Db and Data versions of at once to optimize sql requests. However, when upgrading, it's
63: * possible that 'data' column will be created only after all Db installs are passed. So $neededType contains
64: * information on main purpose of calling this routine, and even when 'data' column is absent - it won't require
65: * reissuing new sql just to get 'db' version of module.
66: *
67: * @param string $needType Can be 'db' or 'data'
68: * @return Mage_Core_Model_Resource_Resource
69: */
70: protected function _loadVersionData($needType)
71: {
72: if ((($needType == 'db') && is_null(self::$_versions))
73: || (($needType == 'data') && is_null(self::$_dataVersions))) {
74: self::$_versions = array(); // Db version column always exists
75: self::$_dataVersions = null; // Data version array will be filled only if Data column exist
76:
77: if ($this->_getReadAdapter()->isTableExists($this->getMainTable())) {
78: $select = $this->_getReadAdapter()->select()
79: ->from($this->getMainTable(), '*');
80: $rowset = $this->_getReadAdapter()->fetchAll($select);
81: foreach ($rowset as $row) {
82: self::$_versions[$row['code']] = $row['version'];
83: if (array_key_exists('data_version', $row)) {
84: if (is_null(self::$_dataVersions)) {
85: self::$_dataVersions = array();
86: }
87: self::$_dataVersions[$row['code']] = $row['data_version'];
88: }
89: }
90: }
91: }
92:
93: return $this;
94: }
95:
96:
97: /**
98: * Get Module version from DB
99: *
100: * @param string $resName
101: * @return bool|string
102: */
103: public function getDbVersion($resName)
104: {
105: if (!$this->_getReadAdapter()) {
106: return false;
107: }
108: $this->_loadVersionData('db');
109: return isset(self::$_versions[$resName]) ? self::$_versions[$resName] : false;
110: }
111:
112: /**
113: * Set module version into DB
114: *
115: * @param string $resName
116: * @param string $version
117: * @return int
118: */
119: public function setDbVersion($resName, $version)
120: {
121: $dbModuleInfo = array(
122: 'code' => $resName,
123: 'version' => $version,
124: );
125:
126: if ($this->getDbVersion($resName)) {
127: self::$_versions[$resName] = $version;
128: return $this->_getWriteAdapter()->update($this->getMainTable(),
129: $dbModuleInfo,
130: array('code = ?' => $resName));
131: } else {
132: self::$_versions[$resName] = $version;
133: return $this->_getWriteAdapter()->insert($this->getMainTable(), $dbModuleInfo);
134: }
135: }
136:
137: /**
138: * Get resource data version
139: *
140: * @param string $resName
141: * @return string|false
142: */
143: public function getDataVersion($resName)
144: {
145: if (!$this->_getReadAdapter()) {
146: return false;
147: }
148:
149: $this->_loadVersionData('data');
150:
151: return isset(self::$_dataVersions[$resName]) ? self::$_dataVersions[$resName] : false;
152: }
153:
154: /**
155: * Specify resource data version
156: *
157: * @param string $resName
158: * @param string $version
159: * @return Mage_Core_Model_Resource_Resource
160: */
161: public function setDataVersion($resName, $version)
162: {
163: $data = array(
164: 'code' => $resName,
165: 'data_version' => $version
166: );
167:
168: if ($this->getDbVersion($resName) || $this->getDataVersion($resName)) {
169: self::$_dataVersions[$resName] = $version;
170: $this->_getWriteAdapter()->update($this->getMainTable(), $data, array('code = ?' => $resName));
171: } else {
172: self::$_dataVersions[$resName] = $version;
173: $this->_getWriteAdapter()->insert($this->getMainTable(), $data);
174: }
175: return $this;
176: }
177: }
178: