ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDBMySQL.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
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  return "mysql://".$this->getDBUser().":".$this->getDBPassword().
38  "@".$this->getdbHost().$db_port_str."/".$this->getDBName();
39  }
40 
44  function getHostDSN()
45  {
46  return "mysql://".$this->getDBUser().":".$this->getDBPassword().
47  "@".$this->getdbHost();
48  }
49 
53  function getDBType()
54  {
55  return "mysql";
56  }
57 
61  static function getReservedWords()
62  {
63  // version: 5.1
64  // url: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
65  return array(
66  "ACCESSIBLE", "ADD", "ALL", "ALTER", "ANALYZE", "AND",
67  "AS", "ASC", "ASENSITIVE", "BEFORE", "BETWEEN", "BIGINT",
68  "BINARY", "BLOB", "BOTH", "BY", "CALL", "CASCADE",
69  "CASE", "CHANGE", "CHAR", "CHARACTER", "CHECK", "COLLATE",
70  "COLUMN", "CONDITION", "CONSTRAINT", "CONTINUE", "CONVERT", "CREATE",
71  "CROSS", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "CURSOR",
72  "DATABASE", "DATABASES", "DAY_HOUR", "DAY_MICROSECOND", "DAY_MINUTE", "DAY_SECOND",
73  "DEC", "DECIMAL", "DECLARE", "DEFAULT", "DELAYED", "DELETE",
74  "DESC", "DESCRIBE", "DETERMINISTIC", "DISTINCT", "DISTINCTROW", "DIV",
75  "DOUBLE", "DROP", "DUAL", "EACH", "ELSE", "ELSEIF",
76  "ENCLOSED", "ESCAPED", "EXISTS", "EXIT", "EXPLAIN", "FALSE",
77  "FETCH", "FLOAT", "FLOAT4", "FLOAT8", "FOR", "FORCE",
78  "FOREIGN", "FROM", "FULLTEXT", "GRANT", "GROUP", "HAVING",
79  "HIGH_PRIORITY", "HOUR_MICROSECOND", "HOUR_MINUTE", "HOUR_SECOND", "IF", "IGNORE",
80  "IN", "INDEX", "INFILE", "INNER", "INOUT", "INSENSITIVE",
81  "INSERT", "INT", "INT1", "INT2", "INT3", "INT4",
82  "INT8", "INTEGER", "INTERVAL", "INTO", "IS", "ITERATE",
83  "JOIN", "KEY", "KEYS", "KILL", "LEADING", "LEAVE",
84  "LEFT", "LIKE", "LIMIT", "LINEAR", "LINES", "LOAD",
85  "LOCALTIME", "LOCALTIMESTAMP", "LOCK", "LONG", "LONGBLOB", "LONGTEXT",
86  "LOOP", "LOW_PRIORITY", "MASTER_SSL_VERIFY_SERVER_CERT", "MATCH", "MEDIUMBLOB", "MEDIUMINT",
87  "MEDIUMTEXT", "MIDDLEINT", "MINUTE_MICROSECOND", "MINUTE_SECOND", "MOD", "MODIFIES",
88  "NATURAL", "NOT", "NO_WRITE_TO_BINLOG", "NULL", "NUMERIC", "ON",
89  "OPTIMIZE", "OPTION", "OPTIONALLY", "OR", "ORDER", "OUT",
90  "OUTER", "OUTFILE", "PRECISION", "PRIMARY", "PROCEDURE", "PURGE",
91  "RANGE", "READ", "READS", "READ_WRITE", "REAL", "REFERENCES",
92  "REGEXP", "RELEASE", "RENAME", "REPEAT", "REPLACE", "REQUIRE",
93  "RESTRICT", "RETURN", "REVOKE", "RIGHT", "RLIKE", "SCHEMA",
94  "SCHEMAS", "SECOND_MICROSECOND", "SELECT", "SENSITIVE", "SEPARATOR", "SET",
95  "SHOW", "SMALLINT", "SPATIAL", "SPECIFIC", "SQL", "SQLEXCEPTION",
96  "SQLSTATE", "SQLWARNING", "SQL_BIG_RESULT", "SQL_CALC_FOUND_ROWS", "SQL_SMALL_RESULT", "SSL",
97  "STARTING", "STRAIGHT_JOIN", "TABLE", "TERMINATED", "THEN", "TINYBLOB",
98  "TINYINT", "TINYTEXT", "TO", "TRAILING", "TRIGGER", "TRUE",
99  "UNDO", "UNION", "UNIQUE", "UNLOCK", "UNSIGNED", "UPDATE",
100  "USAGE", "USE", "USING", "UTC_DATE", "UTC_TIME", "UTC_TIMESTAMP",
101  "VALUES", "VARBINARY", "VARCHAR", "VARCHARACTER", "VARYING", "WHEN",
102  "WHERE", "WHILE", "WITH", "WRITE", "XOR", "YEAR_MONTH",
103  "ZEROFILL"
104  );
105  }
106 
110  function initConnection()
111  {
112  // SET 'max_allowed_packet' (only possible for mysql version 4)
113  $this->setMaxAllowedPacket();
114 
115  // NOTE: Two sourcecodes use this or a similar handling:
116  // - classes/class.ilDB.php
117  // - setup/classes/class.ilClient.php
118 
119  $this->query("SET NAMES utf8");
120  if (DEVMODE == 1)
121  {
122  $this->query("SET SESSION SQL_MODE = 'ONLY_FULL_GROUP_BY'");
123  }
124 
125  }
126 
131  function now()
132  {
133  return "now()";
134  }
135 
139  function optimizeTable($a_table)
140  {
141  $this->query("OPTIMIZE TABLE ".$a_table);
142  }
143 
147  function getDBVersion()
148  {
149  $vers = @mysql_get_server_info();
150  if (trim($vers) == "")
151  {
152  $vers = "Unknown";
153  }
154  return $vers;
155  }
156 
157 
162  {
163  $version = explode(".", $this->getDBVersion());
164  if((int) $version[0] < 4)
165  {
166  return false;
167  }
168  return true;
169  }
170 
174  function isMysql4_1()
175  {
176  $version = explode(".", $this->getDBVersion());
177  if ($version[0] == "4" && $version[1] == "1")
178  {
179  return true;
180  }
181 
182  return false;
183  }
184 
193  {
194  $version = explode(".", $this->getDBVersion());
195  if ((int)$version[0] >= 5 ||
196  ((int)$version[0] == 4 && (int)$version[1] >= 1))
197  {
198  return true;
199  }
200 
201  return false;
202  }
203 
207  function checkQuerySize($a_query)
208  {
209  global $lang;
210 
211  if(strlen($a_query) >= $this->max_allowed_packet_size)
212  {
213  return false;
214  }
215  else
216  {
217  return true;
218  }
219  }
220 
226  private function setMaxAllowedPacket()
227  {
228  // GET MYSQL VERSION
229  $query = "SHOW VARIABLES LIKE 'version'";
230  $res = $this->query($query);
231  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
232  {
233  $version = $row->Value;
234  }
235 
236  // CHANG VALUE IF MYSQL VERSION > 4.0
237  // Switched back to "SET GLOBAL ..."
238  // @see http://bugs.mysql.com/bug.php?id=22891
239  // smeyer 2009 07 30
240  if (substr($version,0,1) == "4")
241  {
242  ini_get("post_max_size");
243  $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
244 //echo "-".$query."-";
245  $this->query($query);
246  }
247  // STORE NEW max_size in member variable
248  $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
249  $res = $this->db->query($query);
250 
251  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
252  {
253  $this->max_allowed_packet_size = $row->value;
254  }
255 //echo "-".$this->max_allowed_packet_size."-";
256  return true;
257  }
258 
262  function supportsFulltext()
263  {
264  return true;
265  }
266 
270  function addFulltextIndex($a_table, $a_fields, $a_name = "in")
271  {
272  $i_name = $this->constraintName($a_table, $a_name)."_idx";
273  $f_str = implode($a_fields, ",");
274  $q = "ALTER TABLE $a_table ADD FULLTEXT $i_name ($f_str)";
275  $this->query($q);
276  }
277 
281  function dropFulltextIndex($a_table, $a_name)
282  {
283  $i_name = $this->constraintName($a_table, $a_name)."_idx";
284  $this->query("ALTER TABLE $a_table DROP FULLTEXT $i_name");
285  }
286 
290  function isFulltextIndex($a_table, $a_name)
291  {
292  $set = $this->query("SHOW INDEX FROM ".$a_table);
293  while ($rec = $this->fetchAssoc($set))
294  {
295  if ($rec["Key_name"] == $a_name && $rec["Index_type"] == "FULLTEXT")
296  {
297  return true;
298  }
299  }
300  }
301 
311  public function lockTables($a_tables)
312  {
313  global $ilLog;
314 
315  $lock = 'LOCK TABLES ';
316 
317  $counter = 0;
318  foreach($a_tables as $table)
319  {
320  if($counter++)
321  {
322  $lock .= ', ';
323  }
324  $lock .= ($table['name'].' ');
325 
326  if($table['alias'])
327  {
328  $lock .= ($table['alias'].' ');
329  }
330 
331  switch($table['type'])
332  {
333  case ilDB::LOCK_READ:
334  $lock .= ' READ ';
335  break;
336 
337  case ilDB::LOCK_WRITE:
338  $lock .= ' WRITE ';
339  break;
340  }
341  }
342  $ilLog->write(__METHOD__.': '.$lock);
343  $this->query($lock);
344  }
345 
350  public function unlockTables()
351  {
352  $this->query('UNLOCK TABLES');
353  }
354 }
355 ?>