ResultSetCommon.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: ResultSetCommon.php,v 1.4 2004/05/06 19:47:34 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 00022 require_once 'creole/ResultSet.php'; 00023 00051 class ResultSetCommon extends ResultSet 00052 { 00057 var $fetchmode; 00058 00063 var $conn; 00064 00069 var $result; 00070 00075 var $cursorPos = 0; 00076 00081 var $fields; 00082 00086 var $ignoreAssocCase = false; 00087 00093 function ResultSetCommon(/*Connection*/ &$conn, &$result, $fetchmode = null) 00094 { 00095 if (! is_a($conn, 'Connection')) { 00096 trigger_error ( 00097 "ResultSetCommon::ResultSetCommon(): parameter 1 not of type 'Connection' !", 00098 E_USER_ERROR 00099 ); 00100 } 00101 00102 $this->conn =& $conn; 00103 $this->result =& $result; 00104 00105 if ($fetchmode !== null) { 00106 $this->fetchmode = $fetchmode; 00107 } else { 00108 $this->fetchmode = ResultSet::FETCHMODE_ASSOC(); // default 00109 } 00110 00111 $this->ignoreAssocCase = (($conn->getFlags() & Creole::NO_ASSOC_LOWER()) === Creole::NO_ASSOC_LOWER()); 00112 00113 register_shutdown_function(array($this, '__ResultSetCommon')); 00114 } 00115 00121 function __ResultSetCommon() 00122 { 00123 $this->close(); 00124 } 00125 00129 function & getIterator() 00130 { 00131 require_once 'creole/ResultSetIterator.php'; 00132 return new ResultSetIterator($this); 00133 } 00134 00138 function & getResource() 00139 { 00140 return $this->result; 00141 } 00142 00146 function isIgnoreAssocCase() 00147 { 00148 return $this->ignoreAssocCase; 00149 } 00150 00154 function setFetchmode($mode) 00155 { 00156 $this->fetchmode = $mode; 00157 } 00158 00162 function getFetchmode() 00163 { 00164 return $this->fetchmode; 00165 } 00166 00170 function & previous() 00171 { 00172 // Go back 2 spaces so that we can then advance 1 space. 00173 $ok = $this->seek($this->cursorPos - 2); 00174 if ($ok === false) { 00175 $this->beforeFirst(); 00176 return false; 00177 } 00178 return $this->next(); 00179 } 00180 00184 function relative($offset) 00185 { 00186 // which absolute row number are we seeking 00187 $pos = $this->cursorPos + ($offset - 1); 00188 $ok = $this->seek($pos); 00189 00190 if ($ok === false) { 00191 if ($pos < 0) { 00192 $this->beforeFirst(); 00193 } else { 00194 $this->afterLast(); 00195 } 00196 } else { 00197 $ok = $this->next(); 00198 } 00199 00200 return $ok; 00201 } 00202 00206 function absolute($pos) 00207 { 00208 $ok = $this->seek( $pos - 1 ); // compensate for next() factor 00209 if ($ok === false) { 00210 if ($pos - 1 < 0) { 00211 $this->beforeFirst(); 00212 } else { 00213 $this->afterLast(); 00214 } 00215 } else { 00216 $ok = $this->next(); 00217 } 00218 return $ok; 00219 } 00220 00224 function & first() 00225 { 00226 if($this->cursorPos !== 0) { $this->seek(0); } 00227 return $this->next(); 00228 } 00229 00233 function & last() 00234 { 00235 $last = $this->getRecordCount(); 00236 if (Creole::isError($last)) { 00237 return $last; 00238 } 00239 00240 if($this->cursorPos !== ($last = $last - 1)) { 00241 $this->seek( $last ); 00242 } 00243 00244 return $this->next(); 00245 } 00246 00250 function beforeFirst() 00251 { 00252 $this->cursorPos = 0; 00253 } 00254 00255 00259 function afterLast() 00260 { 00261 $rc = $this->getRecordCount(); 00262 if (Creole::isError($rc)) { 00263 return $rc; 00264 } 00265 00266 $this->cursorPos = $rc + 1; 00267 return true; 00268 } 00269 00270 00274 function isAfterLast() 00275 { 00276 return ($this->cursorPos === $this->getRecordCount() + 1); 00277 } 00278 00282 function isBeforeFirst() 00283 { 00284 return ($this->cursorPos === 0); 00285 } 00286 00290 function getCursorPos() 00291 { 00292 return $this->cursorPos; 00293 } 00294 00298 function & getRow() 00299 { 00300 return $this->fields; 00301 } 00302 00306 function get($column) 00307 { 00308 $idx = (is_int($column) ? $column - 1 : $column); 00309 if (!array_key_exists($idx, $this->fields)) { 00310 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00311 } 00312 return $this->fields[$idx]; 00313 } 00314 00318 function getArray($column) 00319 { 00320 $idx = (is_int($column) ? $column - 1 : $column); 00321 if (!array_key_exists($idx, $this->fields)) { 00322 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00323 } 00324 if ($this->fields[$idx] === null) { return null; } 00325 return (array) unserialize($this->fields[$idx]); 00326 } 00327 00331 function getBoolean($column) 00332 { 00333 $idx = (is_int($column) ? $column - 1 : $column); 00334 if (!array_key_exists($idx, $this->fields)) { 00335 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00336 } 00337 if ($this->fields[$idx] === null) { return null; } 00338 return (boolean) $this->fields[$idx]; 00339 } 00340 00344 function getBlob($column) 00345 { 00346 $idx = (is_int($column) ? $column - 1 : $column); 00347 if (!array_key_exists($idx, $this->fields)) { 00348 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00349 } 00350 00351 if ($this->fields[$idx] === null) { return null; } 00352 require_once 'creole/util/Blob.php'; 00353 $b = new Blob(); 00354 $b->setContents($this->fields[$idx]); 00355 return $b; 00356 } 00357 00361 function getClob($column) 00362 { 00363 $idx = (is_int($column) ? $column - 1 : $column); 00364 if (!array_key_exists($idx, $this->fields)) { 00365 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00366 } 00367 00368 if ($this->fields[$idx] === null) { return null; } 00369 require_once 'creole/util/Clob.php'; 00370 $c = new Clob(); 00371 $c->setContents($this->fields[$idx]); 00372 return $c; 00373 } 00374 00378 function getDate($column, $format = '%x') 00379 { 00380 $idx = (is_int($column) ? $column - 1 : $column); 00381 if (!array_key_exists($idx, $this->fields)) { 00382 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00383 } 00384 00385 if ($this->fields[$idx] === null) { return null; } 00386 $ts = strtotime($this->fields[$idx]); 00387 if ($ts === -1) { 00388 return new SQLException(CREOLE_ERROR_INVALID, "Unable to convert value at column " . $column . " to timestamp: " . $this->fields[$idx]); 00389 } 00390 if ($ts === null) { 00391 return $ts; 00392 } 00393 if (strpos($format, '%') !== false) { 00394 return strftime($format, $ts); 00395 } else { 00396 return date($format, $ts); 00397 } 00398 } 00399 00403 function getFloat($column) 00404 { 00405 $idx = (is_int($column) ? $column - 1 : $column); 00406 if (!array_key_exists($idx, $this->fields)) { 00407 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00408 } 00409 00410 if ($this->fields[$idx] === null) { return null; } 00411 return (float) $this->fields[$idx]; 00412 } 00413 00417 function getInt($column) 00418 { 00419 $idx = (is_int($column) ? $column - 1 : $column); 00420 if (!array_key_exists($idx, $this->fields)) { 00421 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00422 } 00423 00424 if ($this->fields[$idx] === null) { return null; } 00425 return (int) $this->fields[$idx]; 00426 } 00427 00431 function getString($column) 00432 { 00433 $idx = (is_int($column) ? $column - 1 : $column); 00434 if (!array_key_exists($idx, $this->fields)) { 00435 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00436 } 00437 00438 if ($this->fields[$idx] === null) { return null; } 00439 return rtrim((string) $this->fields[$idx]); 00440 } 00441 00445 function getTime($column, $format = '%X') 00446 { 00447 $idx = (is_int($column) ? $column - 1 : $column); 00448 if (!array_key_exists($idx, $this->fields)) { 00449 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00450 } 00451 00452 if ($this->fields[$idx] === null) { return null; } 00453 00454 $ts = strtotime($this->fields[$idx]); 00455 00456 if ($ts === -1) { 00457 return new SQLException(CREOLE_ERROR_INVALID, "Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$idx]); 00458 } 00459 00460 if ($ts === null) { 00461 return $ts; 00462 } 00463 00464 if (strpos($format, '%') !== false) { 00465 return strftime($format, $ts); 00466 } else { 00467 return date($format, $ts); 00468 } 00469 } 00470 00474 function getTimestamp($column, $format = 'Y-m-d H:i:s') 00475 { 00476 $idx = (is_int($column) ? $column - 1 : $column); 00477 if (!array_key_exists($idx, $this->fields)) { 00478 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . $column); 00479 } 00480 00481 if ($this->fields[$idx] === null) { return null; } 00482 00483 $ts = strtotime($this->fields[$idx]); 00484 if ($ts === -1) { 00485 return new SQLException(CREOLE_ERROR_INVALID, "Unable to convert value at column " . $column . " to timestamp: " . $this->fields[$idx]); 00486 } 00487 if ($ts === null) { 00488 return $ts; 00489 } 00490 if (strpos($format, '%') !== false) { 00491 return strftime($format, $ts); 00492 } else { 00493 return date($format, $ts); 00494 } 00495 } 00496 00497 } 00498

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS