db/db.inc.php

00001 <?php if(!function_exists('startedIndexPhp')) { header("location:../index.php"); exit();}
00002 
00003 
00004 
00005 class MysqlException extends Exception {
00006   public $backtrace;
00007 
00008   public function __construct($message=false, $code=false) {
00009     global $sql_obj;
00010     $this->message="";
00011     if($message) {
00012         $this->message="<pre>".$message;
00013     }
00014 
00015     ### define local variables for error.log ###
00016 
00017     $this->message .= $sql_obj->error;
00018 
00019     if(!$code) {
00020       $this->code = $sql_obj->errno;
00021     }
00022     #$this->backtrace = debug_backtrace();
00023 
00024 
00025     if(function_exists('mysql_error') && mysql_error()) {
00026         $mysql_error= mysql_error();
00027     }
00028     else if(confGet('DB_TYPE') == 'mysqli' && $sql_obj && function_exists('mysqli_error') && $sql_obj->getConnect() && mysqli_error($sql_obj->getConnect())) {
00029         $mysql_error= mysqli_error($sql_obj->getConnect());
00030     }
00031     else {
00032         $mysql_error = $sql_obj->error;
00033     }
00034 
00039     trigger_error(sprintf(__("Database exception. Please read %s next steps on database errors.%s") , "<a href=http://streber.pixtur.de/index.php?go=taskView&tsk=1272'>", "</a>"),E_USER_ERROR);
00040   }
00041 }
00042 
00043 interface DB_Connection {
00044   public function prepare($query);
00045   public function execute($query);
00046 }
00047 
00048 interface DB_Statement {
00049   public function execute();
00050   public function bind_param($key, $value);
00051   public function fetch_row();
00052   public function fetch_assoc();
00053   public function fetchall_assoc();
00054 }
00055 
00056 class DB_Mysql implements DB_Connection
00057 {
00058   protected $user;
00059   protected $pass;
00060   protected $dbhost;
00061   protected $dbname;
00062   protected $dbh;
00063 
00064   public function __construct($user=NULL, $pass=NULL, $dbhost=NULL, $dbname=NULL)
00065   {
00066 
00067     if($user) {
00068         $this->user= $user;
00069     }
00070     else {
00071         $this->user = confGet('DB_USERNAME');
00072     }
00073 
00074     if($pass) {
00075         $this->pass= $pass;
00076     }
00077     else {
00078         $this->pass = confGet('DB_PASSWORD');
00079     }
00080     if($dbhost) {
00081         $this->dbhost= $dbhost;
00082     }
00083     else {
00084         $this->dbhost = confGet('HOSTNAME');
00085     }
00086 
00087     if($dbname) {
00088         $this->dbname= $dbname;
00089     }
00090     else {
00091         $this->dbname = confGet('DB_NAME');
00092     }
00093 
00094 
00095   }
00096 
00097 
00098   protected function connect()
00099   {
00100 
00101     global $sql_obj;
00102     if(!is_object($sql_obj)){
00103         $sql_obj = new sql_class($this->dbhost, $this->user, $this->pass, $this->dbname);
00104     }
00105 
00106     if(!$sql_obj->connect()) {
00107       throw new MysqlException;
00108     }
00109     if(!$sql_obj->selectdb()) {
00110       throw new MysqlException;
00111     }
00112 
00113     ### enable utf8 encoding
00114     if(confGet('DB_USE_UTF8_ENCODING')) {
00115         $sql_obj->execute('SET NAMES utf8;');
00116         $sql_obj->execute('SET CHARACTER SET utf8;');
00117     }
00118   }
00119 
00120 
00121 
00122     public function execute($query)
00123     {
00124         global $sql_obj;
00125         if(!is_object($sql_obj)){
00126             $sql_obj = new sql_class($this->dbhost, $this->user, $this->pass, $this->dbname);
00127         }
00128         if(!$sql_obj->connect()) {
00129           throw new MysqlException;
00130         }
00131         $ret = $sql_obj->execute($query);
00132         if(!$ret) {
00133             throw new MysqlException;
00134         }
00135         else if(!is_resource($ret)) {
00136             return TRUE;
00137         }
00138         else {
00139             $stmt = new DB_MysqlStatement($sql_obj, $query);
00140             $stmt->result = $ret;
00141             return $stmt;
00142         }
00143     }
00144 
00145     public function prepare($query)
00146     {
00147         global $sql_obj;
00148         if(!is_object($sql_obj)) {
00149           $this->connect();
00150         }
00151 
00152         return new DB_MysqlStatement($sql_obj, $query);
00153     }
00154 
00155     public function lastId()
00156     {
00157         #echo 'lastID '.$this->id.'<br />';
00158         global $sql_obj;
00159         if(!is_object($sql_obj)) {
00160           $this->connect();
00161         }
00162         #echo 'lastID '.$sql_obj->lastId().'<br />';
00163         return $sql_obj->lastId();
00164     }
00165 
00166 
00173     public   function getVersion()
00174     {
00175         global $sql_obj;
00176         if(!is_object($sql_obj)){
00177             $sql_obj = new sql_class($this->dbhost, $this->user, $this->pass, $this->dbname);
00178         }
00179 
00180         if(!$sql_obj->connect()) {
00181             ### can't connect db... ###
00182             return NULL;
00183         }
00184         if(!$sql_obj->selectdb()) {
00185 
00186             ### can't select... ###
00187             return NULL;
00188         }
00189 
00190         ### get version ###
00191         $prefix= confGet('DB_TABLE_PREFIX');
00192 
00193         if($sql_obj->execute("select * from {$prefix}db where updated is NULL")) {
00194             $row = $sql_obj->fetchArray();
00195             return $row;
00196         }
00197         else {
00198             return NULL;
00199         }
00200     }
00201 
00202 }
00203 
00204 class DB_MysqlStatement implements DB_Statement
00205 {
00206     public $result;
00207     public $binds;
00208     public $query;
00209     public $dbh;
00210     public function __construct($dbh, $query) {
00211         global $sql_obj;
00212         $this->query = $query;
00213         $this->dbh = $dbh;
00214 
00215         if(!is_object($dbh)) {
00216             throw new MysqlException("Not a valid database connection");
00217         }
00218     }
00219     public function bind_param($ph, $pv) {
00220     $this->binds[$ph] = $pv;
00221 
00222     return $this;
00223 }
00224 
00225   #-------------------------------------------------
00226   # execute()
00227   # - requires list of params
00228   #-------------------------------------------------
00229   public function execute() {
00230         global $sql_obj;
00231         $args = func_get_args();
00232         #if(count(func_get_args())) {
00233         #    throw new Exception("pass values as array");
00234         #}
00235 
00236         #--- bind args in member with 1 ----
00237         $this->binds=array();
00238         foreach($args as $index => $name) {
00239             $this->binds[$index + 1] = $name;
00240         }
00241 
00242         $cnt = count($args);
00243         $query = $this->query;
00244 
00245         foreach ($this->binds as $ph => $pv) {
00246             #$query = @str_replace(":$ph", , $query);
00247             $query= preg_replace("/ƒ$ph\b/", "'".$pv."'", $query);
00248         }
00249 
00250         if(!$sql_obj->execute($query)) {
00251             throw new MysqlException("Querry=".$query."\n");
00252         }
00253 
00254         #--- cound traffic for debug-output ----
00255         #global $DB_ITEMS_LOADED;
00256         #$DB_ITEMS_LOADED+= count($this->result);
00257 
00258         return $this;
00259     }
00260 
00261     public function fetch_row()
00262     {
00263         global $sql_obj;
00264         return $sql_obj->fetchRow();
00265     }
00266 
00267     public function fetch_assoc()
00268     {
00269         global $sql_obj;
00270         $row=array();
00271 
00272         if($res=$sql_obj->fetchArray()) {
00273             foreach($res as $key=>$value) {
00274 
00275                 #$row[$key]= stripslashes(stripslashes(stripslashes($value)));
00276                 $row[$key]= stripslashes($value);
00277             }
00278         }
00279         return $row;
00280     }
00281 
00282     public function fetchall_assoc()
00283     {
00284         global $sql_obj;
00285         $retval = array();
00286         while($row = $this->fetch_assoc()) {
00287             foreach($row as $key=>$value) {
00288                 #$row[$key]= stripslashes(stripslashes(stripslashes($row[$key])));
00289                 $row[$key]= stripslashes($row[$key]);
00290             }
00291             $retval[] = $row;
00292 
00293             #--- cound traffic for debug-output ----
00294             global $DB_ITEMS_LOADED;
00295             $DB_ITEMS_LOADED+= count($row);
00296 
00297         }
00298         return $retval;
00299     }
00300 }
00301 
00302 class DB_Result {
00303   protected $stmt;
00304   protected $result = array();
00305   private $rowIndex = 0;
00306   private $currIndex = 0;
00307   private $done = false;
00308 
00309   public function __construct(DB_Statement $stmt)
00310   {
00311     $this->stmt = $stmt;
00312 
00313   }
00314   public function first()
00315   {
00316     if(!$this->result) {
00317       $this->result[$this->rowIndex++] = $this->stmt->fetch_assoc();
00318     }
00319     $this->currIndex = 0;
00320     return $this;
00321   }
00322   public function last()
00323   {
00324     if(!$this->done) {
00325       array_push($this->result, $this->stmt->fetchall_assoc());
00326     }
00327     $this->done = true;
00328     $this->currIndex = $this->rowIndex = count($this->result) - 1;
00329     return $this;
00330   }
00331   public function next()
00332   {
00333     if($this->done) {
00334       return false;
00335     }
00336     $offset = $this->currIndex + 1;
00337     if(!$this->result[$offset]) {
00338       $row = $this->stmt->fetch_assoc();
00339       if(!$row) {
00340         $this->done = true;
00341         return false;
00342       }
00343       $this->result[$offset] = $row;
00344       ++$this->rowIndex;
00345       ++$this->currIndex;
00346       return $this;
00347     }
00348     else {
00349       ++$this->currIndex;
00350       return $this;
00351     }
00352   }
00353   public function prev()
00354   {
00355     if($this->currIndex == 0) {
00356       return false;
00357     }
00358     --$this->currIndex;
00359     return $this;
00360   }
00361   public function __get($value)
00362   {
00363     if(array_key_exists($value, $this->result[$this->currIndex])) {
00364       return $this->result[$this->currIndex][$value];
00365     }
00366   }
00367 }
00368 
00369 require_once(dirname(__FILE__)."/../".confGet('DIR_SETTINGS').confGet('FILE_DB_SETTINGS'));
00370 
00371 
00372 
00373 class DB_Mysql_Debug extends DB_Mysql {
00374   protected $elapsedTime;
00375   public function execute($query) {
00376     // set timer;
00377     parent::execute($query);
00378     // end timer;
00379   }
00380   public function getElapsedTime() {
00381     return $this->$elapsedTime;
00382   }
00383 }
00384 
00385 ?>

Generated on Sun Mar 4 17:19:28 2007 for streber by  doxygen 1.5.1-p1