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

/php/main/inc/lib/symfony/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 335 lines | 206 code | 43 blank | 86 comment | 29 complexity | 382295ab0d30057c45787f77e931e5b3 MD5 | raw file
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\Mysqli;
  20. use Doctrine\DBAL\Driver\Statement;
  21. use PDO;
  22. /**
  23. * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
  24. */
  25. class MysqliStatement implements \IteratorAggregate, Statement
  26. {
  27. protected static $_paramTypeMap = array(
  28. PDO::PARAM_STR => 's',
  29. PDO::PARAM_BOOL => 'i',
  30. PDO::PARAM_NULL => 's',
  31. PDO::PARAM_INT => 'i',
  32. PDO::PARAM_LOB => 's' // TODO Support LOB bigger then max package size.
  33. );
  34. protected $_conn;
  35. protected $_stmt;
  36. /**
  37. * @var null|false|array
  38. */
  39. protected $_columnNames;
  40. /**
  41. * @var null|array
  42. */
  43. protected $_rowBindedValues;
  44. /**
  45. * @var array
  46. */
  47. protected $_bindedValues;
  48. /**
  49. * Contains ref values for bindValue()
  50. *
  51. * @var array
  52. */
  53. protected $_values = array();
  54. protected $_defaultFetchStyle = PDO::FETCH_BOTH;
  55. public function __construct(\mysqli $conn, $prepareString)
  56. {
  57. $this->_conn = $conn;
  58. $this->_stmt = $conn->prepare($prepareString);
  59. if (false === $this->_stmt) {
  60. throw new MysqliException($this->_conn->error, $this->_conn->errno);
  61. }
  62. $paramCount = $this->_stmt->param_count;
  63. if (0 < $paramCount) {
  64. // Index 0 is types
  65. // Need to init the string else php think we are trying to access it as a array.
  66. $bindedValues = array(0 => str_repeat('s', $paramCount));
  67. $null = null;
  68. for ($i = 1; $i < $paramCount; $i++) {
  69. $bindedValues[] =& $null;
  70. }
  71. $this->_bindedValues = $bindedValues;
  72. }
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function bindParam($column, &$variable, $type = null)
  78. {
  79. if (null === $type) {
  80. $type = 's';
  81. } else {
  82. if (isset(self::$_paramTypeMap[$type])) {
  83. $type = self::$_paramTypeMap[$type];
  84. } else {
  85. throw new MysqliException("Unkown type: '{$type}'");
  86. }
  87. }
  88. $this->_bindedValues[$column] =& $variable;
  89. $this->_bindedValues[0][$column - 1] = 's';
  90. return true;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function bindValue($param, $value, $type = null)
  96. {
  97. if (null === $type) {
  98. $type = 's';
  99. } else {
  100. if (isset(self::$_paramTypeMap[$type])) {
  101. $type = self::$_paramTypeMap[$type];
  102. } else {
  103. throw new MysqliException("Unknown type: '{$type}'");
  104. }
  105. }
  106. $this->_values[$param] = $value;
  107. $this->_bindedValues[$param] =& $this->_values[$param];
  108. $this->_bindedValues[0][$param - 1] = 's';
  109. return true;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function execute($params = null)
  115. {
  116. if (null !== $this->_bindedValues) {
  117. if (null !== $params) {
  118. if (!$this->_bindValues($params)) {
  119. throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
  120. }
  121. } else {
  122. if (!call_user_func_array(array($this->_stmt, 'bind_param'), $this->_bindedValues)) {
  123. throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
  124. }
  125. }
  126. }
  127. if (!$this->_stmt->execute()) {
  128. throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
  129. }
  130. if (null === $this->_columnNames) {
  131. $meta = $this->_stmt->result_metadata();
  132. if (false !== $meta) {
  133. $columnNames = array();
  134. foreach ($meta->fetch_fields() as $col) {
  135. $columnNames[] = $col->name;
  136. }
  137. $meta->free();
  138. $this->_columnNames = $columnNames;
  139. $this->_rowBindedValues = array_fill(0, count($columnNames), NULL);
  140. $refs = array();
  141. foreach ($this->_rowBindedValues as $key => &$value) {
  142. $refs[$key] =& $value;
  143. }
  144. if (!call_user_func_array(array($this->_stmt, 'bind_result'), $refs)) {
  145. throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
  146. }
  147. } else {
  148. $this->_columnNames = false;
  149. }
  150. }
  151. // We have a result.
  152. if (false !== $this->_columnNames) {
  153. $this->_stmt->store_result();
  154. }
  155. return true;
  156. }
  157. /**
  158. * Bind a array of values to bound parameters
  159. *
  160. * @param array $values
  161. * @return boolean
  162. */
  163. private function _bindValues($values)
  164. {
  165. $params = array();
  166. $types = str_repeat('s', count($values));
  167. $params[0] = $types;
  168. foreach ($values as &$v) {
  169. $params[] =& $v;
  170. }
  171. return call_user_func_array(array($this->_stmt, 'bind_param'), $params);
  172. }
  173. /**
  174. * @return null|false|array
  175. */
  176. private function _fetch()
  177. {
  178. $ret = $this->_stmt->fetch();
  179. if (true === $ret) {
  180. $values = array();
  181. foreach ($this->_rowBindedValues as $v) {
  182. // Mysqli converts them to a scalar type it can fit in.
  183. $values[] = null === $v ? null : (string)$v;
  184. }
  185. return $values;
  186. }
  187. return $ret;
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function fetch($fetchStyle = null)
  193. {
  194. $values = $this->_fetch();
  195. if (null === $values) {
  196. return null;
  197. }
  198. if (false === $values) {
  199. throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
  200. }
  201. $fetchStyle = $fetchStyle ?: $this->_defaultFetchStyle;
  202. switch ($fetchStyle) {
  203. case PDO::FETCH_NUM:
  204. return $values;
  205. case PDO::FETCH_ASSOC:
  206. return array_combine($this->_columnNames, $values);
  207. case PDO::FETCH_BOTH:
  208. $ret = array_combine($this->_columnNames, $values);
  209. $ret += $values;
  210. return $ret;
  211. default:
  212. throw new MysqliException("Unknown fetch type '{$fetchStyle}'");
  213. }
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function fetchAll($fetchStyle = null)
  219. {
  220. $fetchStyle = $fetchStyle ?: $this->_defaultFetchStyle;
  221. $a = array();
  222. while (($row = $this->fetch($fetchStyle)) !== null) {
  223. $a[] = $row;
  224. }
  225. return $a;
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public function fetchColumn($columnIndex = 0)
  231. {
  232. $row = $this->fetch(PDO::FETCH_NUM);
  233. if (null === $row) {
  234. return null;
  235. }
  236. return $row[$columnIndex];
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function errorCode()
  242. {
  243. return $this->_stmt->errno;
  244. }
  245. /**
  246. * {@inheritdoc}
  247. */
  248. public function errorInfo()
  249. {
  250. return $this->_stmt->error;
  251. }
  252. /**
  253. * {@inheritdoc}
  254. */
  255. public function closeCursor()
  256. {
  257. $this->_stmt->free_result();
  258. return true;
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. public function rowCount()
  264. {
  265. if (false === $this->_columnNames) {
  266. return $this->_stmt->affected_rows;
  267. }
  268. return $this->_stmt->num_rows;
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function columnCount()
  274. {
  275. return $this->_stmt->field_count;
  276. }
  277. /**
  278. * {@inheritdoc}
  279. */
  280. public function setFetchMode($fetchMode = PDO::FETCH_BOTH)
  281. {
  282. $this->_defaultFetchStyle = $fetchMode;
  283. }
  284. /**
  285. * {@inheritdoc}
  286. */
  287. public function getIterator()
  288. {
  289. $data = $this->fetchAll($this->_defaultFetchStyle);
  290. return new \ArrayIterator($data);
  291. }
  292. }