ILIAS  Release_4_1_x_branch Revision 61804
 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  $version = $this->getDBVersion();
229 
230  // CHANG VALUE IF MYSQL VERSION > 4.0
231  // Switched back to "SET GLOBAL ..."
232  // @see http://bugs.mysql.com/bug.php?id=22891
233  // smeyer 2009 07 30
234  if (substr($version,0,1) == "4")
235  {
236  ini_get("post_max_size");
237  $query = "SET GLOBAL max_allowed_packet = ".(int) ini_get("post_max_size") * 1024 * 1024;
238 //echo "-".$query."-";
239  $this->query($query);
240  }
241  // STORE NEW max_size in member variable
242  $query = "SHOW VARIABLES LIKE 'max_allowed_packet'";
243  $res = $this->db->query($query);
244 
245  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
246  {
247  $this->max_allowed_packet_size = $row->value;
248  }
249 //echo "-".$this->max_allowed_packet_size."-";
250  return true;
251  }
252 
256  function supportsFulltext()
257  {
258  return true;
259  }
260 
264  function addFulltextIndex($a_table, $a_fields, $a_name = "in")
265  {
266  $i_name = $this->constraintName($a_table, $a_name)."_idx";
267  $f_str = implode($a_fields, ",");
268  $q = "ALTER TABLE $a_table ADD FULLTEXT $i_name ($f_str)";
269  $this->query($q);
270  }
271 
275  function dropFulltextIndex($a_table, $a_name)
276  {
277  $i_name = $this->constraintName($a_table, $a_name)."_idx";
278  $this->query("ALTER TABLE $a_table DROP FULLTEXT $i_name");
279  }
280 
284  function isFulltextIndex($a_table, $a_name)
285  {
286  $set = $this->query("SHOW INDEX FROM ".$a_table);
287  while ($rec = $this->fetchAssoc($set))
288  {
289  if ($rec["Key_name"] == $a_name && $rec["Index_type"] == "FULLTEXT")
290  {
291  return true;
292  }
293  }
294  }
295 
305  public function lockTables($a_tables)
306  {
307  global $ilLog;
308 
309  $lock = 'LOCK TABLES ';
310 
311  $counter = 0;
312  foreach($a_tables as $table)
313  {
314  if($counter++)
315  {
316  $lock .= ', ';
317  }
318  $lock .= ($table['name'].' ');
319 
320  if($table['alias'])
321  {
322  $lock .= ($table['alias'].' ');
323  }
324 
325  switch($table['type'])
326  {
327  case ilDB::LOCK_READ:
328  $lock .= ' READ ';
329  break;
330 
331  case ilDB::LOCK_WRITE:
332  $lock .= ' WRITE ';
333  break;
334  }
335  }
336  $ilLog->write(__METHOD__.': '.$lock);
337  $this->query($lock);
338  }
339 
344  public function unlockTables()
345  {
346  $this->query('UNLOCK TABLES');
347  }
348 }
349 ?>