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 MySQLiResultSet extends ResultSetCommon implements ResultSet {
00040 public function seek($rownum)
00041 {
00042
00043
00044 if (!@mysqli_data_seek($this->result, $rownum)) {
00045 return false;
00046 }
00047
00048 $this->cursorPos = $rownum;
00049
00050 return true;
00051 }
00052
00056 public function next()
00057 {
00058 $this->fields = mysqli_fetch_array($this->result, $this->fetchmode);
00059 $resource = $this->conn->getResource();
00060
00061 if (!$this->fields) {
00062 $errno = mysqli_errno($resource);
00063
00064 if (!$errno) {
00065
00066 $this->afterLast();
00067 return false;
00068 } else {
00069 throw new SQLException("Error fetching result", mysqli_error($resource));
00070 }
00071 }
00072
00073 if ($this->fetchmode === ResultSet::FETCHMODE_ASSOC && $this->lowerAssocCase) {
00074 $this->fields = array_change_key_case($this->fields, CASE_LOWER);
00075 }
00076
00077
00078 $this->cursorPos++;
00079 return true;
00080 }
00081
00085 public function getRecordCount()
00086 {
00087 $rows = @mysqli_num_rows($this->result);
00088
00089 if ($rows === null) {
00090 throw new SQLException("Error fetching num rows", mysqli_error($this->conn->getResource()));
00091 }
00092
00093 return (int) $rows;
00094 }
00095
00099 public function close()
00100 {
00101 @mysqli_free_result($this->result);
00102 $this->fields = array();
00103 }
00104
00110 public function getString($column)
00111 {
00112 $idx = (is_int($column) ? $column - 1 : $column);
00113
00114 if (!array_key_exists($idx, $this->fields)) {
00115 throw new SQLException("Invalid resultset column: " . $column);
00116 }
00117
00118 if ($this->fields[$idx] === null) {
00119 return null;
00120 }
00121
00122 return (string) $this->fields[$idx];
00123 }
00124
00131 public function getTimestamp($column, $format='Y-m-d H:i:s')
00132 {
00133 if (is_int($column)) {
00134
00135 $column--;
00136 }
00137
00138 if (!array_key_exists($column, $this->fields)) {
00139 throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column));
00140 }
00141
00142 if ($this->fields[$column] === null) {
00143 return null;
00144 }
00145
00146 $ts = strtotime($this->fields[$column]);
00147
00148 if ($ts === -1 || $ts === false) {
00149
00150
00151 if (preg_match('/([\d]{4})([\d]{2})([\d]{2})([\d]{2})([\d]{2})([\d]{2})/', $this->fields[$column], $matches)) {
00152
00153
00154 $ts = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
00155 }
00156 }
00157
00158 if ($ts === -1 || $ts === false) {
00159
00160 throw new SQLException("Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$column]);
00161 }
00162
00163 if ($format === null) {
00164 return $ts;
00165 }
00166
00167 if (strpos($format, '%') !== false) {
00168 return strftime($format, $ts);
00169 } else {
00170 return date($format, $ts);
00171 }
00172 }
00173 }