Skip to content

Commit

Permalink
Extract method and rename variable refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude committed Sep 11, 2018
1 parent 48e29f3 commit 988cd79
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class MysqliStatement implements \IteratorAggregate, Statement
* Contains values from bindValue() that need to be sent
* using send_long_data *after* bind_param has been called.
*
* @var mixed[]
* @var string[]|resource[]
*/
protected $_longData = [];

Expand Down Expand Up @@ -189,7 +189,7 @@ public function execute($params = null)
if (!call_user_func_array([$this->_stmt, 'bind_param'], [$this->types] + $this->_bindedValues)) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno);
}
$this->_sendLongData();
$this->_processLongData();
}
}

Expand Down Expand Up @@ -247,26 +247,43 @@ public function execute($params = null)
}

/**
* Send BLOB type streams using mysqli::send_long_data
* Handle $this->_longData after regular query parameters have been bound
*
* @throws MysqliException
*/
private function _sendLongData()
private function _processLongData()
{
foreach ($this->_longData as $paramNr => $resource) {
if (is_resource($resource) && get_resource_type($resource) === 'stream') {
while (! feof($resource)) {
$longData = fread($resource, 8192);
if (! $this->_stmt->send_long_data($paramNr, $longData)) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno);
foreach ($this->_longData as $paramNr => $value) {
if (is_resource($value) && get_resource_type($value) === 'stream') {
$stream = $value;
while (! feof($stream)) {
$chunk = fread($stream, 8192);
if ($chunk === false) {
throw new MysqliException("Failed processing the stream resource for parameter offset ${paramNr}.");
}
$this->_sendLongData($paramNr, $chunk);
}
} else {
if (! $this->_stmt->send_long_data($paramNr, $resource)) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno);
}
$this->_sendLongData($paramNr, $value);
}
}
}

/**
* Bind parameters using send_long_data
*
* @param int $paramNr Parameter offset
* @param string $longData A chunk of data to send
*
* @throws MysqliException
*/
private function _sendLongData($paramNr, $longData): void
{
if (! $this->_stmt->send_long_data($paramNr, $longData)) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno);
}
}

/**
* Binds a array of values to bound parameters.
*
Expand Down

0 comments on commit 988cd79

Please sign in to comment.