Spaces:
Sleeping
Sleeping
File size: 6,223 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | <?php
/*
* $Id: MySQLResultSet.php,v 1.24 2006/01/17 19:44:39 hlellelid Exp $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://creole.phpdb.org>.
*/
require_once 'creole/ResultSet.php';
require_once 'creole/common/ResultSetCommon.php';
/**
* MySQL implementation of ResultSet class.
*
* MySQL supports OFFSET / LIMIT natively; this means that no adjustments or checking
* are performed. We will assume that if the lmitSQL() operation failed that an
* exception was thrown, and that OFFSET/LIMIT will never be emulated for MySQL.
*
* @author Hans Lellelid <hans@xmpl.org>
* @version $Revision: 1.24 $
* @package creole.drivers.mysql
*/
class MySQLResultSet extends ResultSetCommon implements ResultSet {
/**
* @see ResultSet::seek()
*/
public function seek($rownum)
{
// MySQL rows start w/ 0, but this works, because we are
// looking to move the position _before_ the next desired position
if (!@mysql_data_seek($this->result, $rownum)) {
return false;
}
$this->cursorPos = $rownum;
return true;
}
/**
* @see ResultSet::next()
*/
public function next()
{
$this->fields = mysql_fetch_array($this->result, $this->fetchmode);
if (!$this->fields) {
$errno = mysql_errno($this->conn->getResource());
if (!$errno) {
// We've advanced beyond end of recordset.
$this->afterLast();
return false;
} else {
throw new SQLException("Error fetching result", mysql_error($this->conn->getResource()));
}
}
/*else {
if (is_array($this->fields)) {
foreach ($this->fields as $sKey => $sValue) {
if (function_exists('mb_detect_encoding')) {
if (strtoupper(mb_detect_encoding($sValue)) == 'UTF-8') {
$this->fields[$sKey] = utf8_encode($sValue);
}
}
}
}
}*/
if ($this->fetchmode === ResultSet::FETCHMODE_ASSOC && $this->lowerAssocCase) {
$this->fields = array_change_key_case($this->fields, CASE_LOWER);
}
// Advance cursor position
$this->cursorPos++;
return true;
}
/**
* @see ResultSet::getRecordCount()
*/
function getRecordCount()
{
$rows = @mysql_num_rows($this->result);
if ($rows === null) {
throw new SQLException("Error fetching num rows", mysql_error($this->conn->getResource()));
}
return (int) $rows;
}
/**
* @see ResultSet::close()
*/
function close()
{
if(is_resource($this->result))
@mysql_free_result($this->result);
$this->fields = array();
}
/**
* Get string version of column.
* No rtrim() necessary for MySQL, as this happens natively.
* @see ResultSet::getString()
*/
public function getString($column)
{
$idx = (is_int($column) ? $column - 1 : $column);
if (!array_key_exists($idx, $this->fields)) { throw new SQLException("Invalid resultset column: " . $column); }
if ($this->fields[$idx] === null) { return null; }
return (string) $this->fields[$idx];
}
/**
* Returns a unix epoch timestamp based on either a TIMESTAMP or DATETIME field.
* @param mixed $column Column name (string) or index (int) starting with 1.
* @return string
* @throws SQLException - If the column specified is not a valid key in current field array.
*/
function getTimestamp($column, $format='Y-m-d H:i:s')
{
if (is_int($column)) { $column--; } // because Java convention is to start at 1
if (!array_key_exists($column, $this->fields)) { throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); }
if ($this->fields[$column] === null) { return null; }
if (($this->fields[$column] == '0000-00-00 00:00:00') || ($this->fields[$column] == '0000-00-00')) {
$ts = '943916400';
}
else {
$ts = strtotime($this->fields[$column]);
}
if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE
// otherwise it's an ugly MySQL timestamp!
// YYYYMMDDHHMMSS
if (preg_match('/([\d]{4})([\d]{2})([\d]{2})([\d]{2})([\d]{2})([\d]{2})/', $this->fields[$column], $matches)) {
// YYYY MM DD HH MM SS
// $1 $2 $3 $4 $5 $6
$ts = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
}
}
if ($ts === -1 || $ts === false) { // if it's still -1, then there's nothing to be done; use a different method.
throw new SQLException("Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$column]);
}
if ($format === null) {
return $ts;
}
if (strpos($format, '%') !== false) {
return strftime($format, $ts);
} else {
return date($format, $ts);
}
}
}
|