PDOStatement.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. * Constructor
  25. *
  26. * @param \PDOStatement $statement Original statement to be decorated.
  27. * @param \Cake\Database\Driver $driver Driver instance.
  28. */
  29. public function __construct(Statement $statement = null, $driver = null) {
  30. $this->_statement = $statement;
  31. $this->_driver = $driver;
  32. }
  33. /**
  34. * Assign a value to a positional or named variable in prepared query. If using
  35. * positional variables you need to start with index one, if using named params then
  36. * just use the name in any order.
  37. *
  38. * You can pass PDO compatible constants for binding values with a type or optionally
  39. * any type name registered in the Type class. Any value will be converted to the valid type
  40. * representation if needed.
  41. *
  42. * It is not allowed to combine positional and named variables in the same statement
  43. *
  44. * ## Examples:
  45. *
  46. * {{{
  47. * $statement->bindValue(1, 'a title');
  48. * $statement->bindValue(2, 5, PDO::INT);
  49. * $statement->bindValue('active', true, 'boolean');
  50. * $statement->bindValue(5, new \DateTime(), 'date');
  51. * }}}
  52. *
  53. * @param string|int $column name or param position to be bound
  54. * @param mixed $value The value to bind to variable in query
  55. * @param string|int $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 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. }