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
00036 class MySQLResultSet extends ResultSetCommon implements ResultSet {
00037
00041 public function seek($rownum)
00042 {
00043
00044
00045 if (!@mysql_data_seek($this->result, $rownum)) {
00046 return false;
00047 }
00048 $this->cursorPos = $rownum;
00049 return true;
00050 }
00051
00055 public function next()
00056 {
00057 $this->fields = mysql_fetch_array($this->result, $this->fetchmode);
00058
00059 if (!$this->fields) {
00060 $errno = mysql_errno($this->conn->getResource());
00061 if (!$errno) {
00062
00063 $this->afterLast();
00064 return false;
00065 } else {
00066 throw new SQLException("Error fetching result", mysql_error($this->conn->getResource()));
00067 }
00068 }
00069
00070 if ($this->fetchmode === ResultSet::FETCHMODE_ASSOC && $this->lowerAssocCase) {
00071 $this->fields = array_change_key_case($this->fields, CASE_LOWER);
00072 }
00073
00074
00075 $this->cursorPos++;
00076 return true;
00077 }
00078
00082 function getRecordCount()
00083 {
00084 $rows = @mysql_num_rows($this->result);
00085 if ($rows === null) {
00086 throw new SQLException("Error fetching num rows", mysql_error($this->conn->getResource()));
00087 }
00088 return (int) $rows;
00089 }
00090
00094 function close()
00095 {
00096 @mysql_free_result($this->result);
00097 $this->fields = array();
00098 }
00099
00105 public function getString($column)
00106 {
00107 $idx = (is_int($column) ? $column - 1 : $column);
00108 if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
00109 if ($this->fields[$idx] === null) { return null; }
00110 return (string) $this->fields[$idx];
00111 }
00112
00119 function getTimestamp($column, $format='Y-m-d H:i:s')
00120 {
00121 if (is_int($column)) { $column--; }
00122 if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); }
00123 if ($this->fields[$column] === null) { return null; }
00124
00125 $ts = strtotime($this->fields[$column]);
00126 if ($ts === -1 || $ts === false) {
00127
00128
00129 if (preg_match('/([\d]{4})([\d]{2})([\d]{2})([\d]{2})([\d]{2})([\d]{2})/', $this->fields[$column], $matches)) {
00130
00131
00132 $ts = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
00133 }
00134 }
00135 if ($ts === -1 || $ts === false) {
00136 throw new SQLException("Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$column]);
00137 }
00138 if ($format === null) {
00139 return $ts;
00140 }
00141 if (strpos($format, '%') !== false) {
00142 return strftime($format, $ts);
00143 } else {
00144 return date($format, $ts);
00145 }
00146 }
00147
00148 }