Statement.class.php (4855B)
1 <?php 2 // OpenRat Content Management System 3 // Copyright (C) 2002-2006 Jan Dankert, jandankert@jandankert.de 4 // 5 // This program is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU General Public License 7 // as published by the Free Software Foundation; either version 2 8 // of the License, or (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19 namespace database; 20 use database\driver\PDODriver; 21 use logger\Logger; 22 use util\exception\DatabaseException; 23 24 /** 25 * Darstellung einer Datenbank-Abfrage. 26 * 27 * @author Jan Dankert 28 * @package openrat.database 29 */ 30 class Statement 31 { 32 33 /** 34 * SQL-Objekt. 35 * 36 * @var SQL 37 */ 38 var $sql; 39 40 /** 41 * Client. 42 * 43 * @var PDODriver 44 */ 45 var $client; 46 47 48 /** 49 * Datenbank-Konfiguration 50 * @var array 51 */ 52 var $conf; 53 54 /** 55 * Statement 56 * @var \PDOStatement 57 */ 58 var $stmt; 59 60 /** 61 * Statement constructor. 62 * @param $sql string Sql 63 * @param $client Database 64 * @param $conf array 65 */ 66 public function __construct($sql, $client, $conf ) 67 { 68 // Tabellen-Praefixe ergaenzen. 69 $this->conf = $conf; 70 $this->client = $client; 71 72 $sql = str_replace('{{',$conf['prefix'],$sql); 73 $sql = str_replace('}}',$conf['suffix'],$sql); 74 75 $this->sql = new Sql( $sql ); 76 77 // Statement an die Datenbank schicken 78 $this->stmt = $this->client->prepare( $this->sql->query,$this->sql->param ); 79 } 80 81 82 /** 83 * Ausfuehren einer Datenbankanfrage. 84 * 85 * @return Object (Result) 86 */ 87 public function execute() 88 { 89 // Ausfuehren... 90 return $this->client->execute($this->stmt, $this->sql); 91 } 92 93 94 /** 95 * Ermittelt genau 1 Datenbankergebnis aus einer SQL-Anfrage. 96 * Falls es mehrere Treffer gibt, wird die 1. Spalte aus der 1. Zeile genommen. 97 * 98 * @return String|null 99 */ 100 public function getOne() 101 { 102 $this->execute(); 103 104 return $this->client->fetchFirstColumn($this->stmt); 105 } 106 107 108 /** 109 * Ermittelt eine Zeile aus der Datenbank. 110 * 111 * @return array 112 */ 113 public function &getRow() 114 { 115 $this->execute(); 116 $row = $this->client->fetchAssocRow($this->stmt); 117 118 if ( ! is_array($row) ) 119 $row = array(); 120 121 return $row; 122 } 123 124 125 /** 126 * Ermittelt eine (die 1.) Spalte aus dem Datenbankergebnis. 127 * 128 * @return array 129 */ 130 public function getCol() 131 { 132 $this->execute(); 133 134 return $this->client->fetchAllFirstColumn($this->stmt); 135 } 136 137 138 /** 139 * Ermittelt ein assoziatives Array aus der Datenbank. 140 * 141 * @return array 142 */ 143 public function &getAssoc() 144 { 145 $this->execute(); 146 $results = array(); 147 148 while( $row = $this->client->fetchIndexedRow($this->stmt) ) 149 $results[ $row[0] ] = $row[1]; 150 151 return $results; 152 } 153 154 155 /** 156 * Ermittelt alle Datenbankergebniszeilen. 157 * 158 * @return array 159 */ 160 public function getAll() 161 { 162 $this->execute(); 163 164 return $this->client->fetchAllRows($this->stmt); 165 } 166 167 168 /** 169 * Setzt eine Ganzzahl als Parameter.<br> 170 * @param $name string 171 * @param $value integer 172 */ 173 function setInt( $name,$value ) 174 { 175 $this->client->bind( $this->stmt, $name, (int)$value ); 176 } 177 178 179 180 /** 181 * Setzt eine Ganzzahl als Parameter.<br> 182 * @param $name string 183 * @param $value integer 184 */ 185 function setIntOrNull( $name,$value ) 186 { 187 if ( is_int($value)) 188 $this->setInt( $name,$value); 189 else 190 $this->setNull( $name ); 191 } 192 193 194 /** 195 * Setzt eine Ganzzahl als Parameter.<br> 196 * @param $name string 197 * @param $value integer 198 */ 199 function setStringOrNull( $name,$value ) 200 { 201 if ( is_string($value)) 202 $this->setString( $name,$value); 203 else 204 $this->setNull( $name ); 205 } 206 207 208 209 /** 210 * Setzt eine Zeichenkette als Parameter.<br> 211 * 212 * @param $name string 213 * @param $value string 214 */ 215 function setString( $name,$value ) 216 { 217 $this->client->bind( $this->stmt, $name, (string)$value ); 218 } 219 220 221 222 /** 223 * Setzt einen bool'schen Wert als Parameter.<br> 224 * Ist der Parameterwert wahr, dann wird eine 1 gesetzt. Sonst 0.<br> 225 * 226 * @param $name string 227 * @param $value bool 228 229 */ 230 function setBoolean( $name,$value ) 231 { 232 if ( $value ) 233 $this->setInt( $name,1 ); 234 else 235 $this->setInt( $name,0 ); 236 } 237 238 239 240 /** 241 * Setzt einen Parameter auf den Wert <code>null</code>.<br> 242 * 243 * @param $name string Name des Parameters 244 */ 245 function setNull( $name ) 246 { 247 $this->client->bind( $this->stmt, $name, null ); 248 } 249 250 251 }