PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Resource/Helper/Abstract.php

https://bitbucket.org/sevenly/magento-ce
PHP | 326 lines | 184 code | 21 blank | 121 comment | 19 complexity | 77f2c74b5123e6fd51d5acf7c7338627 MD5 | raw file
  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. * Abstract resource helper class
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Core_Model_Resource_Helper_Abstract
  34. {
  35. /**
  36. * Read adapter instance
  37. *
  38. * @var Varien_Db_Adapter_Interface
  39. */
  40. protected $_readAdapter;
  41. /**
  42. * Write adapter instance
  43. *
  44. * @var Varien_Db_Adapter_Interface
  45. */
  46. protected $_writeAdapter;
  47. /**
  48. * Resource helper module prefix
  49. *
  50. * @var string
  51. */
  52. protected $_modulePrefix;
  53. /**
  54. * Initialize resource helper instance
  55. *
  56. * @param string $module
  57. */
  58. public function __construct($module)
  59. {
  60. $this->_modulePrefix = (string)$module;
  61. }
  62. /**
  63. * Retrieve connection for read data
  64. *
  65. * @return Varien_Db_Adapter_Interface
  66. */
  67. protected function _getReadAdapter()
  68. {
  69. if ($this->_readAdapter === null) {
  70. $this->_readAdapter = $this->_getConnection('read');
  71. }
  72. return $this->_readAdapter;
  73. }
  74. /**
  75. * Retrieve connection for write data
  76. *
  77. * @return Varien_Db_Adapter_Interface
  78. */
  79. protected function _getWriteAdapter()
  80. {
  81. if ($this->_writeAdapter === null) {
  82. $this->_writeAdapter = $this->_getConnection('write');
  83. }
  84. return $this->_writeAdapter;
  85. }
  86. /**
  87. * Retrieves connection to the resource
  88. *
  89. * @param string $name
  90. * @return Varien_Db_Adapter_Interface
  91. */
  92. protected function _getConnection($name)
  93. {
  94. $connection = sprintf('%s_%s', $this->_modulePrefix, $name);
  95. /** @var $resource Mage_Core_Model_Resource */
  96. $resource = Mage::getSingleton('core/resource');
  97. return $resource->getConnection($connection);
  98. }
  99. /**
  100. * Escapes value, that participates in LIKE, with '\' symbol.
  101. * Note: this func cannot be used on its own, because different RDMBS may use different default escape symbols,
  102. * so you should either use addLikeEscape() to produce LIKE construction, or add escape symbol on your own.
  103. *
  104. * By default escapes '_', '%' and '\' symbols. If some masking symbols must not be escaped, then you can set
  105. * appropriate options in $options.
  106. *
  107. * $options can contain following flags:
  108. * - 'allow_symbol_mask' - the '_' symbol will not be escaped
  109. * - 'allow_string_mask' - the '%' symbol will not be escaped
  110. * - 'position' ('any', 'start', 'end') - expression will be formed so that $value will be found at position within string,
  111. * by default when nothing set - string must be fully matched with $value
  112. *
  113. * @param string $value
  114. * @param array $options
  115. * @return string
  116. */
  117. public function escapeLikeValue($value, $options = array())
  118. {
  119. $value = str_replace('\\', '\\\\', $value);
  120. $from = array();
  121. $to = array();
  122. if (empty($options['allow_symbol_mask'])) {
  123. $from[] = '_';
  124. $to[] = '\_';
  125. }
  126. if (empty($options['allow_string_mask'])) {
  127. $from[] = '%';
  128. $to[] = '\%';
  129. }
  130. if ($from) {
  131. $value = str_replace($from, $to, $value);
  132. }
  133. if (isset($options['position'])) {
  134. switch ($options['position']) {
  135. case 'any':
  136. $value = '%' . $value . '%';
  137. break;
  138. case 'start':
  139. $value = $value . '%';
  140. break;
  141. case 'end':
  142. $value = '%' . $value;
  143. break;
  144. }
  145. }
  146. return $value;
  147. }
  148. /**
  149. * Escapes, quotes and adds escape symbol to LIKE expression.
  150. * For options and escaping see escapeLikeValue().
  151. *
  152. * @param string $value
  153. * @param array $options
  154. * @return Zend_Db_Expr
  155. *
  156. * @see escapeLikeValue()
  157. */
  158. abstract public function addLikeEscape($value, $options = array());
  159. /**
  160. * Returns case insensitive LIKE construction.
  161. * For options and escaping see escapeLikeValue().
  162. *
  163. * @param string $field
  164. * @param string $value
  165. * @param array $options
  166. * @return Zend_Db_Expr
  167. *
  168. * @see escapeLikeValue()
  169. */
  170. public function getCILike($field, $value, $options = array())
  171. {
  172. $quotedField = $this->_getReadAdapter()->quoteIdentifier($field);
  173. return new Zend_Db_Expr($quotedField . ' LIKE ' . $this->addLikeEscape($value, $options));
  174. }
  175. /**
  176. * Converts old pre-MMDB column definition for MySQL to new cross-db column DDL definition.
  177. * Used to convert data from 3rd party extensions that hasn't been updated to MMDB style yet.
  178. *
  179. * E.g. Converts type 'varchar(255)' to array('type' => Varien_Db_Ddl_Table::TYPE_TEXT, 'length' => 255)
  180. *
  181. * @param array $column
  182. * @return array
  183. */
  184. public function convertOldColumnDefinition($column)
  185. {
  186. // Match type and size - e.g. varchar(100) or decimal(12,4) or int
  187. $matches = array();
  188. $definition = trim($column['type']);
  189. if (!preg_match('/([^(]*)(\\((.*)\\))?/', $definition, $matches)) {
  190. throw Mage::exception(
  191. 'Mage_Core',
  192. Mage::helper('core')->__("Wrong old style column type definition: {$definition}.")
  193. );
  194. }
  195. $length = null;
  196. $proposedLength = (isset($matches[3]) && strlen($matches[3])) ? $matches[3] : null;
  197. switch (strtolower($matches[1])) {
  198. case 'bool':
  199. $length = null;
  200. $type = Varien_Db_Ddl_Table::TYPE_BOOLEAN;
  201. break;
  202. case 'char':
  203. case 'varchar':
  204. case 'tinytext':
  205. $length = $proposedLength;
  206. if (!$length) {
  207. $length = 255;
  208. }
  209. $type = Varien_Db_Ddl_Table::TYPE_TEXT;
  210. break;
  211. case 'text':
  212. $length = $proposedLength;
  213. if (!$length) {
  214. $length = '64k';
  215. }
  216. $type = Varien_Db_Ddl_Table::TYPE_TEXT;
  217. break;
  218. case 'mediumtext':
  219. $length = $proposedLength;
  220. if (!$length) {
  221. $length = '16M';
  222. }
  223. $type = Varien_Db_Ddl_Table::TYPE_TEXT;
  224. break;
  225. case 'longtext':
  226. $length = $proposedLength;
  227. if (!$length) {
  228. $length = '4G';
  229. }
  230. $type = Varien_Db_Ddl_Table::TYPE_TEXT;
  231. break;
  232. case 'blob':
  233. $length = $proposedLength;
  234. if (!$length) {
  235. $length = '64k';
  236. }
  237. $type = Varien_Db_Ddl_Table::TYPE_BLOB;
  238. break;
  239. case 'mediumblob':
  240. $length = $proposedLength;
  241. if (!$length) {
  242. $length = '16M';
  243. }
  244. $type = Varien_Db_Ddl_Table::TYPE_BLOB;
  245. break;
  246. case 'longblob':
  247. $length = $proposedLength;
  248. if (!$length) {
  249. $length = '4G';
  250. }
  251. $type = Varien_Db_Ddl_Table::TYPE_BLOB;
  252. break;
  253. case 'tinyint':
  254. case 'smallint':
  255. $type = Varien_Db_Ddl_Table::TYPE_SMALLINT;
  256. break;
  257. case 'mediumint':
  258. case 'int':
  259. $type = Varien_Db_Ddl_Table::TYPE_INTEGER;
  260. break;
  261. case 'bigint':
  262. $type = Varien_Db_Ddl_Table::TYPE_BIGINT;
  263. break;
  264. case 'float':
  265. $type = Varien_Db_Ddl_Table::TYPE_FLOAT;
  266. break;
  267. case 'decimal':
  268. case 'numeric':
  269. $length = $proposedLength;
  270. $type = Varien_Db_Ddl_Table::TYPE_DECIMAL;
  271. break;
  272. case 'datetime':
  273. $type = Varien_Db_Ddl_Table::TYPE_DATETIME;
  274. break;
  275. case 'timestamp':
  276. case 'time':
  277. $type = Varien_Db_Ddl_Table::TYPE_TIMESTAMP;
  278. break;
  279. case 'date':
  280. $type = Varien_Db_Ddl_Table::TYPE_DATE;
  281. break;
  282. default:
  283. throw Mage::exception(
  284. 'Mage_Core',
  285. Mage::helper('core')->__("Unknown old style column type definition: {$definition}.")
  286. );
  287. }
  288. $result = array(
  289. 'type' => $type,
  290. 'length' => $length,
  291. 'unsigned' => $column['unsigned'],
  292. 'nullable' => $column['is_null'],
  293. 'default' => $column['default'],
  294. 'identity' => stripos($column['extra'], 'auto_increment') !== false
  295. );
  296. /**
  297. * Process the case when 'is_null' prohibits null value, and 'default' proposed to be null.
  298. * It just means that default value not specified, and we must remove it from column definition.
  299. */
  300. if (false === $column['is_null'] && null === $column['default']) {
  301. unset($result['default']);
  302. }
  303. return $result;
  304. }
  305. }