PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/exportTypes/Excel/PHPExcel/CachedObjectStorage/SQLite.php

https://gitlab.com/adamlwalker/generatedata
PHP | 270 lines | 111 code | 42 blank | 117 comment | 19 complexity | c5b100c96e347db172693a238e0e6bd5 MD5 | raw file
  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 ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_SQLite
  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_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 Exception
  53. */
  54. private 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 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 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 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 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 the parent worksheet
  105. $this->_currentObject->attach($this->_parent);
  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 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 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 Exception(sqlite_error_string($this->_DBHandle->lastError()));
  145. $this->_currentCellIsDirty = false;
  146. } // function deleteCacheData()
  147. /**
  148. * Get a list of all cell addresses currently held in cache
  149. *
  150. * @return array of string
  151. */
  152. public function getCellList() {
  153. $query = "SELECT id FROM kvp_".$this->_TableName;
  154. $cellIdsResult = $this->_DBHandle->unbufferedQuery($query,SQLITE_ASSOC);
  155. if ($cellIdsResult === false)
  156. throw new Exception(sqlite_error_string($this->_DBHandle->lastError()));
  157. $cellKeys = array();
  158. foreach($cellIdsResult as $row) {
  159. $cellKeys[] = $row['id'];
  160. }
  161. return $cellKeys;
  162. } // function getCellList()
  163. /**
  164. * Clone the cell collection
  165. *
  166. * @param PHPExcel_Worksheet $parent The new worksheet
  167. * @return void
  168. */
  169. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  170. // Get a new id for the new table name
  171. $tableName = str_replace('.','_',$this->_getUniqueID());
  172. if (!$this->_DBHandle->queryExec('CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)
  173. AS SELECT * FROM kvp_'.$this->_TableName))
  174. throw new Exception(sqlite_error_string($this->_DBHandle->lastError()));
  175. // Copy the existing cell cache file
  176. $this->_TableName = $tableName;
  177. } // function copyCellCollection()
  178. /**
  179. * Clear the cell collection and disconnect from our parent
  180. *
  181. * @return void
  182. */
  183. public function unsetWorksheetCells() {
  184. if(!is_null($this->_currentObject)) {
  185. $this->_currentObject->detach();
  186. $this->_currentObject = $this->_currentObjectID = null;
  187. }
  188. // detach ourself from the worksheet, so that it can then delete this object successfully
  189. $this->_parent = null;
  190. // Close down the temporary cache file
  191. $this->__destruct();
  192. } // function unsetWorksheetCells()
  193. /**
  194. * Initialise this new cell collection
  195. *
  196. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  197. */
  198. public function __construct(PHPExcel_Worksheet $parent) {
  199. parent::__construct($parent);
  200. if (is_null($this->_DBHandle)) {
  201. $this->_TableName = str_replace('.','_',$this->_getUniqueID());
  202. $_DBName = ':memory:';
  203. $this->_DBHandle = new SQLiteDatabase($_DBName);
  204. if ($this->_DBHandle === false)
  205. throw new Exception(sqlite_error_string($this->_DBHandle->lastError()));
  206. if (!$this->_DBHandle->queryExec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)'))
  207. throw new Exception(sqlite_error_string($this->_DBHandle->lastError()));
  208. }
  209. } // function __construct()
  210. /**
  211. * Destroy this cell collection
  212. */
  213. public function __destruct() {
  214. $this->_DBHandle = null;
  215. } // function __destruct()
  216. /**
  217. * Identify whether the caching method is currently available
  218. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  219. *
  220. * @return boolean
  221. */
  222. public static function cacheMethodIsAvailable() {
  223. if (!function_exists('sqlite_open')) {
  224. return false;
  225. }
  226. return true;
  227. }
  228. }