openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Sql.class.php (3040B)


      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 logger\Logger;
     21 use LogicException;
     22 use util\exception\DatabaseException;
     23 
     24 /**
     25  * SQL-Anweisung.<br>
     26  * <br>
     27  * Darstellen eines SQL-Statements incl. Methoden zum Fuellen von
     28  * Platzhaltern im SQL-Befehl.<br>
     29  * <br>
     30  * Beispiel<br>
     31  * <pre>
     32  * // Neues Objekt erzeugen mit SQL-Anweisung
     33  * $stmt = $db->sql('SELECT * FROM xy WHERE id={uid} AND name={name}');
     34  * 
     35  * // Parameter f�llen
     36  * $stmt->setInt   ('uid' ,1      );
     37  * $stmt->setString('name','peter');
     38  * 
     39  * // Fertige SQL-Anweisung verwenden
     40  * $stmt->execute();
     41  * </pre>
     42  * <br>
     43  * Ziele dieser Klasse sind:<br>
     44  * - Schreiben einfacher SQL-Anweisungen ohne Stringverarbeitung<br>
     45  * - Verhindern von SQL-Injection.<br>
     46  * <br>
     47  *   
     48  * @author Jan Dankert, $Author$
     49  * @version $Revision$
     50  * @package openrat.services
     51  */
     52 
     53 class Sql
     54 {
     55 	/**
     56 	 * SQL-Anweisung.
     57      * @type string
     58 	 */
     59 	var $query;
     60 	
     61 	/**
     62 	 * Ein 1-dimensionales Array mit den Positionen der Parameter.<br>
     63 	 * <br>
     64 	 * Beispiel:<br>
     65 	 * <pre>
     66 	 * 
     67 	 * Array
     68 	 * (
     69 	 *    [lid] => 16
     70 	 *    [oid] => 24
     71 	 * )
     72 	 * </pre>
     73 	 */
     74 	public $param    = array();
     75 
     76 
     77     /**
     78      * Erzeugt ein SQL-Objekt und analysiert die SQL-Anfrage.
     79      * @param string $query SQL-Query
     80      */
     81 	public function __construct( $query = '' )
     82 	{
     83 		$this->parseSourceQuery( $query );
     84 	}
     85 
     86 
     87 	/**
     88 	 * Die SQL-Anfrage wird auf Parameter untersucht.
     89 	 */
     90 	private function parseSourceQuery( $query )
     91 	{
     92 		Logger::trace( "SQL-query:\n$query" );
     93 		
     94 		while( true )  // Schleife wird solange durchlaufen, solange Parameter gefunden werden.
     95 		{
     96 			$posKlLinks  = strpos($query,'{');
     97 			$posKlRechts = strpos($query,'}');
     98 			
     99 			if 	( $posKlLinks === false || $posKlRechts === false )
    100 				break; // Schleife abbrechen, wenn kein Parameter mehr gefunden wird.
    101 				
    102 			$nameParam = substr($query,$posKlLinks+1,$posKlRechts-$posKlLinks-1);  // Name Parameter
    103 			
    104 			if	( isset($this->param[$nameParam ]))
    105 				throw new DatabaseException( "The named parameter '$nameParam'' is used more than one time in the SQL query:\n$query" );
    106 				
    107 			$this->param[$nameParam] = $posKlLinks;
    108 			
    109 			$query = substr($query,0,$posKlLinks).substr($query,$posKlRechts+1);
    110 		}
    111 		
    112 		$this->query = $query;
    113 
    114 	}
    115 
    116 
    117 }
    118 
    119  
    120 ?>