StatementInterface.php 6.6 KB

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