2011-12-03 00:24:44 +00:00
< ? php
/**
* PHPExcel
*
2013-05-14 11:20:28 +00:00
* Copyright ( c ) 2006 - 2013 PHPExcel
2011-12-03 00:24:44 +00:00
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*
* @ category PHPExcel
* @ package PHPExcel_CachedObjectStorage
2013-05-14 11:20:28 +00:00
* @ copyright Copyright ( c ) 2006 - 2013 PHPExcel ( http :// www . codeplex . com / PHPExcel )
2011-12-03 00:24:44 +00:00
* @ license http :// www . gnu . org / licenses / old - licenses / lgpl - 2.1 . txt LGPL
* @ version ##VERSION##, ##DATE##
*/
/**
* PHPExcel_CachedObjectStorage_SQLite
*
* @ category PHPExcel
* @ package PHPExcel_CachedObjectStorage
2013-05-14 11:20:28 +00:00
* @ copyright Copyright ( c ) 2006 - 2013 PHPExcel ( http :// www . codeplex . com / PHPExcel )
2011-12-03 00:24:44 +00:00
*/
class PHPExcel_CachedObjectStorage_SQLite extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
2012-03-21 23:55:54 +00:00
/**
* Database table name
*
* @ var string
*/
2011-12-03 00:24:44 +00:00
private $_TableName = null ;
2012-03-21 23:55:54 +00:00
/**
* Database handle
*
* @ var resource
*/
2011-12-03 00:24:44 +00:00
private $_DBHandle = null ;
2012-03-21 23:55:54 +00:00
/**
* Store cell data in cache for the current cell object if it ' s " dirty " ,
* and the 'nullify' the current cell object
*
* @ return void
2013-01-15 21:42:28 +00:00
* @ throws PHPExcel_Exception
2012-03-21 23:55:54 +00:00
*/
2012-11-29 23:57:12 +00:00
protected function _storeData () {
2011-12-03 00:24:44 +00:00
if ( $this -> _currentCellIsDirty ) {
$this -> _currentObject -> detach ();
2011-12-03 18:49:27 +00:00
if ( ! $this -> _DBHandle -> queryExec ( " INSERT OR REPLACE INTO kvp_ " . $this -> _TableName . " VALUES(' " . $this -> _currentObjectID . " ',' " . sqlite_escape_string ( serialize ( $this -> _currentObject )) . " ') " ))
2013-01-15 21:42:28 +00:00
throw new PHPExcel_Exception ( sqlite_error_string ( $this -> _DBHandle -> lastError ()));
2011-12-03 00:24:44 +00:00
$this -> _currentCellIsDirty = false ;
}
$this -> _currentObjectID = $this -> _currentObject = null ;
} // function _storeData()
/**
2012-03-11 17:58:38 +00:00
* Add or Update a cell in cache identified by coordinate address
2011-12-03 00:24:44 +00:00
*
2012-03-11 17:58:38 +00:00
* @ param string $pCoord Coordinate address of the cell to update
* @ param PHPExcel_Cell $cell Cell to update
* @ return void
2013-01-15 21:42:28 +00:00
* @ throws PHPExcel_Exception
2011-12-03 00:24:44 +00:00
*/
public function addCacheData ( $pCoord , PHPExcel_Cell $cell ) {
if (( $pCoord !== $this -> _currentObjectID ) && ( $this -> _currentObjectID !== null )) {
$this -> _storeData ();
}
$this -> _currentObjectID = $pCoord ;
$this -> _currentObject = $cell ;
$this -> _currentCellIsDirty = true ;
return $cell ;
} // function addCacheData()
/**
* Get cell at a specific coordinate
*
* @ param string $pCoord Coordinate of the cell
2013-01-15 21:42:28 +00:00
* @ throws PHPExcel_Exception
2011-12-03 00:24:44 +00:00
* @ return PHPExcel_Cell Cell that was found , or null if not found
*/
public function getCacheData ( $pCoord ) {
if ( $pCoord === $this -> _currentObjectID ) {
return $this -> _currentObject ;
}
$this -> _storeData ();
$query = " SELECT value FROM kvp_ " . $this -> _TableName . " WHERE id=' " . $pCoord . " ' " ;
$cellResultSet = $this -> _DBHandle -> query ( $query , SQLITE_ASSOC );
2011-12-03 18:49:27 +00:00
if ( $cellResultSet === false ) {
2013-01-15 21:42:28 +00:00
throw new PHPExcel_Exception ( sqlite_error_string ( $this -> _DBHandle -> lastError ()));
2011-12-03 18:49:27 +00:00
} elseif ( $cellResultSet -> numRows () == 0 ) {
2011-12-03 00:24:44 +00:00
// Return null if requested entry doesn't exist in cache
return null ;
}
// Set current entry to the requested entry
$this -> _currentObjectID = $pCoord ;
$cellResult = $cellResultSet -> fetchSingle ();
$this -> _currentObject = unserialize ( $cellResult );
2013-02-21 11:44:33 +00:00
// Re-attach this as the cell's parent
$this -> _currentObject -> attach ( $this );
2011-12-03 00:24:44 +00:00
// Return requested entry
return $this -> _currentObject ;
} // function getCacheData()
/**
2012-03-11 17:58:38 +00:00
* Is a value set for an indexed cell ?
2011-12-03 00:24:44 +00:00
*
2012-03-11 17:58:38 +00:00
* @ param string $pCoord Coordinate address of the cell to check
* @ return boolean
2011-12-03 00:24:44 +00:00
*/
public function isDataSet ( $pCoord ) {
if ( $pCoord === $this -> _currentObjectID ) {
return true ;
}
// Check if the requested entry exists in the cache
$query = " SELECT id FROM kvp_ " . $this -> _TableName . " WHERE id=' " . $pCoord . " ' " ;
$cellResultSet = $this -> _DBHandle -> query ( $query , SQLITE_ASSOC );
2011-12-03 18:49:27 +00:00
if ( $cellResultSet === false ) {
2013-01-15 21:42:28 +00:00
throw new PHPExcel_Exception ( sqlite_error_string ( $this -> _DBHandle -> lastError ()));
2011-12-03 18:49:27 +00:00
} elseif ( $cellResultSet -> numRows () == 0 ) {
2011-12-03 00:24:44 +00:00
// Return null if requested entry doesn't exist in cache
return false ;
}
return true ;
} // function isDataSet()
/**
2012-03-11 17:58:38 +00:00
* Delete a cell in cache identified by coordinate address
2011-12-03 00:24:44 +00:00
*
2012-03-11 17:58:38 +00:00
* @ param string $pCoord Coordinate address of the cell to delete
2013-01-15 21:42:28 +00:00
* @ throws PHPExcel_Exception
2011-12-03 00:24:44 +00:00
*/
public function deleteCacheData ( $pCoord ) {
if ( $pCoord === $this -> _currentObjectID ) {
$this -> _currentObject -> detach ();
$this -> _currentObjectID = $this -> _currentObject = null ;
}
// Check if the requested entry exists in the cache
$query = " DELETE FROM kvp_ " . $this -> _TableName . " WHERE id=' " . $pCoord . " ' " ;
2011-12-03 18:49:27 +00:00
if ( ! $this -> _DBHandle -> queryExec ( $query ))
2013-01-15 21:42:28 +00:00
throw new PHPExcel_Exception ( sqlite_error_string ( $this -> _DBHandle -> lastError ()));
2011-12-03 00:24:44 +00:00
$this -> _currentCellIsDirty = false ;
} // function deleteCacheData()
2013-02-27 23:13:49 +00:00
/**
* Move a cell object from one address to another
*
* @ param string $fromAddress Current address of the cell to move
* @ param string $toAddress Destination address of the cell to move
* @ return boolean
*/
public function moveCell ( $fromAddress , $toAddress ) {
if ( $fromAddress === $this -> _currentObjectID ) {
$this -> _currentObjectID = $toAddress ;
}
$query = " DELETE FROM kvp_ " . $this -> _TableName . " WHERE id=' " . $toAddress . " ' " ;
$result = $this -> _DBHandle -> exec ( $query );
if ( $result === false )
throw new PHPExcel_Exception ( $this -> _DBHandle -> lastErrorMsg ());
$query = " UPDATE kvp_ " . $this -> _TableName . " SET id=' " . $toAddress . " ' WHERE id=' " . $fromAddress . " ' " ;
$result = $this -> _DBHandle -> exec ( $query );
if ( $result === false )
throw new PHPExcel_Exception ( $this -> _DBHandle -> lastErrorMsg ());
return TRUE ;
} // function moveCell()
2011-12-03 00:24:44 +00:00
/**
2012-03-11 17:58:38 +00:00
* Get a list of all cell addresses currently held in cache
2011-12-03 00:24:44 +00:00
*
2012-03-11 17:58:38 +00:00
* @ return array of string
2011-12-03 00:24:44 +00:00
*/
public function getCellList () {
2012-11-29 23:57:12 +00:00
if ( $this -> _currentObjectID !== null ) {
$this -> _storeData ();
}
2011-12-03 00:24:44 +00:00
$query = " SELECT id FROM kvp_ " . $this -> _TableName ;
$cellIdsResult = $this -> _DBHandle -> unbufferedQuery ( $query , SQLITE_ASSOC );
2011-12-03 18:49:27 +00:00
if ( $cellIdsResult === false )
2013-01-15 21:42:28 +00:00
throw new PHPExcel_Exception ( sqlite_error_string ( $this -> _DBHandle -> lastError ()));
2011-12-03 00:24:44 +00:00
$cellKeys = array ();
foreach ( $cellIdsResult as $row ) {
$cellKeys [] = $row [ 'id' ];
}
return $cellKeys ;
} // function getCellList()
/**
2012-03-11 17:58:38 +00:00
* Clone the cell collection
2011-12-03 00:24:44 +00:00
*
2012-03-21 23:55:54 +00:00
* @ param PHPExcel_Worksheet $parent The new worksheet
2012-03-11 17:58:38 +00:00
* @ return void
2011-12-03 00:24:44 +00:00
*/
public function copyCellCollection ( PHPExcel_Worksheet $parent ) {
2012-11-29 23:57:12 +00:00
$this -> _currentCellIsDirty ;
$this -> _storeData ();
2011-12-03 00:24:44 +00:00
// Get a new id for the new table name
$tableName = str_replace ( '.' , '_' , $this -> _getUniqueID ());
2011-12-03 18:49:27 +00:00
if ( ! $this -> _DBHandle -> queryExec ( 'CREATE TABLE kvp_' . $tableName . ' ( id VARCHAR ( 12 ) PRIMARY KEY , value BLOB )
AS SELECT * FROM kvp_ ' . $this -> _TableName ))
2013-01-15 21:42:28 +00:00
throw new PHPExcel_Exception ( sqlite_error_string ( $this -> _DBHandle -> lastError ()));
2011-12-03 00:24:44 +00:00
// Copy the existing cell cache file
$this -> _TableName = $tableName ;
} // function copyCellCollection()
2012-03-21 23:55:54 +00:00
/**
* Clear the cell collection and disconnect from our parent
*
* @ return void
*/
2011-12-03 00:24:44 +00:00
public function unsetWorksheetCells () {
if ( ! is_null ( $this -> _currentObject )) {
$this -> _currentObject -> detach ();
$this -> _currentObject = $this -> _currentObjectID = null ;
}
// detach ourself from the worksheet, so that it can then delete this object successfully
$this -> _parent = null ;
// Close down the temporary cache file
$this -> __destruct ();
} // function unsetWorksheetCells()
2012-03-21 23:55:54 +00:00
/**
* Initialise this new cell collection
*
* @ param PHPExcel_Worksheet $parent The worksheet for this cell collection
*/
2011-12-03 00:24:44 +00:00
public function __construct ( PHPExcel_Worksheet $parent ) {
parent :: __construct ( $parent );
if ( is_null ( $this -> _DBHandle )) {
$this -> _TableName = str_replace ( '.' , '_' , $this -> _getUniqueID ());
$_DBName = ':memory:' ;
$this -> _DBHandle = new SQLiteDatabase ( $_DBName );
2011-12-03 18:49:27 +00:00
if ( $this -> _DBHandle === false )
2013-01-15 21:42:28 +00:00
throw new PHPExcel_Exception ( sqlite_error_string ( $this -> _DBHandle -> lastError ()));
2011-12-03 18:49:27 +00:00
if ( ! $this -> _DBHandle -> queryExec ( 'CREATE TABLE kvp_' . $this -> _TableName . ' (id VARCHAR(12) PRIMARY KEY, value BLOB)' ))
2013-01-15 21:42:28 +00:00
throw new PHPExcel_Exception ( sqlite_error_string ( $this -> _DBHandle -> lastError ()));
2011-12-03 00:24:44 +00:00
}
} // function __construct()
2012-03-21 23:55:54 +00:00
/**
* Destroy this cell collection
*/
2011-12-03 00:24:44 +00:00
public function __destruct () {
2013-03-06 17:19:22 +00:00
if ( ! is_null ( $this -> _DBHandle )) {
$this -> _DBHandle -> queryExec ( 'DROP TABLE kvp_' . $this -> _TableName );
$this -> _DBHandle -> close ();
}
2011-12-03 00:24:44 +00:00
$this -> _DBHandle = null ;
} // function __destruct()
2011-12-09 12:10:46 +00:00
/**
2012-03-11 17:58:38 +00:00
* Identify whether the caching method is currently available
* Some methods are dependent on the availability of certain extensions being enabled in the PHP build
2011-12-09 12:10:46 +00:00
*
2012-03-11 17:58:38 +00:00
* @ return boolean
2011-12-09 12:10:46 +00:00
*/
public static function cacheMethodIsAvailable () {
if ( ! function_exists ( 'sqlite_open' )) {
return false ;
}
return true ;
}
2011-12-03 00:24:44 +00:00
}