class StatementWrapper implements IteratorAggregate, StatementInterface (View source)

Implementation of StatementInterface encapsulating PDOStatement.

Properties

protected Connection $connection

The Drupal database connection object.

protected object $clientStatement

The client database Statement object.

protected bool $rowCountEnabled

Is rowCount() execution allowed.

Methods

__construct(Connection $connection, object $client_connection, string $query, array $options, bool $row_count_enabled = FALSE)

Constructs a StatementWrapper object.

__get($name)

Implements the magic __get() method.

__set($name, $value)

Implements the magic __set() method.

__call($method, $arguments) deprecated

Implements the magic __call() method.

object
getClientStatement()

Returns the client-level database statement object.

string
getConnectionTarget()

{@inheritdoc}

true
execute($args = [], $options = [])

Executes a prepared statement.

The
getQueryString()

Gets the query string of this statement.

An
fetchCol($index = 0)

Returns an entire single column of a result set as an indexed array.

An
fetchAllAssoc($key, $fetch = NULL)

Returns the result set as an associative array keyed by the given field.

An
fetchAllKeyed($key_index = 0, $value_index = 1)

Returns the entire result set as a single associative array.

A
fetchField($index = 0)

Returns a single field from the next record of a result set.

An
fetchAssoc()

Fetches the next row and returns it as an associative array.

mixed
fetchObject(string $class_name = NULL, array $constructor_arguments = NULL)

Fetches the next row and returns it as an object.

The
rowCount()

Returns the number of rows affected by the last SQL statement.

setFetchMode($mode, $a1 = NULL, $a2 = [])

Sets the default fetch mode for this statement.

A
fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL)

Fetches the next row from a result set.

An
fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL)

Returns an array containing all of the result set rows.

getIterator()

{@inheritdoc}

bool
bindColumn(mixed $column, mixed $param, int $type = 0, int $maxlen = 0, mixed $driverdata = NULL) deprecated

Bind a column to a PHP variable.

bool
bindParam(mixed $parameter, mixed $variable, int $data_type = \PDO::PARAM_STR, int $length = 0, mixed $driver_options = NULL) deprecated

Binds a parameter to the specified variable name.

Details

__construct(Connection $connection, object $client_connection, string $query, array $options, bool $row_count_enabled = FALSE)

Constructs a StatementWrapper object.

Parameters

Connection $connection

Drupal database connection object.

object $client_connection

Client database connection object, for example \PDO.

string $query

The SQL query string.

array $options

Array of query options.

bool $row_count_enabled

(optional) Enables counting the rows affected. Defaults to FALSE.

__get($name)

Implements the magic __get() method.

Remove the method before Drupal 10.

Parameters

$name

See also

https://www.drupal.org/i/3210310

__set($name, $value)

Implements the magic __set() method.

Remove the method before Drupal 10.

Parameters

$name
$value

See also

https://www.drupal.org/i/3210310

__call($method, $arguments) deprecated

deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Access the client-level statement object via ::getClientStatement().

Implements the magic __call() method.

Parameters

$method
$arguments

See also

https://www.drupal.org/node/3177488

object getClientStatement()

Returns the client-level database statement object.

This method should normally be used only within database driver code.

Return Value

object

The client-level database statement, for example \PDOStatement.

string getConnectionTarget()

{@inheritdoc}

Return Value

string

true execute($args = [], $options = [])

Executes a prepared statement.

Parameters

$args

An array of values with as many elements as there are bound parameters in the SQL statement being executed. This can be NULL.

$options

An array of options for this query.

Return Value

true

on success, or FALSE on failure.

The getQueryString()

Gets the query string of this statement.

Return Value

The

query string, in its form with placeholders.

An fetchCol($index = 0)

Returns an entire single column of a result set as an indexed array.

Note that this method will run the result set to the end.

Parameters

$index

The index of the column number to fetch.

Return Value

An

indexed array, or an empty array if there is no result set.

An fetchAllAssoc($key, $fetch = NULL)

Returns the result set as an associative array keyed by the given field.

If the given key appears multiple times, later records will overwrite earlier ones.

Parameters

$key

The name of the field on which to index the array.

$fetch

The fetchmode to use. If set to PDO::FETCH_ASSOC, PDO::FETCH_NUM, or PDO::FETCH_BOTH the returned value with be an array of arrays. For any other value it will be an array of objects. By default, the fetch mode set for the query will be used.

Return Value

An

associative array, or an empty array if there is no result set.

An fetchAllKeyed($key_index = 0, $value_index = 1)

Returns the entire result set as a single associative array.

This method is only useful for two-column result sets. It will return an associative array where the key is one column from the result set and the value is another field. In most cases, the default of the first two columns is appropriate.

Note that this method will run the result set to the end.

Parameters

$key_index

The numeric index of the field to use as the array key.

$value_index

The numeric index of the field to use as the array value.

Return Value

An

associative array, or an empty array if there is no result set.

A fetchField($index = 0)

Returns a single field from the next record of a result set.

Parameters

$index

The numeric index of the field to return. Defaults to the first field.

Return Value

A

single field from the next record, or FALSE if there is no next record.

An fetchAssoc()

Fetches the next row and returns it as an associative array.

This method corresponds to PDOStatement::fetchObject(), but for associative arrays. For some reason PDOStatement does not have a corresponding array helper method, so one is added.

Return Value

An

associative array, or FALSE if there is no next row.

mixed fetchObject(string $class_name = NULL, array $constructor_arguments = NULL)

Fetches the next row and returns it as an object.

The object will be of the class specified by StatementInterface::setFetchMode() or stdClass if not specified.

phpcs:disable Drupal.Commenting

Parameters

string $class_name
array $constructor_arguments

Return Value

mixed

The object of specified class or \stdClass if not specified. Returns FALSE or NULL if there is no next row.

The rowCount()

Returns the number of rows affected by the last SQL statement.

Return Value

The

number of rows affected by the last DELETE, INSERT, or UPDATE statement executed or throws \Drupal\Core\Database\RowCountException if the last executed statement was SELECT.

Exceptions

RowCountException

setFetchMode($mode, $a1 = NULL, $a2 = [])

Sets the default fetch mode for this statement.

See http://php.net/manual/pdo.constants.php for the definition of the constants used.

Parameters

$mode

One of the PDO::FETCH_* constants.

$a1

An option depending of the fetch mode specified by $mode:

  • for PDO::FETCH_COLUMN, the index of the column to fetch
  • for PDO::FETCH_CLASS, the name of the class to create
  • for PDO::FETCH_INTO, the object to add the data to
$a2

If $mode is PDO::FETCH_CLASS, the optional arguments to pass to the constructor.

A fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL)

Fetches the next row from a result set.

See http://php.net/manual/pdo.constants.php for the definition of the constants used.

Parameters

$mode

One of the PDO::FETCH_* constants. Default to what was specified by setFetchMode().

$cursor_orientation

Not implemented in all database drivers, don't use.

$cursor_offset

Not implemented in all database drivers, don't use.

Return Value

A

result, formatted according to $mode.

An fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL)

Returns an array containing all of the result set rows.

Parameters

$mode

One of the PDO::FETCH_* constants.

$column_index

If $mode is PDO::FETCH_COLUMN, the index of the column to fetch.

$constructor_arguments

If $mode is PDO::FETCH_CLASS, the arguments to pass to the constructor.

Return Value

An

array of results.

getIterator()

{@inheritdoc}

bool bindColumn(mixed $column, mixed $param, int $type = 0, int $maxlen = 0, mixed $driverdata = NULL) deprecated

deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. StatementWrapper::bindColumn should not be called. Access the client-level statement object via ::getClientStatement().

Bind a column to a PHP variable.

Parameters

mixed $column

Number of the column (1-indexed) or name of the column in the result set. If using the column name, be aware that the name should match the case of the column, as returned by the driver.

mixed $param

Name of the PHP variable to which the column will be bound.

int $type

(Optional) data type of the parameter, specified by the PDO::PARAM_* constants.

int $maxlen

(Optional) a hint for pre-allocation.

mixed $driverdata

(Optional) optional parameter(s) for the driver.

Return Value

bool

Returns TRUE on success or FALSE on failure.

See also

https://www.drupal.org/node/3177488

bool bindParam(mixed $parameter, mixed $variable, int $data_type = \PDO::PARAM_STR, int $length = 0, mixed $driver_options = NULL) deprecated

deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. StatementWrapper::bindParam should not be called. Access the client-level statement object via ::getClientStatement().

Binds a parameter to the specified variable name.

Parameters

mixed $parameter

Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name.

mixed $variable

Name of the PHP variable to bind to the SQL statement parameter.

int $data_type

(Optional) explicit data type for the parameter using the PDO::PARAM_* constants. To return an INOUT parameter from a stored procedure, use the bitwise OR operator to set the PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter.

int $length

(Optional) length of the data type. To indicate that a parameter is an OUT parameter from a stored procedure, you must explicitly set the length.

mixed $driver_options

(Optional) Driver options.

Return Value

bool

Returns TRUE on success or FALSE on failure.

See also

https://www.drupal.org/node/3177488