StatementInterface.php 5.1 KB

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