ResultSetCommon.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: ResultSetCommon.php,v 1.6 2004/05/06 17:31:02 hlellelid 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 00053 abstract class ResultSetCommon { 00054 00059 protected $fetchmode; 00060 00065 protected $conn; 00066 00071 protected $result; 00072 00077 protected $cursorPos = 0; 00078 00083 protected $fields; 00084 00088 protected $ignoreAssocCase = false; 00089 00093 public function __construct(Connection $conn, $result, $fetchmode = null) 00094 { 00095 $this->conn = $conn; 00096 $this->result = $result; 00097 if ($fetchmode !== null) { 00098 $this->fetchmode = $fetchmode; 00099 } else { 00100 $this->fetchmode = ResultSet::FETCHMODE_ASSOC; // default 00101 } 00102 $this->ignoreAssocCase = (($conn->getFlags() & Creole::NO_ASSOC_LOWER) === Creole::NO_ASSOC_LOWER); 00103 } 00104 00110 public function __destruct() 00111 { 00112 $this->close(); 00113 } 00114 00118 public function getIterator() 00119 { 00120 require_once 'creole/ResultSetIterator.php'; 00121 return new ResultSetIterator($this); 00122 } 00123 00127 public function getResource() 00128 { 00129 return $this->result; 00130 } 00131 00135 public function isIgnoreAssocCase() 00136 { 00137 return $this->ignoreAssocCase; 00138 } 00139 00143 public function setFetchmode($mode) 00144 { 00145 $this->fetchmode = $mode; 00146 } 00147 00151 public function getFetchmode() 00152 { 00153 return $this->fetchmode; 00154 } 00155 00159 public function previous() 00160 { 00161 // Go back 2 spaces so that we can then advance 1 space. 00162 $ok = $this->seek($this->cursorPos - 2); 00163 if ($ok === false) { 00164 $this->beforeFirst(); 00165 return false; 00166 } 00167 return $this->next(); 00168 } 00169 00173 public function relative($offset) 00174 { 00175 // which absolute row number are we seeking 00176 $pos = $this->cursorPos + ($offset - 1); 00177 $ok = $this->seek($pos); 00178 00179 if ($ok === false) { 00180 if ($pos < 0) { 00181 $this->beforeFirst(); 00182 } else { 00183 $this->afterLast(); 00184 } 00185 } else { 00186 $ok = $this->next(); 00187 } 00188 00189 return $ok; 00190 } 00191 00192 00193 00197 public function absolute($pos) 00198 { 00199 $ok = $this->seek( $pos - 1 ); // compensate for next() factor 00200 if ($ok === false) { 00201 if ($pos - 1 < 0) { 00202 $this->beforeFirst(); 00203 } else { 00204 $this->afterLast(); 00205 } 00206 } else { 00207 $ok = $this->next(); 00208 } 00209 return $ok; 00210 } 00211 00212 00216 public function first() 00217 { 00218 if($this->cursorPos !== 0) { $this->seek(0); } 00219 return $this->next(); 00220 } 00221 00225 public function last() 00226 { 00227 if($this->cursorPos !== ($last = $this->getRecordCount() - 1)) { 00228 $this->seek( $last ); 00229 } 00230 return $this->next(); 00231 } 00232 00236 public function beforeFirst() 00237 { 00238 $this->cursorPos = 0; 00239 } 00240 00241 00245 public function afterLast() 00246 { 00247 $this->cursorPos = $this->getRecordCount() + 1; 00248 } 00249 00250 00254 public function isAfterLast() 00255 { 00256 return ($this->cursorPos === $this->getRecordCount() + 1); 00257 } 00258 00262 public function isBeforeFirst() 00263 { 00264 return ($this->cursorPos === 0); 00265 } 00266 00270 public function getCursorPos() 00271 { 00272 return $this->cursorPos; 00273 } 00274 00278 public function getRow() 00279 { 00280 return $this->fields; 00281 } 00282 00286 public function get($column) 00287 { 00288 $idx = (is_int($column) ? $column - 1 : $column); 00289 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00290 return $this->fields[$idx]; 00291 } 00292 00296 public function getArray($column) 00297 { 00298 $idx = (is_int($column) ? $column - 1 : $column); 00299 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00300 if ($this->fields[$idx] === null) { return null; } 00301 return (array) unserialize($this->fields[$idx]); 00302 } 00303 00307 public function getBoolean($column) 00308 { 00309 $idx = (is_int($column) ? $column - 1 : $column); 00310 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00311 if ($this->fields[$idx] === null) { return null; } 00312 return (boolean) $this->fields[$idx]; 00313 } 00314 00318 public function getBlob($column) 00319 { 00320 $idx = (is_int($column) ? $column - 1 : $column); 00321 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00322 if ($this->fields[$idx] === null) { return null; } 00323 require_once 'creole/util/Blob.php'; 00324 $b = new Blob(); 00325 $b->setContents($this->fields[$idx]); 00326 return $b; 00327 } 00328 00332 public function getClob($column) 00333 { 00334 $idx = (is_int($column) ? $column - 1 : $column); 00335 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00336 if ($this->fields[$idx] === null) { return null; } 00337 require_once 'creole/util/Clob.php'; 00338 $c = new Clob(); 00339 $c->setContents($this->fields[$idx]); 00340 return $c; 00341 } 00342 00346 public function getDate($column, $format = '%x') 00347 { 00348 $idx = (is_int($column) ? $column - 1 : $column); 00349 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00350 if ($this->fields[$idx] === null) { return null; } 00351 $ts = strtotime($this->fields[$idx]); 00352 if ($ts === -1) { 00353 throw new SQLException("Unable to convert value at column " . $column . " to timestamp: " . $this->fields[$idx]); 00354 } 00355 if ($format === null) { 00356 return $ts; 00357 } 00358 if (strpos($format, '%') !== false) { 00359 return strftime($format, $ts); 00360 } else { 00361 return date($format, $ts); 00362 } 00363 } 00364 00368 public function getFloat($column) 00369 { 00370 $idx = (is_int($column) ? $column - 1 : $column); 00371 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00372 if ($this->fields[$idx] === null) { return null; } 00373 return (float) $this->fields[$idx]; 00374 } 00375 00379 public function getInt($column) 00380 { 00381 $idx = (is_int($column) ? $column - 1 : $column); 00382 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00383 if ($this->fields[$idx] === null) { return null; } 00384 return (int) $this->fields[$idx]; 00385 } 00386 00390 public function getString($column) 00391 { 00392 $idx = (is_int($column) ? $column - 1 : $column); 00393 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00394 if ($this->fields[$idx] === null) { return null; } 00395 return rtrim((string) $this->fields[$idx]); 00396 } 00397 00401 public function getTime($column, $format = '%X') 00402 { 00403 $idx = (is_int($column) ? $column - 1 : $column); 00404 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00405 if ($this->fields[$idx] === null) { return null; } 00406 00407 $ts = strtotime($this->fields[$idx]); 00408 00409 if ($ts === -1) { 00410 throw new SQLException("Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$idx]); 00411 } 00412 if ($format === null) { 00413 return $ts; 00414 } 00415 if (strpos($format, '%') !== false) { 00416 return strftime($format, $ts); 00417 } else { 00418 return date($format, $ts); 00419 } 00420 } 00421 00425 public function getTimestamp($column, $format = 'Y-m-d H:i:s') 00426 { 00427 $idx = (is_int($column) ? $column - 1 : $column); 00428 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); } 00429 if ($this->fields[$idx] === null) { return null; } 00430 00431 $ts = strtotime($this->fields[$idx]); 00432 if ($ts === -1) { 00433 throw new SQLException("Unable to convert value at column " . $column . " to timestamp: " . $this->fields[$idx]); 00434 } 00435 if ($format === null) { 00436 return $ts; 00437 } 00438 if (strpos($format, '%') !== false) { 00439 return strftime($format, $ts); 00440 } else { 00441 return date($format, $ts); 00442 } 00443 } 00444 } 00445

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS