• 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 
00256         function checkQuerySize($a_query)
00257         {
00258                 global $lang;
00259 
00260                 if(strlen($a_query) >= $this->max_allowed_packet_size)
00261                 {
00262                         return false;
00263                 }
00264                 else
00265                 {
00266                         return true;
00267                 }
00268         }
00269 
00270         
00271 
00272 
00273         // PRIVATE
00274         function setMaxAllowedPacket()
00275         {
00276 
00277                 // GET MYSQL VERSION
00278                 $query = "SHOW VARIABLES LIKE 'version'";
00279                 $res = $this->db->query($query);
00280                 if(DB::isError($res))
00281                 {
00282                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00283                 }
00284                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00285                 {
00286                         $version = $row->Value;
00287                 }
00288 
00289                 // CHANG VALUE IF MYSQL VERSION > 4.0
00290                 if(substr($version,0,1) == "4")
00291                 {
00292                         ini_get("post_max_size");
00293                         $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
00294                         $this->db->query($query);
00295                         if(DB::isError($res))
00296                         {
00297                                 $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00298                         }
00299                 }
00300                 // STORE NEW max_size in member variable
00301                 $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
00302                 if(DB::isError($res))
00303                 {
00304                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00305                 }
00306                 $res = $this->db->query($query);
00307                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00308                 {
00309                         $this->max_allowed_packet_size = $row->Value;
00310                 }
00311                 #var_dump("<pre>",$this->max_allowed_packet_size,"<pre>");
00312                 return true;
00313         }
00314 
00320         function _lockTables($a_table_params)
00321         {
00322                 global $ilDB;
00323                 
00324                 $lock_str = 'LOCK TABLES ';
00325                 $counter = 0;
00326                 foreach($a_table_params as $table_name => $type)
00327                 {
00328                         $lock_str .= $counter++ ? ',' : '';
00329                         $lock_str .= $table_name.' '.$type;
00330                 }
00331 
00332                 $ilDB->query($lock_str);
00333 
00334                 return true;
00335         }
00336         function _unlockTables()
00337         {
00338                 global $ilDB;
00339                 
00340                 $ilDB->query('UNLOCK TABLES');
00341 
00342                 return true;
00343         }
00344         
00348         function getMySQLVersion()
00349         {
00350                 return mysql_get_server_info();
00351         }
00352         
00356         function isMysql4_1()
00357         {
00358                 $version = explode(".", $this->getMysqlVersion());
00359                 if ($version[0] == "4" && $version[1] == "1")
00360                 {
00361                         return true;
00362                 }
00363                 
00364                 return false;
00365         }
00366 
00373         function isMysql4_1OrHigher()
00374         {
00375                 $version = explode(".", $this->getMysqlVersion());
00376                 if ((int)$version[0] >= 5 ||
00377                         ((int)$version[0] == 4 && (int)$version[1] >= 1))
00378                 {
00379                         return true;
00380                 }
00381                 
00382                 return false;
00383         }
00384         
00385 
00386 } //end Class
00387 ?>

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1