ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDBMySQL.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once ("./Services/Database/classes/class.ilDB.php");
6 
18 class ilDBMySQL extends ilDB
19 {
25 
29  function getDSN()
30  {
31  $db_port_str = "";
32  if (trim($this->getdbPort()) != "")
33  {
34  $db_port_str = ":".$this->getdbPort();
35  }
36 
37  $driver = $this->isMySQLi() ? "mysqli" : "mysql";
38 
39  return $driver."://".$this->getDBUser().":".$this->getDBPassword().
40  "@".$this->getdbHost().$db_port_str."/".$this->getDBName();
41  }
42 
43  protected function isMySQLi()
44  {
45  return ($this->getSubType() == "mysqli");
46  }
47 
51  function getHostDSN()
52  {
53  $driver = $this->isMySQLi() ? "mysqli" : "mysql";
54 
55  return $driver."://".$this->getDBUser().":".$this->getDBPassword().
56  "@".$this->getdbHost();
57  }
58 
62  function getDBType()
63  {
64  return "mysql";
65  }
66 
70  static function getReservedWords()
71  {
72  // version: 5.1
73  // url: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
74  return array(
75  "ACCESSIBLE", "ADD", "ALL", "ALTER", "ANALYZE", "AND",
76  "AS", "ASC", "ASENSITIVE", "BEFORE", "BETWEEN", "BIGINT",
77  "BINARY", "BLOB", "BOTH", "BY", "CALL", "CASCADE",
78  "CASE", "CHANGE", "CHAR", "CHARACTER", "CHECK", "COLLATE",
79  "COLUMN", "CONDITION", "CONSTRAINT", "CONTINUE", "CONVERT", "CREATE",
80  "CROSS", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "CURSOR",
81  "DATABASE", "DATABASES", "DAY_HOUR", "DAY_MICROSECOND", "DAY_MINUTE", "DAY_SECOND",
82  "DEC", "DECIMAL", "DECLARE", "DEFAULT", "DELAYED", "DELETE",
83  "DESC", "DESCRIBE", "DETERMINISTIC", "DISTINCT", "DISTINCTROW", "DIV",
84  "DOUBLE", "DROP", "DUAL", "EACH", "ELSE", "ELSEIF",
85  "ENCLOSED", "ESCAPED", "EXISTS", "EXIT", "EXPLAIN", "FALSE",
86  "FETCH", "FLOAT", "FLOAT4", "FLOAT8", "FOR", "FORCE",
87  "FOREIGN", "FROM", "FULLTEXT", "GRANT", "GROUP", "HAVING",
88  "HIGH_PRIORITY", "HOUR_MICROSECOND", "HOUR_MINUTE", "HOUR_SECOND", "IF", "IGNORE",
89  "IN", "INDEX", "INFILE", "INNER", "INOUT", "INSENSITIVE",
90  "INSERT", "INT", "INT1", "INT2", "INT3", "INT4",
91  "INT8", "INTEGER", "INTERVAL", "INTO", "IS", "ITERATE",
92  "JOIN", "KEY", "KEYS", "KILL", "LEADING", "LEAVE",
93  "LEFT", "LIKE", "LIMIT", "LINEAR", "LINES", "LOAD",
94  "LOCALTIME", "LOCALTIMESTAMP", "LOCK", "LONG", "LONGBLOB", "LONGTEXT",
95  "LOOP", "LOW_PRIORITY", "MASTER_SSL_VERIFY_SERVER_CERT", "MATCH", "MEDIUMBLOB", "MEDIUMINT",
96  "MEDIUMTEXT", "MIDDLEINT", "MINUTE_MICROSECOND", "MINUTE_SECOND", "MOD", "MODIFIES",
97  "NATURAL", "NOT", "NO_WRITE_TO_BINLOG", "NULL", "NUMERIC", "ON",
98  "OPTIMIZE", "OPTION", "OPTIONALLY", "OR", "ORDER", "OUT",
99  "OUTER", "OUTFILE", "PRECISION", "PRIMARY", "PROCEDURE", "PURGE",
100  "RANGE", "READ", "READS", "READ_WRITE", "REAL", "REFERENCES",
101  "REGEXP", "RELEASE", "RENAME", "REPEAT", "REPLACE", "REQUIRE",
102  "RESTRICT", "RETURN", "REVOKE", "RIGHT", "RLIKE", "SCHEMA",
103  "SCHEMAS", "SECOND_MICROSECOND", "SELECT", "SENSITIVE", "SEPARATOR", "SET",
104  "SHOW", "SMALLINT", "SPATIAL", "SPECIFIC", "SQL", "SQLEXCEPTION",
105  "SQLSTATE", "SQLWARNING", "SQL_BIG_RESULT", "SQL_CALC_FOUND_ROWS", "SQL_SMALL_RESULT", "SSL",
106  "STARTING", "STRAIGHT_JOIN", "TABLE", "TERMINATED", "THEN", "TINYBLOB",
107  "TINYINT", "TINYTEXT", "TO", "TRAILING", "TRIGGER", "TRUE",
108  "UNDO", "UNION", "UNIQUE", "UNLOCK", "UNSIGNED", "UPDATE",
109  "USAGE", "USE", "USING", "UTC_DATE", "UTC_TIME", "UTC_TIMESTAMP",
110  "VALUES", "VARBINARY", "VARCHAR", "VARCHARACTER", "VARYING", "WHEN",
111  "WHERE", "WHILE", "WITH", "WRITE", "XOR", "YEAR_MONTH",
112  "ZEROFILL"
113  );
114  }
115 
119  function initConnection()
120  {
121  // SET 'max_allowed_packet' (only possible for mysql version 4)
122  $this->setMaxAllowedPacket();
123 
124  // NOTE: Two sourcecodes use this or a similar handling:
125  // - classes/class.ilDB.php
126  // - setup/classes/class.ilClient.php
127 
128  $this->query("SET NAMES utf8");
129  if (DEVMODE == 1)
130  {
131  $this->query("SET SESSION SQL_MODE = 'ONLY_FULL_GROUP_BY'");
132  }
133 
134  $this->query("SET SESSION STORAGE_ENGINE = 'MYISAM'");
135  }
136 
141  function now()
142  {
143  return "now()";
144  }
145 
149  function optimizeTable($a_table)
150  {
151  $this->query("OPTIMIZE TABLE ".$a_table);
152  }
153 
157  function getDBVersion()
158  {
159  if(!$this->isMySQLi())
160  {
161  $vers = @mysql_get_server_info();
162  }
163  else
164  {
165  $vers = @mysqli_get_server_info($this->db->connection);
166  }
167  if (trim($vers) == "")
168  {
169  $vers = "Unknown";
170  }
171  return $vers;
172  }
173 
174 
179  {
180  $version = explode(".", $this->getDBVersion());
181  if((int) $version[0] < 4)
182  {
183  return false;
184  }
185  return true;
186  }
187 
191  function isMysql4_1()
192  {
193  $version = explode(".", $this->getDBVersion());
194  if ($version[0] == "4" && $version[1] == "1")
195  {
196  return true;
197  }
198 
199  return false;
200  }
201 
210  {
211  $version = explode(".", $this->getDBVersion());
212  if ((int)$version[0] >= 5 ||
213  ((int)$version[0] == 4 && (int)$version[1] >= 1))
214  {
215  return true;
216  }
217 
218  return false;
219  }
220 
224  function checkQuerySize($a_query)
225  {
226  global $lang;
227 
228  if(strlen($a_query) >= $this->max_allowed_packet_size)
229  {
230  return false;
231  }
232  else
233  {
234  return true;
235  }
236  }
237 
243  protected function setMaxAllowedPacket()
244  {
245  $version = $this->getDBVersion();
246 
247  // CHANG VALUE IF MYSQL VERSION > 4.0
248  // Switched back to "SET GLOBAL ..."
249  // @see http://bugs.mysql.com/bug.php?id=22891
250  // smeyer 2009 07 30
251  if (substr($version,0,1) == "4")
252  {
253  ini_get("post_max_size");
254  $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
255 //echo "-".$query."-";
256  $this->query($query);
257  }
258  // STORE NEW max_size in member variable
259  $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
260  $res = $this->db->query($query);
261 
262  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
263  {
264  $this->max_allowed_packet_size = $row->value;
265  }
266 //echo "-".$this->max_allowed_packet_size."-";
267  return true;
268  }
269 
273  function supportsFulltext()
274  {
275  return true;
276  }
277 
281  function addFulltextIndex($a_table, $a_fields, $a_name = "in")
282  {
283  $i_name = $this->constraintName($a_table, $a_name)."_idx";
284  $f_str = implode($a_fields, ",");
285  $q = "ALTER TABLE $a_table ADD FULLTEXT $i_name ($f_str)";
286  $this->query($q);
287  }
288 
292  function dropFulltextIndex($a_table, $a_name)
293  {
294  $i_name = $this->constraintName($a_table, $a_name)."_idx";
295  $this->query("ALTER TABLE $a_table DROP FULLTEXT $i_name");
296  }
297 
301  function isFulltextIndex($a_table, $a_name)
302  {
303  $set = $this->query("SHOW INDEX FROM ".$a_table);
304  while ($rec = $this->fetchAssoc($set))
305  {
306  if ($rec["Key_name"] == $a_name && $rec["Index_type"] == "FULLTEXT")
307  {
308  return true;
309  }
310  }
311  }
312 
322  public function lockTables($a_tables)
323  {
324  global $ilLog;
325 
326  $lock = 'LOCK TABLES ';
327 
328  $counter = 0;
329  foreach($a_tables as $table)
330  {
331  if($counter++)
332  {
333  $lock .= ', ';
334  }
335  $lock .= ($table['name'].' ');
336 
337  if($table['alias'])
338  {
339  $lock .= ($table['alias'].' ');
340  }
341 
342  switch($table['type'])
343  {
344  case ilDB::LOCK_READ:
345  $lock .= ' READ ';
346  break;
347 
348  case ilDB::LOCK_WRITE:
349  $lock .= ' WRITE ';
350  break;
351  }
352  }
353  $ilLog->write(__METHOD__.': '.$lock);
354  $this->query($lock);
355  }
356 
361  public function unlockTables()
362  {
363  $this->query('UNLOCK TABLES');
364  }
365 
366  protected function getCreateTableOptions()
367  {
368  // InnoDB is default engine for MySQL >= 5.5
369  return array('type' => 'MyISAM');
370  }
371 
372  public function getErrorNo()
373  {
374  if(!$this->isMySQLi())
375  {
376  return mysql_errno();
377  }
378  else
379  {
380  return mysqli_errno($this->db->connection);
381  }
382  }
383 
384  public function getLastError()
385  {
386  if(!$this->isMySQLi())
387  {
388  return mysql_error();
389  }
390  else
391  {
392  return mysqli_error($this->db->connection);
393  }
394  }
395 }
396 ?>