File modules/database/Statement.class.php

Last commit: Thu Feb 16 01:04:38 2023 +0100	Jan Dankert	New: Tags for base objects.
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 try { 91 return $this->client->execute($this->stmt, $this->sql); 92 } catch( \Exception $e ) { 93 throw new DatabaseException('Database statement failed'."\n".$this->sql->query,$e); 94 } 95 } 96 97 98 /** 99 * Ermittelt genau 1 Datenbankergebnis aus einer SQL-Anfrage. 100 * Falls es mehrere Treffer gibt, wird die 1. Spalte aus der 1. Zeile genommen. 101 * 102 * @return String|null 103 */ 104 public function getOne() 105 { 106 $this->execute(); 107 108 return $this->client->fetchFirstColumn($this->stmt); 109 } 110 111 112 /** 113 * Ermittelt eine Zeile aus der Datenbank. 114 * 115 * @return array 116 */ 117 public function &getRow() 118 { 119 $this->execute(); 120 $row = $this->client->fetchAssocRow($this->stmt); 121 122 if ( ! is_array($row) ) 123 $row = array(); 124 125 return $row; 126 } 127 128 129 /** 130 * Ermittelt eine (die 1.) Spalte aus dem Datenbankergebnis. 131 * 132 * @return array 133 */ 134 public function getCol() 135 { 136 $this->execute(); 137 138 return $this->client->fetchAllFirstColumn($this->stmt); 139 } 140 141 142 /** 143 * Ermittelt ein assoziatives Array aus der Datenbank. 144 * 145 * @return array 146 */ 147 public function &getAssoc() 148 { 149 $this->execute(); 150 $results = array(); 151 152 while( $row = $this->client->fetchIndexedRow($this->stmt) ) 153 $results[ $row[0] ] = $row[1]; 154 155 return $results; 156 } 157 158 159 /** 160 * Ermittelt alle Datenbankergebniszeilen. 161 * 162 * @return array 163 */ 164 public function getAll() 165 { 166 $this->execute(); 167 168 return $this->client->fetchAllRows($this->stmt); 169 } 170 171 172 /** 173 * Setzt eine Ganzzahl als Parameter.<br> 174 * @param $name string 175 * @param $value integer 176 */ 177 function setInt( $name,$value ) 178 { 179 $this->client->bind( $this->stmt, $name, (int)$value ); 180 } 181 182 183 184 /** 185 * Setzt eine Ganzzahl als Parameter.<br> 186 * @param $name string 187 * @param $value integer 188 */ 189 function setIntOrNull( $name,$value ) 190 { 191 if ( is_int($value)) 192 $this->setInt( $name,$value); 193 else 194 $this->setNull( $name ); 195 } 196 197 198 /** 199 * Setzt eine Ganzzahl als Parameter.<br> 200 * @param $name string 201 * @param $value integer 202 */ 203 function setStringOrNull( $name,$value ) 204 { 205 if ( is_string($value)) 206 $this->setString( $name,$value); 207 else 208 $this->setNull( $name ); 209 } 210 211 212 213 /** 214 * Setzt eine Zeichenkette als Parameter.<br> 215 * 216 * @param $name string 217 * @param $value string 218 */ 219 function setString( $name,$value ) 220 { 221 $this->client->bind( $this->stmt, $name, (string)$value ); 222 } 223 224 225 226 /** 227 * Setzt einen bool'schen Wert als Parameter.<br> 228 * Ist der Parameterwert wahr, dann wird eine 1 gesetzt. Sonst 0.<br> 229 * 230 * @param $name string 231 * @param $value bool 232 233 */ 234 function setBoolean( $name,$value ) 235 { 236 if ( $value ) 237 $this->setInt( $name,1 ); 238 else 239 $this->setInt( $name,0 ); 240 } 241 242 243 244 /** 245 * Setzt einen Parameter auf den Wert <code>null</code>.<br> 246 * 247 * @param $name string Name des Parameters 248 */ 249 function setNull( $name ) 250 { 251 $this->client->bind( $this->stmt, $name, null ); 252 } 253 254 255 }
Download modules/database/Statement.class.php
History Thu, 16 Feb 2023 01:04:38 +0100 Jan Dankert New: Tags for base objects. Sun, 7 Mar 2021 00:10:20 +0100 Jan Dankert Refactoring: Hopefully more performance while accessing the database resultsets. Sat, 6 Mar 2021 23:32:55 +0100 Jan Dankert Fix: Forgot to execute the query ;) Sat, 6 Mar 2021 22:11:06 +0100 Jan Dankert Cleanup: PDODriver#fetchrow() now only needs 1 argument. Mon, 30 Nov 2020 09:57:36 +0100 Jan Dankert Fix: aborting transaction before changing the database connection; Refactoring: Cleanup databases Sat, 31 Oct 2020 00:43:29 +0100 Jan Dankert New: Support for OpenId Connect; Removed: Support for LDAP. Sat, 29 Aug 2020 03:23:06 +0200 Jan Dankert Refactoring: Improved Exception-Handling; New: Generating pages using a page context which considers page aliases. Thu, 30 May 2019 00:10:13 +0200 Jan Dankert New: Aliases integriert, jedoch noch nicht sprachspezifisch (folgt noch). Thu, 6 Sep 2018 00:13:50 +0200 Jan Dankert Beim Testen von Querys muss berücksichtigt werden, dass bereits beim Prepare der Anfragen eine Exception auftreten kann. Sat, 21 Jul 2018 00:00:21 +0200 Jan Dankert Datenbank-Modul weiter aufgeräumt und alten Kram entfernt. Das erzeugte Prepared-Statement wird nun im Statement gespeichert, da wo es hingehört. Fri, 8 Dec 2017 22:29:13 +0100 Jan Dankert Neuer Objekttyp 'url' mit Modelklasse, Actionklasse und anderen Änderungen. Darüber hinaus benötigt die Methode query() aus dem Statement keinen Parameter mehr. Fri, 8 Dec 2017 00:06:49 +0100 Jan Dankert Nur kleine Optimierungen im Database-Modul. Fri, 8 Dec 2017 00:00:13 +0100 Jan Dankert Refactoring: Statement::getOne() und andere benötigen keinen Parameter mehr. Thu, 7 Dec 2017 23:18:17 +0100 Jan Dankert Refactoring: Statement::getAll() benötigt keinen Parameter mehr. Tue, 5 Dec 2017 23:56:04 +0100 Jan Dankert Datenbank-Klassen auf Namespace umgestellt. Sun, 3 Dec 2017 03:11:38 +0100 Jan Dankert Refactoring: Datenbank-Funktionen in ein eigenes "Modul" ausgelagert.