PgSQLResultSet.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: PgSQLResultSet.php,v 1.24 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 00022 require_once 'creole/ResultSet.php'; 00023 require_once 'creole/common/ResultSetCommon.php'; 00024 00032 class PgSQLResultSet extends ResultSetCommon implements ResultSet { 00033 00046 public function seek($rownum) 00047 { 00048 if ($rownum < 0) { 00049 return false; 00050 } 00051 00052 // PostgreSQL rows start w/ 0, but this works, because we are 00053 // looking to move the position _before_ the next desired position 00054 $this->cursorPos = $rownum; 00055 return true; 00056 } 00057 00061 public function next() 00062 { 00063 // must suppress errors here because we are jumping to rownum that may not exist w/ fetch_array command 00064 $this->fields = @pg_fetch_array($this->result, $this->cursorPos, $this->fetchmode); 00065 00066 if (!$this->fields) { 00067 $err = @pg_result_error($this->result); 00068 if (!$err) { 00069 // We've advanced beyond end of recordset. 00070 $this->afterLast(); 00071 return false; 00072 } else { 00073 throw new SQLException("Error fetching result", $err); 00074 } 00075 } 00076 00077 // Advance cursor position 00078 $this->cursorPos++; 00079 return true; 00080 } 00081 00085 public function getRecordCount() 00086 { 00087 $rows = @pg_num_rows($this->result); 00088 if ($rows === null) { 00089 throw new SQLException("Error fetching num rows", pg_result_error($this->result)); 00090 } 00091 return (int) $rows; 00092 } 00093 00097 public function close() 00098 { 00099 $this->fields = array(); 00100 pg_free_result($this->result); 00101 } 00102 00108 private function strToArray($str) 00109 { 00110 $str = substr($str, 1, -1); // remove { } 00111 $res = array(); 00112 00113 $subarr = array(); 00114 $in_subarr = 0; 00115 00116 $toks = explode(',', $str); 00117 foreach($toks as $tok) { 00118 if ($in_subarr > 0) { // already in sub-array? 00119 $subarr[$in_subarr][] = $tok; 00120 if ('}' === substr($tok, -1, 1)) { // check to see if we just added last component 00121 $res[] = $this->strToArray(implode(',', $subarr[$in_subarr])); 00122 $in_subarr--; 00123 } 00124 } elseif ($tok{0} === '{') { // we're inside a new sub-array 00125 if ('}' !== substr($tok, -1, 1)) { 00126 $in_subarr++; 00127 // if sub-array has more than one element 00128 $subarr[$in_subarr] = array(); 00129 $subarr[$in_subarr][] = $tok; 00130 } else { 00131 $res[] = $this->strToArray($tok); 00132 } 00133 } else { // not sub-array 00134 $val = trim($tok, '"'); // remove " (surrounding strings) 00135 // perform type castng here? 00136 $res[] = $val; 00137 } 00138 } 00139 00140 return $res; 00141 } 00142 00150 public function getArray($column) 00151 { 00152 if (is_int($column)) { $column--; } // because Java convention is to start at 1 00153 if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); } 00154 if ($this->fields[$column] === null) { return null; } 00155 return $this->strToArray($this->fields[$column]); 00156 } 00157 00165 public function getBlob($column) 00166 { 00167 if (is_int($column)) { $column--; } // because Java convention is to start at 1 00168 if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); } 00169 if ($this->fields[$column] === null) { return null; } 00170 require_once 'creole/util/Blob.php'; 00171 $b = new Blob(); 00172 $b->setContents(pg_unescape_bytea($this->fields[$column])); 00173 return $b; 00174 } 00175 00181 public function getBoolean($column) 00182 { 00183 if (is_int($column)) { $column--; } // because Java convention is to start at 1 00184 if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); } 00185 if ($this->fields[$column] === null) { return null; } 00186 return ($this->fields[$column] === 't'); 00187 } 00188 00189 }

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS