00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 require_once 'creole/ResultSet.php';
00023 require_once 'creole/common/ResultSetCommon.php';
00024
00032 class PgSQLResultSet extends ResultSetCommon implements ResultSet {
00033
00034
00039
00040
00041
00042
00043
00044
00045
00046
00059 public function seek($rownum)
00060 {
00061 if ($rownum < 0) {
00062 return false;
00063 }
00064
00065
00066
00067 $this->cursorPos = $rownum;
00068 return true;
00069 }
00070
00074 public function next()
00075 {
00076
00077 $this->fields = @pg_fetch_array($this->result, $this->cursorPos, $this->fetchmode);
00078
00079 if (!$this->fields) {
00080 $err = @pg_result_error($this->result);
00081 if (!$err) {
00082
00083 $this->afterLast();
00084 return false;
00085 } else {
00086 throw new SQLException("Error fetching result", $err);
00087 }
00088 }
00089
00090 if ($this->fetchmode === ResultSet::FETCHMODE_ASSOC && $this->lowerAssocCase) {
00091 $this->fields = array_change_key_case($this->fields, CASE_LOWER);
00092 }
00093
00094 $this->cursorPos++;
00095 return true;
00096 }
00097
00101 public function getRecordCount()
00102 {
00103 $rows = @pg_num_rows($this->result);
00104 if ($rows === null) {
00105 throw new SQLException("Error fetching num rows", pg_result_error($this->result));
00106 }
00107 return (int) $rows;
00108 }
00109
00113 public function close()
00114 {
00115 $this->fields = array();
00116 @pg_free_result($this->result);
00117 }
00118
00124 private function strToArray($str)
00125 {
00126 $str = substr($str, 1, -1);
00127 $res = array();
00128
00129 $subarr = array();
00130 $in_subarr = 0;
00131
00132 $toks = explode(',', $str);
00133 foreach($toks as $tok) {
00134 if ($in_subarr > 0) {
00135 $subarr[$in_subarr][] = $tok;
00136 if ('}' === substr($tok, -1, 1)) {
00137 $res[] = $this->strToArray(implode(',', $subarr[$in_subarr]));
00138 $in_subarr--;
00139 }
00140 } elseif ($tok{0} === '{') {
00141 if ('}' !== substr($tok, -1, 1)) {
00142 $in_subarr++;
00143
00144 $subarr[$in_subarr] = array();
00145 $subarr[$in_subarr][] = $tok;
00146 } else {
00147 $res[] = $this->strToArray($tok);
00148 }
00149 } else {
00150 $val = trim($tok, '"');
00151
00152 $res[] = $val;
00153 }
00154 }
00155
00156 return $res;
00157 }
00158
00166 public function getArray($column)
00167 {
00168 if (is_int($column)) { $column--; }
00169 if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); }
00170 if ($this->fields[$column] === null) { return null; }
00171 return $this->strToArray($this->fields[$column]);
00172 }
00173
00181 public function getBlob($column)
00182 {
00183 if (is_int($column)) { $column--; }
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 require_once 'creole/util/Blob.php';
00187 $b = new Blob();
00188 $b->setContents(pg_unescape_bytea($this->fields[$column]));
00189 return $b;
00190 }
00191
00197 public function getBoolean($column)
00198 {
00199 if (is_int($column)) { $column--; }
00200 if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); }
00201 if ($this->fields[$column] === null) { return null; }
00202 return ($this->fields[$column] === 't');
00203 }
00204
00205 }