Sql.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 // ---------------------------------------------------------------------------
00003 // $Id$
00004 // ---------------------------------------------------------------------------
00005 // OpenRat Content Management System
00006 // Copyright (C) 2002-2006 Jan Dankert, jandankert@jandankert.de
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 // ---------------------------------------------------------------------------
00022 
00023 
00024 
00054 class Sql
00055 {
00059      var $src      = '';
00060      
00064      var $query    = '';
00065      
00066      
00070      var $data     = Array();
00071      
00094      var $param    = array();
00095      
00096      
00097      var $dbid     = '';
00098      
00099      
00103      function Sql( $query = '', $dbid='' )
00104      {
00105           $this->dbid  = $dbid;
00106           $this->parseSourceQuery( $query );
00107 
00108           $this->data  = array();
00109      }
00110 
00111 
00112 
00116      function parseSourceQuery( $query )
00117      {
00118           $this->src   = $query; // Wir merken uns die Ur-Abfrage, evtl. für Fehlermeldungen interessant.
00119           
00120           while( true )  // Schleife wird solange durchlaufen, solange Parameter gefunden werden.
00121           {
00122                $posKlLinks  = strpos($query,'{');
00123                $posKlRechts = strpos($query,'}');
00124                
00125                if   ( $posKlLinks === false )
00126                     break; // Schleife abbrechen, wenn kein Parameter mehr gefunden wird.
00127                     
00128                $nameParam = substr($query,$posKlLinks+1,$posKlRechts-$posKlLinks-1);  // Name Parameter
00129                
00130                if   ( !isset($this->param[$nameParam ]))
00131                     $this->param   [$nameParam ] = array();
00132                     
00133                $this->param[$nameParam ][] = $posKlLinks;
00134                
00135                $query = substr($query,0,$posKlLinks).substr($query,$posKlRechts+1);
00136           }
00137           
00138           $this->query = $query;
00139 
00140           // Tabellennamen in die Platzhalter setzen.
00141           // Dies ist noch OpenRat-spezifisch und sollte bei einer sauberen Abstraktion woanders gemacht werden. Aber wo?
00142           foreach( table_names($this->dbid) as $t=>$name )
00143           {
00144                $this->setParam($t,$name,false );
00145           }
00146           
00147 //        Html::debug($this->param);    
00148      }
00149 
00150 
00151 
00156      function setQuery( $query = '' )
00157      {
00158           $this->parseSourceQuery( $query );
00159 
00160           // Bereits vorhande Parameter setzen.        
00161           foreach( $this->data as $name=>$data )
00162           {
00163                if   ( $data['type']=='string'     ) $this->setString    ($name,$data['value'] );
00164                if   ( $data['type']=='stringlist' ) $this->setStringList($name,$data['value'] );
00165                if   ( $data['type']=='int'        ) $this->setInt       ($name,$data['value'] );
00166                if   ( $data['type']=='intlist'    ) $this->setIntList   ($name,$data['value'] );
00167                if   ( $data['type']=='null'       ) $this->setNull      ($name                );
00168           }
00169      }
00170 
00171 
00172 
00182      function setParam( $name,$value,$dieIfUnknown=true)
00183      {
00184 
00185           //   Nett gemeint, führt aber aktuell zu Fehlern, weil an vielen Stellen zu viele Parameter gefüllt werden.
00186           //   Daher erstmal deaktiviert.
00187           //        if   ( !isset($this->param[$name]) )
00188           //        {
00189           //             if   ( $dieIfUnknown )
00190           //                  die("parameter '$name' unknown. SQL=".$this->src);
00191           //             else
00192           //                  return;
00193           //        }
00194 
00195           if   ( !isset($this->param[$name]) )
00196                return; // Parameter nicht vorhanden.
00197 
00198           if   ( is_array($this->param[$name]) )
00199           {
00200                foreach( $this->param[$name] as $idx=>$xyz )
00201                {
00202                     $pos = $this->param[$name][$idx];
00203                     
00204                     $this->query = substr( $this->query,0,$pos ).$value.substr( $this->query,$pos );
00205                
00206                     foreach( $this->param as $pn=>$par)
00207                     {
00208                          foreach( $par as $i=>$p )
00209                          {
00210                               if   ( $p > $pos )
00211                                    $this->param[$pn][$i]=$p+strlen($value);
00212                          }
00213                     }
00214                     
00215                }
00216           }
00217           
00218           unset( $this->param[$name] );
00219      }
00220      
00221 
00222 
00230      function setVar( $name,$value )
00231      {
00232           if   ( is_string($value) )
00233                $this->setString( $name,$value );
00234 
00235           if   ( is_null($value) )
00236                $this->setNull( $name );
00237           
00238           if   ( is_int($value) )
00239                $this->setInt( $name,$value );
00240      }
00241 
00242 
00243      
00250      function setInt( $name,$value )
00251      {
00252           $this->data[ $name ] = array( 'type'=>'int','value'=>$value );
00253           
00254           $this->setParam($name,intval($value));
00255 //        $this->query = str_replace( '{'.$name.'}',intval($value),$this->query );
00256      }
00257 
00258 
00259 
00266      function setIntList( $name,$values )
00267      {
00268           $this->data[ $name ] = array( 'type'=>'intlist','value'=>$values );
00269           
00270           $values  = array_map('intval',$values);
00271           $this->setParam($name,implode(',',$values) );
00272 //        $this->query = str_replace( '{'.$name.'}',intval($value),$this->query );
00273      }
00274 
00275 
00276 
00283      function setString( $name,$value )
00284      {
00285           $this->data[ $name ] = array( 'type'=>'string','value'=>$value );
00286 
00287           //if ( defined('CONF_ADDSLASHES') && CONF_ADDSLASHES )
00288           
00289           $value = addslashes( $value );
00290 
00291           $value = "'".$value."'";
00292 
00293           $this->setParam($name,$value);
00294 //        $this->query = str_replace( '{'.$name.'}',$value,$this->query );
00295      }
00296 
00297 
00298 
00305      function setStringList( $name,$values )
00306      {
00307           $this->data[ $name ] = array( 'type'=>'stringlist','value'=>$values );
00308 
00309           $values  = array_map('addslashes',$values);
00310           
00311           $value = "'".implode("','",$values)."'";
00312 
00313           $this->setParam($name,$value);
00314      }
00315 
00316 
00317 
00325      function setBoolean( $name,$value )
00326      {
00327           if   ( $value )
00328                $this->setInt( $name,1 );
00329           else $this->setInt( $name,0 );
00330      }
00331 
00332 
00333 
00339      function setNull( $name )
00340      {
00341           $this->data[ $name ] = array( 'type'=>'null' );
00342           $this->setParam($name,'NULL');
00343      }
00344      
00345 
00346 
00351      function &query()
00352      {
00353           return $this->getQuery();
00354      }
00355 
00356 
00357 
00361      function &getQuery()
00362      {
00363 // Abbruch, wenn es noch nicht gefüllte Parameter gibt.
00364 // Da diese Methode eh kaum verwendet wird, erstmal deaktiviert.
00365 //        if   ( count($this->param) > 0 )
00366 //             die('parameters not bound: '+implode(',',$this->param) );
00367                
00368           return $this->query;
00369      }
00370 }
00371 
00372  
00373 ?>

Erzeugt am Thu May 14 00:55:49 2009 für OpenRat von  doxygen 1.5.8