• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilDBx.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
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 
00025 
00026 //pear DB abstraction layer
00027 require_once ("DB.php");
00028 
00042 class ilDBx extends PEAR
00043 {
00049         var $error_class;
00050 
00055         var $db;
00056 
00061         var $result;
00062 
00067         var $max_allowed_packet_size;
00068 
00069 
00077         function ilDBx($dsn)
00078         {
00079                 //call parent constructor
00080                 $parent = get_parent_class($this);
00081                 $this->$parent();
00082 
00083                 //set up error handling
00084                 $this->error_class = new ilErrorHandling();
00085                 $this->setErrorHandling(PEAR_ERROR_CALLBACK, array($this->error_class,'errorHandler'));
00086 
00087                 //check dsn
00088                 if ($dsn=="")
00089                         $this->raiseError("no DSN given", $this->error_class->FATAL);
00090 
00091                 $this->dsn = $dsn;
00092 
00093                 //connect to database
00094                 $this->db = DB::connect($this->dsn, true);
00095 
00096                 //check error
00097                 if (DB::isError($this->db)) {
00098                         $this->raiseError($this->db->getMessage(), $this->error_class->FATAL);
00099                 }
00100 
00101                 // SET 'max_allowed_packet' (only possible for mysql version 4)
00102                 $this->setMaxAllowedPacket();
00103                 
00104                 // set names
00105                 // please also see modules/dateplaner/classes/class.ilCalInterface.php->setNames
00106                 if ($this->isMysql4_1OrHigher())
00107                 {
00108                         $this->query("SET NAMES utf8");
00109                 }
00110 
00111                 return true;
00112         } //end constructor
00113 
00117         function _ilDBx() {
00118                 //$this->db->disconnect();
00119         } //end destructor
00120 
00124         function disconnect()
00125         {
00126 //              $this->db->disconnect();
00127         }
00128 
00138         function query($sql)
00139         {
00140                 $r = $this->db->query($sql);
00141 
00142                 if (DB::isError($r))
00143                 {
00144                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00145                 }
00146                 else
00147                 {
00148                         return $r;
00149                 }
00150         } //end function
00151 
00152 
00156         function quote($a_query, $null_as_empty_string = true)
00157         {
00158                 if ($null_as_empty_string)
00159                 {
00160                         if ($a_query == "")
00161                         {
00162                                 $a_query = "";
00163                         }
00164                 }
00165 
00166                 // maybe quoteSmart should be used in the future
00167                 return $this->db->quote($a_query);
00168         }
00169 
00170 
00180         function getRow($sql,$mode = DB_FETCHMODE_OBJECT)
00181         {
00182                 $r = $this->db->getrow($sql,$mode);
00183 
00184                 if (DB::isError($r))
00185                 {
00186                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00187                 }
00188                 else
00189                 {
00190                         return $r;
00191                 }
00192         } //end function
00193 
00194 
00198         function getLastInsertId()
00199         {
00200                 $r = $this->query("SELECT LAST_INSERT_ID()");
00201                 $row = $r->fetchRow();
00202 
00203                 return $row[0];
00204         }
00205 
00211         function prepare($query)
00212         {
00213                 return $this->db->prepare($query);
00214         }
00215 
00222         function executeMultiple($stmt,$data)
00223         {
00224                 $res = $this->db->executeMultiple($stmt,$data);
00225 
00226                 if (DB::isError($res))
00227                 {
00228                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00229                 }
00230                 else
00231                 {
00232                         return $res;
00233                 }
00234         }
00235 
00242         function execute($stmt,$data)
00243         {
00244                 $res = $this->db->execute($stmt,$data);
00245 
00246                 if (DB::isError($res))
00247                 {
00248                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00249                 }
00250                 else
00251                 {
00252                         return $res;
00253                 }
00254         }
00255 
00264         function autoExecute($a_tablename,$a_fields,$a_mode = DB_AUTOQUERY_INSERT,$a_where = false)
00265         {
00266                 $res = $this->db->autoExecute($a_tablename,$a_fields,$a_mode,$a_where);
00267 
00268                 if (DB::isError($res))
00269                 {
00270                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00271                 }
00272                 else
00273                 {
00274                         return $res;
00275                 }
00276         }
00277         function checkQuerySize($a_query)
00278         {
00279                 global $lang;
00280 
00281                 if(strlen($a_query) >= $this->max_allowed_packet_size)
00282                 {
00283                         return false;
00284                 }
00285                 else
00286                 {
00287                         return true;
00288                 }
00289         }
00290 
00291         
00292 
00293 
00294         // PRIVATE
00295         function setMaxAllowedPacket()
00296         {
00297 
00298                 // GET MYSQL VERSION
00299                 $query = "SHOW VARIABLES LIKE 'version'";
00300                 $res = $this->db->query($query);
00301                 if(DB::isError($res))
00302                 {
00303                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00304                 }
00305                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00306                 {
00307                         $version = $row->Value;
00308                 }
00309 
00310                 // CHANG VALUE IF MYSQL VERSION > 4.0
00311                 if(substr($version,0,1) == "4")
00312                 {
00313                         ini_get("post_max_size");
00314                         $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
00315                         $this->db->query($query);
00316                         if(DB::isError($res))
00317                         {
00318                                 $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00319                         }
00320                 }
00321                 // STORE NEW max_size in member variable
00322                 $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
00323                 if(DB::isError($res))
00324                 {
00325                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00326                 }
00327                 $res = $this->db->query($query);
00328                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00329                 {
00330                         $this->max_allowed_packet_size = $row->Value;
00331                 }
00332                 #var_dump("<pre>",$this->max_allowed_packet_size,"<pre>");
00333                 return true;
00334         }
00335 
00341         function _lockTables($a_table_params)
00342         {
00343                 global $ilDB;
00344                 
00345                 $lock_str = 'LOCK TABLES ';
00346                 $counter = 0;
00347                 foreach($a_table_params as $table_name => $type)
00348                 {
00349                         $lock_str .= $counter++ ? ',' : '';
00350                         $lock_str .= $table_name.' '.$type;
00351                 }
00352 
00353                 $ilDB->query($lock_str);
00354 
00355                 return true;
00356         }
00357         function _unlockTables()
00358         {
00359                 global $ilDB;
00360                 
00361                 $ilDB->query('UNLOCK TABLES');
00362 
00363                 return true;
00364         }
00365         
00369         function getMySQLVersion()
00370         {
00371                 return mysql_get_server_info();
00372         }
00373         
00377         function isMysql4_1()
00378         {
00379                 $version = explode(".", $this->getMysqlVersion());
00380                 if ($version[0] == "4" && $version[1] == "1")
00381                 {
00382                         return true;
00383                 }
00384                 
00385                 return false;
00386         }
00387 
00394         function isMysql4_1OrHigher()
00395         {
00396                 $version = explode(".", $this->getMysqlVersion());
00397                 if ((int)$version[0] >= 5 ||
00398                         ((int)$version[0] == 4 && (int)$version[1] >= 1))
00399                 {
00400                         return true;
00401                 }
00402                 
00403                 return false;
00404         }
00405 
00409         function isMysql4_0OrHigher()
00410         {
00411                 $version = explode(".", $this->getMysqlVersion());
00412                 if((int) $version[0] >= 4)
00413                 {
00414                         return true;
00415                 }
00416                 return false;
00417         }               
00418 
00419 } //end Class
00420 ?>

Generated on Fri Dec 13 2013 10:18:26 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1