StatementInterface.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Drupal\Core\Database;
  3. /**
  4. * Represents a prepared statement.
  5. *
  6. * Child implementations should either extend PDOStatement:
  7. * @code
  8. * class Drupal\Core\Database\Driver\oracle\Statement extends PDOStatement implements Drupal\Core\Database\StatementInterface {}
  9. * @endcode
  10. * or define their own class. If defining their own class, they will also have
  11. * to implement either the Iterator or IteratorAggregate interface before
  12. * Drupal\Core\Database\StatementInterface:
  13. * @code
  14. * class Drupal\Core\Database\Driver\oracle\Statement implements Iterator, Drupal\Core\Database\StatementInterface {}
  15. * @endcode
  16. *
  17. * @ingroup database
  18. */
  19. interface StatementInterface extends \Traversable {
  20. /**
  21. * Constructs a new PDOStatement object.
  22. *
  23. * The PDO manual does not document this constructor, but when overriding the
  24. * PDOStatement class with a custom without this constructor, PDO will throw
  25. * the internal exception/warning:
  26. *
  27. * "PDO::query(): SQLSTATE[HY000]: General error: user-supplied statement does
  28. * not accept constructor arguments"
  29. *
  30. * PDO enforces that the access type of this constructor must be protected,
  31. * and lastly, it also enforces that a custom PDOStatement interface (like
  32. * this) omits the constructor (declaring it results in fatal errors
  33. * complaining about "the access type must not be public" if it is public, and
  34. * "the access type must be omitted" if it is protected; i.e., conflicting
  35. * statements). The access type has to be protected.
  36. */
  37. // protected function __construct(Connection $dbh);
  38. /**
  39. * Executes a prepared statement
  40. *
  41. * @param $args
  42. * An array of values with as many elements as there are bound parameters in
  43. * the SQL statement being executed. This can be NULL.
  44. * @param $options
  45. * An array of options for this query.
  46. *
  47. * @return
  48. * TRUE on success, or FALSE on failure.
  49. */
  50. public function execute($args = [], $options = []);
  51. /**
  52. * Gets the query string of this statement.
  53. *
  54. * @return
  55. * The query string, in its form with placeholders.
  56. */
  57. public function getQueryString();
  58. /**
  59. * Returns the number of rows affected by the last SQL statement.
  60. *
  61. * @return
  62. * The number of rows affected by the last DELETE, INSERT, or UPDATE
  63. * statement executed or throws \Drupal\Core\Database\RowCountException
  64. * if the last executed statement was SELECT.
  65. *
  66. * @throws \Drupal\Core\Database\RowCountException
  67. */
  68. public function rowCount();
  69. /**
  70. * Sets the default fetch mode for this statement.
  71. *
  72. * See http://php.net/manual/pdo.constants.php for the definition of the
  73. * constants used.
  74. *
  75. * @param $mode
  76. * One of the PDO::FETCH_* constants.
  77. * @param $a1
  78. * An option depending of the fetch mode specified by $mode:
  79. * - for PDO::FETCH_COLUMN, the index of the column to fetch
  80. * - for PDO::FETCH_CLASS, the name of the class to create
  81. * - for PDO::FETCH_INTO, the object to add the data to
  82. * @param $a2
  83. * If $mode is PDO::FETCH_CLASS, the optional arguments to pass to the
  84. * constructor.
  85. */
  86. public function setFetchMode($mode, $a1 = NULL, $a2 = []);
  87. /**
  88. * Fetches the next row from a result set.
  89. *
  90. * See http://php.net/manual/pdo.constants.php for the definition of the
  91. * constants used.
  92. *
  93. * @param $mode
  94. * One of the PDO::FETCH_* constants.
  95. * Default to what was specified by setFetchMode().
  96. * @param $cursor_orientation
  97. * Not implemented in all database drivers, don't use.
  98. * @param $cursor_offset
  99. * Not implemented in all database drivers, don't use.
  100. *
  101. * @return
  102. * A result, formatted according to $mode.
  103. */
  104. public function fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL);
  105. /**
  106. * Returns a single field from the next record of a result set.
  107. *
  108. * @param $index
  109. * The numeric index of the field to return. Defaults to the first field.
  110. *
  111. * @return
  112. * A single field from the next record, or FALSE if there is no next record.
  113. */
  114. public function fetchField($index = 0);
  115. /**
  116. * Fetches the next row and returns it as an object.
  117. *
  118. * The object will be of the class specified by StatementInterface::setFetchMode()
  119. * or stdClass if not specified.
  120. */
  121. public function fetchObject();
  122. /**
  123. * Fetches the next row and returns it as an associative array.
  124. *
  125. * This method corresponds to PDOStatement::fetchObject(), but for associative
  126. * arrays. For some reason PDOStatement does not have a corresponding array
  127. * helper method, so one is added.
  128. *
  129. * @return
  130. * An associative array, or FALSE if there is no next row.
  131. */
  132. public function fetchAssoc();
  133. /**
  134. * Returns an array containing all of the result set rows.
  135. *
  136. * @param $mode
  137. * One of the PDO::FETCH_* constants.
  138. * @param $column_index
  139. * If $mode is PDO::FETCH_COLUMN, the index of the column to fetch.
  140. * @param $constructor_arguments
  141. * If $mode is PDO::FETCH_CLASS, the arguments to pass to the constructor.
  142. *
  143. * @return
  144. * An array of results.
  145. */
  146. public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL);
  147. /**
  148. * Returns an entire single column of a result set as an indexed array.
  149. *
  150. * Note that this method will run the result set to the end.
  151. *
  152. * @param $index
  153. * The index of the column number to fetch.
  154. *
  155. * @return
  156. * An indexed array, or an empty array if there is no result set.
  157. */
  158. public function fetchCol($index = 0);
  159. /**
  160. * Returns the entire result set as a single associative array.
  161. *
  162. * This method is only useful for two-column result sets. It will return an
  163. * associative array where the key is one column from the result set and the
  164. * value is another field. In most cases, the default of the first two columns
  165. * is appropriate.
  166. *
  167. * Note that this method will run the result set to the end.
  168. *
  169. * @param $key_index
  170. * The numeric index of the field to use as the array key.
  171. * @param $value_index
  172. * The numeric index of the field to use as the array value.
  173. *
  174. * @return
  175. * An associative array, or an empty array if there is no result set.
  176. */
  177. public function fetchAllKeyed($key_index = 0, $value_index = 1);
  178. /**
  179. * Returns the result set as an associative array keyed by the given field.
  180. *
  181. * If the given key appears multiple times, later records will overwrite
  182. * earlier ones.
  183. *
  184. * @param $key
  185. * The name of the field on which to index the array.
  186. * @param $fetch
  187. * The fetchmode to use. If set to PDO::FETCH_ASSOC, PDO::FETCH_NUM, or
  188. * PDO::FETCH_BOTH the returned value with be an array of arrays. For any
  189. * other value it will be an array of objects. By default, the fetch mode
  190. * set for the query will be used.
  191. *
  192. * @return
  193. * An associative array, or an empty array if there is no result set.
  194. */
  195. public function fetchAllAssoc($key, $fetch = NULL);
  196. }