MSSQLResultSet.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MSSQLResultSet.php,v 1.20 2004/03/20 04:16:49 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00022 require_once 'creole/ResultSet.php'; 00023 require_once 'creole/common/ResultSetCommon.php'; 00024 00036 class MSSQLResultSet extends ResultSetCommon implements ResultSet { 00037 00042 private $offset = 0; 00043 00048 private $limit = 0; 00049 00057 public function _setOffset($offset) 00058 { 00059 $this->offset = $offset; 00060 if ($offset > 0) { 00061 $this->seek(0); // 0 becomes $offset by seek() method 00062 } 00063 } 00064 00072 public function _setLimit($limit) 00073 { 00074 $this->limit = $limit; 00075 } 00076 00080 function seek($rownum) 00081 { 00082 // support emulated OFFSET 00083 $actual = $rownum + $this->offset; 00084 00085 if (($this->limit > 0 && $rownum >= $this->limit) || $rownum < 0) { 00086 // have to check for rownum < 0, because mssql_seek() won't 00087 // complain if the $actual is valid. 00088 return false; 00089 } 00090 00091 // MSSQL rows start w/ 0, but this works, because we are 00092 // looking to move the position _before_ the next desired position 00093 if (!@mssql_data_seek($this->result, $actual)) { 00094 return false; 00095 } 00096 00097 $this->cursorPos = $rownum; 00098 return true; 00099 } 00100 00104 function next() 00105 { 00106 // support emulated LIMIT 00107 if ( $this->limit > 0 && ($this->cursorPos >= $this->limit) ) { 00108 $this->afterLast(); 00109 return false; 00110 } 00111 00112 $this->fields = mssql_fetch_array($this->result, $this->fetchmode); 00113 00114 if (!$this->fields) { 00115 if ($errmsg = mssql_get_last_message()) { 00116 throw new SQLWarning("Error fetching result", $errmsg); 00117 } else { 00118 // We've advanced beyond end of recordset. 00119 $this->afterLast(); 00120 return false; 00121 } 00122 } 00123 00124 if (!$this->ignoreAssocCase) { 00125 $this->fields = array_change_key_case($this->fields, CASE_LOWER); 00126 } 00127 00128 // Advance cursor position 00129 $this->cursorPos++; 00130 return true; 00131 } 00132 00136 function getRecordCount() 00137 { 00138 $rows = @mssql_num_rows($this->result); 00139 if ($rows === null) { 00140 throw new SQLException('Error getting record count', mssql_get_last_message()); 00141 } 00142 // adjust count based on emulated LIMIT/OFFSET 00143 $rows -= $this->offset; 00144 return ($this->limit > 0 && $rows > $this->limit ? $this->limit : $rows); 00145 } 00146 00150 function close() 00151 { 00152 $ret = @mssql_free_result($this->result); 00153 $this->result = false; 00154 $this->fields = array(); 00155 $this->limit = 0; 00156 $this->offset = 0; 00157 } 00158 00159 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS