View source
<?php
namespace Drupal\Core\Database;
class StatementWrapper implements \IteratorAggregate, StatementInterface {
protected $connection;
protected $clientStatement;
protected $rowCountEnabled = FALSE;
public function __construct(Connection $connection, $client_connection, string $query, array $options, bool $row_count_enabled = FALSE) {
$this->connection = $connection;
$this->clientStatement = $client_connection
->prepare($query, $options);
$this->rowCountEnabled = $row_count_enabled;
$this
->setFetchMode(\PDO::FETCH_OBJ);
}
public function __get($name) {
if ($name === 'queryString') {
@trigger_error("StatementWrapper::\$queryString should not be accessed in drupal:9.1.0 and will error in drupal:10.0.0. Access the client-level statement object via ::getClientStatement(). See https://www.drupal.org/node/3177488", E_USER_DEPRECATED);
return $this
->getClientStatement()->queryString;
}
if ($name === 'dbh') {
@trigger_error(__CLASS__ . '::$dbh should not be accessed in drupal:9.3.0 and will error in drupal:10.0.0. Use $this->connection instead. See https://www.drupal.org/node/3186368', E_USER_DEPRECATED);
return $this->connection;
}
if ($name === 'allowRowCount') {
@trigger_error(__CLASS__ . '::$allowRowCount should not be accessed in drupal:9.3.0 and will error in drupal:10.0.0. Use $this->rowCountEnabled instead. See https://www.drupal.org/node/3186368', E_USER_DEPRECATED);
return $this->rowCountEnabled;
}
}
public function __set($name, $value) {
if ($name === 'allowRowCount') {
@trigger_error(__CLASS__ . '::$allowRowCount should not be written in drupal:9.3.0 and will error in drupal:10.0.0. Enable row counting by passing the appropriate argument to the constructor instead. See https://www.drupal.org/node/3186368', E_USER_DEPRECATED);
$this->rowCountEnabled = $value;
}
}
public function __call($method, $arguments) {
if (is_callable([
$this
->getClientStatement(),
$method,
])) {
@trigger_error("StatementWrapper::{$method} should not be called in drupal:9.1.0 and will error in drupal:10.0.0. Access the client-level statement object via ::getClientStatement(). See https://www.drupal.org/node/3177488", E_USER_DEPRECATED);
return call_user_func_array([
$this
->getClientStatement(),
$method,
], $arguments);
}
throw new \BadMethodCallException($method);
}
public function getClientStatement() {
return $this->clientStatement;
}
public function getConnectionTarget() : string {
return $this->connection
->getTarget();
}
public function execute($args = [], $options = []) {
if (isset($options['fetch'])) {
if (is_string($options['fetch'])) {
$this
->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, $options['fetch']);
}
else {
$this
->setFetchMode($options['fetch']);
}
}
$logger = $this->connection
->getLogger();
if (!empty($logger)) {
$query_start = microtime(TRUE);
}
$return = $this->clientStatement
->execute($args);
if (!empty($logger)) {
$query_end = microtime(TRUE);
$logger
->log($this, $args, $query_end - $query_start, $query_start);
}
return $return;
}
public function getQueryString() {
return $this->clientStatement->queryString;
}
public function fetchCol($index = 0) {
return $this
->fetchAll(\PDO::FETCH_COLUMN, $index);
}
public function fetchAllAssoc($key, $fetch = NULL) {
$return = [];
if (isset($fetch)) {
if (is_string($fetch)) {
$this
->setFetchMode(\PDO::FETCH_CLASS, $fetch);
}
else {
$this
->setFetchMode($fetch);
}
}
foreach ($this as $record) {
$record_key = is_object($record) ? $record->{$key} : $record[$key];
$return[$record_key] = $record;
}
return $return;
}
public function fetchAllKeyed($key_index = 0, $value_index = 1) {
$return = [];
$this
->setFetchMode(\PDO::FETCH_NUM);
foreach ($this as $record) {
$return[$record[$key_index]] = $record[$value_index];
}
return $return;
}
public function fetchField($index = 0) {
return $this->clientStatement
->fetchColumn($index);
}
public function fetchAssoc() {
return $this
->fetch(\PDO::FETCH_ASSOC);
}
public function fetchObject(string $class_name = NULL, array $constructor_arguments = NULL) {
if ($class_name) {
return $this->clientStatement
->fetchObject($class_name, $constructor_arguments);
}
return $this->clientStatement
->fetchObject();
}
public function rowCount() {
if ($this->rowCountEnabled) {
return $this->clientStatement
->rowCount();
}
else {
throw new RowCountException();
}
}
public function setFetchMode($mode, $a1 = NULL, $a2 = []) {
switch (func_num_args()) {
case 1:
return $this->clientStatement
->setFetchMode($mode);
case 2:
return $this->clientStatement
->setFetchMode($mode, $a1);
case 3:
default:
return $this->clientStatement
->setFetchMode($mode, $a1, $a2);
}
}
public function fetch($mode = NULL, $cursor_orientation = NULL, $cursor_offset = NULL) {
switch (func_num_args()) {
case 0:
return $this->clientStatement
->fetch();
case 1:
return $this->clientStatement
->fetch($mode);
case 2:
return $this->clientStatement
->fetch($mode, $cursor_orientation);
case 3:
default:
return $this->clientStatement
->fetch($mode, $cursor_orientation, $cursor_offset);
}
}
public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL) {
switch (func_num_args()) {
case 0:
return $this->clientStatement
->fetchAll();
case 1:
return $this->clientStatement
->fetchAll($mode);
case 2:
return $this->clientStatement
->fetchAll($mode, $column_index);
case 3:
default:
return $this->clientStatement
->fetchAll($mode, $column_index, $constructor_arguments);
}
}
public function getIterator() {
return new \ArrayIterator($this
->fetchAll());
}
public function bindColumn($column, &$param, int $type = 0, int $maxlen = 0, $driverdata = NULL) : bool {
@trigger_error("StatementWrapper::bindColumn should not be called in drupal:9.1.0 and will error in drupal:10.0.0. Access the client-level statement object via ::getClientStatement(). See https://www.drupal.org/node/3177488", E_USER_DEPRECATED);
switch (func_num_args()) {
case 2:
return $this->clientStatement
->bindColumn($column, $param);
case 3:
return $this->clientStatement
->bindColumn($column, $param, $type);
case 4:
return $this->clientStatement
->bindColumn($column, $param, $type, $maxlen);
case 5:
return $this->clientStatement
->bindColumn($column, $param, $type, $maxlen, $driverdata);
}
}
public function bindParam($parameter, &$variable, int $data_type = \PDO::PARAM_STR, int $length = 0, $driver_options = NULL) : bool {
@trigger_error("StatementWrapper::bindParam should not be called in drupal:9.1.0 and will error in drupal:10.0.0. Access the client-level statement object via ::getClientStatement(). See https://www.drupal.org/node/3177488", E_USER_DEPRECATED);
switch (func_num_args()) {
case 2:
return $this->clientStatement
->bindParam($parameter, $variable);
case 3:
return $this->clientStatement
->bindParam($parameter, $variable, $data_type);
case 4:
return $this->clientStatement
->bindParam($parameter, $variable, $data_type, $length);
case 5:
return $this->clientStatement
->bindParam($parameter, $variable, $data_type, $length, $driver_options);
}
}
}