You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.8 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage db
  7. * @since 2010-02-19
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. /**
  12. * @property MySQLiDriver $driver
  13. * @property MySQLi_Result $result
  14. */
  15. class MySQLiStatement extends DbStatement
  16. {
  17. protected $map = null;
  18. public function bindParam($param, &$value)
  19. {
  20. if ($this->map === null) {
  21. $this->mapPlaceholders();
  22. }
  23. if (count($this->map) > 0) {
  24. if (!is_string($param) && !is_int($param)) {
  25. throw new Exception('Placeholder must be an array or string');
  26. }
  27. if (is_object($value) && ! ($value instanceof DbExpr)) {
  28. throw new Exception('Objects excepts DbExpr not allowed.');
  29. }
  30. if (isset($this->map[$param])) {
  31. $this->params[$param] = &$value;
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. protected function mapPlaceholders()
  38. {
  39. $matches = array();
  40. if(preg_match_all('/(\?|:[A-z0-9_]+)/u', $this->request, $matches, PREG_OFFSET_CAPTURE)) {
  41. $noname = 0;
  42. foreach ($matches[0] as $id=>$match) {
  43. $match[2] = $matches[1][$id][0];
  44. $name = ($match[2][0] === ':') ? ltrim($match[2], ':') : $noname++;
  45. $this->map[$name]['placeholder'] = $match[0];
  46. $this->map[$name]['offset'][] = $match[1];
  47. }
  48. }
  49. }
  50. protected function assemble()
  51. {
  52. if (empty($this->map)) {
  53. return $this->request;
  54. }
  55. $query = $this->request;
  56. $placeholders = array();
  57. foreach($this->map as $name => $place) {
  58. $value = $this->driver->quote($this->params[$name]);
  59. foreach ($place['offset'] as $offset) {
  60. $placeholders[$offset] = array('placeholder' => $place['placeholder'], 'value' => $value);
  61. }
  62. }
  63. ksort($placeholders);
  64. $increment = 0;
  65. foreach($placeholders as $current_offset => $placeholder) {
  66. $offset = $current_offset + $increment;
  67. $length = mb_strlen($placeholder['placeholder']);
  68. $query = mb_substr($query, 0, $offset) . $placeholder['value'] . mb_substr($query, $offset + $length);
  69. $increment = (($increment - $length) + mb_strlen($placeholder['value']));
  70. }
  71. return $query;
  72. }
  73. /**
  74. * Fetches single row
  75. *
  76. * @param mixed $style
  77. * @return mixed
  78. */
  79. public function fetch($style = Db::FETCH_OBJ)
  80. {
  81. if (!$this->result) {
  82. return false;
  83. }
  84. $row = false;
  85. switch ($style) {
  86. case Db::FETCH_OBJ:
  87. $row = $this->result->fetch_object();
  88. break;
  89. case Db::FETCH_NUM:
  90. $row = $this->result->fetch_array(MYSQLI_NUM);
  91. break;
  92. case Db::FETCH_ASSOC:
  93. $row = $this->result->fetch_assoc();
  94. break;
  95. case Db::FETCH_BOTH:
  96. $row = $this->result->fetch_array(MYSQLI_BOTH);
  97. break;
  98. default:
  99. throw new Exception('Invalid fetch mode "' . $style . '" specified');
  100. }
  101. return $row;
  102. }
  103. /**
  104. * @param string $class
  105. */
  106. public function fetchObject($class = 'stdClass')
  107. {
  108. return $this->result->fetch_object($class);
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function fetchPairs()
  114. {
  115. $data = array();
  116. while ($row = $this->fetch(Db::FETCH_NUM)) {
  117. $data[$row[0]] = $row[1];
  118. }
  119. return $data;
  120. }
  121. public function close()
  122. {
  123. if ($this->result !== null) {
  124. $this->result->close();
  125. $this->result = null;
  126. }
  127. }
  128. public function affectedRows()
  129. {
  130. return $this->driver->getConnection()->affected_rows;
  131. }
  132. public function numRows()
  133. {
  134. if ($this->result) {
  135. return $this->result->num_rows;
  136. }
  137. return false;
  138. }
  139. protected function driverExecute($request)
  140. {
  141. /**
  142. * @var MySQLi
  143. */
  144. $mysqli = $this->driver->getConnection();
  145. if (DEBUG) {
  146. $profiler = Profiler::getInstance()->profilerCommand('MySQL', $request);
  147. $result = $mysqli->query($request);
  148. $profiler->end();
  149. } else {
  150. $result = $mysqli->query($request);
  151. }
  152. if ($result === false) {
  153. $message = $mysqli->error . "\nQuery: \"" . $request . '"';
  154. throw new Exception($message, $mysqli->errno);
  155. }
  156. if ($result instanceof MySQLi_Result) {
  157. $this->result = $result;
  158. }
  159. return true;
  160. }
  161. }