PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/application/third_party/PHPExcel/CachedObjectStorage/SQLite3.php

https://gitlab.com/dmsapiens/physicians
PHP | 322 lines | 147 code | 52 blank | 123 comment | 24 complexity | cc00485caa16375e4425d17cc300ecd8 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_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. private $_selectQuery;
  48. private $_insertQuery;
  49. private $_updateQuery;
  50. private $_deleteQuery;
  51. /**
  52. * Store cell data in cache for the current cell object if it's "dirty",
  53. * and the 'nullify' the current cell object
  54. *
  55. * @return void
  56. * @throws PHPExcel_Exception
  57. */
  58. protected function _storeData() {
  59. if ($this->_currentCellIsDirty) {
  60. $this->_currentObject->detach();
  61. $this->_insertQuery->bindValue('id',$this->_currentObjectID,SQLITE3_TEXT);
  62. $this->_insertQuery->bindValue('data',serialize($this->_currentObject),SQLITE3_BLOB);
  63. $result = $this->_insertQuery->execute();
  64. if ($result === false)
  65. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  66. $this->_currentCellIsDirty = false;
  67. }
  68. $this->_currentObjectID = $this->_currentObject = null;
  69. } // function _storeData()
  70. /**
  71. * Add or Update a cell in cache identified by coordinate address
  72. *
  73. * @param string $pCoord Coordinate address of the cell to update
  74. * @param PHPExcel_Cell $cell Cell to update
  75. * @return void
  76. * @throws PHPExcel_Exception
  77. */
  78. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  79. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  80. $this->_storeData();
  81. }
  82. $this->_currentObjectID = $pCoord;
  83. $this->_currentObject = $cell;
  84. $this->_currentCellIsDirty = true;
  85. return $cell;
  86. } // function addCacheData()
  87. /**
  88. * Get cell at a specific coordinate
  89. *
  90. * @param string $pCoord Coordinate of the cell
  91. * @throws PHPExcel_Exception
  92. * @return PHPExcel_Cell Cell that was found, or null if not found
  93. */
  94. public function getCacheData($pCoord) {
  95. if ($pCoord === $this->_currentObjectID) {
  96. return $this->_currentObject;
  97. }
  98. $this->_storeData();
  99. $this->_selectQuery->bindValue('id',$pCoord,SQLITE3_TEXT);
  100. $cellResult = $this->_selectQuery->execute();
  101. if ($cellResult === FALSE) {
  102. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  103. }
  104. $cellData = $cellResult->fetchArray(SQLITE3_ASSOC);
  105. if ($cellData === FALSE) {
  106. // Return null if requested entry doesn't exist in cache
  107. return NULL;
  108. }
  109. // Set current entry to the requested entry
  110. $this->_currentObjectID = $pCoord;
  111. $this->_currentObject = unserialize($cellData['value']);
  112. // Re-attach this as the cell's parent
  113. $this->_currentObject->attach($this);
  114. // Return requested entry
  115. return $this->_currentObject;
  116. } // function getCacheData()
  117. /**
  118. * Is a value set for an indexed cell?
  119. *
  120. * @param string $pCoord Coordinate address of the cell to check
  121. * @return boolean
  122. */
  123. public function isDataSet($pCoord) {
  124. if ($pCoord === $this->_currentObjectID) {
  125. return TRUE;
  126. }
  127. // Check if the requested entry exists in the cache
  128. $this->_selectQuery->bindValue('id',$pCoord,SQLITE3_TEXT);
  129. $cellResult = $this->_selectQuery->execute();
  130. if ($cellResult === FALSE) {
  131. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  132. }
  133. $cellData = $cellResult->fetchArray(SQLITE3_ASSOC);
  134. return ($cellData === FALSE) ? FALSE : TRUE;
  135. } // function isDataSet()
  136. /**
  137. * Delete a cell in cache identified by coordinate address
  138. *
  139. * @param string $pCoord Coordinate address of the cell to delete
  140. * @throws PHPExcel_Exception
  141. */
  142. public function deleteCacheData($pCoord) {
  143. if ($pCoord === $this->_currentObjectID) {
  144. $this->_currentObject->detach();
  145. $this->_currentObjectID = $this->_currentObject = NULL;
  146. }
  147. // Check if the requested entry exists in the cache
  148. $this->_deleteQuery->bindValue('id',$pCoord,SQLITE3_TEXT);
  149. $result = $this->_deleteQuery->execute();
  150. if ($result === FALSE)
  151. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  152. $this->_currentCellIsDirty = FALSE;
  153. } // function deleteCacheData()
  154. /**
  155. * Move a cell object from one address to another
  156. *
  157. * @param string $fromAddress Current address of the cell to move
  158. * @param string $toAddress Destination address of the cell to move
  159. * @return boolean
  160. */
  161. public function moveCell($fromAddress, $toAddress) {
  162. if ($fromAddress === $this->_currentObjectID) {
  163. $this->_currentObjectID = $toAddress;
  164. }
  165. $this->_deleteQuery->bindValue('id',$toAddress,SQLITE3_TEXT);
  166. $result = $this->_deleteQuery->execute();
  167. if ($result === false)
  168. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  169. $this->_updateQuery->bindValue('toid',$toAddress,SQLITE3_TEXT);
  170. $this->_updateQuery->bindValue('fromid',$fromAddress,SQLITE3_TEXT);
  171. $result = $this->_updateQuery->execute();
  172. if ($result === false)
  173. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  174. return TRUE;
  175. } // function moveCell()
  176. /**
  177. * Get a list of all cell addresses currently held in cache
  178. *
  179. * @return array of string
  180. */
  181. public function getCellList() {
  182. if ($this->_currentObjectID !== null) {
  183. $this->_storeData();
  184. }
  185. $query = "SELECT id FROM kvp_".$this->_TableName;
  186. $cellIdsResult = $this->_DBHandle->query($query);
  187. if ($cellIdsResult === false)
  188. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  189. $cellKeys = array();
  190. while ($row = $cellIdsResult->fetchArray(SQLITE3_ASSOC)) {
  191. $cellKeys[] = $row['id'];
  192. }
  193. return $cellKeys;
  194. } // function getCellList()
  195. /**
  196. * Clone the cell collection
  197. *
  198. * @param PHPExcel_Worksheet $parent The new worksheet
  199. * @return void
  200. */
  201. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  202. $this->_currentCellIsDirty;
  203. $this->_storeData();
  204. // Get a new id for the new table name
  205. $tableName = str_replace('.','_',$this->_getUniqueID());
  206. if (!$this->_DBHandle->exec('CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)
  207. AS SELECT * FROM kvp_'.$this->_TableName))
  208. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  209. // Copy the existing cell cache file
  210. $this->_TableName = $tableName;
  211. } // function copyCellCollection()
  212. /**
  213. * Clear the cell collection and disconnect from our parent
  214. *
  215. * @return void
  216. */
  217. public function unsetWorksheetCells() {
  218. if(!is_null($this->_currentObject)) {
  219. $this->_currentObject->detach();
  220. $this->_currentObject = $this->_currentObjectID = null;
  221. }
  222. // detach ourself from the worksheet, so that it can then delete this object successfully
  223. $this->_parent = null;
  224. // Close down the temporary cache file
  225. $this->__destruct();
  226. } // function unsetWorksheetCells()
  227. /**
  228. * Initialise this new cell collection
  229. *
  230. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  231. */
  232. public function __construct(PHPExcel_Worksheet $parent) {
  233. parent::__construct($parent);
  234. if (is_null($this->_DBHandle)) {
  235. $this->_TableName = str_replace('.','_',$this->_getUniqueID());
  236. $_DBName = ':memory:';
  237. $this->_DBHandle = new SQLite3($_DBName);
  238. if ($this->_DBHandle === false)
  239. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  240. if (!$this->_DBHandle->exec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)'))
  241. throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
  242. }
  243. $this->_selectQuery = $this->_DBHandle->prepare("SELECT value FROM kvp_".$this->_TableName." WHERE id = :id");
  244. $this->_insertQuery = $this->_DBHandle->prepare("INSERT OR REPLACE INTO kvp_".$this->_TableName." VALUES(:id,:data)");
  245. $this->_updateQuery = $this->_DBHandle->prepare("UPDATE kvp_".$this->_TableName." SET id=:toId WHERE id=:fromId");
  246. $this->_deleteQuery = $this->_DBHandle->prepare("DELETE FROM kvp_".$this->_TableName." WHERE id = :id");
  247. } // function __construct()
  248. /**
  249. * Destroy this cell collection
  250. */
  251. public function __destruct() {
  252. if (!is_null($this->_DBHandle)) {
  253. $this->_DBHandle->exec('DROP TABLE kvp_'.$this->_TableName);
  254. $this->_DBHandle->close();
  255. }
  256. $this->_DBHandle = null;
  257. } // function __destruct()
  258. /**
  259. * Identify whether the caching method is currently available
  260. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  261. *
  262. * @return boolean
  263. */
  264. public static function cacheMethodIsAvailable() {
  265. if (!class_exists('SQLite3',FALSE)) {
  266. return false;
  267. }
  268. return true;
  269. }
  270. }