8 include_once (
"MDB2.php");
55 "text" => array(
"length",
"notnull",
"default",
"fixed"),
56 "integer" => array(
"length",
"notnull",
"default",
"unsigned"),
57 "float" => array(
"notnull",
"default"),
58 "date" => array(
"notnull",
"default"),
59 "time" => array(
"notnull",
"default"),
60 "timestamp" => array(
"notnull",
"default"),
61 "clob" => array(
"notnull",
"default"),
62 "blob" => array(
"notnull",
"default")
83 $this->db_user = $a_user;
103 $this->db_port = $a_port;
113 return $this->db_port;
123 $this->db_host = $a_host;
143 $this->db_password = $a_password;
153 return $this->db_password;
163 $this->db_name = $a_name;
179 abstract function getDSN();
208 global $ilClientIniFile;
211 if (is_object($tmpClientIniFile))
212 $clientIniFile = $tmpClientIniFile;
214 $clientIniFile = $ilClientIniFile;
216 if (is_object($clientIniFile ))
218 $this->
setDBUser($clientIniFile ->readVariable(
"db",
"user"));
219 $this->
setDBHost($clientIniFile ->readVariable(
"db",
"host"));
220 $this->
setDBPort($clientIniFile ->readVariable(
"db",
"port"));
221 $this->
setDBPassword($clientIniFile ->readVariable(
"db",
"pass"));
222 $this->
setDBName($clientIniFile ->readVariable(
"db",
"name"));
229 function connect($a_return_false_for_error =
false)
236 if ($this->
getDSN() ==
"")
271 array(
"use_transactions" =>
true));
279 $this->db->disconnect();
322 array(
"use_transactions" =>
true));
334 $this->db->setOption(
"portability", $this->db->getOption(
"portability") - $cur);
337 $this->db->setOption(
"portability", $this->db->getOption(
"portability") - $cur);
375 $a_level = $this->error_class->FATAL;
385 $stack = $e->getTraceAsString();
388 if(is_object($ilLog))
391 $a_res->getMessage().
"<br />".$a_res->getUserInfo().
"<br />".$stack, $a_level);
405 $a_level = $this->error_class->FATAL;
420 $this->db->loadModule(
'Extended');
445 if ($a_collation !=
"")
447 $sql =
"CREATE DATABASE ".$a_name.
448 " CHARACTER SET ".$a_charset.
449 " COLLATE ".$a_collation;
453 $sql =
"CREATE DATABASE ".$a_name.
454 " CHARACTER SET ".$a_charset;
457 return $this->
query($sql,
false);
468 function createTable($a_name, $a_definition_array, $a_drop_table =
false,
469 $a_ignore_erros =
false)
472 if ($a_options ==
"")
474 $a_options = array();
480 $this->
raisePearError(
"ilDB Error: createTable(".$a_name.
")<br />".
487 $this->
raisePearError(
"ilDB Error: createTable(".$a_name.
")<br />".
496 $manager = $this->db->loadModule(
'Manager');
497 $r = $manager->createTable($a_name, $a_definition_array, $a_options);
499 return $this->
handleError($r,
"createTable(".$a_name.
")");
508 function dropTable($a_name, $a_error_if_not_existing =
true)
510 if (!$a_error_if_not_existing)
513 if (!in_array($a_name, $tables))
519 $manager = $this->db->loadModule(
'Manager');
524 $constraints = $manager->listTableConstraints($a_name);
525 $this->
handleError($constraints,
"dropTable(".$a_name.
"), listTableConstraints");
526 foreach ($constraints as $c)
528 if (
substr($c, 0, 4) !=
"sys_")
530 $r = $manager->dropConstraint($a_name, $c);
531 $this->
handleError($r,
"dropTable(".$a_name.
"), dropConstraint");
536 $indexes = $manager->listTableIndexes($a_name);
537 $this->
handleError($indexes,
"dropTable(".$a_name.
"), listTableIndexes");
538 foreach ($indexes as $i)
540 $r = $manager->dropIndex($a_name, $i);
541 $this->
handleError($r,
"dropTable(".$a_name.
"), dropIndex");
546 $seqs = $manager->listSequences();
547 if (in_array($a_name, $seqs))
549 $r = $manager->dropSequence($a_name);
550 $this->
handleError($r,
"dropTable(".$a_name.
"), dropSequence");
554 $r = $manager->dropTable($a_name);
556 return $this->
handleError($r,
"dropTable(".$a_name.
")");
566 if ($a_options ==
"")
568 $a_options = array();
571 $manager = $this->db->loadModule(
'Manager');
572 $r = $manager->alterTable($a_name, $a_changes,
false);
574 return $this->
handleError($r,
"alterTable(".$a_name.
")");
588 $manager = $this->db->loadModule(
'Manager');
592 $this->
raisePearError(
"ilDB Error: addTableColumn(".$a_table.
", ".$a_column.
")<br />".
597 $this->
raisePearError(
"ilDB Error: addTableColumn(".$a_table.
", ".$a_column.
")<br />".
603 $a_column => $a_attributes
607 $r = $manager->alterTable($a_table, $changes,
false);
609 return $this->
handleError($r,
"addTableColumn(".$a_table.
", ".$a_column.
")");
622 $manager = $this->db->loadModule(
'Manager');
630 $r = $manager->alterTable($a_table, $changes,
false);
632 return $this->
handleError($r,
"dropTableColumn(".$a_table.
", ".$a_column.
")");
645 $manager = $this->db->loadModule(
'Manager');
646 $reverse = $this->db->loadModule(
'Reverse');
647 $def = $reverse->getTableFieldDefinition($a_table, $a_column);
649 $this->
handleError($def,
"modifyTableColumn(".$a_table.
")");
651 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php"))
653 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
657 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
660 $best_alt = $analyzer->getBestDefinitionAlternative($def);
661 $def = $def[$best_alt];
662 unset($def[
"nativetype"]);
663 unset($def[
"mdb2type"]);
666 $type = ($a_attributes[
"type"] !=
"")
667 ? $a_attributes[
"type"]
669 foreach ($def as $k => $v)
671 if ($k !=
"type" && !in_array($k, $this->allowed_attributes[$type]))
677 foreach ($a_attributes as $k => $v)
679 $check_array[$k] = $v;
683 $this->
raisePearError(
"ilDB Error: modifyTableColumn(".$a_table.
", ".$a_column.
")<br />".
688 if ($this->getDbType() ==
"oracle")
690 if ($def[
"notnull"] ==
true && $a_attributes[
"notnull"] ==
true)
692 unset($def[
"notnull"]);
693 unset($a_attributes[
"notnull"]);
697 foreach ($a_attributes as $a => $v)
702 $a_attributes[
"definition"] = $def;
706 $a_column => $a_attributes
710 $r = $manager->alterTable($a_table, $changes,
false);
712 return $this->
handleError($r,
"modifyTableColumn(".$a_table.
")");
728 $this->
raisePearError(
"ilDB Error: renameTableColumn(".$a_table.
",".$a_column.
",".$a_new_column.
")<br />".
732 $manager = $this->db->loadModule(
'Manager');
733 $reverse = $this->db->loadModule(
'Reverse');
734 $def = $reverse->getTableFieldDefinition($a_table, $a_column);
736 $this->
handleError($def,
"renameTableColumn(".$a_table.
",".$a_column.
",".$a_new_column.
")");
738 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php"))
740 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
744 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
748 $best_alt = $analyzer->getBestDefinitionAlternative($def);
749 $def = $def[$best_alt];
750 unset($def[
"nativetype"]);
751 unset($def[
"mdb2type"]);
753 $f[
"definition"] = $def;
754 $f[
"name"] = $a_new_column;
762 $r = $manager->alterTable($a_table, $changes,
false);
764 return $this->
handleError($r,
"renameTableColumn(".$a_table.
",".$a_column.
",".$a_new_column.
")");
778 $this->
raisePearError(
"ilDB Error: renameTable(".$a_name.
",".$a_new_name.
")<br />".
782 $manager = $this->db->loadModule(
'Manager');
783 $r = $manager->alterTable($a_name, array(
"name" => $a_new_name),
false);
785 $query =
"UPDATE abstraction_progress ".
786 "SET table_name = ".$this->db->quote($a_new_name,
'text').
" ".
787 "WHERE table_name = ".$this->db->quote($a_name,
'text');
790 return $this->
handleError($r,
"renameTable(".$a_name.
",".$a_new_name.
")");
802 $manager = $this->db->loadModule(
'Manager');
805 foreach ($a_fields as $f)
807 $fields[$f] = array();
809 $definition = array (
813 $r = $manager->createConstraint($a_table,
816 return $this->
handleError($r,
"addPrimaryKey(".$a_table.
")");
835 $manager = $this->db->loadModule(
'Manager');
837 $r = $manager->dropConstraint($a_table,
840 return $this->
handleError($r,
"dropPrimaryKey(".$a_table.
")");
850 function addIndex($a_table, $a_fields, $a_name =
"in", $a_fulltext =
false)
852 $manager = $this->db->loadModule(
'Manager');
857 $this->
raisePearError(
"ilDB Error: addIndex(".$a_table.
",".$a_name.
")<br />".
862 foreach ($a_fields as $f)
864 $fields[$f] = array();
866 $definition = array (
872 $r = $manager->createIndex($a_table, $this->
constraintName($a_table, $a_name), $definition);
882 return $this->
handleError($r,
"addIndex(".$a_table.
")");
910 $manager = $this->db->loadModule(
'Manager');
914 $r = $manager->dropIndex($a_table, $this->
constraintName($a_table, $a_name));
918 $this->dropFulltextIndex($a_table, $a_name);
921 return $this->
handleError($r,
"dropIndex(".$a_table.
")");
933 $manager = $this->db->loadModule(
'Manager');
938 $this->
raisePearError(
"ilDB Error: addUniqueConstraint(".$a_table.
",".$a_name.
")<br />".
943 foreach ($a_fields as $f)
945 $fields[$f] = array();
947 $definition = array (
952 $r = $manager->createConstraint($a_table, $this->
constraintName($a_table, $a_name), $definition);
954 return $this->
handleError($r,
"addUniqueConstraint(".$a_table.
")");
962 $manager = $this->db->loadModule(
'Manager');
964 $r = $manager->createSequence($a_table_name, $a_start);
966 return $this->
handleError($r,
"createSequence(".$a_table_name.
")");
975 $manager = $this->db->loadModule(
'Manager');
977 $r = $manager->dropSequence($a_table_name);
979 return $this->
handleError($r,
"dropSequence(".$a_table_name.
")");
989 if (!preg_match (
"/^[a-z]+[_a-z0-9]*$/", $a_name))
991 $this->error_str =
"Table name must only contain _a-z0-9 and must start with a-z.";
997 $this->error_str =
"Invalid table name '".$a_name.
"' (Reserved Word).";
1001 if (strtolower(
substr($a_name, 0, 4)) ==
"sys_")
1003 $this->error_str =
"Invalid table name '".$a_name.
"'. Name must not start with 'sys_'.";
1007 if (strlen($a_name) > 22)
1009 $this->error_str =
"Invalid table name '".$a_name.
"'. Maximum table identifer lenght is 22 bytes.";
1023 foreach ($a_cols as $col => $def)
1060 if (!in_array($a_def[
"type"], array(
"text",
"integer",
"float",
"date",
"time",
"timestamp",
"clob",
"blob")))
1062 switch ($a_def[
"type"])
1065 $this->error_str =
"Invalid column type '".$a_def[
"type"].
"'. Use integer(1) instead.";
1069 $this->error_str =
"Invalid column type '".$a_def[
"type"].
"'. Use float or integer instead.";
1073 $this->error_str =
"Invalid column type '".$a_def[
"type"].
"'. Allowed types are: ".
1074 "text, integer, float, date, time, timestamp, clob and blob.";
1081 foreach ($a_def as $k => $v)
1085 $this->error_str =
"Attribute '".$k.
"' is not allowed for column type '".$a_def[
"type"].
"'.";
1091 switch ($a_def[
"type"])
1094 if ($a_def[
"length"] < 1 || $a_def[
"length"] > 4000)
1096 if (!$a_modify_mode || isset($a_def[
"length"]))
1098 $this->error_str =
"Invalid length '".$a_def[
"length"].
"' for type text.".
1099 " Length must be >=1 and <= 4000.";
1106 if (!in_array($a_def[
"length"], array(1, 2, 3, 4, 8)))
1108 if (!$a_modify_mode || isset($a_def[
"length"]))
1110 $this->error_str =
"Invalid length '".$a_def[
"length"].
"' for type integer.".
1111 " Length must be 1, 2, 3, 4 or 8 (bytes).";
1115 if ($a_def[
"unsigned"])
1117 $this->error_str =
"Unsigned attribut must not be true for type integer.";
1133 if (!preg_match (
"/^[a-z]+[_a-z0-9]*$/", $a_name))
1135 $this->error_str =
"Invalid column name '".$a_name.
"'. Column name must only contain _a-z0-9 and must start with a-z.";
1141 $this->error_str =
"Invalid column name '".$a_name.
"' (Reserved Word).";
1145 if (strtolower(
substr($a_name, 0, 4)) ==
"sys_")
1147 $this->error_str =
"Invalid column name '".$a_name.
"'. Name must not start with 'sys_'.";
1151 if (strlen($a_name) > 30)
1153 $this->error_str =
"Invalid column name '".$a_name.
"'. Maximum column identifer lenght is 30 bytes.";
1167 if (!preg_match (
"/^[a-z]+[_a-z0-9]*$/", $a_name))
1169 $this->error_str =
"Invalid column name '".$a_name.
"'. Column name must only contain _a-z0-9 and must start with a-z.";
1175 $this->error_str =
"Invalid column name '".$a_name.
"' (Reserved Word).";
1179 if (strlen($a_name) > 3)
1181 $this->error_str =
"Invalid index name '".$a_name.
"'. Maximum index identifer lenght is 3 bytes.";
1200 return $a_constraint;
1209 include_once(
"./Services/Database/classes/class.ilDBMySQL.php");
1211 if (in_array(strtoupper($a_word), $mysql_reserved_words))
1215 include_once(
"./Services/Database/classes/class.ilDBOracle.php");
1217 if (in_array(strtoupper($a_word), $oracle_reserved_words))
1221 include_once(
"./Services/Database/classes/class.ilDBPostgreSQL.php");
1223 if (in_array(strtoupper($a_word), $postgres_reserved_words))
1244 function query($sql, $a_handle_error =
true)
1248 if (is_object($ilBench))
1250 $ilBench->startDbBench($sql);
1252 $r = $this->db->query($sql);
1253 if (is_object($ilBench))
1255 $ilBench->stopDbBench();
1258 if ($a_handle_error)
1273 function queryF($a_query, $a_types, $a_values)
1275 if (!is_array($a_types) || !is_array($a_values) ||
1276 count($a_types) != count($a_values))
1278 $this->
raisePearError(
"ilDB::queryF: types and values must be arrays of same size.");
1280 $quoted_values = array();
1281 foreach($a_types as $k =>
$t)
1283 $quoted_values[] = $this->
quote($a_values[$k],
$t);
1285 $query = vsprintf($a_query, $quoted_values);
1299 if (!is_array($a_types) || !is_array($a_values) ||
1300 count($a_types) != count($a_values))
1302 $this->
raisePearError(
"ilDB::manipulateF: types and values must be arrays of same size.");
1304 $quoted_values = array();
1305 foreach($a_types as $k =>
$t)
1307 $quoted_values[] = $this->
quote($a_values[$k],
$t);
1309 $query = vsprintf($a_query, $quoted_values);
1319 $pos1 = strpos(strtolower($sql),
"from ");
1323 $tablef =
substr($sql, $pos1+5);
1324 $pos2 = strpos(strtolower($tablef),
" ");
1327 $table =
substr($tablef, 0, $pos2);
1334 if (trim($table) !=
"")
1336 if (!is_array($this->ttt) || !in_array($table, $this->ttt))
1339 $this->ttt[] = $table;
1344 echo
"<br><b>".$sql.
"</b>";
1353 $this->db->setLimit($a_limit, $a_offset);
1364 $r = $this->db->nextId($a_table_name,
false);
1366 return $this->
handleError($r,
"nextId(".$a_table_name.
")");
1381 $r = $this->db->exec($sql);
1383 return $this->
handleError($r,
"manipulate(".$sql.
")");
1394 function prepare($a_query, $a_types = null, $a_result_types = null)
1396 $res = $this->db->prepare($a_query, $a_types, $a_result_types);
1426 $res = $a_stmt->execute($a_data);
1442 $res = $this->db->extended->executeMultiple($a_stmt,$a_data);
1444 return $this->
handleError(
$res,
"executeMultiple(".$a_stmt->query.
")");
1456 $field_values = array();
1457 $placeholders = array();
1462 foreach ($a_columns as $k => $col)
1465 $placeholders[] =
"%s";
1466 $placeholders2[] =
":$k";
1468 $values[] = $col[1];
1469 $field_values[$k] = $col[1];
1470 if ($col[0] ==
"blob" || $col[0] ==
"clob")
1478 $st = $this->db->prepare(
"INSERT INTO ".$a_table.
" (".implode($fields,
",").
") VALUES (".
1481 $this->
handleError($st,
"insert / prepare/execute(".$a_table.
")");
1483 $r = $st->execute($field_values);
1487 $this->
handleError($r,
"insert / prepare/execute(".$a_table.
")");
1492 $q =
"INSERT INTO ".$a_table.
" (".implode($fields,
",").
") VALUES (".
1493 implode($placeholders,
",").
")";
1507 function update($a_table, $a_columns, $a_where)
1510 $field_values = array();
1511 $placeholders = array();
1516 foreach ($a_columns as $k => $col)
1519 $placeholders[] =
"%s";
1520 $placeholders2[] =
":$k";
1522 $values[] = $col[1];
1523 $field_values[$k] = $col[1];
1524 if ($col[0] ==
"blob" || $col[0] ==
"clob")
1533 $q =
"UPDATE ".$a_table.
" SET ";
1535 foreach ($fields as $k => $field)
1537 $q.= $lim.$field.
" = ".$placeholders2[$k];
1542 foreach ($a_where as $k => $col)
1544 $q.= $lim.$k.
" = ".$this->
quote($col[1], $col[0]);
1548 $r = $st->execute($field_values);
1551 $this->
handleError($r,
"update / prepare/execute(".$a_table.
")");
1556 foreach ($a_where as $k => $col)
1559 $values[] = $col[1];
1560 $field_values[$k] = $col;
1562 $q =
"UPDATE ".$a_table.
" SET ";
1564 foreach ($fields as $k => $field)
1566 $q.= $lim.$field.
" = ".$placeholders[$k];
1571 foreach ($a_where as $k => $col)
1573 $q.= $lim.$k.
" = %s";
1589 function replace($a_table, $a_pk_columns, $a_other_columns)
1592 $a_columns = array_merge($a_pk_columns, $a_other_columns);
1594 $field_values = array();
1595 $placeholders = array();
1600 foreach ($a_columns as $k => $col)
1603 $placeholders[] =
"%s";
1604 $placeholders2[] =
":$k";
1606 $values[] = $col[1];
1607 $field_values[$k] = $col[1];
1608 if ($col[0] ==
"blob" || $col[0] ==
"clob")
1616 $st = $this->db->prepare(
"REPLACE INTO ".$a_table.
" (".implode($fields,
",").
") VALUES (".
1618 $r = $st->execute($field_values);
1620 $this->
handleError($r,
"insert / prepare/execute(".$a_table.
")");
1625 $q =
"REPLACE INTO ".$a_table.
" (".implode($fields,
",").
") VALUES (".
1626 implode($placeholders,
",").
")";
1647 return $a_st->free();
1667 return $a_set->numRows();
1685 function in($a_field, $a_values, $negate =
false, $a_type =
"")
1687 if (count($a_values) == 0)
1693 $str = $a_field.(($negate) ?
" NOT" :
"").
" IN (?".str_repeat(
",?", count($a_values) - 1).
")";
1697 $str = $a_field.(($negate) ?
" NOT" :
"").
" IN (";
1699 foreach ($a_values as $v)
1701 $str.= $sep.$this->quote($v, $a_type);
1715 if (!is_array($a_arr))
1721 $type_arr = array_fill(0, $a_cnt, $a_type);
1725 $type_arr = array();
1727 return array_merge($a_arr, $type_arr);
1749 public function concat($a_values,$a_allow_null =
true)
1751 if(!count($a_values))
1756 $concat =
' CONCAT(';
1758 foreach($a_values as $field_info)
1760 $val = $field_info[0];
1769 $concat .=
'COALESCE(';
1791 function substr($a_exp, $a_pos = 1, $a_len = -1)
1796 $lenstr =
", ".$a_len;
1798 return " SUBSTR(".$a_exp.
", ".$a_pos.$lenstr.
") ";
1809 return " UPPER(".$a_exp.
") ";
1820 return " LOWER(".$a_exp.
") ";
1830 public function locate($a_needle,$a_string,$a_start_pos = 1)
1832 $locate =
' LOCATE( ';
1833 $locate .= $a_needle;
1835 $locate .= $a_string;
1837 $locate .= $a_start_pos;
1848 function like($a_col, $a_type, $a_value =
"?", $case_insensitive =
true)
1850 if (!in_array($a_type, array(
"text",
"clob",
"blob")))
1852 $this->
raisePearError(
"Like: Invalid column type '".$a_type.
"'.", $this->error_class->FATAL);
1854 if ($a_value ==
"?")
1856 if ($case_insensitive)
1858 return "UPPER(".$a_col.
") LIKE(UPPER(?))";
1862 return $a_col .
" LIKE(?)";
1867 if ($case_insensitive)
1870 return " UPPER(".$a_col.
") LIKE(UPPER(".$this->
quote($a_value,
'text').
"))";
1875 return " ".$a_col.
" LIKE(".$this->
quote($a_value,
'text').
")";
1884 function equals($a_col, $a_value, $a_type, $a_empty_or_null =
false)
1886 if (!$a_empty_or_null || $a_value !=
"")
1888 return $a_col.
" = ".$this->
quote($a_value, $a_type);
1892 return "(".$a_col.
" = '' OR $a_col IS NULL)";
1899 function equalsNot($a_col, $a_value, $a_type, $a_empty_or_null =
false)
1901 if (!$a_empty_or_null)
1903 return $a_col.
" <> ".$this->
quote($a_value, $a_type);
1907 return "(".$a_col.
" <> ".$this->
quote($a_value, $a_type).
" OR ".
1912 return "(".$a_col.
" <> '' AND $a_col IS NOT NULL)";
1924 return "FROM_UNIXTIME(".$a_expr.
")";
1932 return "UNIX_TIMESTAMP()";
1958 if (is_array($tables))
1960 if (in_array($a_table, $tables))
1978 $column_visibility =
false;
1979 $manager = $this->db->loadModule(
'Manager');
1980 $r = $manager->listTableFields($a_table);
1984 foreach($r as $field)
1986 if ($field == $a_column_name)
1988 $column_visibility =
true;
1993 return $column_visibility;
2003 $manager = $this->db->loadModule(
'Manager');
2004 $r = $manager->listTables();
2022 function quote($a_query, $a_type = null)
2024 if ($a_query ==
"" && is_null($a_type))
2030 if($a_type ==
'integer' && !is_null($a_query))
2032 return (
int) $a_query;
2034 if ($a_type ==
"blob" || $a_type ==
"clob")
2036 $this->
raisePearError(
"ilDB::quote: Quoting not allowed on type '".$a_type.
"'. Please use ilDB->insert and ilDB->update to write clobs.", $this->error_class->FATAL);
2039 return $this->db->quote($a_query, $a_type);
2051 return $this->db->quoteIdentifier($a_identifier);
2065 if (!$this->db->supports(
'transactions'))
2067 $this->
raisePearError(
"ilDB::beginTransaction: Transactions are not supported.", $this->error_class->FATAL);
2069 $res = $this->db->beginTransaction();
2079 $res = $this->db->commit();
2089 $res = $this->db->rollback();
2099 abstract public function lockTables($a_tables);
2124 $res = $this->db->autoExecute($a_tablename,$a_fields,$a_mode,$a_where);
2140 $res = $this->db->lastInsertId();
2160 $set = $this->db->query($sql);
2183 $set = $this->
query($sql);
2184 $r = $set->fetchRow($mode);