Mysql.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Mysql DB Statement
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\DB\Statement\Pdo;
  12. use Magento\Framework\DB\Statement\Parameter;
  13. class Mysql extends \Zend_Db_Statement_Pdo
  14. {
  15. /**
  16. * Executes statement with binding values to it.
  17. * Allows transferring specific options to DB driver.
  18. *
  19. * @param array $params Array of values to bind to parameter placeholders.
  20. * @return bool
  21. * @throws \Zend_Db_Statement_Exception
  22. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  23. */
  24. public function _executeWithBinding(array $params)
  25. {
  26. // Check whether we deal with named bind
  27. $isPositionalBind = true;
  28. foreach ($params as $k => $v) {
  29. if (!is_int($k)) {
  30. $isPositionalBind = false;
  31. break;
  32. }
  33. }
  34. /* @var $statement \PDOStatement */
  35. $statement = $this->_stmt;
  36. $bindValues = [];
  37. // Separate array with values, as they are bound by reference
  38. foreach ($params as $name => $param) {
  39. $dataType = \PDO::PARAM_STR;
  40. $length = null;
  41. $driverOptions = null;
  42. if ($param instanceof Parameter) {
  43. if ($param->getIsBlob()) {
  44. // Nothing to do there - default options are fine for MySQL driver
  45. } else {
  46. $dataType = $param->getDataType();
  47. $length = $param->getLength();
  48. $driverOptions = $param->getDriverOptions();
  49. }
  50. $bindValues[$name] = $param->getValue();
  51. } else {
  52. $bindValues[$name] = $param;
  53. }
  54. $paramName = $isPositionalBind ? $name + 1 : $name;
  55. $statement->bindParam($paramName, $bindValues[$name], $dataType, $length, $driverOptions);
  56. }
  57. try {
  58. return $statement->execute();
  59. } catch (\PDOException $e) {
  60. throw new \Zend_Db_Statement_Exception($e->getMessage(), (int)$e->getCode(), $e);
  61. }
  62. }
  63. /**
  64. * Executes a prepared statement.
  65. *
  66. * @param array $params OPTIONAL Values to bind to parameter placeholders.
  67. * @return bool
  68. * @throws \Zend_Db_Statement_Exception
  69. */
  70. public function _execute(array $params = null)
  71. {
  72. $specialExecute = false;
  73. if ($params) {
  74. foreach ($params as $param) {
  75. if ($param instanceof Parameter) {
  76. $specialExecute = true;
  77. break;
  78. }
  79. }
  80. }
  81. if ($specialExecute) {
  82. return $this->_executeWithBinding($params);
  83. } else {
  84. return parent::_execute($params);
  85. }
  86. }
  87. }