StatementInterface.php 5.0 KB

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