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

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

https://bitbucket.org/dnejedly/eaparts
PHP | 371 lines | 249 code | 16 blank | 106 comment | 20 complexity | 73fa1f4cf809e502acbe281e73aeccd8 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. * Resource helper class for MySql Varien DB Adapter
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_Resource_Helper_Mysql4 extends Mage_Core_Model_Resource_Helper_Abstract
  34. {
  35. /**
  36. * Returns expresion for field unification
  37. *
  38. * @param string $field
  39. * @return Zend_Db_Expr
  40. */
  41. public function castField($field)
  42. {
  43. return $field;
  44. }
  45. /**
  46. * Returns analytic expression for database column
  47. *
  48. * @param string $column
  49. * @param string $groupAliasName OPTIONAL
  50. * @param string $orderBy OPTIONAL
  51. * @return Zend_Db_Expr
  52. */
  53. public function prepareColumn($column, $groupAliasName = null, $orderBy = null)
  54. {
  55. return new Zend_Db_Expr((string)$column);
  56. }
  57. /**
  58. * Returns select query with analytic functions
  59. *
  60. * @param Varien_Db_Select $select
  61. * @return string
  62. */
  63. public function getQueryUsingAnalyticFunction(Varien_Db_Select $select)
  64. {
  65. return $select->assemble();
  66. }
  67. /**
  68. *
  69. * Returns Insert From Select On Duplicate query with analytic functions
  70. *
  71. * @param Varien_Db_Select $select
  72. * @param string $table
  73. * @param array $table
  74. * @return string
  75. */
  76. public function getInsertFromSelectUsingAnalytic(Varien_Db_Select $select, $table, $fields)
  77. {
  78. return $select->insertFromSelect($table, $fields);
  79. }
  80. /**
  81. * Correct limitation of queries with UNION
  82. * No need to do additional actions on MySQL
  83. *
  84. * @param Varien_Db_Select $select
  85. * @return Varien_Db_Select
  86. */
  87. public function limitUnion($select)
  88. {
  89. return $select;
  90. }
  91. /**
  92. * Returns array of quoted orders with direction
  93. *
  94. * @param Varien_Db_Select $select
  95. * @param bool $autoReset
  96. * @return array
  97. */
  98. protected function _prepareOrder(Varien_Db_Select $select, $autoReset = false)
  99. {
  100. $selectOrders = $select->getPart(Zend_Db_Select::ORDER);
  101. if (!$selectOrders) {
  102. return array();
  103. }
  104. $orders = array();
  105. foreach ($selectOrders as $term) {
  106. if (is_array($term)) {
  107. if (!is_numeric($term[0])) {
  108. $orders[] = sprintf('%s %s', $this->_getReadAdapter()->quoteIdentifier($term[0], true), $term[1]);
  109. }
  110. } else {
  111. if (!is_numeric($term)) {
  112. $orders[] = $this->_getReadAdapter()->quoteIdentifier($term, true);
  113. }
  114. }
  115. }
  116. if ($autoReset) {
  117. $select->reset(Zend_Db_Select::ORDER);
  118. }
  119. return $orders;
  120. }
  121. /**
  122. * Truncate alias name from field.
  123. *
  124. * Result string depends from second optional argument $reverse
  125. * which can be true if you need the first part of the field.
  126. * Field can be with 'dot' delimiter.
  127. *
  128. * @param string $field
  129. * @param bool $reverse OPTIONAL
  130. * @return string
  131. */
  132. protected function _truncateAliasName($field, $reverse = false)
  133. {
  134. $string = $field;
  135. if (!is_numeric($field) && (strpos($field, '.') !== false)) {
  136. $size = strpos($field, '.');
  137. if ($reverse) {
  138. $string = substr($field, 0, $size);
  139. } else {
  140. $string = substr($field, $size + 1);
  141. }
  142. }
  143. return $string;
  144. }
  145. /**
  146. * Returns quoted group by fields
  147. *
  148. * @param Varien_Db_Select $select
  149. * @param bool $autoReset
  150. * @return array
  151. */
  152. protected function _prepareGroup(Varien_Db_Select $select, $autoReset = false)
  153. {
  154. $selectGroups = $select->getPart(Zend_Db_Select::GROUP);
  155. if (!$selectGroups) {
  156. return array();
  157. }
  158. $groups = array();
  159. foreach ($selectGroups as $term) {
  160. $groups[] = $this->_getReadAdapter()->quoteIdentifier($term, true);
  161. }
  162. if ($autoReset) {
  163. $select->reset(Zend_Db_Select::GROUP);
  164. }
  165. return $groups;
  166. }
  167. /**
  168. * Prepare and returns having array
  169. *
  170. * @param Varien_Db_Select $select
  171. * @param bool $autoReset
  172. * @return array
  173. * @throws Zend_Db_Exception
  174. */
  175. protected function _prepareHaving(Varien_Db_Select $select, $autoReset = false)
  176. {
  177. $selectHavings = $select->getPart(Zend_Db_Select::HAVING);
  178. if (!$selectHavings) {
  179. return array();
  180. }
  181. $havings = array();
  182. $columns = $select->getPart(Zend_Db_Select::COLUMNS);
  183. foreach ($columns as $columnEntry) {
  184. $correlationName = (string)$columnEntry[1];
  185. $column = $columnEntry[2];
  186. foreach ($selectHavings as $having) {
  187. /**
  188. * Looking for column expression in the having clause
  189. */
  190. if (strpos($having, $correlationName) !== false) {
  191. if (is_string($column)) {
  192. /**
  193. * Replace column expression to column alias in having clause
  194. */
  195. $havings[] = str_replace($correlationName, $column, $having);
  196. } else {
  197. throw new Zend_Db_Exception(sprintf("Can't prepare expression without column alias: '%s'", $correlationName));
  198. }
  199. }
  200. }
  201. }
  202. if ($autoReset) {
  203. $select->reset(Zend_Db_Select::HAVING);
  204. }
  205. return $havings;
  206. }
  207. /**
  208. *
  209. * @param string $query
  210. * @param int $limitCount
  211. * @param int $limitOffset
  212. * @param array $columnList
  213. * @return string
  214. */
  215. protected function _assembleLimit($query, $limitCount, $limitOffset, $columnList = array())
  216. {
  217. if ($limitCount !== null) {
  218. $limitCount = intval($limitCount);
  219. if ($limitCount <= 0) {
  220. // throw new Exception("LIMIT argument count={$limitCount} is not valid");
  221. }
  222. $limitOffset = intval($limitOffset);
  223. if ($limitOffset < 0) {
  224. // throw new Exception("LIMIT argument offset={$limitOffset} is not valid");
  225. }
  226. if ($limitOffset + $limitCount != $limitOffset + 1) {
  227. $columns = array();
  228. foreach ($columnList as $columnEntry) {
  229. $columns[] = $columnEntry[2] ? $columnEntry[2] : $columnEntry[1];
  230. }
  231. $query = sprintf('%s LIMIT %s, %s', $query, $limitCount, $limitOffset);
  232. }
  233. }
  234. return $query;
  235. }
  236. /**
  237. * Prepare select column list
  238. *
  239. * @param Varien_Db_Select $select
  240. * @param $groupByCondition OPTIONAL
  241. * @return array
  242. * @throws Zend_Db_Exception
  243. */
  244. public function prepareColumnsList(Varien_Db_Select $select, $groupByCondition = null)
  245. {
  246. if (!count($select->getPart(Zend_Db_Select::FROM))) {
  247. return $select->getPart(Zend_Db_Select::COLUMNS);
  248. }
  249. $columns = $select->getPart(Zend_Db_Select::COLUMNS);
  250. $tables = $select->getPart(Zend_Db_Select::FROM);
  251. $preparedColumns = array();
  252. foreach ($columns as $columnEntry) {
  253. list($correlationName, $column, $alias) = $columnEntry;
  254. if ($column instanceof Zend_Db_Expr) {
  255. if ($alias !== null) {
  256. if (preg_match('/(^|[^a-zA-Z_])^(SELECT)?(SUM|MIN|MAX|AVG|COUNT)\s*\(/i', $column, $matches)) {
  257. $column = $this->prepareColumn($column, $groupByCondition);
  258. }
  259. $preparedColumns[strtoupper($alias)] = array(null, $column, $alias);
  260. } else {
  261. throw new Zend_Db_Exception("Can't prepare expression without alias");
  262. }
  263. } else {
  264. if ($column == Zend_Db_Select::SQL_WILDCARD) {
  265. if ($tables[$correlationName]['tableName'] instanceof Zend_Db_Expr) {
  266. throw new Zend_Db_Exception("Can't prepare expression when tableName is instance of Zend_Db_Expr");
  267. }
  268. $tableColumns = $this->_getReadAdapter()->describeTable($tables[$correlationName]['tableName']);
  269. foreach(array_keys($tableColumns) as $col) {
  270. $preparedColumns[strtoupper($col)] = array($correlationName, $col, null);
  271. }
  272. } else {
  273. $columnKey = is_null($alias) ? $column : $alias;
  274. $preparedColumns[strtoupper($columnKey)] = array($correlationName, $column, $alias);
  275. }
  276. }
  277. }
  278. // $select->reset(Zend_Db_Select::COLUMNS);
  279. // $select->setPart(Zend_Db_Select::COLUMNS, array_values($preparedColumns));
  280. return $preparedColumns;
  281. }
  282. /**
  283. * Add prepared column group_concat expression
  284. *
  285. * @param Varien_Db_Select $select
  286. * @param string $fieldAlias Field alias which will be added with column group_concat expression
  287. * @param string $fields
  288. * @param string $groupConcatDelimiter
  289. * @param string $fieldsDelimiter
  290. * @param string $additionalWhere
  291. * @return Varien_Db_Select
  292. */
  293. public function addGroupConcatColumn($select, $fieldAlias, $fields, $groupConcatDelimiter = ',', $fieldsDelimiter = '', $additionalWhere = '')
  294. {
  295. if (is_array($fields)) {
  296. $fieldExpr = $this->_getReadAdapter()->getConcatSql($fields, $fieldsDelimiter);
  297. } else {
  298. $fieldExpr = $fields;
  299. }
  300. if ($additionalWhere) {
  301. $fieldExpr = $this->_getReadAdapter()->getCheckSql($additionalWhere, $fieldExpr, "''");
  302. }
  303. $separator = '';
  304. if ($groupConcatDelimiter) {
  305. $separator = sprintf(" SEPARATOR '%s'", $groupConcatDelimiter);
  306. }
  307. $select->columns(array($fieldAlias => new Zend_Db_Expr(sprintf('GROUP_CONCAT(%s%s)', $fieldExpr, $separator))));
  308. return $select;
  309. }
  310. /**
  311. * Returns expression of days passed from $startDate to $endDate
  312. *
  313. * @param string|Zend_Db_Expr $startDate
  314. * @param string|Zend_Db_Expr $endDate
  315. * @return Zend_Db_Expr
  316. */
  317. public function getDateDiff($startDate, $endDate)
  318. {
  319. $dateDiff = '(TO_DAYS(' . $endDate . ') - TO_DAYS(' . $startDate . '))';
  320. return new Zend_Db_Expr($dateDiff);
  321. }
  322. /**
  323. * Escapes and quotes LIKE value.
  324. * Stating escape symbol in expression is not required, because we use standard MySQL escape symbol.
  325. * For options and escaping see escapeLikeValue().
  326. *
  327. * @param string $value
  328. * @param array $options
  329. * @return Zend_Db_Expr
  330. *
  331. * @see escapeLikeValue()
  332. */
  333. public function addLikeEscape($value, $options = array())
  334. {
  335. $value = $this->escapeLikeValue($value, $options);
  336. return new Zend_Db_Expr($this->_getReadAdapter()->quote($value));
  337. }
  338. }