Make prepared statements re-usable

This commit is contained in:
Mark Baker 2013-03-06 17:43:12 +00:00
parent 8a97503f17
commit 5940e40362
1 changed files with 24 additions and 17 deletions

View File

@ -49,6 +49,11 @@ class PHPExcel_CachedObjectStorage_SQLite3 extends PHPExcel_CachedObjectStorage_
*/ */
private $_DBHandle = null; private $_DBHandle = null;
private $_selectQuery;
private $_insertQuery;
private $_updateQuery;
private $_deleteQuery;
/** /**
* Store cell data in cache for the current cell object if it's "dirty", * Store cell data in cache for the current cell object if it's "dirty",
* and the 'nullify' the current cell object * and the 'nullify' the current cell object
@ -60,10 +65,9 @@ class PHPExcel_CachedObjectStorage_SQLite3 extends PHPExcel_CachedObjectStorage_
if ($this->_currentCellIsDirty) { if ($this->_currentCellIsDirty) {
$this->_currentObject->detach(); $this->_currentObject->detach();
$query = $this->_DBHandle->prepare("INSERT OR REPLACE INTO kvp_".$this->_TableName." VALUES(:id,:data)"); $this->_insertQuery->bindValue('id',$this->_currentObjectID,SQLITE3_TEXT);
$query->bindValue('id',$this->_currentObjectID,SQLITE3_TEXT); $this->_insertQuery->bindValue('data',serialize($this->_currentObject),SQLITE3_BLOB);
$query->bindValue('data',serialize($this->_currentObject),SQLITE3_BLOB); $result = $this->_insertQuery->execute();
$result = $query->execute();
if ($result === false) if ($result === false)
throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
$this->_currentCellIsDirty = false; $this->_currentCellIsDirty = false;
@ -106,9 +110,8 @@ class PHPExcel_CachedObjectStorage_SQLite3 extends PHPExcel_CachedObjectStorage_
} }
$this->_storeData(); $this->_storeData();
$query = $this->_DBHandle->prepare("SELECT value FROM kvp_".$this->_TableName." WHERE id = :id"); $this->_selectQuery->bindValue('id',$pCoord,SQLITE3_TEXT);
$query->bindValue('id',$pCoord,SQLITE3_TEXT); $cellResult = $this->_selectQuery->execute();
$cellResult = $query->execute();
if ($cellResult === FALSE) { if ($cellResult === FALSE) {
throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
} }
@ -142,9 +145,8 @@ class PHPExcel_CachedObjectStorage_SQLite3 extends PHPExcel_CachedObjectStorage_
} }
// Check if the requested entry exists in the cache // Check if the requested entry exists in the cache
$query = $this->_DBHandle->prepare("SELECT value FROM kvp_".$this->_TableName." WHERE id = :id"); $this->_selectQuery->bindValue('id',$pCoord,SQLITE3_TEXT);
$query->bindValue('id',$pCoord,SQLITE3_TEXT); $cellResult = $this->_selectQuery->execute();
$cellResult = $query->execute();
if ($cellResult === FALSE) { if ($cellResult === FALSE) {
throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
} }
@ -167,9 +169,8 @@ class PHPExcel_CachedObjectStorage_SQLite3 extends PHPExcel_CachedObjectStorage_
} }
// Check if the requested entry exists in the cache // Check if the requested entry exists in the cache
$query = $this->_DBHandle->prepare("DELETE FROM kvp_".$this->_TableName." WHERE id = :id"); $this->_deleteQuery->bindValue('id',$pCoord,SQLITE3_TEXT);
$query->bindValue('id',$pCoord,SQLITE3_TEXT); $result = $this->_deleteQuery->execute();
$result = $query->execute();
if ($result === FALSE) if ($result === FALSE)
throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
@ -189,13 +190,14 @@ class PHPExcel_CachedObjectStorage_SQLite3 extends PHPExcel_CachedObjectStorage_
$this->_currentObjectID = $toAddress; $this->_currentObjectID = $toAddress;
} }
$query = "DELETE FROM kvp_".$this->_TableName." WHERE id='".$toAddress."'"; $this->_deleteQuery->bindValue('id',$toAddress,SQLITE3_TEXT);
$result = $this->_DBHandle->exec($query); $result = $this->_deleteQuery->execute();
if ($result === false) if ($result === false)
throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
$query = "UPDATE kvp_".$this->_TableName." SET id='".$toAddress."' WHERE id='".$fromAddress."'"; $this->_updateQuery->bindValue('toid',$toAddress,SQLITE3_TEXT);
$result = $this->_DBHandle->exec($query); $this->_updateQuery->bindValue('fromid',$fromAddress,SQLITE3_TEXT);
$result = $this->_updateQuery->execute();
if ($result === false) if ($result === false)
throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
@ -283,6 +285,11 @@ class PHPExcel_CachedObjectStorage_SQLite3 extends PHPExcel_CachedObjectStorage_
if (!$this->_DBHandle->exec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)')) if (!$this->_DBHandle->exec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)'))
throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
} }
$this->_selectQuery = $this->_DBHandle->prepare("SELECT value FROM kvp_".$this->_TableName." WHERE id = :id");
$this->_insertQuery = $this->_DBHandle->prepare("INSERT OR REPLACE INTO kvp_".$this->_TableName." VALUES(:id,:data)");
$this->_updateQuery = $this->_DBHandle->prepare("UPDATE kvp_".$this->_TableName." SET id=:toId WHERE id=:fromId");
$this->_deleteQuery = $this->_DBHandle->prepare("DELETE FROM kvp_".$this->_TableName." WHERE id = :id");
} // function __construct() } // function __construct()