Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored MySQLiStatement::$columnNames #3817

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
use IteratorAggregate;
use mysqli;
use mysqli_stmt;
use stdClass;
use function array_combine;
use function array_fill;
use function array_key_exists;
use function array_map;
use function assert;
use function count;
use function feof;
Expand Down Expand Up @@ -50,8 +52,28 @@ final class MysqliStatement implements IteratorAggregate, Statement
/** @var mysqli_stmt */
private $stmt;

/** @var string[]|false|null */
private $columnNames;
/**
* Whether the statement result metadata has been fetched.
*
* @var bool
*/
private $metadataFetched = false;

/**
* Whether the statement result has columns. The property should be used only after the result metadata
* has been fetched ({@see $metadataFetched}). Otherwise, the property value is undetermined.
*
* @var bool
*/
private $hasColumns = false;

/**
* Mapping of statement result column indexes to their names. The property should be used only
* if the statement result has columns ({@see $hasColumns}). Otherwise, the property value is undetermined.
*
* @var array<int,string>
*/
private $columnNames = [];

/** @var mixed[] */
private $rowBoundValues = [];
Expand Down Expand Up @@ -151,26 +173,27 @@ public function execute(?array $params = null) : void
throw StatementError::new($this->stmt);
}

if ($this->columnNames === null) {
if (! $this->metadataFetched) {
$meta = $this->stmt->result_metadata();
if ($meta !== false) {
$this->hasColumns = true;

$fields = $meta->fetch_fields();
assert(is_array($fields));

$columnNames = [];
foreach ($fields as $col) {
$columnNames[] = $col->name;
}
$this->columnNames = array_map(static function (stdClass $field) : string {
return $field->name;
}, $fields);

$meta->free();

$this->columnNames = $columnNames;
} else {
$this->columnNames = false;
$this->hasColumns = false;
}

$this->metadataFetched = true;
}

if ($this->columnNames !== false) {
if ($this->hasColumns) {
// Store result of every execution which has it. Otherwise it will be impossible
// to execute a new statement in case if the previous one has non-fetched rows
// @link http://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html
Expand Down Expand Up @@ -331,7 +354,6 @@ public function fetch(?int $fetchMode = null, ...$args)
return $values;
}

assert(is_array($this->columnNames));
$assoc = array_combine($this->columnNames, $values);
assert(is_array($assoc));

Expand Down Expand Up @@ -404,11 +426,11 @@ public function closeCursor() : void
*/
public function rowCount() : int
{
if ($this->columnNames === false) {
return $this->stmt->affected_rows;
if ($this->hasColumns) {
return $this->stmt->num_rows;
}

return $this->stmt->num_rows;
return $this->stmt->affected_rows;
}

/**
Expand Down