View source
<?php
namespace Drupal\Core\Database;
class StatementPrefetch implements \Iterator, StatementInterface {
protected $queryString;
protected $driverOptions;
protected $connection;
protected $pdoConnection;
protected $data = [];
protected $currentRow = NULL;
protected $currentKey = NULL;
protected $columnNames = NULL;
protected $rowCount = NULL;
protected $resultRowCount = 0;
protected $fetchStyle = \PDO::FETCH_OBJ;
protected $fetchOptions = [
'class' => 'stdClass',
'constructor_args' => [],
'object' => NULL,
'column' => 0,
];
protected $defaultFetchStyle = \PDO::FETCH_OBJ;
protected $defaultFetchOptions = [
'class' => 'stdClass',
'constructor_args' => [],
'object' => NULL,
'column' => 0,
];
protected $rowCountEnabled = FALSE;
public function __construct(\PDO $pdo_connection, Connection $connection, $query, array $driver_options = [], bool $row_count_enabled = FALSE) {
$this->pdoConnection = $pdo_connection;
$this->connection = $connection;
$this->queryString = $query;
$this->driverOptions = $driver_options;
$this->rowCountEnabled = $row_count_enabled;
}
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, $options['fetch']);
}
else {
$this
->setFetchMode($options['fetch']);
}
}
$logger = $this->connection
->getLogger();
if (!empty($logger)) {
$query_start = microtime(TRUE);
}
$statement = $this
->getStatement($this->queryString, $args);
if (!$statement) {
$this
->throwPDOException();
}
$return = $statement
->execute($args);
if (!$return) {
$this
->throwPDOException();
}
if ($this->rowCountEnabled) {
$this->rowCount = $statement
->rowCount();
}
$this->data = $statement
->fetchAll(\PDO::FETCH_ASSOC);
unset($statement);
$this->resultRowCount = count($this->data);
if ($this->resultRowCount) {
$this->columnNames = array_keys($this->data[0]);
}
else {
$this->columnNames = [];
}
if (!empty($logger)) {
$query_end = microtime(TRUE);
$logger
->log($this, $args, $query_end - $query_start, $query_start);
}
$this
->next();
return $return;
}
protected function throwPDOException() {
$error_info = $this->connection
->errorInfo();
$exception = new \PDOException("SQLSTATE[" . $error_info[0] . "]: General error " . $error_info[1] . ": " . $error_info[2]);
$exception->errorInfo = $error_info;
throw $exception;
}
protected function getStatement($query, &$args = []) {
return $this->connection
->prepare($query, $this->driverOptions);
}
public function getQueryString() {
return $this->queryString;
}
public function setFetchMode($mode, $a1 = NULL, $a2 = []) {
$this->defaultFetchStyle = $mode;
switch ($mode) {
case \PDO::FETCH_CLASS:
$this->defaultFetchOptions['class'] = $a1;
if ($a2) {
$this->defaultFetchOptions['constructor_args'] = $a2;
}
break;
case \PDO::FETCH_COLUMN:
$this->defaultFetchOptions['column'] = $a1;
break;
case \PDO::FETCH_INTO:
$this->defaultFetchOptions['object'] = $a1;
break;
}
$this->fetchStyle = $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
}
public function current() {
if (isset($this->currentRow)) {
switch ($this->fetchStyle) {
case \PDO::FETCH_ASSOC:
return $this->currentRow;
case \PDO::FETCH_BOTH:
return $this->currentRow + array_values($this->currentRow);
case \PDO::FETCH_NUM:
return array_values($this->currentRow);
case \PDO::FETCH_LAZY:
case \PDO::FETCH_OBJ:
return (object) $this->currentRow;
case \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE:
$class_name = array_shift($this->currentRow);
case \PDO::FETCH_CLASS:
if (!isset($class_name)) {
$class_name = $this->fetchOptions['class'];
}
if (count($this->fetchOptions['constructor_args'])) {
$reflector = new \ReflectionClass($class_name);
$result = $reflector
->newInstanceArgs($this->fetchOptions['constructor_args']);
}
else {
$result = new $class_name();
}
foreach ($this->currentRow as $k => $v) {
$result->{$k} = $v;
}
return $result;
case \PDO::FETCH_INTO:
foreach ($this->currentRow as $k => $v) {
$this->fetchOptions['object']->{$k} = $v;
}
return $this->fetchOptions['object'];
case \PDO::FETCH_COLUMN:
if (isset($this->columnNames[$this->fetchOptions['column']])) {
return $this->currentRow[$this->columnNames[$this->fetchOptions['column']]];
}
else {
return;
}
}
}
}
public function key() {
return $this->currentKey;
}
public function rewind() {
}
public function next() {
if (!empty($this->data)) {
$this->currentRow = reset($this->data);
$this->currentKey = key($this->data);
unset($this->data[$this->currentKey]);
}
else {
$this->currentRow = NULL;
}
}
public function valid() {
return isset($this->currentRow);
}
public function rowCount() {
if ($this->rowCountEnabled) {
return $this->rowCount;
}
else {
throw new RowCountException();
}
}
public function fetch($fetch_style = NULL, $cursor_orientation = \PDO::FETCH_ORI_NEXT, $cursor_offset = NULL) {
if (isset($this->currentRow)) {
$this->fetchStyle = $fetch_style ?? $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
$return = $this
->current();
$this
->next();
$this->fetchStyle = $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
return $return;
}
else {
return FALSE;
}
}
public function fetchColumn($index = 0) {
if (isset($this->currentRow) && isset($this->columnNames[$index])) {
$return = $this->currentRow[$this->columnNames[$index]];
$this
->next();
return $return;
}
else {
return FALSE;
}
}
public function fetchField($index = 0) {
return $this
->fetchColumn($index);
}
public function fetchObject(string $class_name = NULL, array $constructor_arguments = NULL) {
if (isset($this->currentRow)) {
if (!isset($class_name)) {
$result = (object) $this->currentRow;
}
else {
$this->fetchStyle = \PDO::FETCH_CLASS;
$this->fetchOptions = [
'class' => $class_name,
'constructor_args' => $constructor_arguments,
];
$result = $this
->current();
$this->fetchStyle = $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
}
$this
->next();
return $result;
}
else {
return FALSE;
}
}
public function fetchAssoc() {
if (isset($this->currentRow)) {
$result = $this->currentRow;
$this
->next();
return $result;
}
else {
return FALSE;
}
}
public function fetchAll($mode = NULL, $column_index = NULL, $constructor_arguments = NULL) {
$this->fetchStyle = $mode ?? $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
if (isset($column_index)) {
$this->fetchOptions['column'] = $column_index;
}
if (isset($constructor_arguments)) {
$this->fetchOptions['constructor_args'] = $constructor_arguments;
}
$result = [];
while (isset($this->currentRow)) {
$result[] = $this
->current();
$this
->next();
}
$this->fetchStyle = $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
return $result;
}
public function fetchCol($index = 0) {
if (isset($this->columnNames[$index])) {
$result = [];
while (isset($this->currentRow)) {
$result[] = $this->currentRow[$this->columnNames[$index]];
$this
->next();
}
return $result;
}
else {
return [];
}
}
public function fetchAllKeyed($key_index = 0, $value_index = 1) {
if (!isset($this->columnNames[$key_index]) || !isset($this->columnNames[$value_index])) {
return [];
}
$key = $this->columnNames[$key_index];
$value = $this->columnNames[$value_index];
$result = [];
while (isset($this->currentRow)) {
$result[$this->currentRow[$key]] = $this->currentRow[$value];
$this
->next();
}
return $result;
}
public function fetchAllAssoc($key, $fetch_style = NULL) {
$this->fetchStyle = $fetch_style ?? $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
$result = [];
while (isset($this->currentRow)) {
$result_row = $this
->current();
$result[$this->currentRow[$key]] = $result_row;
$this
->next();
}
$this->fetchStyle = $this->defaultFetchStyle;
$this->fetchOptions = $this->defaultFetchOptions;
return $result;
}
}