Skip to content

Commit

Permalink
use mysqli::use_result()
Browse files Browse the repository at this point in the history
  • Loading branch information
shen2 committed May 29, 2015
1 parent f208f47 commit f32a4b3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ public function isConnected()
return $this->_isConnected;
}

public function newStatement($sql){
return $this->_waitingQueue[] = new Statement($this, $sql);
public function newStatement($sql, $storeResult = true){
return $this->_waitingQueue[] = new Statement($this, $sql, $storeResult);
}

/**
Expand All @@ -546,7 +546,7 @@ public function flushQueue($untilStatement = null){
while($this->more_results()){
$this->next_result();
$statement = array_shift($this->_fetchingQueue);
$statement->setResult($this->store_result());
$statement->setResult($statement->getStoreResult() ? $this->store_result() : $this->use_result());

if ($this->errno)
throw new AdapterException($this->error, $this->errno);
Expand All @@ -570,7 +570,7 @@ public function queryStatement($statement){
$sql .= ";\n" . implode(";\n", $this->_waitingQueue);

$this->multi_query($sql);
$statement->setResult($this->store_result());
$statement->setResult($statement->getStoreResult() ? $this->store_result() : $this->use_result());

$this->_fetchingQueue = $this->_waitingQueue;

Expand Down
25 changes: 13 additions & 12 deletions src/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ public function __call($method, array $args)
* @return Statement
*/
public function dispatch(){
return $this->_adapter->newStatement($this);
return $this->_adapter->newStatement($this);
}

/**
Expand Down Expand Up @@ -1195,7 +1195,7 @@ public function yieldClass($class, $ctor_args = array()){
*/
public function fetchAll()
{
$stmt = $this->_adapter->newStatement($this);
$stmt = $this->_adapter->newStatement($this, false);
if (isset($this->_table)){
return $stmt->getDataObjectArray($this->_table, $this->isReadOnly());
}
Expand All @@ -1217,7 +1217,7 @@ public function fetchAll()
*/
public function fetchAssoc()
{
return $this->_adapter->newStatement($this)->getAssocArray();
return $this->_adapter->newStatement($this, false)->getAssocArray();
}

/**
Expand All @@ -1227,7 +1227,7 @@ public function fetchAssoc()
*/
public function fetchCol()
{
return $this->_adapter->newStatement($this)->getColumnArray(0);
return $this->_adapter->newStatement($this, false)->getColumnArray(0);
}

/**
Expand All @@ -1238,7 +1238,7 @@ public function fetchCol()
*/
public function fetchField($name)
{
return $this->_adapter->newStatement($this)->getFieldArray($name);
return $this->_adapter->newStatement($this, false)->getFieldArray($name);
}

/**
Expand All @@ -1251,7 +1251,7 @@ public function fetchField($name)
*/
public function fetchPairs()
{
return $this->_adapter->newStatement($this)->getKeyPairArray();
return $this->_adapter->newStatement($this, false)->getKeyPairArray();
}

/**
Expand All @@ -1260,7 +1260,7 @@ public function fetchPairs()
*/
public function fetchAssocMap()
{
return $this->_adapter->newStatement($this)->getAssocMapArray();
return $this->_adapter->newStatement($this, false)->getAssocMapArray();
}

/**
Expand All @@ -1269,7 +1269,7 @@ public function fetchAssocMap()
* @return array
*/
public function fetchFunc($func){
return $this->_adapter->newStatement($this)->getFuncArray($func);
return $this->_adapter->newStatement($this, false)->getFuncArray($func);
}

/**
Expand All @@ -1279,7 +1279,7 @@ public function fetchFunc($func){
* @return array
*/
public function fetchClass($class, $ctor_args = array()){
return $this->_adapter->newStatement($this)->getObjectArray($class, $ctor_args);
return $this->_adapter->newStatement($this, false)->getObjectArray($class, $ctor_args);
}

/**
Expand All @@ -1291,9 +1291,9 @@ public function fetchClass($class, $ctor_args = array()){
public function fetchRow()
{
$this->_parts[self::LIMIT_COUNT] = 1;
$result = $this->_adapter->newStatement($this)->getResult();
$result = $this->_adapter->newStatement($this, false)->getResult();
$data = $result->fetch_assoc();

$result->close();
if (isset($this->_table) && $data !== null){
$rowClass = $this->_table;
return new $rowClass($data, true, $this->isReadOnly());
Expand All @@ -1309,8 +1309,9 @@ public function fetchRow()
*/
public function fetchOne()
{
$result = $this->_adapter->newStatement($this)->getResult();
$result = $this->_adapter->newStatement($this, false)->getResult();
$row = $result->fetch_row();
$result->close();
return $row === null ? null : $row[0];
}

Expand Down
68 changes: 45 additions & 23 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ class Statement implements \IteratorAggregate, \Countable

protected $_result = null;

/**
*
* @var bool
*/
protected $_storeResult;

/**
* 构造函数
*
* @param $connection
* @param $select
* @param $fetchMode
* @param $fetchArgument
* @param $ctorArgs
* @param $storeResult
*/
public function __construct($connection, $select){
public function __construct($connection, $select, $storeResult = true){
$this->_connection = $connection;
$this->_select = $select;
$this->_storeResult = $storeResult;
}

public function __toString(){
Expand All @@ -52,6 +57,10 @@ public function setResult($result){
return $this;
}

public function getStoreResult(){
return $this->_storeResult;
}

/**
* @return \Generator
*/
Expand All @@ -65,10 +74,11 @@ public function getIterator(){
public function getAssocGenerator(){
if (!isset($this->_result)) $this->_query();

$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($row = $this->_result->fetch_assoc()){
yield $row;
}
if (!$this->_storeResult) $this->_result->free();
}

/**
Expand All @@ -77,11 +87,12 @@ public function getAssocGenerator(){
public function getAssocMapGenerator(){
if (!isset($this->_result)) $this->_query();

$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_assoc()){
$key = current($data);
yield $key => $data;
}
if (!$this->_storeResult) $this->_result->free();
}

/**
Expand All @@ -91,10 +102,11 @@ public function getAssocMapGenerator(){
public function getColumnGenerator($colno = 0){
if (!isset($this->_result)) $this->_query();

$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_row()){
yield $data[$colno];
}
if (!$this->_storeResult) $this->_result->free();
}

/**
Expand All @@ -105,10 +117,11 @@ public function getColumnGenerator($colno = 0){
public function getFieldGenerator($name){
if (!isset($this->_result)) $this->_query();

$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_assoc()){
yield $data[$name];
}
if (!$this->_storeResult) $this->_result->free();
}

/**
Expand All @@ -117,10 +130,11 @@ public function getFieldGenerator($name){
public function getDataObjectGenerator($rowClass, $readOnly = false){
if (!isset($this->_result)) $this->_query();

$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_assoc()){
yield new $rowClass($data, true, $readOnly);
}
if (!$this->_storeResult) $this->_result->free();
}

/**
Expand All @@ -129,10 +143,11 @@ public function getDataObjectGenerator($rowClass, $readOnly = false){
public function getFuncGenerator($func){
if (!isset($this->_result)) $this->_query();

$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_assoc()){
yield $func($data);
}
if (!$this->_storeResult) $this->_result->free();
}

/**
Expand All @@ -141,10 +156,11 @@ public function getFuncGenerator($func){
public function getKeyPairGenerator(){
if (!isset($this->_result)) $this->_query();

$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_row()){
yield $data[0] => $data[1];
}
if (!$this->_storeResult) $this->_result->free();
}

/**
Expand All @@ -153,10 +169,11 @@ public function getKeyPairGenerator(){
public function getObjectGenerator($rowClass, $params){
if (!isset($this->_result)) $this->_query();

$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($row = $this->_result->fetch_object($rowClass, $params)){
yield $row;
}
if (!$this->_storeResult) $this->_result->free();
}

public function assemble(){
Expand Down Expand Up @@ -187,11 +204,12 @@ public function getAssocArray(){
if (!isset($this->_result)) $this->_query();

$rowset = [];
$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);

while($data = $this->_result->fetch_assoc()){
$rowset[] = $data;
}
if (!$this->_storeResult) $this->_result->free();
return $rowset;
}

Expand All @@ -202,11 +220,12 @@ public function getAssocMapArray(){
if (!isset($this->_result)) $this->_query();

$rowset = [];
$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_assoc()){
$key = current($data);
$rowset[$key] = $data;
}
if (!$this->_storeResult) $this->_result->free();
return $rowset;
}

Expand All @@ -218,11 +237,11 @@ public function getColumnArray($colno = 0){

$rowset = [];
$index = 0;
$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_row()){
$rowset[$index++] = $data[$colno];
}

if (!$this->_storeResult) $this->_result->free();
return $rowset;
}

Expand All @@ -234,11 +253,11 @@ public function getFieldArray($name){

$rowset = [];
$index = 0;
$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_assoc()){
$rowset[$index++] = $data[$name];
}

if (!$this->_storeResult) $this->_result->free();
return $rowset;
}

Expand All @@ -249,12 +268,12 @@ public function getDataObjectArray($rowClass, $readOnly = false){
if (!isset($this->_result)) $this->_query();

$rowset = [];
$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);

while($data = $this->_result->fetch_assoc()){
$rowset[] = new $rowClass($data, true, $readOnly);
}

if (!$this->_storeResult) $this->_result->free();
return $rowset;
}

Expand All @@ -265,10 +284,11 @@ public function getFuncArray($func){
if (!isset($this->_result)) $this->_query();

$rowset = [];
$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_assoc()){
$rowset[] = $func($data);
}
if (!$this->_storeResult) $this->_result->free();
return $rowset;
}

Expand All @@ -279,10 +299,11 @@ public function getKeyPairArray(){
if (!isset($this->_result)) $this->_query();

$rowset = [];
$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($data = $this->_result->fetch_row()){
$rowset[$data[0]] = $data[1];
}
if (!$this->_storeResult) $this->_result->free();
return $rowset;
}

Expand All @@ -293,10 +314,11 @@ public function getObjectArray($rowClass, $params){
if (!isset($this->_result)) $this->_query();

$rowset = [];
$this->_result->data_seek(0);
if ($this->_storeResult) $this->_result->data_seek(0);
while($row = $this->_result->fetch_object($rowClass, $params)){
$rowset[] = $row;
}
if (!$this->_storeResult) $this->_result->free();
return $rowset;
}

Expand Down

0 comments on commit f32a4b3

Please sign in to comment.