00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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