00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00089 protected $lowerAssocCase = false;
00090
00095 protected $rtrimString = false;
00096
00100 public function __construct(Connection $conn, $result, $fetchmode = null)
00101 {
00102 $this->conn = $conn;
00103 $this->result = $result;
00104 if ($fetchmode !== null) {
00105 $this->fetchmode = $fetchmode;
00106 } else {
00107 $this->fetchmode = ResultSet::FETCHMODE_ASSOC;
00108 }
00109 $this->lowerAssocCase = (($conn->getFlags() & Creole::COMPAT_ASSOC_LOWER) === Creole::COMPAT_ASSOC_LOWER);
00110 $this->rtrimString = (($conn->getFlags() & Creole::COMPAT_RTRIM_STRING) === Creole::COMPAT_RTRIM_STRING);
00111 }
00112
00118 public function __destruct()
00119 {
00120 $this->close();
00121 }
00122
00126 public function getIterator()
00127 {
00128 require_once 'creole/ResultSetIterator.php';
00129 return new ResultSetIterator($this);
00130 }
00131
00135 public function getResource()
00136 {
00137 return $this->result;
00138 }
00139
00143 public function isLowerAssocCase()
00144 {
00145 return $this->lowerAssocCase;
00146 }
00147
00151 public function setFetchmode($mode)
00152 {
00153 $this->fetchmode = $mode;
00154 }
00155
00159 public function getFetchmode()
00160 {
00161 return $this->fetchmode;
00162 }
00163
00167 public function previous()
00168 {
00169
00170 $ok = $this->seek($this->cursorPos - 2);
00171 if ($ok === false) {
00172 $this->beforeFirst();
00173 return false;
00174 }
00175 return $this->next();
00176 }
00177
00181 public function relative($offset)
00182 {
00183
00184 $pos = $this->cursorPos + ($offset - 1);
00185 $ok = $this->seek($pos);
00186
00187 if ($ok === false) {
00188 if ($pos < 0) {
00189 $this->beforeFirst();
00190 } else {
00191 $this->afterLast();
00192 }
00193 } else {
00194 $ok = $this->next();
00195 }
00196
00197 return $ok;
00198 }
00199
00203 public function absolute($pos)
00204 {
00205 $ok = $this->seek( $pos - 1 );
00206 if ($ok === false) {
00207 if ($pos - 1 < 0) {
00208 $this->beforeFirst();
00209 } else {
00210 $this->afterLast();
00211 }
00212 } else {
00213 $ok = $this->next();
00214 }
00215 return $ok;
00216 }
00217
00221 public function first()
00222 {
00223 if($this->cursorPos !== 0) { $this->seek(0); }
00224 return $this->next();
00225 }
00226
00230 public function last()
00231 {
00232 if($this->cursorPos !== ($last = $this->getRecordCount() - 1)) {
00233 $this->seek( $last );
00234 }
00235 return $this->next();
00236 }
00237
00241 public function beforeFirst()
00242 {
00243 $this->cursorPos = 0;
00244 }
00245
00249 public function afterLast()
00250 {
00251 $this->cursorPos = $this->getRecordCount() + 1;
00252 }
00253
00257 public function isAfterLast()
00258 {
00259 return ($this->cursorPos === $this->getRecordCount() + 1);
00260 }
00261
00265 public function isBeforeFirst()
00266 {
00267 return ($this->cursorPos === 0);
00268 }
00269
00273 public function getCursorPos()
00274 {
00275 return $this->cursorPos;
00276 }
00277
00281 public function getRow()
00282 {
00283 return $this->fields;
00284 }
00285
00289 public function get($column)
00290 {
00291 $idx = (is_int($column) ? $column - 1 : $column);
00292 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00293 return $this->fields[$idx];
00294 }
00295
00299 public function getArray($column)
00300 {
00301 $idx = (is_int($column) ? $column - 1 : $column);
00302 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00303 if ($this->fields[$idx] === null) { return null; }
00304 return (array) unserialize($this->fields[$idx]);
00305 }
00306
00310 public function getBoolean($column)
00311 {
00312 $idx = (is_int($column) ? $column - 1 : $column);
00313 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00314 if ($this->fields[$idx] === null) { return null; }
00315 return (boolean) $this->fields[$idx];
00316 }
00317
00321 public function getBlob($column)
00322 {
00323 $idx = (is_int($column) ? $column - 1 : $column);
00324 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00325 if ($this->fields[$idx] === null) { return null; }
00326 require_once 'creole/util/Blob.php';
00327 $b = new Blob();
00328 $b->setContents($this->fields[$idx]);
00329 return $b;
00330 }
00331
00335 public function getClob($column)
00336 {
00337 $idx = (is_int($column) ? $column - 1 : $column);
00338 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00339 if ($this->fields[$idx] === null) { return null; }
00340 require_once 'creole/util/Clob.php';
00341 $c = new Clob();
00342 $c->setContents($this->fields[$idx]);
00343 return $c;
00344 }
00345
00349 public function getDate($column, $format = '%x')
00350 {
00351 $idx = (is_int($column) ? $column - 1 : $column);
00352 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00353 if ($this->fields[$idx] === null) { return null; }
00354 $ts = strtotime($this->fields[$idx]);
00355 if ($ts === -1 || $ts === false) {
00356 throw new SQLException("Unable to convert value at column " . $column . " to timestamp: " . $this->fields[$idx]);
00357 }
00358 if ($format === null) {
00359 return $ts;
00360 }
00361 if (strpos($format, '%') !== false) {
00362 return strftime($format, $ts);
00363 } else {
00364 return date($format, $ts);
00365 }
00366 }
00367
00371 public function getFloat($column)
00372 {
00373 $idx = (is_int($column) ? $column - 1 : $column);
00374 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00375 if ($this->fields[$idx] === null) { return null; }
00376 return (float) $this->fields[$idx];
00377 }
00378
00382 public function getInt($column)
00383 {
00384 $idx = (is_int($column) ? $column - 1 : $column);
00385 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00386 if ($this->fields[$idx] === null) { return null; }
00387 return (int) $this->fields[$idx];
00388 }
00389
00393 public function getString($column)
00394 {
00395 $idx = (is_int($column) ? $column - 1 : $column);
00396 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00397 if ($this->fields[$idx] === null) { return null; }
00398 return ($this->rtrimString ? rtrim($this->fields[$idx]) : (string) $this->fields[$idx]);
00399 }
00400
00404 public function getTime($column, $format = '%X')
00405 {
00406 $idx = (is_int($column) ? $column - 1 : $column);
00407 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00408 if ($this->fields[$idx] === null) { return null; }
00409
00410 $ts = strtotime($this->fields[$idx]);
00411
00412 if ($ts === -1 || $ts === false) {
00413 throw new SQLException("Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$idx]);
00414 }
00415 if ($format === null) {
00416 return $ts;
00417 }
00418 if (strpos($format, '%') !== false) {
00419 return strftime($format, $ts);
00420 } else {
00421 return date($format, $ts);
00422 }
00423 }
00424
00428 public function getTimestamp($column, $format = 'Y-m-d H:i:s')
00429 {
00430 $idx = (is_int($column) ? $column - 1 : $column);
00431 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00432 if ($this->fields[$idx] === null) { return null; }
00433
00434 $ts = strtotime($this->fields[$idx]);
00435 if ($ts === -1 || $ts === false) {
00436 throw new SQLException("Unable to convert value at column " . $column . " to timestamp: " . $this->fields[$idx]);
00437 }
00438 if ($format === null) {
00439 return $ts;
00440 }
00441 if (strpos($format, '%') !== false) {
00442 return strftime($format, $ts);
00443 } else {
00444 return date($format, $ts);
00445 }
00446 }
00447 }