StatementDecorator.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database\Statement;
  18. use Cake\Database\StatementInterface;
  19. use Cake\Database\TypeConverterTrait;
  20. /**
  21. * Represents a database statement. Statements contains queries that can be
  22. * executed multiple times by binding different values on each call. This class
  23. * also helps convert values to their valid representation for the corresponding
  24. * types.
  25. *
  26. * This class is but a decorator of an actual statement implementation, such as
  27. * PDOStatement.
  28. */
  29. class StatementDecorator implements StatementInterface, \Countable, \IteratorAggregate {
  30. use TypeConverterTrait;
  31. /**
  32. * Statement instance implementation, such as PDOStatement
  33. * or any other custom implementation
  34. *
  35. * @var mixed
  36. */
  37. protected $_statement;
  38. /**
  39. * Reference to the driver object associated to this statement
  40. *
  41. * @var Cake\Database\Driver
  42. */
  43. protected $_driver;
  44. /**
  45. * Constructor
  46. *
  47. * @param Statement implementation such as PDOStatement
  48. * @return void
  49. */
  50. public function __construct($statement = null, $driver = null) {
  51. $this->_statement = $statement;
  52. $this->_driver = $driver;
  53. }
  54. /**
  55. * Magic getter to return $queryString as read-only
  56. *
  57. * @param string $property internal property to get
  58. * @return mixed
  59. */
  60. public function __get($property) {
  61. if ($property === 'queryString') {
  62. return $this->_statement->queryString;
  63. }
  64. }
  65. /**
  66. * Assign a value to an positional or named variable in prepared query. If using
  67. * positional variables you need to start with index one, if using named params then
  68. * just use the name in any order.
  69. *
  70. * It is not allowed to combine positional and named variables in the same statement
  71. *
  72. * ## Examples:
  73. *
  74. * `$statement->bindValue(1, 'a title');`
  75. * `$statement->bindValue('active', true, 'boolean');`
  76. * `$statement->bindValue(5, new \DateTime(), 'date');`
  77. *
  78. * @param string|integer $column name or param position to be bound
  79. * @param mixed $value the value to bind to variable in query
  80. * @param string $type name of configured Type class
  81. * @return void
  82. */
  83. public function bindValue($column, $value, $type = 'string') {
  84. $this->_statement->bindValue($column, $value, $type);
  85. }
  86. /**
  87. * Closes a cursor in the database, freeing up any resources and memory
  88. * allocated to it. In most cases you don't need to call this method, as it is
  89. * automatically called after fetching all results from the result set.
  90. *
  91. * @return void
  92. */
  93. public function closeCursor() {
  94. $this->_statement->closeCursor();
  95. }
  96. /**
  97. * Returns the number of columns this statement's results will contain
  98. *
  99. * ## Example:
  100. *
  101. * {{{
  102. * $statement = $connection->prepare('SELECT id, title from articles');
  103. * $statement->execute();
  104. * echo $statement->columnCount(); // outputs 2
  105. * }}}
  106. *
  107. * @return integer
  108. */
  109. public function columnCount() {
  110. return $this->_statement->columnCount();
  111. }
  112. /**
  113. * Returns the error code for the last error that occurred when executing this statement
  114. *
  115. * @return integer|string
  116. */
  117. public function errorCode() {
  118. return $this->_statement->errorCode();
  119. }
  120. /**
  121. * Returns the error information for the last error that occurred when executing
  122. * this statement
  123. *
  124. * @return array
  125. */
  126. public function errorInfo() {
  127. return $this->_statement->errorInfo();
  128. }
  129. /**
  130. * Executes the statement by sending the SQL query to the database. It can optionally
  131. * take an array or arguments to be bound to the query variables. Please note
  132. * that binding parameters from this method will not perform any custom type conversion
  133. * as it would normally happen when calling `bindValue`
  134. *
  135. * $param array $params list of values to be bound to query
  136. * @return boolean true on success, false otherwise
  137. */
  138. public function execute($params = null) {
  139. return $this->_statement->execute($params);
  140. }
  141. /**
  142. * Returns the next row for the result set after executing this statement.
  143. * Rows can be fetched to contain columns as names or positions. If no
  144. * rows are left in result set, this method will return false
  145. *
  146. * ## Example:
  147. *
  148. * {{{
  149. * $statement = $connection->prepare('SELECT id, title from articles');
  150. * $statement->execute();
  151. * print_r($statement->fetch('assoc')); // will show array('id' => 1, 'title' => 'a title')
  152. * }}}
  153. *
  154. * @param string $type 'num' for positional columns, assoc for named columns
  155. * @return mixed|boolean result array containing columns and values or false if no results
  156. * are left
  157. */
  158. public function fetch($type = 'num') {
  159. return $this->_statement->fetch($type);
  160. }
  161. /**
  162. * Returns an array with all rows resulting from executing this statement
  163. *
  164. * ## Example:
  165. *
  166. * {{{
  167. * $statement = $connection->prepare('SELECT id, title from articles');
  168. * $statement->execute();
  169. * print_r($statement->fetchAll('assoc')); // will show [0 => ['id' => 1, 'title' => 'a title']]
  170. * }}}
  171. *
  172. * @param string $type num for fetching columns as positional keys or assoc for column names as keys
  173. * @return array list of all results from database for this statement
  174. */
  175. public function fetchAll($type = 'num') {
  176. return $this->_statement->fetchAll($type);
  177. }
  178. /**
  179. * Returns the number of rows affected by this SQL statement
  180. *
  181. * ## Example:
  182. *
  183. * {{{
  184. * $statement = $connection->prepare('SELECT id, title from articles');
  185. * $statement->execute();
  186. * print_r($statement->rowCount()); // will show 1
  187. * }}}
  188. *
  189. * @return integer
  190. */
  191. public function rowCount() {
  192. return $this->_statement->rowCount();
  193. }
  194. /**
  195. * Statements are iterable as arrays, this method will return
  196. * the iterator object for traversing all items in the result.
  197. *
  198. * ## Example:
  199. *
  200. * {{{
  201. * $statement = $connection->prepare('SELECT id, title from articles');
  202. * $statement->execute();
  203. * foreach ($statement as $row) {
  204. * //do stuff
  205. * }
  206. * }}}
  207. *
  208. * @return Iterator
  209. */
  210. public function getIterator() {
  211. return $this->_statement;
  212. }
  213. /**
  214. * Statements can be passed as argument for count()
  215. * to return the number for affected rows from last execution
  216. *
  217. * @return integer
  218. */
  219. public function count() {
  220. return $this->rowCount();
  221. }
  222. /**
  223. * Binds a set of values to statement object with corresponding type
  224. *
  225. * @param array $params list of values to be bound
  226. * @param array $types list of types to be used, keys should match those in $params
  227. * @return void
  228. */
  229. public function bind($params, $types) {
  230. if (empty($params)) {
  231. return;
  232. }
  233. $annonymousParams = is_int(key($params)) ? true : false;
  234. $offset = 1;
  235. foreach ($params as $index => $value) {
  236. $type = null;
  237. if (isset($types[$index])) {
  238. $type = $types[$index];
  239. }
  240. if ($annonymousParams) {
  241. $index += $offset;
  242. }
  243. $this->bindValue($index, $value, $type);
  244. }
  245. }
  246. }