3 namespace Org\Mxchange\CoreFramework\Result\Database;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
7 use Org\Mxchange\CoreFramework\Criteria\Local\LocalUpdateCriteria;
8 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
9 use Org\Mxchange\CoreFramework\Database\Frontend\DatabaseWrapper;
10 use Org\Mxchange\CoreFramework\Database\Backend\BaseDatabaseBackend;
11 use Org\Mxchange\CoreFramework\Result\Search\SearchableResult;
12 use Org\Mxchange\CoreFramework\Result\Update\UpdateableResult;
15 use \SeekableIterator;
18 * A database result class
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 <<<<<<< HEAD:framework/main/classes/database/result/class_CachedDatabaseResult.php
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
26 >>>>>>> Some updates::inc/main/classes/database/result/class_CachedDatabaseResult.php
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResult, UpdateableResult, SeekableIterator {
44 // Exception constants
45 const EXCEPTION_INVALID_DATABASE_RESULT = 0x1c0;
46 const EXCEPTION_RESULT_UPDATE_FAILED = 0x1c1;
49 * Current position in array
51 private $currentPos = -1;
56 private $currentRow = NULL;
61 private $resultArray = array();
64 * Array of out-dated entries
66 private $outDated = array();
71 private $affectedRows = 0;
76 private $foundValue = '';
79 * Protected constructor
83 protected function __construct () {
84 // Call parent constructor
85 parent::__construct(__CLASS__);
89 * Creates an instance of this result by a provided result array
91 * @param $resultArray The array holding the result from query
92 * @return $resultInstance An instance of this class
94 public static final function createCachedDatabaseResult (array $resultArray) {
96 $resultInstance = new CachedDatabaseResult();
98 // Set the result array
99 $resultInstance->setResultArray($resultArray);
102 $resultInstance->setAffectedRows(count($resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]));
104 // Return the instance
105 return $resultInstance;
109 * Setter for result array
111 * @param $resultArray The array holding the result from query
114 protected final function setResultArray (array $resultArray) {
115 $this->resultArray = $resultArray;
119 * Updates the current entry by given update criteria
121 * @param $updateInstance An instance of an Updateable criteria
124 private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) {
125 // Get the current entry key
126 $entryKey = $this->key();
128 // Now get the update criteria array and update all entries
129 foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
131 $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$entryKey][$criteriaKey] = $criteriaValue;
133 // Mark it as out-dated
134 $this->outDated[$criteriaKey] = 1;
139 * "Iterator" method next() to advance to the next valid entry. This method
140 * does also check if result is invalid
142 * @return $nextValid Whether the next entry is valid
144 public function next () {
145 // Default is not valid
148 // Is the result valid?
149 if ($this->valid()) {
150 // Next entry found, so count one up and cache it
152 $this->currentRow = $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos];
161 * Seeks for to a specified position
163 * @param $index Index to seek for
166 public function seek ($index) {
167 // Rewind to beginning
170 // Search for the entry
171 while (($this->currentPos < $index) && ($this->valid())) {
178 * Gives back the current position or null if not found
180 * @return $current Current element to give back
182 public function current () {
183 // Default is not found
186 // Does the current enty exist?
187 if (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos])) {
189 $current = $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos];
197 * Checks if next() and rewind will give a valid result
199 * @return $isValid Whether the next/rewind entry is valid
201 public function valid () {
202 // By default nothing is valid
206 //*NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] this->currentPos=' . $this->currentPos);
208 // Check if all is fine ...
209 if (($this->ifStatusIsOkay()) && (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][($this->currentPos + 1)])) && (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][0]))) {
219 * Returns count of entries
221 * @return $isValid Whether the next/rewind entry is valid
223 public function count () {
225 return count($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]);
229 * Determines whether the status of the query was fine (BaseDatabaseBackend::RESULT_OKAY)
231 * @return $ifStatusOkay Whether the status of the query was okay
233 public function ifStatusIsOkay () {
234 $ifStatusOkay = ((isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS])) && ($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS] === BaseDatabaseBackend::RESULT_OKAY));
235 //*NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] ifStatusOkay=' . intval($ifStatusOkay));
236 return $ifStatusOkay;
240 * Gets the current key of iteration
242 * @return $currentPos Key from iterator
244 public function key () {
245 return $this->currentPos;
249 * Rewind to the beginning and clear array $currentRow
253 public function rewind () {
254 $this->currentPos = -1;
255 $this->currentRow = array();
259 * Searches for an entry in data result and returns it
261 * @param $criteriaInstance The criteria to look inside the data set
262 * @return $result Found result entry
265 public function searchEntry (LocalSearchCriteria $criteriaInstance) {
266 $this->debugBackTrace('[' . '[' . __METHOD__ . ':' . __LINE__ . ']: Unfinished!');
270 * Adds an update request to the database result for writing it to the
273 * @param $criteriaInstance An instance of a updateable criteria
275 * @throws ResultUpdateException If no result was updated
277 public function add2UpdateQueue (LocalUpdateCriteria $criteriaInstance) {
278 // Rewind the pointer
281 // Get search criteria
282 $searchInstance = $criteriaInstance->getSearchInstance();
284 // And start looking for the result
286 while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) {
289 $currentEntry = $this->current();
291 // Is this entry found?
292 if ($searchInstance->ifEntryMatches($currentEntry)) {
294 $this->updateCurrentEntryByCriteria($criteriaInstance);
301 // If no entry is found/updated throw an exception
302 if ($foundEntries == 0) {
303 // Throw an exception here
304 throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED);
308 $this->setAffectedRows($foundEntries);
310 // Set update instance
311 $this->setUpdateInstance($criteriaInstance);
315 * Setter for affected rows
317 * @param $rows Number of affected rows
320 public final function setAffectedRows ($rows) {
321 $this->affectedRows = $rows;
325 * Getter for affected rows
327 * @return $rows Number of affected rows
329 public final function getAffectedRows () {
330 return $this->affectedRows;
334 * Getter for found value of previous found() call
336 * @return $foundValue Found value of previous found() call
338 public final function getFoundValue () {
339 return $this->foundValue;
343 * Checks whether we have out-dated entries or not
345 * @return $needsUpdate Whether we have out-dated entries
347 public function ifDataNeedsFlush () {
348 $needsUpdate = (count($this->outDated) > 0);
353 * Adds registration elements to a given dataset instance
355 * @param $criteriaInstance An instance of a StoreableCriteria class
358 public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
359 // Walk only through out-dated columns
360 foreach ($this->outDated as $key => $dummy) {
361 // Does this key exist?
362 //* DEBUG: */ echo "outDated: {$key}<br />\n";
363 if ($this->find($key)) {
365 $criteriaInstance->addCriteria($key, $this->getFoundValue());
371 * Find a key inside the result array
373 * @param $key The key we shall find
374 * @return $found Whether the key was found or not
376 public function find ($key) {
377 // By default nothing is found
380 // Rewind the pointer
383 // Walk through all entries
384 while ($this->valid()) {
385 // Advance to next entry
388 // Get the whole array
389 $currentEntry = $this->current();
391 // Is the element there?
392 if (isset($currentEntry[$key])) {
397 $this->foundValue = $currentEntry[$key];
399 // And stop searching
409 * Solver for result index value with call-back method
411 * @param $databaseColumn Database column where the index might be found
412 * @param $wrapperInstance The wrapper instance to ask for array element
413 * @para $callBack Call-back object for setting the index;
414 * 0=object instance,1=method name
416 * @todo Find a caching way without modifying the result array
418 public function solveResultIndex ($databaseColumn, DatabaseWrapper $wrapperInstance, array $callBack) {
419 // By default nothing is found
422 // Is the element in result itself found?
423 if ($this->find($databaseColumn)) {
425 $indexValue = $this->getFoundValue();
426 } elseif ($this->find($wrapperInstance->getIndexKey())) {
428 $indexValue = $this->getFoundValue();
432 call_user_func_array($callBack, array($indexValue));