3 * A database result class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, UpdateableResult, SeekableIterator {
25 // Exception constants
26 const EXCEPTION_INVALID_DATABASE_RESULT = 0x1c0;
27 const EXCEPTION_RESULT_UPDATE_FAILED = 0x1c1;
30 * Current position in array
32 private $currentPos = -1;
37 private $currentRow = NULL;
42 private $resultArray = array();
45 * Array of out-dated entries
47 private $outDated = array();
52 private $affectedRows = 0;
57 private $foundValue = '';
60 * Protected constructor
64 protected function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
70 * Creates an instance of this result by a provided result array
72 * @param $resultArray The array holding the result from query
73 * @return $resultInstance An instance of this class
75 public static final function createDatabaseResult (array $resultArray) {
77 $resultInstance = new DatabaseResult();
79 // Set the result array
80 $resultInstance->setResultArray($resultArray);
83 $resultInstance->setAffectedRows(count($resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]));
85 // Return the instance
86 return $resultInstance;
90 * Setter for result array
92 * @param $resultArray The array holding the result from query
95 protected final function setResultArray (array $resultArray) {
96 $this->resultArray = $resultArray;
100 * Updates the current entry by given update criteria
102 * @param $updateInstance An instance of an Updateable criteria
105 private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) {
106 // Get the current entry key
107 $entryKey = $this->key();
109 // Now get the update criteria array and update all entries
110 foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
112 $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$entryKey][$criteriaKey] = $criteriaValue;
114 // Mark it as out-dated
115 $this->outDated[$criteriaKey] = 1;
120 * "Iterator" method next() to advance to the next valid entry. This method
121 * does also check if result is invalid
123 * @return $nextValid Whether the next entry is valid
125 public function next () {
126 // Default is not valid
129 // Is the result valid?
130 if ($this->valid()) {
131 // Next entry found, so count one up and cache it
133 $this->currentRow = $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos];
142 * Seeks for to a specified position
144 * @param $index Index to seek for
147 public function seek ($index) {
148 // Rewind to beginning
151 // Search for the entry
152 while (($this->currentPos < $index) && ($this->valid())) {
159 * Gives back the current position or null if not found
161 * @return $current Current element to give back
163 public function current () {
164 // Default is not found
167 // Does the current enty exist?
168 if (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos])) {
170 $current = $this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][$this->currentPos];
178 * Checks if next() and rewind will give a valid result
180 * @return $isValid Whether the next/rewind entry is valid
182 public function valid () {
183 // By default nothing is valid
187 if (($this->ifStatusIsOkay()) && (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][($this->currentPos + 1)])) && (isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS][0]))) {
197 * Returns count of entries
199 * @return $isValid Whether the next/rewind entry is valid
201 public function count () {
203 return count($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_ROWS]);
207 * Determines whether the status of the query was fine (BaseDatabaseBackend::RESULT_OKAY)
209 * @return $ifStatusOkay Whether the status of the query was okay
211 public function ifStatusIsOkay () {
212 return ((isset($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS])) && ($this->resultArray[BaseDatabaseBackend::RESULT_INDEX_STATUS] === BaseDatabaseBackend::RESULT_OKAY));
216 * Gets the current key of iteration
218 * @return $currentPos Key from iterator
220 public function key () {
221 return $this->currentPos;
225 * Rewind to the beginning and clear array $currentRow
229 public function rewind () {
230 $this->currentPos = -1;
231 $this->currentRow = array();
235 * Searches for an entry in data result and returns it
237 * @param $criteriaInstance The criteria to look inside the data set
238 * @return $result Found result entry
241 public function searchEntry (LocalSearchCriteria $criteriaInstance) {
242 $this->debugBackTrace('[' . '[' . __METHOD__ . ':' . __LINE__ . ']: Unfinished!');
246 * Adds an update request to the database result for writing it to the
249 * @param $criteriaInstance An instance of a updateable criteria
251 * @throws ResultUpdateException If no result was updated
253 public function add2UpdateQueue (LocalUpdateCriteria $criteriaInstance) {
254 // Rewind the pointer
257 // Get search criteria
258 $searchInstance = $criteriaInstance->getSearchInstance();
260 // And start looking for the result
262 while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) {
265 $currentEntry = $this->current();
267 // Is this entry found?
268 if ($searchInstance->ifEntryMatches($currentEntry)) {
270 $this->updateCurrentEntryByCriteria($criteriaInstance);
277 // If no entry is found/updated throw an exception
278 if ($foundEntries == 0) {
279 // Throw an exception here
280 throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED);
284 $this->setAffectedRows($foundEntries);
286 // Set update instance
287 $this->setUpdateInstance($criteriaInstance);
291 * Setter for affected rows
293 * @param $rows Number of affected rows
296 public final function setAffectedRows ($rows) {
297 $this->affectedRows = $rows;
301 * Getter for affected rows
303 * @return $rows Number of affected rows
305 public final function getAffectedRows () {
306 return $this->affectedRows;
310 * Getter for found value of previous found() call
312 * @return $foundValue Found value of previous found() call
314 public final function getFoundValue () {
315 return $this->foundValue;
319 * Checks whether we have out-dated entries or not
321 * @return $needsUpdate Whether we have out-dated entries
323 public function ifDataNeedsFlush () {
324 $needsUpdate = (count($this->outDated) > 0);
329 * Adds registration elements to a given dataset instance
331 * @param $criteriaInstance An instance of a StoreableCriteria class
332 * @param $requestInstance An instance of a Requestable class
335 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
336 // Walk only through out-dated columns
337 foreach ($this->outDated as $key => $dummy) {
338 // Does this key exist?
339 //* DEBUG: */ echo "outDated: {$key}<br />\n";
340 if ($this->find($key)) {
342 $criteriaInstance->addCriteria($key, $this->getFoundValue());
348 * Find a key inside the result array
350 * @param $key The key we shall find
351 * @return $found Whether the key was found or not
353 public function find ($key) {
354 // By default nothing is found
357 // Rewind the pointer
360 // Walk through all entries
361 while ($this->valid()) {
362 // Advance to next entry
365 // Get the whole array
366 $currentEntry = $this->current();
368 // Is the element there?
369 if (isset($currentEntry[$key])) {
374 $this->foundValue = $currentEntry[$key];
376 // And stop searching
386 * Solver for result index value with call-back method
388 * @param $databaseColumn Database column where the index might be found
389 * @param $wrapperInstance The wrapper instance to ask for array element
390 * @para $callBack Call-back object for setting the index;
391 * 0=object instance,1=method name
393 * @todo Find a caching way without modifying the result array
395 public function solveResultIndex ($databaseColumn, DatabaseWrapper $wrapperInstance, array $callBack) {
396 // By default nothing is found
399 // Is the element in result itself found?
400 if ($this->find($databaseColumn)) {
402 $indexValue = $this->getFoundValue();
403 } elseif ($this->find($wrapperInstance->getIndexKey())) {
405 $indexValue = $this->getFoundValue();
409 call_user_func_array($callBack, array($indexValue));