class_DatabaseResult.php
Go to the documentation of this file.00001 <?php
00024 class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, UpdateableResult, SeekableIterator {
00025
00026 const EXCEPTION_INVALID_DATABASE_RESULT = 0x1c0;
00027 const EXCEPTION_RESULT_UPDATE_FAILED = 0x1c1;
00028
00032 private $currentPos = -1;
00033
00037 private $currentRow = null;
00038
00042 private $resultArray = array();
00043
00047 private $outDated = array();
00048
00052 private $affectedRows = 0;
00053
00057 private $foundValue = "";
00058
00064 protected function __construct () {
00065
00066 parent::__construct(__CLASS__);
00067
00068
00069 $this->removeNumberFormaters();
00070 $this->removeSystemArray();
00071 }
00072
00079 public final static function createDatabaseResult (array $resultArray) {
00080
00081 $resultInstance = new DatabaseResult();
00082
00083
00084 $resultInstance->setResultArray($resultArray);
00085
00086
00087 return $resultInstance;
00088 }
00089
00096 protected final function setResultArray (array $resultArray) {
00097 $this->resultArray = $resultArray;
00098 }
00099
00106 private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) {
00107
00108 $entryKey = $this->key();
00109
00110
00111 foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) {
00112
00113 $this->resultArray['rows'][$entryKey][$criteriaKey] = $criteriaValue;
00114
00115
00116 $this->outDated[$criteriaKey] = 1;
00117 }
00118 }
00119
00126 public function next () {
00127
00128 $nextValid = false;
00129
00130
00131 if ($this->valid()) {
00132
00133 $this->currentPos++;
00134 $this->currentRow = $this->resultArray['rows'][$this->currentPos];
00135 $nextValid = true;
00136 }
00137
00138
00139 return $nextValid;
00140 }
00141
00148 public function seek ($index) {
00149
00150 $this->rewind();
00151
00152
00153 while ($this->currentPos < $index && ($this->valid())) {
00154
00155 $this->next();
00156 }
00157 }
00158
00164 public function current () {
00165
00166 $current = null;
00167
00168
00169 if (isset($this->resultArray['rows'][$this->currentPos])) {
00170
00171 $current = $this->resultArray['rows'][$this->currentPos];
00172 }
00173
00174
00175 return $current;
00176 }
00177
00183 public function valid () {
00184
00185 $isValid = false;
00186
00187
00188 if (($this->ifStatusIsOkay()) && (isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
00189
00190 $isValid = true;
00191 }
00192
00193
00194 return $isValid;
00195 }
00196
00202 public function ifStatusIsOkay () {
00203 return ((isset($this->resultArray['status'])) && ($this->resultArray['status'] === "ok"));
00204 }
00205
00211 public function key () {
00212 return $this->currentPos;
00213 }
00214
00220 public function rewind () {
00221 $this->currentPos = -1;
00222 $this->currentRow = array();
00223 }
00224
00232 public function searchEntry (LocalSearchCriteria $criteriaInstance) {
00233 die(__METHOD__.": Unfinished!");
00234 }
00235
00244 public function add2UpdateQueue (LocalUpdateCriteria $criteriaInstance) {
00245
00246 $this->rewind();
00247
00248
00249 $searchInstance = $criteriaInstance->getSearchInstance();
00250
00251
00252 $foundEntries = 0;
00253 while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) {
00254
00255 $this->next();
00256 $currentEntry = $this->current();
00257
00258
00259 if ($searchInstance->ifEntryMatches($currentEntry)) {
00260
00261 $this->updateCurrentEntryByCriteria($criteriaInstance);
00262
00263
00264 $foundEntries++;
00265 }
00266 }
00267
00268
00269 $this->setAffectedRows($foundEntries);
00270
00271
00272 if ($foundEntries == 0) {
00273
00274 throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED);
00275 }
00276
00277
00278 $this->setSearchInstance($searchInstance);
00279 }
00280
00287 public final function setAffectedRows ($rows) {
00288 $this->affectedRows = $rows;
00289 }
00290
00296 public final function getAffectedRows () {
00297 return $this->affectedRows;
00298 }
00299
00305 public final function getFoundValue () {
00306 return $this->foundValue;
00307 }
00308
00314 public function ifDataNeedsFlush () {
00315 $needsUpdate = (count($this->outDated) > 0);
00316 return $needsUpdate;
00317 }
00318
00325 public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
00326
00327 foreach ($this->outDated as $key => $dummy) {
00328
00329
00330 if ($this->find($key)) {
00331
00332 $criteriaInstance->addCriteria($key, $this->getFoundValue());
00333 }
00334 }
00335 }
00336
00343 public function find ($key) {
00344
00345 $found = false;
00346
00347
00348 $this->rewind();
00349
00350
00351 while ($this->valid()) {
00352
00353 $this->next();
00354
00355
00356 $currentEntry = $this->current();
00357
00358
00359 if (isset($currentEntry[$key])) {
00360
00361 $found = true;
00362
00363
00364 $this->foundValue = $currentEntry[$key];
00365
00366
00367 break;
00368 }
00369 }
00370
00371
00372 return $found;
00373 }
00374
00385 public function solveResultIndex ($databaseColumn, BaseDatabaseWrapper $wrapperInstance, array $callBack) {
00386
00387 $indexValue = 0;
00388
00389
00390 if ($this->find($databaseColumn)) {
00391
00392 $indexValue = $this->getFoundValue();
00393 } elseif ($this->find($wrapperInstance->getIndexKey())) {
00394
00395 $indexValue = $this->getFoundValue();
00396 }
00397
00398
00399 call_user_func_array($callBack, array($indexValue));
00400 }
00401 }
00402
00403
00404 ?>