ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SQLite.php
Go to the documentation of this file.
1 <?php
37 
43  private $_TableName = null;
44 
50  private $_DBHandle = null;
51 
59  protected function _storeData() {
60  if ($this->_currentCellIsDirty && !empty($this->_currentObjectID)) {
61  $this->_currentObject->detach();
62 
63  if (!$this->_DBHandle->queryExec("INSERT OR REPLACE INTO kvp_".$this->_TableName." VALUES('".$this->_currentObjectID."','".sqlite_escape_string(serialize($this->_currentObject))."')"))
64  throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
65  $this->_currentCellIsDirty = false;
66  }
67  $this->_currentObjectID = $this->_currentObject = null;
68  } // function _storeData()
69 
70 
79  public function addCacheData($pCoord, PHPExcel_Cell $cell) {
80  if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
81  $this->_storeData();
82  }
83 
84  $this->_currentObjectID = $pCoord;
85  $this->_currentObject = $cell;
86  $this->_currentCellIsDirty = true;
87 
88  return $cell;
89  } // function addCacheData()
90 
91 
99  public function getCacheData($pCoord) {
100  if ($pCoord === $this->_currentObjectID) {
101  return $this->_currentObject;
102  }
103  $this->_storeData();
104 
105  $query = "SELECT value FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
106  $cellResultSet = $this->_DBHandle->query($query,SQLITE_ASSOC);
107  if ($cellResultSet === false) {
108  throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
109  } elseif ($cellResultSet->numRows() == 0) {
110  // Return null if requested entry doesn't exist in cache
111  return null;
112  }
113 
114  // Set current entry to the requested entry
115  $this->_currentObjectID = $pCoord;
116 
117  $cellResult = $cellResultSet->fetchSingle();
118  $this->_currentObject = unserialize($cellResult);
119  // Re-attach this as the cell's parent
120  $this->_currentObject->attach($this);
121 
122  // Return requested entry
123  return $this->_currentObject;
124  } // function getCacheData()
125 
126 
133  public function isDataSet($pCoord) {
134  if ($pCoord === $this->_currentObjectID) {
135  return true;
136  }
137 
138  // Check if the requested entry exists in the cache
139  $query = "SELECT id FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
140  $cellResultSet = $this->_DBHandle->query($query,SQLITE_ASSOC);
141  if ($cellResultSet === false) {
142  throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
143  } elseif ($cellResultSet->numRows() == 0) {
144  // Return null if requested entry doesn't exist in cache
145  return false;
146  }
147  return true;
148  } // function isDataSet()
149 
150 
157  public function deleteCacheData($pCoord) {
158  if ($pCoord === $this->_currentObjectID) {
159  $this->_currentObject->detach();
160  $this->_currentObjectID = $this->_currentObject = null;
161  }
162 
163  // Check if the requested entry exists in the cache
164  $query = "DELETE FROM kvp_".$this->_TableName." WHERE id='".$pCoord."'";
165  if (!$this->_DBHandle->queryExec($query))
166  throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
167 
168  $this->_currentCellIsDirty = false;
169  } // function deleteCacheData()
170 
171 
179  public function moveCell($fromAddress, $toAddress) {
180  if ($fromAddress === $this->_currentObjectID) {
181  $this->_currentObjectID = $toAddress;
182  }
183 
184  $query = "DELETE FROM kvp_".$this->_TableName." WHERE id='".$toAddress."'";
185  $result = $this->_DBHandle->exec($query);
186  if ($result === false)
187  throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
188 
189  $query = "UPDATE kvp_".$this->_TableName." SET id='".$toAddress."' WHERE id='".$fromAddress."'";
190  $result = $this->_DBHandle->exec($query);
191  if ($result === false)
192  throw new PHPExcel_Exception($this->_DBHandle->lastErrorMsg());
193 
194  return TRUE;
195  } // function moveCell()
196 
197 
203  public function getCellList() {
204  if ($this->_currentObjectID !== null) {
205  $this->_storeData();
206  }
207 
208  $query = "SELECT id FROM kvp_".$this->_TableName;
209  $cellIdsResult = $this->_DBHandle->unbufferedQuery($query,SQLITE_ASSOC);
210  if ($cellIdsResult === false)
211  throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
212 
213  $cellKeys = array();
214  foreach($cellIdsResult as $row) {
215  $cellKeys[] = $row['id'];
216  }
217 
218  return $cellKeys;
219  } // function getCellList()
220 
221 
228  public function copyCellCollection(PHPExcel_Worksheet $parent) {
230  $this->_storeData();
231 
232  // Get a new id for the new table name
233  $tableName = str_replace('.','_',$this->_getUniqueID());
234  if (!$this->_DBHandle->queryExec('CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)
235  AS SELECT * FROM kvp_'.$this->_TableName))
236  throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
237 
238  // Copy the existing cell cache file
239  $this->_TableName = $tableName;
240  } // function copyCellCollection()
241 
242 
248  public function unsetWorksheetCells() {
249  if(!is_null($this->_currentObject)) {
250  $this->_currentObject->detach();
251  $this->_currentObject = $this->_currentObjectID = null;
252  }
253  // detach ourself from the worksheet, so that it can then delete this object successfully
254  $this->_parent = null;
255 
256  // Close down the temporary cache file
257  $this->__destruct();
258  } // function unsetWorksheetCells()
259 
260 
266  public function __construct(PHPExcel_Worksheet $parent) {
267  parent::__construct($parent);
268  if (is_null($this->_DBHandle)) {
269  $this->_TableName = str_replace('.','_',$this->_getUniqueID());
270  $_DBName = ':memory:';
271 
272  $this->_DBHandle = new SQLiteDatabase($_DBName);
273  if ($this->_DBHandle === false)
274  throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
275  if (!$this->_DBHandle->queryExec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)'))
276  throw new PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError()));
277  }
278  } // function __construct()
279 
280 
284  public function __destruct() {
285  if (!is_null($this->_DBHandle)) {
286  $this->_DBHandle->queryExec('DROP TABLE kvp_'.$this->_TableName);
287  }
288  $this->_DBHandle = null;
289  } // function __destruct()
290 
291 
298  public static function cacheMethodIsAvailable() {
299  if (!function_exists('sqlite_open')) {
300  return false;
301  }
302 
303  return true;
304  }
305 
306 }
addCacheData($pCoord, PHPExcel_Cell $cell)
Add or Update a cell in cache identified by coordinate address.
Definition: SQLite.php:79
$result
isDataSet($pCoord)
Is a value set for an indexed cell?
Definition: SQLite.php:133
moveCell($fromAddress, $toAddress)
Move a cell object from one address to another.
Definition: SQLite.php:179
$toAddress
__destruct()
Destroy this cell collection.
Definition: SQLite.php:284
getCellList()
Get a list of all cell addresses currently held in cache.
Definition: SQLite.php:203
_getUniqueID()
Generate a unique ID for cache referencing.
Definition: CacheBase.php:311
$query
__construct(PHPExcel_Worksheet $parent)
Initialise this new cell collection.
Definition: SQLite.php:266
Create styles array
The data for the language used.
unsetWorksheetCells()
Clear the cell collection and disconnect from our parent.
Definition: SQLite.php:248
copyCellCollection(PHPExcel_Worksheet $parent)
Clone the cell collection.
Definition: SQLite.php:228
_storeData()
Store cell data in cache for the current cell object if it&#39;s "dirty", and the &#39;nullify&#39; the current c...
Definition: SQLite.php:59
getCacheData($pCoord)
Get cell at a specific coordinate.
Definition: SQLite.php:99
static cacheMethodIsAvailable()
Identify whether the caching method is currently available Some methods are dependent on the availabi...
Definition: SQLite.php:298
deleteCacheData($pCoord)
Delete a cell in cache identified by coordinate address.
Definition: SQLite.php:157