SQLite.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_CachedObjectStorage
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_SQLite
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_SQLite extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. /**
  36. * Database table name
  37. *
  38. * @var string
  39. */
  40. private $_TableName = null;
  41. /**
  42. * Database handle
  43. *
  44. * @var resource
  45. */
  46. private $_DBHandle = null;
  47. /**
  48. * Store cell data in cache for the current cell object if it's "dirty",
  49. * and the 'nullify' the current cell object
  50. *
  51. * @return void
  52. * @throws PHPExcel_Exception
  53. */
  54. protected function _storeData() {
  55. if ($this->_currentCellIsDirty) {
  56. $this->_currentObject->detach();
  57. if (!$this->_DBHandle->queryExec("INSERT OR REPLACE INTO kvp_".$this->_TableName." VALUES('".$this->_currentObjectID."','".sqlite_escape_string(serialize($this->_currentObject))."')"))
  58. throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
  59. $this->_currentCellIsDirty = false;
  60. }
  61. $this->_currentObjectID = $this->_currentObject = null;
  62. } // function _storeData()
  63. /**
  64. * Add or Update a cell in cache identified by coordinate address
  65. *
  66. * @param string $pCoord Coordinate address of the cell to update
  67. * @param PHPExcel_Cell $cell Cell to update
  68. * @return void
  69. * @throws PHPExcel_Exception
  70. */
  71. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  72. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  73. $this->_storeData();
  74. }
  75. $this->_currentObjectID = $pCoord;
  76. $this->_currentObject = $cell;
  77. $this->_currentCellIsDirty = true;
  78. return $cell;
  79. } // function addCacheData()
  80. /**
  81. * Get cell at a specific coordinate
  82. *
  83. * @param string $pCoord Coordinate of the cell
  84. * @throws PHPExcel_Exception
  85. * @return PHPExcel_Cell Cell that was found, or null if not found
  86. */
  87. public function getCacheData($pCoord) {
  88. if ($pCoord === $this->_currentObjectID) {
  89. return $this->_currentObject;
  90. }
  91. $this->_storeData();
  92. $query = "SELECT value FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
  93. $cellResultSet = $this->_DBHandle->query($query,SQLITE_ASSOC);
  94. if ($cellResultSet === false) {
  95. throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
  96. } elseif ($cellResultSet->numRows() == 0) {
  97. // Return null if requested entry doesn't exist in cache
  98. return null;
  99. }
  100. // Set current entry to the requested entry
  101. $this->_currentObjectID = $pCoord;
  102. $cellResult = $cellResultSet->fetchSingle();
  103. $this->_currentObject = unserialize($cellResult);
  104. // Re-attach this as the cell's parent
  105. $this->_currentObject->attach($this);
  106. // Return requested entry
  107. return $this->_currentObject;
  108. } // function getCacheData()
  109. /**
  110. * Is a value set for an indexed cell?
  111. *
  112. * @param string $pCoord Coordinate address of the cell to check
  113. * @return boolean
  114. */
  115. public function isDataSet($pCoord) {
  116. if ($pCoord === $this->_currentObjectID) {
  117. return true;
  118. }
  119. // Check if the requested entry exists in the cache
  120. $query = "SELECT id FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
  121. $cellResultSet = $this->_DBHandle->query($query,SQLITE_ASSOC);
  122. if ($cellResultSet === false) {
  123. throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
  124. } elseif ($cellResultSet->numRows() == 0) {
  125. // Return null if requested entry doesn't exist in cache
  126. return false;
  127. }
  128. return true;
  129. } // function isDataSet()
  130. /**
  131. * Delete a cell in cache identified by coordinate address
  132. *
  133. * @param string $pCoord Coordinate address of the cell to delete
  134. * @throws PHPExcel_Exception
  135. */
  136. public function deleteCacheData($pCoord) {
  137. if ($pCoord === $this->_currentObjectID) {
  138. $this->_currentObject->detach();
  139. $this->_currentObjectID = $this->_currentObject = null;
  140. }
  141. // Check if the requested entry exists in the cache
  142. $query = "DELETE FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
  143. if (!$this->_DBHandle->queryExec($query))
  144. throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
  145. $this->_currentCellIsDirty = false;
  146. } // function deleteCacheData()
  147. /**
  148. * Move a cell object from one address to another
  149. *
  150. * @param string $fromAddress Current address of the cell to move
  151. * @param string $toAddress Destination address of the cell to move
  152. * @return boolean
  153. */
  154. public function moveCell($fromAddress, $toAddress) {
  155. if ($fromAddress === $this->_currentObjectID) {
  156. $this->_currentObjectID = $toAddress;
  157. }
  158. $query = "DELETE FROM kvp_".$this->_TableName." WHERE id='".$toAddress."'";
  159. $result = $this->_DBHandle->exec($query);
  160. if ($result === false)
  161. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  162. $query = "UPDATE kvp_".$this->_TableName." SET id='".$toAddress."' WHERE id='".$fromAddress."'";
  163. $result = $this->_DBHandle->exec($query);
  164. if ($result === false)
  165. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  166. return TRUE;
  167. } // function moveCell()
  168. /**
  169. * Get a list of all cell addresses currently held in cache
  170. *
  171. * @return array of string
  172. */
  173. public function getCellList() {
  174. if ($this->_currentObjectID !== null) {
  175. $this->_storeData();
  176. }
  177. $query = "SELECT id FROM kvp_".$this->_TableName;
  178. $cellIdsResult = $this->_DBHandle->unbufferedQuery($query,SQLITE_ASSOC);
  179. if ($cellIdsResult === false)
  180. throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
  181. $cellKeys = array();
  182. foreach($cellIdsResult as $row) {
  183. $cellKeys[] = $row['id'];
  184. }
  185. return $cellKeys;
  186. } // function getCellList()
  187. /**
  188. * Clone the cell collection
  189. *
  190. * @param PHPExcel_Worksheet $parent The new worksheet
  191. * @return void
  192. */
  193. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  194. $this->_currentCellIsDirty;
  195. $this->_storeData();
  196. // Get a new id for the new table name
  197. $tableName = str_replace('.','_',$this->_getUniqueID());
  198. if (!$this->_DBHandle->queryExec('CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)
  199. AS SELECT * FROM kvp_'.$this->_TableName))
  200. throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
  201. // Copy the existing cell cache file
  202. $this->_TableName = $tableName;
  203. } // function copyCellCollection()
  204. /**
  205. * Clear the cell collection and disconnect from our parent
  206. *
  207. * @return void
  208. */
  209. public function unsetWorksheetCells() {
  210. if(!is_null($this->_currentObject)) {
  211. $this->_currentObject->detach();
  212. $this->_currentObject = $this->_currentObjectID = null;
  213. }
  214. // detach ourself from the worksheet, so that it can then delete this object successfully
  215. $this->_parent = null;
  216. // Close down the temporary cache file
  217. $this->__destruct();
  218. } // function unsetWorksheetCells()
  219. /**
  220. * Initialise this new cell collection
  221. *
  222. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  223. */
  224. public function __construct(PHPExcel_Worksheet $parent) {
  225. parent::__construct($parent);
  226. if (is_null($this->_DBHandle)) {
  227. $this->_TableName = str_replace('.','_',$this->_getUniqueID());
  228. $_DBName = ':memory:';
  229. $this->_DBHandle = new SQLiteDatabase($_DBName);
  230. if ($this->_DBHandle === false)
  231. throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
  232. if (!$this->_DBHandle->queryExec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)'))
  233. throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
  234. }
  235. } // function __construct()
  236. /**
  237. * Destroy this cell collection
  238. */
  239. public function __destruct() {
  240. if (!is_null($this->_DBHandle)) {
  241. $this->_DBHandle->queryExec('DROP TABLE kvp_'.$this->_TableName);
  242. }
  243. $this->_DBHandle = null;
  244. } // function __destruct()
  245. /**
  246. * Identify whether the caching method is currently available
  247. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  248. *
  249. * @return boolean
  250. */
  251. public static function cacheMethodIsAvailable() {
  252. if (!function_exists('sqlite_open')) {
  253. return false;
  254. }
  255. return true;
  256. }
  257. }