DataSet.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: DataSet.php,v 1.2 2004/05/22 13:38:13 micha Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 * 00021 * This product includes software based on the Village framework, 00022 * http://share.whichever.com/index.php?SCREEN=village. 00023 */ 00024 00025 require_once 'jargon/Record.php'; 00026 include_once 'jargon/DataSetException.php'; 00027 00064 class DataSet /*implements IteratorAggregate*/ 00065 { 00067 function ALL_RECORDS() { return 0; } 00068 00070 var $records; 00071 00073 var $conn; 00074 00076 var $allRecordsRetrieved = false; 00077 00079 var $recordRetrievedCount = 0; 00080 00082 var $lastFetchSize = 0; 00083 00085 var $totalFetchCount = 0; 00086 00088 var $columns; 00089 00091 var $selectSql; 00092 00094 var $keyDef; 00095 00097 var $resultSet; 00098 00100 var $stmt; 00101 00112 function & getIterator() 00113 { 00114 $it =& new DataSetIterator($this); 00115 return $it; 00116 } 00117 00123 function &resultSet() 00124 { 00125 if ($this->resultSet === null) { 00126 return new DataSetException (0, "ResultSet is null."); 00127 } 00128 return $this->resultSet; 00129 } 00130 00136 function allRecordsRetrieved() 00137 { 00138 return $this->allRecordsRetrieved; 00139 } 00140 00146 function setAllRecordsRetrieved($set) 00147 { 00148 $this->allRecordsRetrieved = $set; 00149 } 00150 00158 function &removeRecord(/*Record*/ &$rec) 00159 { 00160 Creole::typeHint($rec, 'Record', 'DataSet', 'removeRecord'); 00161 $loc = array_search($rec, $this->records, true); 00162 00163 if ($loc === false) { 00164 return false; 00165 } 00166 00167 return array_splice($this->records, $loc, 1); 00168 } 00169 00176 function &clearRecords() 00177 { 00178 $this->records = null; 00179 return $this; 00180 } 00181 00187 function &releaseRecords() 00188 { 00189 $this->records = null; 00190 $this->recordRetrievedCount = 0; 00191 $this->lastFetchSize = 0; 00192 $this->setAllRecordsRetrieved(false); 00193 return $this; 00194 } 00195 00202 function close() 00203 { 00204 $this->releaseRecords(); 00205 $this->schema = null; 00206 00207 if ($this->resultSet !== null && !is_a($this,'QueryDataSet')) { 00208 /* no check for return value as it returns void */ 00209 $this->resultSet->close(); 00210 } 00211 00212 $this->resultSet = null; 00213 00214 if ( $this->stmt !== null ) { 00215 $this->stmt->close(); 00216 } 00217 00218 $this->conn = null; 00219 } 00220 00227 function &reset() 00228 { 00229 if (! ($this->resultSet !== null && is_a($this, 'QueryDataSet'))) { 00230 return $this->releaseRecords(); 00231 } else { 00232 return new DataSetException(0, "You cannot call reset() on a QueryDataSet."); 00233 } 00234 } 00235 00241 function &connection() 00242 { 00243 return $this->conn; 00244 } 00245 00251 function &schema() 00252 { 00253 return $this->schema; 00254 } 00255 00266 function & getRecord($pos) 00267 { 00268 if ($this->containsRecord($pos)) { 00269 $rec =& $this->records[$pos]; 00270 if (is_a($this, 'TableDataSet')) { 00271 /* only throws an exception if not TableDataSet */ 00272 $rec->markForUpdate(); 00273 } 00274 $this->recordRetrievedCount++; 00275 return $rec; 00276 } 00277 00278 return false; 00279 } 00280 00292 function & findRecord($pos) 00293 { 00294 if (! $this->containsRecord($pos)) { 00295 return false; 00296 } 00297 00298 return $this->records[$pos]; 00299 } 00300 00307 function containsRecord($pos) 00308 { 00309 return (isset($this->records[$pos])); 00310 } 00311 00325 function fetchRecords($p1 = 0, $p2 = null) 00326 { 00327 if ($p2 !== null) { 00328 $start = $p1; 00329 $max = $p2; 00330 } else { 00331 $start = 0; 00332 $max = $p1; 00333 } 00334 00335 if ($this->lastFetchSize() > 0 && $this->records !== null) { 00336 return new DataSetException("You must call DataSet::clearRecords() before executing DataSet::fetchRecords() again!"); 00337 } 00338 00339 if ($this->stmt === null && $this->resultSet === null) { 00340 $this->stmt =& $this->conn->createStatement(); 00341 $this->stmt->setOffset($start); 00342 $this->stmt->setLimit($max); 00343 // reset, since native limit applied 00344 $start = 0; 00345 $max = 0; 00346 $rs =& $this->stmt->executeQuery($this->selectSql); 00347 00348 if (Creole::isError($rs)) { 00349 return $rs; 00350 } 00351 00352 $this->resultSet =& $rs; 00353 } 00354 00355 if ($this->resultSet !== null) 00356 { 00357 $this->records = array(); 00358 00359 $startCounter = 0; 00360 $fetchCount = 0; 00361 00362 while (! $this->allRecordsRetrieved() ) 00363 { 00364 $e = null; 00365 00366 if (($e = $this->resultSet->next()) === true) 00367 { 00368 if ($startCounter >= $start) { 00369 $this->records[] =& new Record($this); 00370 $fetchCount++; 00371 if ($fetchCount === $max) { // check after because we must fetch at least 1 00372 break; 00373 } 00374 } else { 00375 $startCounter++; 00376 } 00377 } 00378 else if (Creole::isError($e)) { 00379 $this->stmt->close(); 00380 return $e; 00381 } 00382 else { 00383 $this->setAllRecordsRetrieved(true); 00384 break; 00385 } 00386 } 00387 $this->lastFetchSize = $fetchCount; 00388 } 00389 00390 return $this; 00391 } 00392 00398 function lastFetchSize() 00399 { 00400 return $this->lastFetchSize; 00401 } 00402 00408 function & keydef() 00409 { 00410 return $this->keyDef; 00411 } 00412 00416 function __toString() 00417 { 00418 $sb = ""; 00419 for ($i = 0, $size = $this->size(); $i < $size; $i++) { 00420 $sb .= $this->getRecord($i); 00421 } 00422 return $sb; 00423 } 00424 00431 function getSelectSql() 00432 { 00433 trigger_error ( 00434 "DataSet::getSelectSql(): abstract function has to be reimplemented !", 00435 E_USER_ERROR 00436 ); 00437 } 00438 00444 function & getColumns() 00445 { 00446 return $this->columns; 00447 } 00448 00454 function size() 00455 { 00456 if ( $this->records === null ) { 00457 return 0; 00458 } 00459 return count($this->records); 00460 } 00461 } 00462 00463 00478 class DataSetIterator /*implements Iterator*/ 00479 { 00480 var $ds; 00481 var $size; 00482 var $pos; 00483 00487 function DataSetIterator(/*DataSet*/ &$ds) 00488 { 00489 if (! is_a($rec, 'DataSet')) { 00490 trigger_error ( 00491 "DataSetIterator::DataSetIterator(): parameter 1 not of type 'DataSet' !", 00492 E_USER_ERROR 00493 ); 00494 } 00495 00496 $this->ds =& $ds; 00497 $this->size = $ds->size(); 00498 } 00499 00500 function rewind() 00501 { 00502 $this->pos = 0; 00503 } 00504 00505 function valid() 00506 { 00507 return $this->pos < $this->size; 00508 } 00509 00510 function key() 00511 { 00512 return $this->pos; 00513 } 00514 00515 function & current() 00516 { 00517 return $this->ds->getRecord($this->pos); 00518 } 00519 00520 function next() 00521 { 00522 $this->pos++; 00523 } 00524 }

This file is part of the Creole[php4] library.


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS