• 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                 return true;
00105         } //end constructor
00106 
00110         function _ilDBx() {
00111                 //$this->db->disconnect();
00112         } //end destructor
00113 
00117         function disconnect()
00118         {
00119 //              $this->db->disconnect();
00120         }
00121 
00131         function query($sql)
00132         {
00133                 $r = $this->db->query($sql);
00134 
00135                 if (DB::isError($r))
00136                 {
00137                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00138                 }
00139                 else
00140                 {
00141                         return $r;
00142                 }
00143         } //end function
00144 
00145 
00149         function quote($a_query, $null_as_empty_string = true)
00150         {
00151                 if ($null_as_empty_string)
00152                 {
00153                         if ($a_query == "")
00154                         {
00155                                 $a_query = "";
00156                         }
00157                 }
00158 
00159                 // maybe quoteSmart should be used in the future
00160                 return $this->db->quote($a_query);
00161         }
00162 
00163 
00173         function getRow($sql,$mode = DB_FETCHMODE_OBJECT)
00174         {
00175                 $r = $this->db->getrow($sql,$mode);
00176 
00177                 if (DB::isError($r))
00178                 {
00179                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00180                 }
00181                 else
00182                 {
00183                         return $r;
00184                 }
00185         } //end function
00186 
00187 
00191         function getLastInsertId()
00192         {
00193                 $r = $this->query("SELECT LAST_INSERT_ID()");
00194                 $row = $r->fetchRow();
00195 
00196                 return $row[0];
00197         }
00198 
00204         function prepare($query)
00205         {
00206                 return $this->db->prepare($query);
00207         }
00208 
00215         function executeMultiple($stmt,$data)
00216         {
00217                 $res = $this->db->executeMultiple($stmt,$data);
00218 
00219                 if (DB::isError($res))
00220                 {
00221                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00222                 }
00223                 else
00224                 {
00225                         return $res;
00226                 }
00227         }
00228 
00235         function execute($stmt,$data)
00236         {
00237                 $res = $this->db->execute($stmt,$data);
00238 
00239                 if (DB::isError($res))
00240                 {
00241                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00242                 }
00243                 else
00244                 {
00245                         return $res;
00246                 }
00247         }
00248 
00249         function checkQuerySize($a_query)
00250         {
00251                 global $lang;
00252 
00253                 if(strlen($a_query) >= $this->max_allowed_packet_size)
00254                 {
00255                         return false;
00256                 }
00257                 else
00258                 {
00259                         return true;
00260                 }
00261         }
00262 
00263         
00264 
00265 
00266         // PRIVATE
00267         function setMaxAllowedPacket()
00268         {
00269 
00270                 // GET MYSQL VERSION
00271                 $query = "SHOW VARIABLES LIKE 'version'";
00272                 $res = $this->db->query($query);
00273                 if(DB::isError($res))
00274                 {
00275                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00276                 }
00277                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00278                 {
00279                         $version = $row->Value;
00280                 }
00281 
00282                 // CHANG VALUE IF MYSQL VERSION > 4.0
00283                 if(substr($version,0,1) == "4")
00284                 {
00285                         ini_get("post_max_size");
00286                         $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
00287                         $this->db->query($query);
00288                         if(DB::isError($res))
00289                         {
00290                                 $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00291                         }
00292                 }
00293                 // STORE NEW max_size in member variable
00294                 $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
00295                 if(DB::isError($res))
00296                 {
00297                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00298                 }
00299                 $res = $this->db->query($query);
00300                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00301                 {
00302                         $this->max_allowed_packet_size = $row->Value;
00303                 }
00304                 #var_dump("<pre>",$this->max_allowed_packet_size,"<pre>");
00305                 return true;
00306         }
00307 
00313         function _lockTables($a_table_params)
00314         {
00315                 global $ilDB;
00316                 
00317                 $lock_str = 'LOCK TABLES ';
00318                 $counter = 0;
00319                 foreach($a_table_params as $table_name => $type)
00320                 {
00321                         $lock_str .= $counter++ ? ',' : '';
00322                         $lock_str .= $table_name.' '.$type;
00323                 }
00324 
00325                 $ilDB->query($lock_str);
00326 
00327                 return true;
00328         }
00329         function _unlockTables()
00330         {
00331                 global $ilDB;
00332                 
00333                 $ilDB->query('UNLOCK TABLES');
00334 
00335                 return true;
00336         }
00337 
00338                 
00339 
00340 } //end Class
00341 ?>

Generated on Fri Dec 13 2013 08:00:14 for ILIAS Release_3_3_x_branch .rev 46803 by  doxygen 1.7.1