Query.php

Go to the documentation of this file.
00001 <?php 00002 00003 /* 00004 * $Id: Query.php,v 1.7 2004/04/24 14:45:39 hlellelid Exp $ 00005 * 00006 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00007 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00008 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00009 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00010 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00011 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00012 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00013 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00014 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00015 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00016 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * This software consists of voluntary contributions made by many individuals 00019 * and is licensed under the LGPL. For more information please see 00020 * <http://creole.phpdb.org>. 00021 */ 00022 00038 class Query { 00039 00041 protected $conn; 00042 00044 protected $sql; 00045 00047 protected $max = 0; 00048 00050 protected $start = 0; 00051 00057 public function __construct(Connection $conn, $sql = null) 00058 { 00059 $this->conn = $conn; 00060 $this->sql = $sql; 00061 } 00062 00067 public function setSql($sql) 00068 { 00069 $this->sql = $sql; 00070 } 00071 00076 public function setStart($v) 00077 { 00078 $this->start = $v; 00079 } 00080 00086 public function setMax($v) 00087 { 00088 $this->max = $v; 00089 } 00090 00096 public function getRows() 00097 { 00098 $stmt = $this->conn->createStatement(); 00099 if ($this->max) $stmt->setLimit($this->max); 00100 if ($this->start) $stmt->setOffset($this->start); 00101 $rs = $stmt->executeQuery($this->sql); 00102 $results = array(); 00103 while($rs->next()) { 00104 $results[] = $rs->getRow(); 00105 } 00106 $rs->close(); 00107 $stmt->close(); 00108 return $results; 00109 } 00110 00117 public function getRow() 00118 { 00119 $stmt = $this->conn->createStatement(); 00120 if ($this->max) $stmt->setLimit($this->max); 00121 if ($this->start) $stmt->setOffset($this->start); 00122 $rs = $stmt->executeQuery($this->sql); 00123 $rs->next(); 00124 $results = $rs->getRow(); 00125 $rs->close(); 00126 $stmt->close(); 00127 return $results; 00128 } 00129 00135 public function getCol() 00136 { 00137 $stmt = $this->conn->createStatement(); 00138 if ($this->max) $stmt->setLimit($this->max); 00139 if ($this->start) $stmt->setOffset($this->start); 00140 $rs = $stmt->executeQuery($this->sql); 00141 $results = array(); 00142 while($rs->next()) { 00143 $results[] = array_shift($rs->getRow()); 00144 } 00145 $rs->close(); 00146 $stmt->close(); 00147 return $results; 00148 } 00149 00155 public function getOne() 00156 { 00157 $stmt = $this->conn->createStatement(); 00158 if ($this->max) $stmt->setLimit($this->max); 00159 if ($this->start) $stmt->setOffset($this->start); 00160 $rs = $stmt->executeQuery($this->sql); 00161 $rs->next(); 00162 $res = array_shift($rs->getRow()); 00163 $rs->close(); 00164 $stmt->close(); 00165 return $res; 00166 } 00167 00210 public function getAssoc($scalar = false) 00211 { 00212 $stmt = $this->conn->createStatement(); 00213 if ($this->max) $stmt->setLimit($this->max); 00214 if ($this->start) $stmt->setOffset($this->start); 00215 $rs = $stmt->executeQuery($this->sql); 00216 00217 $numcols = null; 00218 $results = array(); 00219 while($rs->next()) { 00220 $fields = $rs->getRow(); 00221 if ($numcols === null) { 00222 $numcols = count($fields); 00223 } 00224 if (!$scalar || ($numcols > 2)) { 00225 $results[ array_shift($fields) ] = array_values($fields); 00226 } else { 00227 $results[ array_shift($fields) ] = array_shift($fields); 00228 } 00229 } 00230 00231 $rs->close(); 00232 $stmt->close(); 00233 00234 return $results; 00235 } 00236 00256 public function getDataSet() 00257 { 00258 include_once 'jargon/QueryDataSet.php'; 00259 $qds = new QueryDataSet($this->conn, $this->sql); 00260 $qds->fetchRecords($this->start, $this->max); 00261 return $qds; 00262 } 00263 } 00264

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS