• Main Page
  • Related Pages
  • Modules
  • 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 
00041 class ilDBx extends PEAR
00042 {
00048         var $error_class;
00049 
00054         var $db;
00055 
00060         var $result;
00061 
00066         var $max_allowed_packet_size;
00067 
00068 
00076         function ilDBx($dsn)
00077         {
00078                 //call parent constructor
00079                 $parent = get_parent_class($this);
00080                 $this->$parent();
00081 
00082                 //set up error handling
00083                 $this->error_class = new ilErrorHandling();
00084                 $this->setErrorHandling(PEAR_ERROR_CALLBACK, array($this->error_class,'errorHandler'));
00085 
00086                 //check dsn
00087                 if ($dsn=="")
00088                         $this->raiseError("no DSN given", $this->error_class->FATAL);
00089 
00090                 $this->dsn = $dsn;
00091 
00092                 //connect to database
00093                 $this->db = DB::connect($this->dsn, true);
00094 
00095                 //check error
00096                 if (DB::isError($this->db)) {
00097                         $this->raiseError($this->db->getMessage(), $this->error_class->FATAL);
00098                 }
00099 
00100                 // SET 'max_allowed_packet' (only possible for mysql version 4)
00101                 $this->setMaxAllowedPacket();
00102                 
00103                 // NOTE: Three sourcecodes use this or a similar handling:
00104                 // - classes/class.ilDBx.php
00105                 // - calendar/classes/class.ilCalInterface.php->setNames
00106                 // - setup/classes/class.ilClient.php
00107                 if ($this->isMysql4_1OrHigher())
00108                 {
00109                         $this->query("SET NAMES utf8");
00110                         $this->query("SET SESSION SQL_MODE = ''");
00111                 }
00112 
00113                 return true;
00114         } //end constructor
00115 
00119         function _ilDBx(){
00120                 //$this->db->disconnect();
00121         } //end destructor
00122 
00126         function disconnect()
00127         {
00128 //              $this->db->disconnect();
00129         }
00130 
00140         function query($sql)
00141         {
00142                 $r = $this->db->query($sql);
00143 
00144                 if (DB::isError($r))
00145                 {
00146                         $err = "<br>Details: ".mysql_error();
00147                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql.$err."</font>", $this->error_class->FATAL);
00148                 }
00149                 else
00150                 {
00151                         return $r;
00152                 }
00153         } //end function
00154 
00155 
00165         function getOne($sql)
00166         {
00167                 $r = $this->db->getOne($sql);
00168 
00169                 if (DB::isError($r))
00170                 {
00171                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00172                 }
00173                 else
00174                 {
00175                         return $r;
00176                 }
00177         } //end function
00178 
00179 
00183         function quote($a_query, $null_as_empty_string = true)
00184         {
00185                 if ($null_as_empty_string)
00186                 {
00187                         if ($a_query == "")
00188                         {
00189                                 $a_query = "";
00190                         }
00191                 }
00192 
00193                 if (method_exists($this->db, "quoteSmart"))
00194                 {
00195                         return $this->db->quoteSmart($a_query);
00196                 }
00197                 else
00198                 {
00199                         return $this->db->quote($a_query);
00200                 }
00201         }
00202         
00208         function affectedRows()
00209         {
00210                 return $this->db->affectedRows();
00211         }
00212 
00222         function getRow($sql,$mode = DB_FETCHMODE_OBJECT)
00223         {
00224                 $r = $this->db->getrow($sql,$mode);
00225 
00226                 if (DB::isError($r))
00227                 {
00228                         $this->raiseError($r->getMessage()."<br><font size=-1>SQL: ".$sql."</font>", $this->error_class->FATAL);
00229                 }
00230                 else
00231                 {
00232                         return $r;
00233                 }
00234         } //end function
00235 
00236 
00240         function getLastInsertId()
00241         {
00242                 $r = $this->query("SELECT LAST_INSERT_ID()");
00243                 $row = $r->fetchRow();
00244 
00245                 return $row[0] ? $row[0] : false;
00246         }
00247 
00253         function prepare($query)
00254         {
00255                 return $this->db->prepare($query);
00256         }
00257 
00264         function executeMultiple($stmt,$data)
00265         {
00266                 $res = $this->db->executeMultiple($stmt,$data);
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 
00284         function execute($stmt,$data)
00285         {
00286                 $res = $this->db->execute($stmt,$data);
00287 
00288                 if (DB::isError($res))
00289                 {
00290                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00291                 }
00292                 else
00293                 {
00294                         return $res;
00295                 }
00296         }
00297 
00306         function autoExecute($a_tablename,$a_fields,$a_mode = DB_AUTOQUERY_INSERT,$a_where = false)
00307         {
00308                 $res = $this->db->autoExecute($a_tablename,$a_fields,$a_mode,$a_where);
00309 
00310                 if (DB::isError($res))
00311                 {
00312                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$data."</font>", $this->error_class->FATAL);
00313                 }
00314                 else
00315                 {
00316                         return $res;
00317                 }
00318         }
00319         function checkQuerySize($a_query)
00320         {
00321                 global $lang;
00322 
00323                 if(strlen($a_query) >= $this->max_allowed_packet_size)
00324                 {
00325                         return false;
00326                 }
00327                 else
00328                 {
00329                         return true;
00330                 }
00331         }
00332 
00333         
00334 
00335 
00336         // PRIVATE
00337         function setMaxAllowedPacket()
00338         {
00339 
00340                 // GET MYSQL VERSION
00341                 $query = "SHOW VARIABLES LIKE 'version'";
00342                 $res = $this->db->query($query);
00343                 if(DB::isError($res))
00344                 {
00345                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00346                 }
00347                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00348                 {
00349                         $version = $row->Value;
00350                 }
00351 
00352                 // CHANG VALUE IF MYSQL VERSION > 4.0
00353                 if(substr($version,0,1) == "4")
00354                 {
00355                         ini_get("post_max_size");
00356                         $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
00357                         $this->db->query($query);
00358                         if(DB::isError($res))
00359                         {
00360                                 $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00361                         }
00362                 }
00363                 // STORE NEW max_size in member variable
00364                 $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
00365                 if(DB::isError($res))
00366                 {
00367                         $this->raiseError($res->getMessage()."<br><font size=-1>SQL: ".$query."</font>", $this->error_class->FATAL);
00368                 }
00369                 $res = $this->db->query($query);
00370                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00371                 {
00372                         $this->max_allowed_packet_size = $row->Value;
00373                 }
00374                 #var_dump("<pre>",$this->max_allowed_packet_size,"<pre>");
00375                 return true;
00376         }
00377 
00383         function _lockTables($a_table_params)
00384         {
00385                 global $ilDB;
00386                 
00387                 $lock_str = 'LOCK TABLES ';
00388                 $counter = 0;
00389                 foreach($a_table_params as $table_name => $type)
00390                 {
00391                         $lock_str .= $counter++ ? ',' : '';
00392                         $lock_str .= $table_name.' '.$type;
00393                 }
00394 
00395                 $ilDB->query($lock_str);
00396 
00397                 return true;
00398         }
00399         function _unlockTables()
00400         {
00401                 global $ilDB;
00402                 
00403                 $ilDB->query('UNLOCK TABLES');
00404 
00405                 return true;
00406         }
00407         
00411         function getMySQLVersion()
00412         {
00413                 return mysql_get_server_info();
00414         }
00415         
00419         function isMysql4_1()
00420         {
00421                 $version = explode(".", $this->getMysqlVersion());
00422                 if ($version[0] == "4" && $version[1] == "1")
00423                 {
00424                         return true;
00425                 }
00426                 
00427                 return false;
00428         }
00429 
00438         function isMysql4_1OrHigher()
00439         {
00440                 $version = explode(".", $this->getMysqlVersion());
00441                 if ((int)$version[0] >= 5 ||
00442                         ((int)$version[0] == 4 && (int)$version[1] >= 1))
00443                 {
00444                         return true;
00445                 }
00446                 
00447                 return false;
00448         }
00449 
00453         function isMysql4_0OrHigher()
00454         {
00455                 $version = explode(".", $this->getMysqlVersion());
00456                 if((int) $version[0] >= 4)
00457                 {
00458                         return true;
00459                 }
00460                 return false;
00461         }
00462         
00470         function tableColumnExists($a_table, $a_column_name)
00471         {
00472                 $column_visibility = FALSE;
00473                 $query = "SHOW COLUMNS FROM `$a_table`";
00474                 $res = $this->db->query($query);
00475                 if ($res->numRows())
00476                 {
00477                         while ($data = $res->fetchRow(DB_FETCHMODE_ASSOC))
00478                         {
00479                                 if (strcmp($data["Field"], $a_column_name) == 0)
00480                                 {
00481                                         $column_visibility = TRUE;
00482                                 }
00483                         }
00484                 }
00485                 return $column_visibility;
00486         }
00487 
00488 } //end Class
00489 ?>

Generated on Fri Dec 13 2013 17:56:47 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1