PDOStatement.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Statement;
  16. use PDO;
  17. use PDOStatement as Statement;
  18. /**
  19. * Decorator for \PDOStatement class mainly used for converting human readable
  20. * fetch modes into PDO constants.
  21. */
  22. class PDOStatement extends StatementDecorator
  23. {
  24. /**
  25. * Constructor
  26. *
  27. * @param \PDOStatement|null $statement Original statement to be decorated.
  28. * @param \Cake\Database\Driver|null $driver Driver instance.
  29. */
  30. public function __construct(Statement $statement = null, $driver = null)
  31. {
  32. parent::__construct($statement, $driver);
  33. }
  34. /**
  35. * Assign a value to a positional or named variable in prepared query. If using
  36. * positional variables you need to start with index one, if using named params then
  37. * just use the name in any order.
  38. *
  39. * You can pass PDO compatible constants for binding values with a type or optionally
  40. * any type name registered in the Type class. Any value will be converted to the valid type
  41. * representation if needed.
  42. *
  43. * It is not allowed to combine positional and named variables in the same statement
  44. *
  45. * ### Examples:
  46. *
  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. *
  54. * @param string|int $column name or param position to be bound
  55. * @param mixed $value The value to bind to variable in query
  56. * @param string|int $type PDO type or name of configured Type class
  57. * @return void
  58. */
  59. public function bindValue($column, $value, $type = 'string')
  60. {
  61. if ($type === null) {
  62. $type = 'string';
  63. }
  64. if (!ctype_digit($type)) {
  65. list($value, $type) = $this->cast($value, $type);
  66. }
  67. $this->_statement->bindValue($column, $value, $type);
  68. }
  69. /**
  70. * Returns the next row for the result set after executing this statement.
  71. * Rows can be fetched to contain columns as names or positions. If no
  72. * rows are left in result set, this method will return false
  73. *
  74. * ### Example:
  75. *
  76. * ```
  77. * $statement = $connection->prepare('SELECT id, title from articles');
  78. * $statement->execute();
  79. * print_r($statement->fetch('assoc')); // will show ['id' => 1, 'title' => 'a title']
  80. * ```
  81. *
  82. * @param string $type 'num' for positional columns, assoc for named columns
  83. * @return array|false Result array containing columns and values or false if no results
  84. * are left
  85. */
  86. public function fetch($type = parent::FETCH_TYPE_NUM)
  87. {
  88. if ($type === static::FETCH_TYPE_NUM) {
  89. return $this->_statement->fetch(PDO::FETCH_NUM);
  90. }
  91. if ($type === static::FETCH_TYPE_ASSOC) {
  92. return $this->_statement->fetch(PDO::FETCH_ASSOC);
  93. }
  94. if ($type === static::FETCH_TYPE_OBJ) {
  95. return $this->_statement->fetch(PDO::FETCH_OBJ);
  96. }
  97. return $this->_statement->fetch($type);
  98. }
  99. /**
  100. * Returns an array with all rows resulting from executing this statement
  101. *
  102. * ### Example:
  103. *
  104. * ```
  105. * $statement = $connection->prepare('SELECT id, title from articles');
  106. * $statement->execute();
  107. * print_r($statement->fetchAll('assoc')); // will show [0 => ['id' => 1, 'title' => 'a title']]
  108. * ```
  109. *
  110. * @param string $type num for fetching columns as positional keys or assoc for column names as keys
  111. * @return array list of all results from database for this statement
  112. */
  113. public function fetchAll($type = parent::FETCH_TYPE_NUM)
  114. {
  115. if ($type === static::FETCH_TYPE_NUM) {
  116. return $this->_statement->fetchAll(PDO::FETCH_NUM);
  117. }
  118. if ($type === static::FETCH_TYPE_ASSOC) {
  119. return $this->_statement->fetchAll(PDO::FETCH_ASSOC);
  120. }
  121. if ($type === static::FETCH_TYPE_OBJ) {
  122. return $this->_statement->fetchAll(PDO::FETCH_OBJ);
  123. }
  124. return $this->_statement->fetchAll($type);
  125. }
  126. }