StatementCommon.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: StatementCommon.php,v 1.3 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 00029 abstract class StatementCommon { 00030 00035 protected $conn; 00036 00041 protected $resultSet; 00042 00047 protected $updateCount; 00048 00053 protected $warnings = array(); 00054 00059 protected $resultClass; 00060 00065 protected $stmt; 00066 00071 protected $limit = 0; 00072 00078 protected $offset = 0; 00079 00085 function __construct(Connection $conn) 00086 { 00087 $this->conn = $conn; 00088 } 00089 00097 public function setLimit($v) 00098 { 00099 $this->limit = (int) $v; 00100 } 00101 00106 public function getLimit() 00107 { 00108 return $this->limit; 00109 } 00110 00119 public function setOffset($v) 00120 { 00121 $this->offset = (int) $v; 00122 } 00123 00129 public function getOffset() 00130 { 00131 return $this->offset; 00132 } 00133 00141 public function close() 00142 { 00143 // do nothing here (subclasses will implement) 00144 } 00145 00157 public function execute($sql, $fetchmode = null) 00158 { 00159 00160 if (!$this->isSelect($sql)) { 00161 $this->updateCount = $this->executeUpdate($sql); 00162 return false; 00163 } else { 00164 $this->resultSet = $this->executeQuery($sql, $fetchmode); 00165 if ($this->resultSet->getRecordCount() === 0) { 00166 return false; 00167 } 00168 return true; 00169 } 00170 } 00171 00179 public function getResultSet() 00180 { 00181 return $this->resultSet; 00182 } 00183 00189 public function getUpdateCount() 00190 { 00191 return $this->updateCount; 00192 } 00193 00219 protected function isSelect($sql) 00220 { 00221 // is first word is SELECT, then return true, unless it's SELECT INTO ... 00222 // this doesn't, however, take comments into account ... 00223 $sql = trim($sql); 00224 return (stripos($sql, 'select') === 0 && stripos($sql, 'select into ') !== 0); 00225 } 00226 00236 public function executeQuery($sql, $fetchmode = null) 00237 { 00238 $this->updateCount = null; 00239 if ($this->limit > 0) { 00240 $this->conn->applyLimit($sql, $this->offset, $this->limit); 00241 } elseif ($this->offset > 0) { 00242 throw new SQLException('Cannot specify an offset without limit.'); 00243 } 00244 $this->resultSet = $this->conn->executeQuery($sql, $fetchmode); 00245 return $this->resultSet; 00246 } 00247 00255 public function executeUpdate($sql) 00256 { 00257 if ($this->resultSet) $this->resultSet->close(); 00258 $this->resultSet = null; 00259 $this->updateCount = $this->conn->executeUpdate($sql); 00260 return $this->updateCount; 00261 } 00262 00276 public function getMoreResults() 00277 { 00278 if ($this->resultSet) $this->resultSet->close(); 00279 $this->resultSet = null; 00280 return false; 00281 } 00282 00287 public function getConnection() 00288 { 00289 return $this->conn; 00290 } 00291 }

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS