From f32a4b32c98859567fc56b24a35e1c7f6a61e75f Mon Sep 17 00:00:00 2001 From: shen2 Date: Fri, 29 May 2015 12:19:58 +0800 Subject: [PATCH] use mysqli::use_result() --- src/Adapter.php | 8 +++--- src/Select.php | 25 ++++++++--------- src/Statement.php | 68 +++++++++++++++++++++++++++++++---------------- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/Adapter.php b/src/Adapter.php index 727c03f..d8c1d88 100755 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -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); } /** @@ -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); @@ -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; diff --git a/src/Select.php b/src/Select.php index fa66c55..db18df0 100644 --- a/src/Select.php +++ b/src/Select.php @@ -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); } /** @@ -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()); } @@ -1217,7 +1217,7 @@ public function fetchAll() */ public function fetchAssoc() { - return $this->_adapter->newStatement($this)->getAssocArray(); + return $this->_adapter->newStatement($this, false)->getAssocArray(); } /** @@ -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); } /** @@ -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); } /** @@ -1251,7 +1251,7 @@ public function fetchField($name) */ public function fetchPairs() { - return $this->_adapter->newStatement($this)->getKeyPairArray(); + return $this->_adapter->newStatement($this, false)->getKeyPairArray(); } /** @@ -1260,7 +1260,7 @@ public function fetchPairs() */ public function fetchAssocMap() { - return $this->_adapter->newStatement($this)->getAssocMapArray(); + return $this->_adapter->newStatement($this, false)->getAssocMapArray(); } /** @@ -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); } /** @@ -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); } /** @@ -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()); @@ -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]; } diff --git a/src/Statement.php b/src/Statement.php index 4e53aaf..daf0b4a 100755 --- a/src/Statement.php +++ b/src/Statement.php @@ -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(){ @@ -52,6 +57,10 @@ public function setResult($result){ return $this; } + public function getStoreResult(){ + return $this->_storeResult; + } + /** * @return \Generator */ @@ -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(); } /** @@ -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(); } /** @@ -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(); } /** @@ -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(); } /** @@ -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(); } /** @@ -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(); } /** @@ -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(); } /** @@ -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(){ @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; }