PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/include/PHPExcel/CachedObjectStorage/SQLite3.php

https://bitbucket.org/sleininger/stock_online
PHP | 277 lines | 118 code | 42 blank | 117 comment | 19 complexity | 4a4cd903c59da6c2e01353aed73fe623 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.7, 2012-05-19
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_SQLite3
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_SQLite3 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 Exception
  53. */
  54. private function _storeData() {
  55. if ($this->_currentCellIsDirty) {
  56. $this->_currentObject->detach();
  57. $query = $this->_DBHandle->prepare("INSERT OR REPLACE INTO kvp_".$this->_TableName." VALUES(:id,:data)");
  58. $query->bindValue('id',$this->_currentObjectID,SQLITE3_TEXT);
  59. $query->bindValue('data',serialize($this->_currentObject),SQLITE3_BLOB);
  60. $result = $query->execute();
  61. if ($result === false)
  62. throw new Exception($this->_DBHandle->lastErrorMsg());
  63. $this->_currentCellIsDirty = false;
  64. }
  65. $this->_currentObjectID = $this->_currentObject = null;
  66. } // function _storeData()
  67. /**
  68. * Add or Update a cell in cache identified by coordinate address
  69. *
  70. * @param string $pCoord Coordinate address of the cell to update
  71. * @param PHPExcel_Cell $cell Cell to update
  72. * @return void
  73. * @throws Exception
  74. */
  75. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  76. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  77. $this->_storeData();
  78. }
  79. $this->_currentObjectID = $pCoord;
  80. $this->_currentObject = $cell;
  81. $this->_currentCellIsDirty = true;
  82. return $cell;
  83. } // function addCacheData()
  84. /**
  85. * Get cell at a specific coordinate
  86. *
  87. * @param string $pCoord Coordinate of the cell
  88. * @throws Exception
  89. * @return PHPExcel_Cell Cell that was found, or null if not found
  90. */
  91. public function getCacheData($pCoord) {
  92. if ($pCoord === $this->_currentObjectID) {
  93. return $this->_currentObject;
  94. }
  95. $this->_storeData();
  96. $query = "SELECT value FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
  97. $cellResult = $this->_DBHandle->querySingle($query);
  98. if ($cellResult === false) {
  99. throw new Exception($this->_DBHandle->lastErrorMsg());
  100. } elseif (is_null($cellResult)) {
  101. // Return null if requested entry doesn't exist in cache
  102. return null;
  103. }
  104. // Set current entry to the requested entry
  105. $this->_currentObjectID = $pCoord;
  106. $this->_currentObject = unserialize($cellResult);
  107. // Re-attach the parent worksheet
  108. $this->_currentObject->attach($this->_parent);
  109. // Return requested entry
  110. return $this->_currentObject;
  111. } // function getCacheData()
  112. /**
  113. * Is a value set for an indexed cell?
  114. *
  115. * @param string $pCoord Coordinate address of the cell to check
  116. * @return boolean
  117. */
  118. public function isDataSet($pCoord) {
  119. if ($pCoord === $this->_currentObjectID) {
  120. return true;
  121. }
  122. // Check if the requested entry exists in the cache
  123. $query = "SELECT id FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
  124. $cellResult = $this->_DBHandle->querySingle($query);
  125. if ($cellResult === false) {
  126. throw new Exception($this->_DBHandle->lastErrorMsg());
  127. } elseif (is_null($cellResult)) {
  128. // Return null if requested entry doesn't exist in cache
  129. return false;
  130. }
  131. return true;
  132. } // function isDataSet()
  133. /**
  134. * Delete a cell in cache identified by coordinate address
  135. *
  136. * @param string $pCoord Coordinate address of the cell to delete
  137. * @throws Exception
  138. */
  139. public function deleteCacheData($pCoord) {
  140. if ($pCoord === $this->_currentObjectID) {
  141. $this->_currentObject->detach();
  142. $this->_currentObjectID = $this->_currentObject = null;
  143. }
  144. // Check if the requested entry exists in the cache
  145. $query = "DELETE FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
  146. $result = $this->_DBHandle->exec($query);
  147. if ($result === false)
  148. throw new Exception($this->_DBHandle->lastErrorMsg());
  149. $this->_currentCellIsDirty = false;
  150. } // function deleteCacheData()
  151. /**
  152. * Get a list of all cell addresses currently held in cache
  153. *
  154. * @return array of string
  155. */
  156. public function getCellList() {
  157. $query = "SELECT id FROM kvp_".$this->_TableName;
  158. $cellIdsResult = $this->_DBHandle->query($query);
  159. if ($cellIdsResult === false)
  160. throw new Exception($this->_DBHandle->lastErrorMsg());
  161. $cellKeys = array();
  162. while ($row = $cellIdsResult->fetchArray(SQLITE3_ASSOC)) {
  163. $cellKeys[] = $row['id'];
  164. }
  165. return $cellKeys;
  166. } // function getCellList()
  167. /**
  168. * Clone the cell collection
  169. *
  170. * @param PHPExcel_Worksheet $parent The new worksheet
  171. * @return void
  172. */
  173. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  174. // Get a new id for the new table name
  175. $tableName = str_replace('.','_',$this->_getUniqueID());
  176. if (!$this->_DBHandle->exec('CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)
  177. AS SELECT * FROM kvp_'.$this->_TableName))
  178. throw new Exception($this->_DBHandle->lastErrorMsg());
  179. // Copy the existing cell cache file
  180. $this->_TableName = $tableName;
  181. } // function copyCellCollection()
  182. /**
  183. * Clear the cell collection and disconnect from our parent
  184. *
  185. * @return void
  186. */
  187. public function unsetWorksheetCells() {
  188. if(!is_null($this->_currentObject)) {
  189. $this->_currentObject->detach();
  190. $this->_currentObject = $this->_currentObjectID = null;
  191. }
  192. // detach ourself from the worksheet, so that it can then delete this object successfully
  193. $this->_parent = null;
  194. // Close down the temporary cache file
  195. $this->__destruct();
  196. } // function unsetWorksheetCells()
  197. /**
  198. * Initialise this new cell collection
  199. *
  200. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  201. */
  202. public function __construct(PHPExcel_Worksheet $parent) {
  203. parent::__construct($parent);
  204. if (is_null($this->_DBHandle)) {
  205. $this->_TableName = str_replace('.','_',$this->_getUniqueID());
  206. $_DBName = ':memory:';
  207. $this->_DBHandle = new SQLite3($_DBName);
  208. if ($this->_DBHandle === false)
  209. throw new Exception($this->_DBHandle->lastErrorMsg());
  210. if (!$this->_DBHandle->exec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)'))
  211. throw new Exception($this->_DBHandle->lastErrorMsg());
  212. }
  213. } // function __construct()
  214. /**
  215. * Destroy this cell collection
  216. */
  217. public function __destruct() {
  218. if (!is_null($this->_DBHandle)) {
  219. $this->_DBHandle->close();
  220. }
  221. $this->_DBHandle = null;
  222. } // function __destruct()
  223. /**
  224. * Identify whether the caching method is currently available
  225. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  226. *
  227. * @return boolean
  228. */
  229. public static function cacheMethodIsAvailable() {
  230. if (!class_exists('SQLite3')) {
  231. return false;
  232. }
  233. return true;
  234. }
  235. }