PDOStatement.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 \PDO;
  19. use \PDOStatement as Statement;
  20. /**
  21. * Decorator for \PDOStatement class mainly used for converting human readable
  22. * fetch modes into PDO constants.
  23. */
  24. class PDOStatement extends StatementDecorator {
  25. /**
  26. * Constructor
  27. *
  28. * @param \PDOStatement original statement to be decorated
  29. * @param \Cake\Database\Driver instance $driver
  30. */
  31. public function __construct(Statement $statement = null, $driver = null) {
  32. $this->_statement = $statement;
  33. $this->_driver = $driver;
  34. }
  35. /**
  36. * Assign a value to an positional or named variable in prepared query. If using
  37. * positional variables you need to start with index one, if using named params then
  38. * just use the name in any order.
  39. *
  40. * You can pass PDO compatible constants for binding values with a type or optionally
  41. * any type name registered in the Type class. Any value will be converted to the valid type
  42. * representation if needed.
  43. *
  44. * It is not allowed to combine positional and named variables in the same statement
  45. *
  46. * ## Examples:
  47. *
  48. * `$statement->bindValue(1, 'a title');`
  49. * `$statement->bindValue(2, 5, PDO::INT);`
  50. * `$statement->bindValue('active', true, 'boolean');`
  51. * `$statement->bindValue(5, new \DateTime(), 'date');`
  52. *
  53. * @param string|integer $column name or param position to be bound
  54. * @param mixed $value the value to bind to variable in query
  55. * @param string|integer $type PDO type or name of configured Type class
  56. * @return void
  57. */
  58. public function bindValue($column, $value, $type = 'string') {
  59. if ($type === null) {
  60. $type = 'string';
  61. }
  62. if (!ctype_digit($type)) {
  63. list($value, $type) = $this->cast($value, $type);
  64. }
  65. $this->_statement->bindValue($column, $value, $type);
  66. }
  67. /**
  68. * Returns the next row for the result set after executing this statement.
  69. * Rows can be fetched to contain columns as names or positions. If no
  70. * rows are left in result set, this method will return false
  71. *
  72. * ## Example:
  73. *
  74. * {{{
  75. * $statement = $connection->prepare('SELECT id, title from articles');
  76. * $statement->execute();
  77. * print_r($statement->fetch('assoc')); // will show array('id' => 1, 'title' => 'a title')
  78. * }}}
  79. *
  80. * @param string $type 'num' for positional columns, assoc for named columns
  81. * @return mixed|boolean result array containing columns and values or false if no results
  82. * are left
  83. */
  84. public function fetch($type = 'num') {
  85. if ($type === 'num') {
  86. return $this->_statement->fetch(PDO::FETCH_NUM);
  87. }
  88. if ($type === 'assoc') {
  89. return $this->_statement->fetch(PDO::FETCH_ASSOC);
  90. }
  91. return $this->_statement->fetch($type);
  92. }
  93. /**
  94. * Returns an array with all rows resulting from executing this statement
  95. *
  96. * ## Example:
  97. *
  98. * {{{
  99. * $statement = $connection->prepare('SELECT id, title from articles');
  100. * $statement->execute();
  101. * print_r($statement->fetchAll('assoc')); // will show [0 => ['id' => 1, 'title' => 'a title']]
  102. * }}}
  103. *
  104. * @param string $type num for fetching columns as positional keys or assoc for column names as keys
  105. * @return array list of all results from database for this statement
  106. */
  107. public function fetchAll($type = 'num') {
  108. if ($type === 'num') {
  109. return $this->_statement->fetchAll(PDO::FETCH_NUM);
  110. }
  111. if ($type === 'assoc') {
  112. return $this->_statement->fetchAll(PDO::FETCH_ASSOC);
  113. }
  114. return $this->_statement->fetchAll($type);
  115. }
  116. }