3 * A database result class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
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;
55 * Protected constructor
59 protected function __construct () {
60 // Call parent constructor
61 parent::__construct(__CLASS__);
63 // Set part description
64 $this->setObjectDescription("Database result");
66 // Create unique ID number
67 $this->generateUniqueId();
70 $this->removeNumberFormaters();
71 $this->removeSystemArray();
75 * Creates an instance of this result by a provided result array
77 * @param $resultArray The array holding the result from query
78 * @return $resultInstance An instance of this class
80 public final static function createDatabaseResult (array $resultArray) {
82 $resultInstance = new DatabaseResult();
84 // Set the result array
85 $resultInstance->setResultArray($resultArray);
87 // Return the instance
88 return $resultInstance;
92 * Setter for result array
94 * @param $resultArray The array holding the result from query
97 protected final function setResultArray (array $resultArray) {
98 $this->resultArray = $resultArray;
102 * Updates the current entry by given update criteria
104 * @param $updateInstance An instance of an Updateable criteria
107 private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) {
108 // Get the current entry key
109 $entryKey = $this->key();
111 // Now get the update criteria array and update all entries
112 foreach ($updateInstance->getUpdateCriteria() as $criteriaKey=>$criteriaValue) {
114 $this->resultArray['rows'][$entryKey][$criteriaKey] = $criteriaValue;
116 // Mark it as out-dated
117 $this->outDated[$criteriaKey] = 1;
122 * "Iterator" method next() to advance to the next valid entry. This method
123 * does also check if the result is invalid
125 * @return $nextValid Wether the next entry is valid
127 public function next () {
128 // Default is not valid
131 // Is the result valid?
132 if ($this->resultArray['status'] === "ok") {
133 // The status is fine so let's have a look for the next entry
134 if ($this->valid()) {
135 // Next entry found, so count one up and cache it
137 $this->currentRow = $this->resultArray['rows'][$this->currentPos];
147 * Seeks for to a specified position
149 * @param $index Index to seek for
152 public function seek ($index) {
153 // Rewind to beginning
156 // Search for the entry
157 while ($this->currentPos < $index && ($this->valid())) {
164 * Gives back the current position or null if not found
166 * @return $current Current element to give back
168 public function current () {
169 // Default is not found
172 // Does the current enty exist?
173 if (isset($this->resultArray['rows'][$this->currentPos])) {
175 $current = $this->resultArray['rows'][$this->currentPos];
183 * Checks if next() and rewind will give a valid result
185 * @return $isValid Wether the next/rewind entry is valid
187 public function valid () {
188 // By default nothing is valid
192 if ((isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
202 * Gets the current key of iteration
204 * @return $currentPos Key from iterator
206 public function key () {
207 return $this->currentPos;
211 * Rewind to the beginning
215 public function rewind () {
216 $this->currentPos = -1;
220 * Searches for an entry in the data result and returns it
222 * @param $criteriaInstance The criteria to look inside the data set
223 * @return $result Found result entry
225 public function searchEntry (LocalSearchCriteria $criteriaInstance) {
226 die(__METHOD__.": Unfinished!");
230 * Adds an update request to the database result for writing it to the
233 * @param $criteriaInstance An instance of a updateable criteria
235 * @throws ResultUpdateException If no result was updated
237 public function add2UpdateQueue (LocalUpdateCriteria $criteriaInstance) {
238 // Rewind the pointer
241 // Get search criteria
242 $searchInstance = $criteriaInstance->getSearchInstance();
244 // And start looking for the result
246 while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) {
249 $currentEntry = $this->current();
251 // Is this entry found?
252 if ($searchInstance->ifEntryMatches($currentEntry)) {
254 $this->updateCurrentEntryByCriteria($criteriaInstance);
262 $this->setAffectedRows($foundEntries);
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);
270 // Set search instance
271 $this->setSearchInstance($searchInstance);
275 * Setter for affected rows
277 * @param $rows Number of affected rows
280 public final function setAffectedRows ($rows) {
281 $this->affectedRows = $rows;
285 * Getter for affected rows
287 * @return $rows Number of affected rows
289 public final function getAffectedRows () {
290 return $this->affectedRows;
294 * Checks wether we have out-dated entries or not
296 * @return $needsUpdate Wether we have out-dated entries
298 public function ifDataNeedsFlush () {
299 $needsUpdate = (count($this->outDated) > 0);
304 * Adds registration elements to a given dataset instance
306 * @param $criteriaInstance An instance of a storeable criteria
309 public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
310 // Rewind the pointer
313 // Walk through all entries
314 while ($this->valid()) {
317 $currentEntry = $this->current();
319 // Walk only through out-dated columns
320 foreach ($this->outDated as $key=>$dummy) {
321 // Does this key exist?
322 //* DEBUG: */ echo "outDated: {$key}<br />\n";
323 if (isset($currentEntry[$key])) {
325 $criteriaInstance->addCriteria($key, $currentEntry[$key]);