• 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                         $this->query("SET SESSION SQL_MODE = ''");
00110                 }
00111 
00112                 return true;
00113         } //end constructor
00114 
00118         function _ilDBx() {
00119                 //$this->db->disconnect();
00120         } //end destructor
00121 
00125         function disconnect()
00126         {
00127 //              $this->db->disconnect();
00128         }
00129 
00139         function query($sql)
00140         {
00141                 $r = $this->db->query($sql);
00142 
00143                 if (DB::isError($r))
00144                 {
00145                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00146                 }
00147                 else
00148                 {
00149                         return $r;
00150                 }
00151         } //end function
00152 
00153 
00163         function getOne($sql)
00164         {
00165                 $r = $this->db->getOne($sql);
00166 
00167                 if (DB::isError($r))
00168                 {
00169                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00170                 }
00171                 else
00172                 {
00173                         return $r;
00174                 }
00175         } //end function
00176 
00177 
00181         function quote($a_query, $null_as_empty_string = true)
00182         {
00183                 if ($null_as_empty_string)
00184                 {
00185                         if ($a_query == "")
00186                         {
00187                                 $a_query = "";
00188                         }
00189                 }
00190 
00191                 // maybe quoteSmart should be used in the future
00192                 return $this->db->quote($a_query);
00193         }
00194 
00195 
00205         function getRow($sql,$mode = DB_FETCHMODE_OBJECT)
00206         {
00207                 $r = $this->db->getrow($sql,$mode);
00208 
00209                 if (DB::isError($r))
00210                 {
00211                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00212                 }
00213                 else
00214                 {
00215                         return $r;
00216                 }
00217         } //end function
00218 
00219 
00223         function getLastInsertId()
00224         {
00225                 $r = $this->query("SELECT LAST_INSERT_ID()");
00226                 $row = $r->fetchRow();
00227 
00228                 return $row[0] ? $row[0] : false;
00229         }
00230 
00236         function prepare($query)
00237         {
00238                 return $this->db->prepare($query);
00239         }
00240 
00247         function executeMultiple($stmt,$data)
00248         {
00249                 $res = $this->db->executeMultiple($stmt,$data);
00250 
00251                 if (DB::isError($res))
00252                 {
00253                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00254                 }
00255                 else
00256                 {
00257                         return $res;
00258                 }
00259         }
00260 
00267         function execute($stmt,$data)
00268         {
00269                 $res = $this->db->execute($stmt,$data);
00270 
00271                 if (DB::isError($res))
00272                 {
00273                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00274                 }
00275                 else
00276                 {
00277                         return $res;
00278                 }
00279         }
00280 
00289         function autoExecute($a_tablename,$a_fields,$a_mode = DB_AUTOQUERY_INSERT,$a_where = false)
00290         {
00291                 $res = $this->db->autoExecute($a_tablename,$a_fields,$a_mode,$a_where);
00292 
00293                 if (DB::isError($res))
00294                 {
00295                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00296                 }
00297                 else
00298                 {
00299                         return $res;
00300                 }
00301         }
00302         function checkQuerySize($a_query)
00303         {
00304                 global $lang;
00305 
00306                 if(strlen($a_query) >= $this->max_allowed_packet_size)
00307                 {
00308                         return false;
00309                 }
00310                 else
00311                 {
00312                         return true;
00313                 }
00314         }
00315 
00316         
00317 
00318 
00319         // PRIVATE
00320         function setMaxAllowedPacket()
00321         {
00322 
00323                 // GET MYSQL VERSION
00324                 $query = "SHOW VARIABLES LIKE 'version'";
00325                 $res = $this->db->query($query);
00326                 if(DB::isError($res))
00327                 {
00328                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00329                 }
00330                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00331                 {
00332                         $version = $row->Value;
00333                 }
00334 
00335                 // CHANG VALUE IF MYSQL VERSION > 4.0
00336                 if(substr($version,0,1) == "4")
00337                 {
00338                         ini_get("post_max_size");
00339                         $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
00340                         $this->db->query($query);
00341                         if(DB::isError($res))
00342                         {
00343                                 $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00344                         }
00345                 }
00346                 // STORE NEW max_size in member variable
00347                 $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
00348                 if(DB::isError($res))
00349                 {
00350                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00351                 }
00352                 $res = $this->db->query($query);
00353                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00354                 {
00355                         $this->max_allowed_packet_size = $row->Value;
00356                 }
00357                 #var_dump("<pre>",$this->max_allowed_packet_size,"<pre>");
00358                 return true;
00359         }
00360 
00366         function _lockTables($a_table_params)
00367         {
00368                 global $ilDB;
00369                 
00370                 $lock_str = 'LOCK TABLES ';
00371                 $counter = 0;
00372                 foreach($a_table_params as $table_name => $type)
00373                 {
00374                         $lock_str .= $counter++ ? ',' : '';
00375                         $lock_str .= $table_name.' '.$type;
00376                 }
00377 
00378                 $ilDB->query($lock_str);
00379 
00380                 return true;
00381         }
00382         function _unlockTables()
00383         {
00384                 global $ilDB;
00385                 
00386                 $ilDB->query('UNLOCK TABLES');
00387 
00388                 return true;
00389         }
00390         
00394         function getMySQLVersion()
00395         {
00396                 return mysql_get_server_info();
00397         }
00398         
00402         function isMysql4_1()
00403         {
00404                 $version = explode(".", $this->getMysqlVersion());
00405                 if ($version[0] == "4" && $version[1] == "1")
00406                 {
00407                         return true;
00408                 }
00409                 
00410                 return false;
00411         }
00412 
00419         function isMysql4_1OrHigher()
00420         {
00421                 $version = explode(".", $this->getMysqlVersion());
00422                 if ((int)$version[0] >= 5 ||
00423                         ((int)$version[0] == 4 && (int)$version[1] >= 1))
00424                 {
00425                         return true;
00426                 }
00427                 
00428                 return false;
00429         }
00430 
00434         function isMysql4_0OrHigher()
00435         {
00436                 $version = explode(".", $this->getMysqlVersion());
00437                 if((int) $version[0] >= 4)
00438                 {
00439                         return true;
00440                 }
00441                 return false;
00442         }               
00443 
00444 } //end Class
00445 ?>

Generated on Fri Dec 13 2013 11:57:53 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1