Skip to content
Snippets Groups Projects
Commit 42c26e21 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2521832 by neclimdul, Crell, amateescu: Uncomment StatementInterface methods

parent 5e77cbed
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -22,7 +22,7 @@
class Statement extends StatementPrefetch implements StatementInterface {
/**
* SQLite specific implementation of getStatement().
* {@inheritdoc}
*
* The PDO SQLite layer doesn't replace numeric placeholders in queries
* correctly, and this makes numeric expressions (such as COUNT(*) >= :count)
......@@ -87,6 +87,9 @@ protected function getStatement($query, &$args = array()) {
return $this->pdoConnection->prepare($query);
}
/**
* {@inheritdoc}
*/
public function execute($args = array(), $options = array()) {
try {
$return = parent::execute($args, $options);
......
......@@ -41,6 +41,9 @@ protected function __construct(Connection $dbh) {
$this->setFetchMode(\PDO::FETCH_OBJ);
}
/**
* {@inheritdoc}
*/
public function execute($args = array(), $options = array()) {
if (isset($options['fetch'])) {
if (is_string($options['fetch'])) {
......@@ -68,14 +71,23 @@ public function execute($args = array(), $options = array()) {
return $return;
}
/**
* {@inheritdoc}
*/
public function getQueryString() {
return $this->queryString;
}
/**
* {@inheritdoc}
*/
public function fetchCol($index = 0) {
return $this->fetchAll(\PDO::FETCH_COLUMN, $index);
}
/**
* {@inheritdoc}
*/
public function fetchAllAssoc($key, $fetch = NULL) {
$return = array();
if (isset($fetch)) {
......@@ -95,6 +107,9 @@ public function fetchAllAssoc($key, $fetch = NULL) {
return $return;
}
/**
* {@inheritdoc}
*/
public function fetchAllKeyed($key_index = 0, $value_index = 1) {
$return = array();
$this->setFetchMode(\PDO::FETCH_NUM);
......@@ -104,11 +119,17 @@ public function fetchAllKeyed($key_index = 0, $value_index = 1) {
return $return;
}
/**
* {@inheritdoc}
*/
public function fetchField($index = 0) {
// Call \PDOStatement::fetchColumn to fetch the field.
return $this->fetchColumn($index);
}
/**
* {@inheritdoc}
*/
public function fetchAssoc() {
// Call \PDOStatement::fetch to fetch the row.
return $this->fetch(\PDO::FETCH_ASSOC);
......@@ -127,4 +148,42 @@ public function rowCount() {
}
}
/**
* {@inheritdoc}
*/
public function setFetchMode($mode, $a1 = NULL, $a2 = array()) {
// Call \PDOStatement::setFetchMode to set fetch mode.
// \PDOStatement is picky about the number of arguments in some cases so we
// need to be pass the exact number of arguments we where given.
switch (func_num_args()) {
case 1:
return parent::setFetchMode($mode);
case 2:
return parent::setFetchMode($mode, $a1);
case 3:
default:
return parent::setFetchMode($mode, $a1, $a2);
}
}
/**
* {@inheritdoc}
*/
public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL) {
// Call \PDOStatement::fetchAll to fetch all rows.
// \PDOStatement is picky about the number of arguments in some cases so we
// need to be pass the exact number of arguments we where given.
switch (func_num_args()) {
case 0:
return parent::fetchAll();
case 1:
return parent::fetchAll($mode);
case 2:
return parent::fetchAll($mode, $column_index);
case 3:
default:
return parent::fetchAll($mode, $column_index, $constructor_arguments);
}
}
}
......@@ -28,14 +28,23 @@ class StatementEmpty implements \Iterator, StatementInterface {
*/
public $allowRowCount = FALSE;
/**
* {@inheritdoc}
*/
public function execute($args = array(), $options = array()) {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getQueryString() {
return '';
}
/**
* {@inheritdoc}
*/
public function rowCount() {
if ($this->allowRowCount) {
return 0;
......@@ -43,61 +52,102 @@ public function rowCount() {
throw new RowCountException();
}
/**
* {@inheritdoc}
*/
public function setFetchMode($mode, $a1 = NULL, $a2 = array()) {
return;
}
/**
* {@inheritdoc}
*/
public function fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL) {
return NULL;
}
/**
* {@inheritdoc}
*/
public function fetchField($index = 0) {
return NULL;
}
/**
* {@inheritdoc}
*/
public function fetchObject() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function fetchAssoc() {
return NULL;
}
function fetchAll($mode = NULL, $column_index = NULL, array $constructor_arguments = array()) {
/**
* {@inheritdoc}
*/
public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL) {
return array();
}
/**
* {@inheritdoc}
*/
public function fetchCol($index = 0) {
return array();
}
/**
* {@inheritdoc}
*/
public function fetchAllKeyed($key_index = 0, $value_index = 1) {
return array();
}
/**
* {@inheritdoc}
*/
public function fetchAllAssoc($key, $fetch = NULL) {
return array();
}
/* Implementations of Iterator. */
/**
* {@inheritdoc}
*/
public function current() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function key() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function rewind() {
// Nothing to do: our DatabaseStatement can't be rewound.
}
/**
* {@inheritdoc}
*/
public function next() {
// Do nothing, since this is an always-empty implementation.
}
/**
* {@inheritdoc}
*/
public function valid() {
return FALSE;
}
}
......@@ -10,11 +10,6 @@
/**
* Represents a prepared statement.
*
* Some methods in that class are purposefully commented out. Due to a change in
* how PHP defines PDOStatement, we can't define a signature for those methods
* that will work the same way between versions older than 5.2.6 and later
* versions. See http://bugs.php.net/bug.php?id=42452 for more details.
*
* Child implementations should either extend PDOStatement:
* @code
* class Drupal\Core\Database\Driver\oracle\Statement extends PDOStatement implements Drupal\Core\Database\StatementInterface {}
......@@ -100,7 +95,7 @@ public function rowCount();
* If $mode is PDO::FETCH_CLASS, the optional arguments to pass to the
* constructor.
*/
// public function setFetchMode($mode, $a1 = NULL, $a2 = array());
public function setFetchMode($mode, $a1 = NULL, $a2 = array());
/**
* Fetches the next row from a result set.
......@@ -119,7 +114,7 @@ public function rowCount();
* @return
* A result, formatted according to $mode.
*/
// public function fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL);
public function fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL);
/**
* Returns a single field from the next record of a result set.
......@@ -138,7 +133,7 @@ public function fetchField($index = 0);
* The object will be of the class specified by StatementInterface::setFetchMode()
* or stdClass if not specified.
*/
// public function fetchObject();
public function fetchObject();
/**
* Fetches the next row and returns it as an associative array.
......@@ -165,7 +160,7 @@ public function fetchAssoc();
* @return
* An array of results.
*/
// function fetchAll($mode = NULL, $column_index = NULL, array $constructor_arguments);
function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL);
/**
* Returns an entire single column of a result set as an indexed array.
......
......@@ -139,14 +139,7 @@ public function __construct(\PDO $pdo_connection, Connection $connection, $query
}
/**
* Executes a prepared statement.
*
* @param $args
* An array of values with as many elements as there are bound parameters in the SQL statement being executed.
* @param $options
* An array of options for this query.
* @return
* TRUE on success, or FALSE on failure.
* {@inheritdoc}
*/
public function execute($args = array(), $options = array()) {
if (isset($options['fetch'])) {
......@@ -236,29 +229,29 @@ protected function getStatement($query, &$args = array()) {
}
/**
* Return the object's SQL query string.
* {@inheritdoc}
*/
public function getQueryString() {
return $this->queryString;
}
/**
* @see \PDOStatement::setFetchMode()
* {@inheritdoc}
*/
public function setFetchMode($fetchStyle, $a2 = NULL, $a3 = NULL) {
$this->defaultFetchStyle = $fetchStyle;
switch ($fetchStyle) {
public function setFetchMode($mode, $a1 = NULL, $a2 = array()) {
$this->defaultFetchStyle = $mode;
switch ($mode) {
case \PDO::FETCH_CLASS:
$this->defaultFetchOptions['class'] = $a2;
if ($a3) {
$this->defaultFetchOptions['constructor_args'] = $a3;
$this->defaultFetchOptions['class'] = $a1;
if ($a2) {
$this->defaultFetchOptions['constructor_args'] = $a2;
}
break;
case \PDO::FETCH_COLUMN:
$this->defaultFetchOptions['column'] = $a2;
$this->defaultFetchOptions['column'] = $a1;
break;
case \PDO::FETCH_INTO:
$this->defaultFetchOptions['object'] = $a2;
$this->defaultFetchOptions['object'] = $a1;
break;
}
......@@ -274,8 +267,8 @@ public function setFetchMode($fetchStyle, $a2 = NULL, $a3 = NULL) {
* array position in $this->data and format it according to $this->fetchStyle
* and $this->fetchMode.
*
* @return
* The current row formatted as requested.
* @return mixed
* The current row formatted as requested.
*/
public function current() {
if (isset($this->currentRow)) {
......@@ -327,16 +320,23 @@ public function current() {
}
}
/* Implementations of Iterator. */
/**
* {@inheritdoc}
*/
public function key() {
return $this->currentKey;
}
/**
* {@inheritdoc}
*/
public function rewind() {
// Nothing to do: our DatabaseStatement can't be rewound.
}
/**
* {@inheritdoc}
*/
public function next() {
if (!empty($this->data)) {
$this->currentRow = reset($this->data);
......@@ -348,6 +348,9 @@ public function next() {
}
}
/**
* {@inheritdoc}
*/
public function valid() {
return isset($this->currentRow);
}
......@@ -365,6 +368,9 @@ public function rowCount() {
}
}
/**
* {@inheritdoc}
*/
public function fetch($fetch_style = NULL, $cursor_orientation = \PDO::FETCH_ORI_NEXT, $cursor_offset = NULL) {
if (isset($this->currentRow)) {
// Set the fetch parameter.
......@@ -398,10 +404,16 @@ public function fetchColumn($index = 0) {
}
}
/**
* {@inheritdoc}
*/
public function fetchField($index = 0) {
return $this->fetchColumn($index);
}
/**
* {@inheritdoc}
*/
public function fetchObject($class_name = NULL, $constructor_args = array()) {
if (isset($this->currentRow)) {
if (!isset($class_name)) {
......@@ -427,6 +439,9 @@ public function fetchObject($class_name = NULL, $constructor_args = array()) {
}
}
/**
* {@inheritdoc}
*/
public function fetchAssoc() {
if (isset($this->currentRow)) {
$result = $this->currentRow;
......@@ -438,14 +453,17 @@ public function fetchAssoc() {
}
}
public function fetchAll($fetch_style = NULL, $fetch_column = NULL, $constructor_args = NULL) {
$this->fetchStyle = isset($fetch_style) ? $fetch_style : $this->defaultFetchStyle;
/**
* {@inheritdoc}
*/
public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL) {
$this->fetchStyle = isset($mode) ? $mode : $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
if (isset($fetch_column)) {
$this->fetchOptions['column'] = $fetch_column;
if (isset($column_index)) {
$this->fetchOptions['column'] = $column_index;
}
if (isset($constructor_args)) {
$this->fetchOptions['constructor_args'] = $constructor_args;
if (isset($constructor_arguments)) {
$this->fetchOptions['constructor_args'] = $constructor_arguments;
}
$result = array();
......@@ -462,6 +480,9 @@ public function fetchAll($fetch_style = NULL, $fetch_column = NULL, $constructor
return $result;
}
/**
* {@inheritdoc}
*/
public function fetchCol($index = 0) {
if (isset($this->columnNames[$index])) {
$result = array();
......@@ -477,6 +498,9 @@ public function fetchCol($index = 0) {
}
}
/**
* {@inheritdoc}
*/
public function fetchAllKeyed($key_index = 0, $value_index = 1) {
if (!isset($this->columnNames[$key_index]) || !isset($this->columnNames[$value_index]))
return array();
......@@ -493,6 +517,9 @@ public function fetchAllKeyed($key_index = 0, $value_index = 1) {
return $result;
}
/**
* {@inheritdoc}
*/
public function fetchAllAssoc($key, $fetch_style = NULL) {
$this->fetchStyle = isset($fetch_style) ? $fetch_style : $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment