[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: MSSQLResultSet.php,v 1.21 2006/01/17 19:44:38 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 * MSSQL implementation of ResultSet. 27 * 28 * MS SQL does not support LIMIT or OFFSET natively so the methods 29 * in here need to perform some adjustments and extra checking to make sure 30 * that this behaves the same as RDBMS drivers using native OFFSET/LIMIT. 31 * 32 * @author Hans Lellelid <hans@xmpl.org> 33 * @version $Revision: 1.21 $ 34 * @package creole.drivers.mssql 35 */ 36 class MSSQLResultSet extends ResultSetCommon implements ResultSet { 37 38 /** 39 * Offset at which to start reading rows. 40 * @var int 41 */ 42 private $offset = 0; 43 44 /** 45 * Maximum rows to retrieve, or 0 if all. 46 * @var int 47 */ 48 private $limit = 0; 49 50 /** 51 * This MSSQL-only function exists to set offset after ResultSet is instantiated. 52 * This function should be "protected" in Java sense: only available to classes in package. 53 * THIS METHOD SHOULD NOT BE CALLED BY ANYTHING EXCEPTION DRIVER CLASSES. 54 * @param int $offset New offset. If great than 0, then seek(0) will be called to move cursor. 55 * @access protected 56 */ 57 public function _setOffset($offset) 58 { 59 $this->offset = $offset; 60 if ($offset > 0) { 61 $this->seek(0); // 0 becomes $offset by seek() method 62 } 63 } 64 65 /** 66 * This MSSQL-only function exists to set limit after ResultSet is instantiated. 67 * This function should be "protected" in Java sense: only available to classes in package. 68 * THIS METHOD SHOULD NOT BE CALLED BY ANYTHING EXCEPTION DRIVER CLASSES. 69 * @param int $limit New limit. 70 * @access protected 71 */ 72 public function _setLimit($limit) 73 { 74 $this->limit = $limit; 75 } 76 77 /** 78 * @see ResultSet::seek() 79 */ 80 function seek($rownum) 81 { 82 // support emulated OFFSET 83 $actual = $rownum + $this->offset; 84 85 if (($this->limit > 0 && $rownum >= $this->limit) || $rownum < 0) { 86 // have to check for rownum < 0, because mssql_seek() won't 87 // complain if the $actual is valid. 88 return false; 89 } 90 91 // MSSQL rows start w/ 0, but this works, because we are 92 // looking to move the position _before_ the next desired position 93 if (!@mssql_data_seek($this->result, $actual)) { 94 return false; 95 } 96 97 $this->cursorPos = $rownum; 98 return true; 99 } 100 101 /** 102 * @see ResultSet::next() 103 */ 104 function next() 105 { 106 // support emulated LIMIT 107 if ( $this->limit > 0 && ($this->cursorPos >= $this->limit) ) { 108 $this->afterLast(); 109 return false; 110 } 111 112 $this->fields = mssql_fetch_array($this->result, $this->fetchmode); 113 114 if (!$this->fields) { 115 if ($errmsg = mssql_get_last_message()) { 116 throw new SQLException("Error fetching result", $errmsg); 117 } else { 118 // We've advanced beyond end of recordset. 119 $this->afterLast(); 120 return false; 121 } 122 } 123 124 if ($this->fetchmode === ResultSet::FETCHMODE_ASSOC && $this->lowerAssocCase) { 125 $this->fields = array_change_key_case($this->fields, CASE_LOWER); 126 } 127 128 // Advance cursor position 129 $this->cursorPos++; 130 return true; 131 } 132 133 /** 134 * @see ResultSet::getRecordCount() 135 */ 136 function getRecordCount() 137 { 138 $rows = @mssql_num_rows($this->result); 139 if ($rows === null) { 140 throw new SQLException('Error getting record count', mssql_get_last_message()); 141 } 142 // adjust count based on emulated LIMIT/OFFSET 143 $rows -= $this->offset; 144 return ($this->limit > 0 && $rows > $this->limit ? $this->limit : $rows); 145 } 146 147 /** 148 * @see ResultSet::close() 149 */ 150 function close() 151 { 152 $ret = @mssql_free_result($this->result); 153 $this->result = false; 154 $this->fields = array(); 155 $this->limit = 0; 156 $this->offset = 0; 157 } 158 159 }
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 |