PgSQLResultSet.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: PgSQLResultSet.php,v 1.2 2004/05/06 19:39:16 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 require_once 'creole/common/ResultSetCommon.php'; 00024 00033 class PgSQLResultSet extends ResultSetCommon 00034 { 00049 function seek($rownum) 00050 { 00051 if ($rownum < 0) { 00052 return false; 00053 } 00054 00055 // PostgreSQL rows start w/ 0, but this works, because we are 00056 // looking to move the position _before_ the next desired position 00057 $this->cursorPos = $rownum; 00058 return true; 00059 } 00060 00066 function next() 00067 { 00068 // must suppress errors here because we are jumping to rownum that may not exist w/ fetch_array command 00069 $this->fields = @pg_fetch_array($this->result, $this->cursorPos, $this->fetchmode); 00070 00071 if (!$this->fields) { 00072 $err = @pg_result_error($this->result); 00073 if (!$err) { 00074 // We've advanced beyond end of recordset. 00075 $this->afterLast(); 00076 return false; 00077 } else { 00078 return new SQLException(CREOLE_ERROR, "Error fetching result", $err); 00079 } 00080 } 00081 00082 // Advance cursor position 00083 $this->cursorPos++; 00084 return true; 00085 } 00086 00092 function getRecordCount() 00093 { 00094 $rows = @pg_num_rows($this->result); 00095 if ($rows === null) { 00096 return new SQLException(CREOLE_ERROR, "Error fetching num rows", pg_result_error($this->result)); 00097 } 00098 return (int) $rows; 00099 } 00100 00106 function close() 00107 { 00108 $this->fields = array(); 00109 @pg_free_result($this->result); 00110 } 00111 00118 function strToArray($str) 00119 { 00120 $str = substr($str, 1, -1); // remove { } 00121 $res = array(); 00122 00123 $subarr = array(); 00124 $in_subarr = 0; 00125 00126 $toks = explode(',', $str); 00127 foreach($toks as $tok) { 00128 if ($in_subarr > 0) { // already in sub-array? 00129 $subarr[$in_subarr][] = $tok; 00130 if ('}' === substr($tok, -1, 1)) { // check to see if we just added last component 00131 $res[] = $this->strToArray(implode(',', $subarr[$in_subarr])); 00132 $in_subarr--; 00133 } 00134 } elseif ($tok{0} === '{') { // we're inside a new sub-array 00135 if ('}' !== substr($tok, -1, 1)) { 00136 $in_subarr++; 00137 // if sub-array has more than one element 00138 $subarr[$in_subarr] = array(); 00139 $subarr[$in_subarr][] = $tok; 00140 } else { 00141 $res[] = $this->strToArray($tok); 00142 } 00143 } else { // not sub-array 00144 $val = trim($tok, '"'); // remove " (surrounding strings) 00145 // perform type castng here? 00146 $res[] = $val; 00147 } 00148 } 00149 00150 return $res; 00151 } 00152 00160 function & getArray($column) 00161 { 00162 if (is_int($column)) { $column--; } // because Java convention is to start at 1 00163 if (!array_key_exists($column, $this->fields)) { 00164 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); 00165 } 00166 if ($this->fields[$column] === null) { return null; } 00167 return $this->strToArray($this->fields[$column]); 00168 } 00169 00177 function & getBlob($column) 00178 { 00179 if (is_int($column)) { $column--; } // because Java convention is to start at 1 00180 if (!array_key_exists($column, $this->fields)) { 00181 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); 00182 } 00183 if ($this->fields[$column] === null) { return null; } 00184 require_once 'creole/util/Blob.php'; 00185 $b =& new Blob(); 00186 $b->setContents(pg_unescape_bytea($this->fields[$column])); 00187 return $b; 00188 } 00189 00195 function getBoolean($column) 00196 { 00197 if (is_int($column)) { $column--; } // because Java convention is to start at 1 00198 if (!array_key_exists($column, $this->fields)) { 00199 return new SQLException(CREOLE_ERROR_INVALID, "Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); 00200 } 00201 if ($this->fields[$column] === null) { return null; } 00202 return ($this->fields[$column] === 't'); 00203 } 00204 00205 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS