StatementInterface.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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;
  16. /**
  17. * Represents a database statement. Concrete implementations
  18. * can either use PDOStatement or a native driver
  19. *
  20. * @property-read string $queryString
  21. */
  22. interface StatementInterface
  23. {
  24. /**
  25. * Used to designate that numeric indexes be returned in a result when calling fetch methods
  26. *
  27. * @var string
  28. */
  29. const FETCH_TYPE_NUM = 'num';
  30. /**
  31. * Used to designate that an associated array be returned in a result when calling fetch methods
  32. *
  33. * @var string
  34. */
  35. const FETCH_TYPE_ASSOC = 'assoc';
  36. /**
  37. * Used to designate that a stdClass object be returned in a result when calling fetch methods
  38. *
  39. * @var string
  40. */
  41. const FETCH_TYPE_OBJ = 'obj';
  42. /**
  43. * Assign a value to a positional or named variable in prepared query. If using
  44. * positional variables you need to start with index one, if using named params then
  45. * just use the name in any order.
  46. *
  47. * It is not allowed to combine positional and named variables in the same statement
  48. *
  49. * ### Examples:
  50. *
  51. * ```
  52. * $statement->bindValue(1, 'a title');
  53. * $statement->bindValue('active', true, 'boolean');
  54. * $statement->bindValue(5, new \DateTime(), 'date');
  55. * ```
  56. *
  57. * @param string|int $column name or param position to be bound
  58. * @param mixed $value The value to bind to variable in query
  59. * @param string $type name of configured Type class
  60. * @return void
  61. */
  62. public function bindValue($column, $value, $type = 'string');
  63. /**
  64. * Closes a cursor in the database, freeing up any resources and memory
  65. * allocated to it. In most cases you don't need to call this method, as it is
  66. * automatically called after fetching all results from the result set.
  67. *
  68. * @return void
  69. */
  70. public function closeCursor();
  71. /**
  72. * Returns the number of columns this statement's results will contain
  73. *
  74. * ### Example:
  75. *
  76. * ```
  77. * $statement = $connection->prepare('SELECT id, title from articles');
  78. * $statement->execute();
  79. * echo $statement->columnCount(); // outputs 2
  80. * ```
  81. *
  82. * @return int
  83. */
  84. public function columnCount();
  85. /**
  86. * Returns the error code for the last error that occurred when executing this statement
  87. *
  88. * @return int|string
  89. */
  90. public function errorCode();
  91. /**
  92. * Returns the error information for the last error that occurred when executing
  93. * this statement
  94. *
  95. * @return array
  96. */
  97. public function errorInfo();
  98. /**
  99. * Executes the statement by sending the SQL query to the database. It can optionally
  100. * take an array or arguments to be bound to the query variables. Please note
  101. * that binding parameters from this method will not perform any custom type conversion
  102. * as it would normally happen when calling `bindValue`
  103. *
  104. * @param array|null $params list of values to be bound to query
  105. * @return bool true on success, false otherwise
  106. */
  107. public function execute($params = null);
  108. /**
  109. * Returns the next row for the result set after executing this statement.
  110. * Rows can be fetched to contain columns as names or positions. If no
  111. * rows are left in result set, this method will return false
  112. *
  113. * ### Example:
  114. *
  115. * ```
  116. * $statement = $connection->prepare('SELECT id, title from articles');
  117. * $statement->execute();
  118. * print_r($statement->fetch('assoc')); // will show ['id' => 1, 'title' => 'a title']
  119. * ```
  120. *
  121. * @param string $type 'num' for positional columns, assoc for named columns
  122. * @return array|false Result array containing columns and values or false if no results
  123. * are left
  124. */
  125. public function fetch($type = 'num');
  126. /**
  127. * Returns an array with all rows resulting from executing this statement
  128. *
  129. * ### Example:
  130. *
  131. * ```
  132. * $statement = $connection->prepare('SELECT id, title from articles');
  133. * $statement->execute();
  134. * print_r($statement->fetchAll('assoc')); // will show [0 => ['id' => 1, 'title' => 'a title']]
  135. * ```
  136. *
  137. * @param string $type num for fetching columns as positional keys or assoc for column names as keys
  138. * @return array list of all results from database for this statement
  139. */
  140. public function fetchAll($type = 'num');
  141. /**
  142. * Returns the number of rows affected by this SQL statement
  143. *
  144. * ### Example:
  145. *
  146. * ```
  147. * $statement = $connection->prepare('SELECT id, title from articles');
  148. * $statement->execute();
  149. * print_r($statement->rowCount()); // will show 1
  150. * ```
  151. *
  152. * @return int
  153. */
  154. public function rowCount();
  155. /**
  156. * Statements can be passed as argument for count()
  157. * to return the number for affected rows from last execution
  158. *
  159. * @return int
  160. */
  161. public function count();
  162. /**
  163. * Binds a set of values to statement object with corresponding type
  164. *
  165. * @param array $params list of values to be bound
  166. * @param array $types list of types to be used, keys should match those in $params
  167. * @return void
  168. */
  169. public function bind($params, $types);
  170. /**
  171. * Returns the latest primary inserted using this statement
  172. *
  173. * @param string|null $table table name or sequence to get last insert value from
  174. * @param string|null $column the name of the column representing the primary key
  175. * @return string
  176. */
  177. public function lastInsertId($table = null, $column = null);
  178. }