[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: MySQLiResultSet.php,v 1.5 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 * MySQLi implementation of ResultSet. 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 Sebastian Bergmann <sb@sebastian-bergmann.de> 33 * @version $Revision: 1.5 $ 34 * @package creole.drivers.mysqli 35 */ 36 class MySQLiResultSet extends ResultSetCommon implements ResultSet { 37 /** 38 * @see ResultSet::seek() 39 */ 40 public function seek($rownum) 41 { 42 // MySQL rows start w/ 0, but this works, because we are 43 // looking to move the position _before_ the next desired position 44 if (!@mysqli_data_seek($this->result, $rownum)) { 45 return false; 46 } 47 48 $this->cursorPos = $rownum; 49 50 return true; 51 } 52 53 /** 54 * @see ResultSet::next() 55 */ 56 public function next() 57 { 58 $this->fields = mysqli_fetch_array($this->result, $this->fetchmode); 59 $resource = $this->conn->getResource(); 60 61 if (!$this->fields) { 62 $errno = mysqli_errno($resource); 63 64 if (!$errno) { 65 // We've advanced beyond end of recordset. 66 $this->afterLast(); 67 return false; 68 } else { 69 throw new SQLException("Error fetching result", mysqli_error($resource)); 70 } 71 } 72 73 if ($this->fetchmode === ResultSet::FETCHMODE_ASSOC && $this->lowerAssocCase) { 74 $this->fields = array_change_key_case($this->fields, CASE_LOWER); 75 } 76 77 // Advance cursor position 78 $this->cursorPos++; 79 return true; 80 } 81 82 /** 83 * @see ResultSet::getRecordCount() 84 */ 85 public function getRecordCount() 86 { 87 $rows = @mysqli_num_rows($this->result); 88 89 if ($rows === null) { 90 throw new SQLException("Error fetching num rows", mysqli_error($this->conn->getResource())); 91 } 92 93 return (int) $rows; 94 } 95 96 /** 97 * @see ResultSet::close() 98 */ 99 public function close() 100 { 101 @mysqli_free_result($this->result); 102 $this->fields = array(); 103 } 104 105 /** 106 * Get string version of column. 107 * No rtrim() necessary for MySQL, as this happens natively. 108 * @see ResultSet::getString() 109 */ 110 public function getString($column) 111 { 112 $idx = (is_int($column) ? $column - 1 : $column); 113 114 if (!array_key_exists($idx, $this->fields)) { 115 throw new SQLException("Invalid resultset column: " . $column); 116 } 117 118 if ($this->fields[$idx] === null) { 119 return null; 120 } 121 122 return (string) $this->fields[$idx]; 123 } 124 125 /** 126 * Returns a unix epoch timestamp based on either a TIMESTAMP or DATETIME field. 127 * @param mixed $column Column name (string) or index (int) starting with 1. 128 * @return string 129 * @throws SQLException - If the column specified is not a valid key in current field array. 130 */ 131 public function getTimestamp($column, $format='Y-m-d H:i:s') 132 { 133 if (is_int($column)) { 134 // because Java convention is to start at 1 135 $column--; 136 } 137 138 if (!array_key_exists($column, $this->fields)) { 139 throw new SQLException("Invalid resultset column: " . (is_int($column) ? $column + 1 : $column)); 140 } 141 142 if ($this->fields[$column] === null) { 143 return null; 144 } 145 146 $ts = strtotime($this->fields[$column]); 147 148 if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE 149 // otherwise it's an ugly MySQL timestamp! 150 // YYYYMMDDHHMMSS 151 if (preg_match('/([\d]{4})([\d]{2})([\d]{2})([\d]{2})([\d]{2})([\d]{2})/', $this->fields[$column], $matches)) { 152 // YYYY MM DD HH MM SS 153 // $1 $2 $3 $4 $5 $6 154 $ts = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]); 155 } 156 } 157 158 if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE 159 // if it's still -1, then there's nothing to be done; use a different method. 160 throw new SQLException("Unable to convert value at column " . (is_int($column) ? $column + 1 : $column) . " to timestamp: " . $this->fields[$column]); 161 } 162 163 if ($format === null) { 164 return $ts; 165 } 166 167 if (strpos($format, '%') !== false) { 168 return strftime($format, $ts); 169 } else { 170 return date($format, $ts); 171 } 172 } 173 }
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 |