3 * A database result class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 final static function createDatabaseResult (array $resultArray) {
77 $resultInstance = new DatabaseResult();
79 // Set the result array
80 $resultInstance->setResultArray($resultArray);
82 // Return the instance
83 return $resultInstance;
87 * Setter for result array
89 * @param $resultArray The array holding the result from query
92 protected final function setResultArray (array $resultArray) {
93 $this->resultArray = $resultArray;
97 * Updates the current entry by given update criteria
99 * @param $updateInstance An instance of an Updateable criteria
102 private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) {
103 // Get the current entry key
104 $entryKey = $this->key();
106 // Now get the update criteria array and update all entries
107 foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
109 $this->resultArray['rows'][$entryKey][$criteriaKey] = $criteriaValue;
111 // Mark it as out-dated
112 $this->outDated[$criteriaKey] = 1;
117 * "Iterator" method next() to advance to the next valid entry. This method
118 * does also check if result is invalid
120 * @return $nextValid Wether the next entry is valid
122 public function next () {
123 // Default is not valid
126 // Is the result valid?
127 if ($this->valid()) {
128 // Next entry found, so count one up and cache it
130 $this->currentRow = $this->resultArray['rows'][$this->currentPos];
139 * Seeks for to a specified position
141 * @param $index Index to seek for
144 public function seek ($index) {
145 // Rewind to beginning
148 // Search for the entry
149 while ($this->currentPos < $index && ($this->valid())) {
156 * Gives back the current position or null if not found
158 * @return $current Current element to give back
160 public function current () {
161 // Default is not found
164 // Does the current enty exist?
165 if (isset($this->resultArray['rows'][$this->currentPos])) {
167 $current = $this->resultArray['rows'][$this->currentPos];
175 * Checks if next() and rewind will give a valid result
177 * @return $isValid Wether the next/rewind entry is valid
179 public function valid () {
180 // By default nothing is valid
184 if (($this->ifStatusIsOkay()) && (isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
194 * Determines wether the status of the query was fine (LocalfileDatabase::RESULT_OKAY)
196 * @return $ifStatusOkay Wether the status of the query was okay
198 public function ifStatusIsOkay () {
199 return ((isset($this->resultArray['status'])) && ($this->resultArray['status'] === LocalfileDatabase::RESULT_OKAY));
203 * Gets the current key of iteration
205 * @return $currentPos Key from iterator
207 public function key () {
208 return $this->currentPos;
212 * Rewind to the beginning and clear array $currentRow
216 public function rewind () {
217 $this->currentPos = -1;
218 $this->currentRow = array();
222 * Searches for an entry in data result and returns it
224 * @param $criteriaInstance The criteria to look inside the data set
225 * @return $result Found result entry
228 public function searchEntry (LocalSearchCriteria $criteriaInstance) {
229 die(__METHOD__.": Unfinished!");
233 * Adds an update request to the database result for writing it to the
236 * @param $criteriaInstance An instance of a updateable criteria
238 * @throws ResultUpdateException If no result was updated
240 public function add2UpdateQueue (LocalUpdateCriteria $criteriaInstance) {
241 // Rewind the pointer
244 // Get search criteria
245 $searchInstance = $criteriaInstance->getSearchInstance();
247 // And start looking for the result
249 while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) {
252 $currentEntry = $this->current();
254 // Is this entry found?
255 if ($searchInstance->ifEntryMatches($currentEntry)) {
257 $this->updateCurrentEntryByCriteria($criteriaInstance);
264 // If no entry is found/updated throw an exception
265 if ($foundEntries == 0) {
266 // Throw an exception here
267 throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED);
271 $this->setAffectedRows($foundEntries);
273 // Set update instance
274 $this->setUpdateInstance($criteriaInstance);
278 * Setter for affected rows
280 * @param $rows Number of affected rows
283 public final function setAffectedRows ($rows) {
284 $this->affectedRows = $rows;
288 * Getter for affected rows
290 * @return $rows Number of affected rows
292 public final function getAffectedRows () {
293 return $this->affectedRows;
297 * Getter for found value of previous found() call
299 * @return $foundValue Found value of previous found() call
301 public final function getFoundValue () {
302 return $this->foundValue;
306 * Checks wether we have out-dated entries or not
308 * @return $needsUpdate Wether we have out-dated entries
310 public function ifDataNeedsFlush () {
311 $needsUpdate = (count($this->outDated) > 0);
316 * Adds registration elements to a given dataset instance
318 * @param $criteriaInstance An instance of a storeable criteria
321 public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
322 // Walk only through out-dated columns
323 foreach ($this->outDated as $key => $dummy) {
324 // Does this key exist?
325 //* DEBUG: */ echo "outDated: {$key}<br />\n";
326 if ($this->find($key)) {
328 $criteriaInstance->addCriteria($key, $this->getFoundValue());
334 * Find a key inside the result array
336 * @param $key The key we shall find
337 * @return $found Wether the key was found or not
339 public function find ($key) {
340 // By default nothing is found
343 // Rewind the pointer
346 // Walk through all entries
347 while ($this->valid()) {
348 // Advance to next entry
351 // Get the whole array
352 $currentEntry = $this->current();
354 // Is the element there?
355 if (isset($currentEntry[$key])) {
360 $this->foundValue = $currentEntry[$key];
362 // And stop searching
372 * Solver for result index value with call-back method
374 * @param $databaseColumn Database column where the index might be found
375 * @param $wrapperInstance The wrapper instance to ask for array element
376 * @para $callBack Call-back object for setting the index;
377 * 0=object instance,1=method name
379 4 * @todo Find a caching way without modifying the result array
381 public function solveResultIndex ($databaseColumn, BaseDatabaseWrapper $wrapperInstance, array $callBack) {
382 // By default nothing is found
385 // Is the element in result itself found?
386 if ($this->find($databaseColumn)) {
388 $indexValue = $this->getFoundValue();
389 } elseif ($this->find($wrapperInstance->getIndexKey())) {
391 $indexValue = $this->getFoundValue();
395 call_user_func_array($callBack, array($indexValue));