For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
10 KiB

  1. <?php
  2. /**
  3. * PHPExcel_CachedObjectStorage_SQLite
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. class PHPExcel_CachedObjectStorage_SQLite extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  28. {
  29. /**
  30. * Database table name
  31. *
  32. * @var string
  33. */
  34. private $TableName = null;
  35. /**
  36. * Database handle
  37. *
  38. * @var resource
  39. */
  40. private $DBHandle = null;
  41. /**
  42. * Store cell data in cache for the current cell object if it's "dirty",
  43. * and the 'nullify' the current cell object
  44. *
  45. * @return void
  46. * @throws PHPExcel_Exception
  47. */
  48. protected function storeData()
  49. {
  50. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  51. $this->currentObject->detach();
  52. if (!$this->DBHandle->queryExec("INSERT OR REPLACE INTO kvp_".$this->TableName." VALUES('".$this->currentObjectID."','".sqlite_escape_string(serialize($this->currentObject))."')")) {
  53. throw new PHPExcel_Exception(sqlite_error_string($this->DBHandle->lastError()));
  54. }
  55. $this->currentCellIsDirty = false;
  56. }
  57. $this->currentObjectID = $this->currentObject = null;
  58. }
  59. /**
  60. * Add or Update a cell in cache identified by coordinate address
  61. *
  62. * @param string $pCoord Coordinate address of the cell to update
  63. * @param PHPExcel_Cell $cell Cell to update
  64. * @return PHPExcel_Cell
  65. * @throws PHPExcel_Exception
  66. */
  67. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  68. {
  69. if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  70. $this->storeData();
  71. }
  72. $this->currentObjectID = $pCoord;
  73. $this->currentObject = $cell;
  74. $this->currentCellIsDirty = true;
  75. return $cell;
  76. }
  77. /**
  78. * Get cell at a specific coordinate
  79. *
  80. * @param string $pCoord Coordinate of the cell
  81. * @throws PHPExcel_Exception
  82. * @return PHPExcel_Cell Cell that was found, or null if not found
  83. */
  84. public function getCacheData($pCoord)
  85. {
  86. if ($pCoord === $this->currentObjectID) {
  87. return $this->currentObject;
  88. }
  89. $this->storeData();
  90. $query = "SELECT value FROM kvp_".$this->TableName." WHERE id='".$pCoord."'";
  91. $cellResultSet = $this->DBHandle->query($query, SQLITE_ASSOC);
  92. if ($cellResultSet === false) {
  93. throw new PHPExcel_Exception(sqlite_error_string($this->DBHandle->lastError()));
  94. } elseif ($cellResultSet->numRows() == 0) {
  95. // Return null if requested entry doesn't exist in cache
  96. return null;
  97. }
  98. // Set current entry to the requested entry
  99. $this->currentObjectID = $pCoord;
  100. $cellResult = $cellResultSet->fetchSingle();
  101. $this->currentObject = unserialize($cellResult);
  102. // Re-attach this as the cell's parent
  103. $this->currentObject->attach($this);
  104. // Return requested entry
  105. return $this->currentObject;
  106. }
  107. /**
  108. * Is a value set for an indexed cell?
  109. *
  110. * @param string $pCoord Coordinate address of the cell to check
  111. * @return boolean
  112. */
  113. public function isDataSet($pCoord)
  114. {
  115. if ($pCoord === $this->currentObjectID) {
  116. return true;
  117. }
  118. // Check if the requested entry exists in the cache
  119. $query = "SELECT id FROM kvp_".$this->TableName." WHERE id='".$pCoord."'";
  120. $cellResultSet = $this->DBHandle->query($query, SQLITE_ASSOC);
  121. if ($cellResultSet === false) {
  122. throw new PHPExcel_Exception(sqlite_error_string($this->DBHandle->lastError()));
  123. } elseif ($cellResultSet->numRows() == 0) {
  124. // Return null if requested entry doesn't exist in cache
  125. return false;
  126. }
  127. return true;
  128. }
  129. /**
  130. * Delete a cell in cache identified by coordinate address
  131. *
  132. * @param string $pCoord Coordinate address of the cell to delete
  133. * @throws PHPExcel_Exception
  134. */
  135. public function deleteCacheData($pCoord)
  136. {
  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. }
  146. $this->currentCellIsDirty = false;
  147. }
  148. /**
  149. * Move a cell object from one address to another
  150. *
  151. * @param string $fromAddress Current address of the cell to move
  152. * @param string $toAddress Destination address of the cell to move
  153. * @return boolean
  154. */
  155. public function moveCell($fromAddress, $toAddress)
  156. {
  157. if ($fromAddress === $this->currentObjectID) {
  158. $this->currentObjectID = $toAddress;
  159. }
  160. $query = "DELETE FROM kvp_".$this->TableName." WHERE id='".$toAddress."'";
  161. $result = $this->DBHandle->exec($query);
  162. if ($result === false) {
  163. throw new PHPExcel_Exception($this->DBHandle->lastErrorMsg());
  164. }
  165. $query = "UPDATE kvp_".$this->TableName." SET id='".$toAddress."' WHERE id='".$fromAddress."'";
  166. $result = $this->DBHandle->exec($query);
  167. if ($result === false) {
  168. throw new PHPExcel_Exception($this->DBHandle->lastErrorMsg());
  169. }
  170. return true;
  171. }
  172. /**
  173. * Get a list of all cell addresses currently held in cache
  174. *
  175. * @return string[]
  176. */
  177. public function getCellList()
  178. {
  179. if ($this->currentObjectID !== null) {
  180. $this->storeData();
  181. }
  182. $query = "SELECT id FROM kvp_".$this->TableName;
  183. $cellIdsResult = $this->DBHandle->unbufferedQuery($query, SQLITE_ASSOC);
  184. if ($cellIdsResult === false) {
  185. throw new PHPExcel_Exception(sqlite_error_string($this->DBHandle->lastError()));
  186. }
  187. $cellKeys = array();
  188. foreach ($cellIdsResult as $row) {
  189. $cellKeys[] = $row['id'];
  190. }
  191. return $cellKeys;
  192. }
  193. /**
  194. * Clone the cell collection
  195. *
  196. * @param PHPExcel_Worksheet $parent The new worksheet
  197. * @return void
  198. */
  199. public function copyCellCollection(PHPExcel_Worksheet $parent)
  200. {
  201. $this->currentCellIsDirty;
  202. $this->storeData();
  203. // Get a new id for the new table name
  204. $tableName = str_replace('.', '_', $this->getUniqueID());
  205. if (!$this->DBHandle->queryExec('CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)
  206. AS SELECT * FROM kvp_'.$this->TableName)
  207. ) {
  208. throw new PHPExcel_Exception(sqlite_error_string($this->DBHandle->lastError()));
  209. }
  210. // Copy the existing cell cache file
  211. $this->TableName = $tableName;
  212. }
  213. /**
  214. * Clear the cell collection and disconnect from our parent
  215. *
  216. * @return void
  217. */
  218. public function unsetWorksheetCells()
  219. {
  220. if (!is_null($this->currentObject)) {
  221. $this->currentObject->detach();
  222. $this->currentObject = $this->currentObjectID = null;
  223. }
  224. // detach ourself from the worksheet, so that it can then delete this object successfully
  225. $this->parent = null;
  226. // Close down the temporary cache file
  227. $this->__destruct();
  228. }
  229. /**
  230. * Initialise this new cell collection
  231. *
  232. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  233. */
  234. public function __construct(PHPExcel_Worksheet $parent)
  235. {
  236. parent::__construct($parent);
  237. if (is_null($this->DBHandle)) {
  238. $this->TableName = str_replace('.', '_', $this->getUniqueID());
  239. $_DBName = ':memory:';
  240. $this->DBHandle = new SQLiteDatabase($_DBName);
  241. if ($this->DBHandle === false) {
  242. throw new PHPExcel_Exception(sqlite_error_string($this->DBHandle->lastError()));
  243. }
  244. if (!$this->DBHandle->queryExec('CREATE TABLE kvp_'.$this->TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)')) {
  245. throw new PHPExcel_Exception(sqlite_error_string($this->DBHandle->lastError()));
  246. }
  247. }
  248. }
  249. /**
  250. * Destroy this cell collection
  251. */
  252. public function __destruct()
  253. {
  254. if (!is_null($this->DBHandle)) {
  255. $this->DBHandle->queryExec('DROP TABLE kvp_'.$this->TableName);
  256. }
  257. $this->DBHandle = null;
  258. }
  259. /**
  260. * Identify whether the caching method is currently available
  261. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  262. *
  263. * @return boolean
  264. */
  265. public static function cacheMethodIsAvailable()
  266. {
  267. if (!function_exists('sqlite_open')) {
  268. return false;
  269. }
  270. return true;
  271. }
  272. }