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