3 namespace CoreFramework\Database\Result;
5 // Import framework stuff
6 use CoreFramework\Criteria\Local\LocalSearchCriteria;
7 use CoreFramework\Criteria\Storing\StoreableCriteria;
8 use CoreFramework\Database\Backend\BaseDatabaseBackend;
9 use CoreFramework\Request\Requestable;
12 * A database result class
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResult, UpdateableResult, SeekableIterator {
34 // Exception constants
35 const EXCEPTION_INVALID_DATABASE_RESULT = 0x1c0;
36 const EXCEPTION_RESULT_UPDATE_FAILED = 0x1c1;
39 * Current position in array
41 private $currentPos = -1;
46 private $currentRow = NULL;
51 private $resultArray = array();
54 * Array of out-dated entries
56 private $outDated = array();
61 private $affectedRows = 0;
66 private $foundValue = '';
69 * Protected constructor
73 protected function __construct () {
74 // Call parent constructor
75 parent::__construct(__CLASS__);
79 * Creates an instance of this result by a provided result array
81 * @param $resultArray The array holding the result from query
82 * @return $resultInstance An instance of this class
84 public static final function createCachedDatabaseResult (array $resultArray) {
86 $resultInstance = new CachedDatabaseResult();
88 // Set the result array
89 $resultInstance->setResultArray($resultArray);
92 $resultInstance->setAffectedRows(count($resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]));
94 // Return the instance
95 return $resultInstance;
99 * Setter for result array
101 * @param $resultArray The array holding the result from query
104 protected final function setResultArray (array $resultArray) {
105 $this->resultArray = $resultArray;
109 * Updates the current entry by given update criteria
111 * @param $updateInstance An instance of an Updateable criteria
114 private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) {
115 // Get the current entry key
116 $entryKey = $this->key();
118 // Now get the update criteria array and update all entries
119 foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
121 $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$entryKey][$criteriaKey] = $criteriaValue;
123 // Mark it as out-dated
124 $this->outDated[$criteriaKey] = 1;
129 * "Iterator" method next() to advance to the next valid entry. This method
130 * does also check if result is invalid
132 * @return $nextValid Whether the next entry is valid
134 public function next () {
135 // Default is not valid
138 // Is the result valid?
139 if ($this->valid()) {
140 // Next entry found, so count one up and cache it
142 $this->currentRow = $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos];
151 * Seeks for to a specified position
153 * @param $index Index to seek for
156 public function seek ($index) {
157 // Rewind to beginning
160 // Search for the entry
161 while (($this->currentPos < $index) && ($this->valid())) {
168 * Gives back the current position or null if not found
170 * @return $current Current element to give back
172 public function current () {
173 // Default is not found
176 // Does the current enty exist?
177 if (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos])) {
179 $current = $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos];
187 * Checks if next() and rewind will give a valid result
189 * @return $isValid Whether the next/rewind entry is valid
191 public function valid () {
192 // By default nothing is valid
196 //*NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] this->currentPos=' . $this->currentPos);
198 // Check if all is fine ...
199 if (($this->ifStatusIsOkay()) && (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][($this->currentPos + 1)])) && (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][0]))) {
209 * Returns count of entries
211 * @return $isValid Whether the next/rewind entry is valid
213 public function count () {
215 return count($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]);
219 * Determines whether the status of the query was fine (BaseDatabaseBackend::RESULT_OKAY)
221 * @return $ifStatusOkay Whether the status of the query was okay
223 public function ifStatusIsOkay () {
224 $ifStatusOkay = ((isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS])) && ($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS] === BaseDatabaseBackend::RESULT_OKAY));
225 //*NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] ifStatusOkay=' . intval($ifStatusOkay));
226 return $ifStatusOkay;
230 * Gets the current key of iteration
232 * @return $currentPos Key from iterator
234 public function key () {
235 return $this->currentPos;
239 * Rewind to the beginning and clear array $currentRow
243 public function rewind () {
244 $this->currentPos = -1;
245 $this->currentRow = array();
249 * Searches for an entry in data result and returns it
251 * @param $criteriaInstance The criteria to look inside the data set
252 * @return $result Found result entry
255 public function searchEntry (LocalSearchCriteria $criteriaInstance) {
256 $this->debugBackTrace('[' . '[' . __METHOD__ . ':' . __LINE__ . ']: Unfinished!');
260 * Adds an update request to the database result for writing it to the
263 * @param $criteriaInstance An instance of a updateable criteria
265 * @throws ResultUpdateException If no result was updated
267 public function add2UpdateQueue (LocalUpdateCriteria $criteriaInstance) {
268 // Rewind the pointer
271 // Get search criteria
272 $searchInstance = $criteriaInstance->getSearchInstance();
274 // And start looking for the result
276 while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) {
279 $currentEntry = $this->current();
281 // Is this entry found?
282 if ($searchInstance->ifEntryMatches($currentEntry)) {
284 $this->updateCurrentEntryByCriteria($criteriaInstance);
291 // If no entry is found/updated throw an exception
292 if ($foundEntries == 0) {
293 // Throw an exception here
294 throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED);
298 $this->setAffectedRows($foundEntries);
300 // Set update instance
301 $this->setUpdateInstance($criteriaInstance);
305 * Setter for affected rows
307 * @param $rows Number of affected rows
310 public final function setAffectedRows ($rows) {
311 $this->affectedRows = $rows;
315 * Getter for affected rows
317 * @return $rows Number of affected rows
319 public final function getAffectedRows () {
320 return $this->affectedRows;
324 * Getter for found value of previous found() call
326 * @return $foundValue Found value of previous found() call
328 public final function getFoundValue () {
329 return $this->foundValue;
333 * Checks whether we have out-dated entries or not
335 * @return $needsUpdate Whether we have out-dated entries
337 public function ifDataNeedsFlush () {
338 $needsUpdate = (count($this->outDated) > 0);
343 * Adds registration elements to a given dataset instance
345 * @param $criteriaInstance An instance of a StoreableCriteria class
346 * @param $requestInstance An instance of a Requestable class
349 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
350 // Walk only through out-dated columns
351 foreach ($this->outDated as $key => $dummy) {
352 // Does this key exist?
353 //* DEBUG: */ echo "outDated: {$key}<br />\n";
354 if ($this->find($key)) {
356 $criteriaInstance->addCriteria($key, $this->getFoundValue());
362 * Find a key inside the result array
364 * @param $key The key we shall find
365 * @return $found Whether the key was found or not
367 public function find ($key) {
368 // By default nothing is found
371 // Rewind the pointer
374 // Walk through all entries
375 while ($this->valid()) {
376 // Advance to next entry
379 // Get the whole array
380 $currentEntry = $this->current();
382 // Is the element there?
383 if (isset($currentEntry[$key])) {
388 $this->foundValue = $currentEntry[$key];
390 // And stop searching
400 * Solver for result index value with call-back method
402 * @param $databaseColumn Database column where the index might be found
403 * @param $wrapperInstance The wrapper instance to ask for array element
404 * @para $callBack Call-back object for setting the index;
405 * 0=object instance,1=method name
407 * @todo Find a caching way without modifying the result array
409 public function solveResultIndex ($databaseColumn, DatabaseWrapper $wrapperInstance, array $callBack) {
410 // By default nothing is found
413 // Is the element in result itself found?
414 if ($this->find($databaseColumn)) {
416 $indexValue = $this->getFoundValue();
417 } elseif ($this->find($wrapperInstance->getIndexKey())) {
419 $indexValue = $this->getFoundValue();
423 call_user_func_array($callBack, array($indexValue));