10 require_once(
'./Services/Database/lib/PEAR/MDB2.php');
11 require_once
'Services/Database/classes/QueryUtils/class.ilMySQLQueryUtils.php';
12 require_once
'Services/Database/interfaces/interface.ilDBInterface.php';
56 "text" =>
array(
"length",
"notnull",
"default",
"fixed"),
57 "integer" =>
array(
"length",
"notnull",
"default",
"unsigned"),
58 "float" =>
array(
"notnull",
"default"),
59 "date" =>
array(
"notnull",
"default"),
60 "time" =>
array(
"notnull",
"default"),
61 "timestamp" =>
array(
"notnull",
"default"),
62 "clob" =>
array(
"notnull",
"default"),
63 "blob" =>
array(
"notnull",
"default")
75 $this->db_user = $a_user;
85 return $this->db_user;
95 $this->db_port = $a_port;
105 return $this->db_port;
115 $this->db_host = $a_host;
125 return $this->db_host;
135 $this->db_password = $a_password;
145 return $this->db_password;
155 $this->db_name = $a_name;
165 return $this->db_name;
171 abstract function getDSN();
203 $this->db->setOption(
'result_buffering',$a_status);
212 global $ilClientIniFile;
215 if (is_object($tmpClientIniFile))
216 $clientIniFile = $tmpClientIniFile;
218 $clientIniFile = $ilClientIniFile;
220 if (is_object($clientIniFile ))
222 $this->
setDBUser($clientIniFile ->readVariable(
"db",
"user"));
223 $this->
setDBHost($clientIniFile ->readVariable(
"db",
"host"));
224 $this->
setDBPort($clientIniFile ->readVariable(
"db",
"port"));
225 $this->
setDBPassword($clientIniFile ->readVariable(
"db",
"pass"));
226 $this->
setDBName($clientIniFile ->readVariable(
"db",
"name"));
233 function connect($a_return_false_for_error =
false)
240 if ($this->
getDSN() ==
"")
275 array(
"use_transactions" =>
true));
283 $this->db->disconnect();
326 array(
"use_transactions" =>
true));
338 $this->db->setOption(
"portability", $this->db->getOption(
"portability") - $cur);
341 $this->db->setOption(
"portability", $this->db->getOption(
"portability") - $cur);
414 $this->use_slave = $a_val;
427 if ($a_level ==
"") {
428 $a_level = $this->error_class->FATAL;
439 $stack = $e->getTraceAsString();
442 if (is_object($ilLog)) {
448 throw new ilDatabaseException(
"ilDB Error: " . $a_info .
"<br />" . $a_res->getMessage() .
"<br />" . $a_res->getUserInfo() .
"<br />" 463 $a_level = $this->error_class->FATAL;
478 $this->db->loadModule(
'Extended');
503 if ($a_collation !=
"")
505 $sql =
"CREATE DATABASE ".$a_name.
506 " CHARACTER SET ".$a_charset.
507 " COLLATE ".$a_collation;
511 $sql =
"CREATE DATABASE ".$a_name.
512 " CHARACTER SET ".$a_charset;
515 return $this->
query($sql,
false);
526 function createTable($a_name, $a_definition_array, $a_drop_table =
false,
527 $a_ignore_erros =
false)
532 $this->
raisePearError(
"ilDB Error: createTable(".$a_name.
")<br />".
539 $this->
raisePearError(
"ilDB Error: createTable(".$a_name.
")<br />".
550 $manager = $this->db->loadModule(
'Manager');
551 $r = $manager->createTable($a_name, $a_definition_array,
$options);
572 function dropTable($a_name, $a_error_if_not_existing =
true)
574 if (!$a_error_if_not_existing)
577 if (!in_array($a_name, $tables))
583 $manager = $this->db->loadModule(
'Manager');
588 $constraints = $manager->listTableConstraints($a_name);
589 $this->
handleError($constraints,
"dropTable(".$a_name.
"), listTableConstraints");
590 foreach ($constraints as $c)
592 if (
substr($c, 0, 4) !=
"sys_")
594 $r = $manager->dropConstraint($a_name, $c);
595 $this->
handleError(
$r,
"dropTable(".$a_name.
"), dropConstraint");
600 $indexes = $manager->listTableIndexes($a_name);
601 $this->
handleError($indexes,
"dropTable(".$a_name.
"), listTableIndexes");
602 foreach ($indexes as $i)
604 $r = $manager->dropIndex($a_name, $i);
610 $seqs = $manager->listSequences();
611 if (in_array($a_name, $seqs))
613 $r = $manager->dropSequence($a_name);
614 $this->
handleError(
$r,
"dropTable(".$a_name.
"), dropSequence");
618 $r = $manager->dropTable($a_name);
630 if ($a_options ==
"")
632 $a_options =
array();
635 $manager = $this->db->loadModule(
'Manager');
636 $r = $manager->alterTable($a_name, $a_changes,
false);
652 $manager = $this->db->loadModule(
'Manager');
656 $this->
raisePearError(
"ilDB Error: addTableColumn(".$a_table.
", ".$a_column.
")<br />".
661 $this->
raisePearError(
"ilDB Error: addTableColumn(".$a_table.
", ".$a_column.
")<br />".
667 $a_column => $a_attributes
671 $r = $manager->alterTable($a_table, $changes,
false);
673 return $this->
handleError(
$r,
"addTableColumn(".$a_table.
", ".$a_column.
")");
686 $manager = $this->db->loadModule(
'Manager');
694 $r = $manager->alterTable($a_table, $changes,
false);
696 return $this->
handleError(
$r,
"dropTableColumn(".$a_table.
", ".$a_column.
")");
709 $manager = $this->db->loadModule(
'Manager');
710 $reverse = $this->db->loadModule(
'Reverse');
711 $def = $reverse->getTableFieldDefinition($a_table, $a_column);
713 $this->
handleError($def,
"modifyTableColumn(".$a_table.
")");
715 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php"))
717 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
721 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
724 $best_alt = $analyzer->getBestDefinitionAlternative($def);
725 $def = $def[$best_alt];
726 unset($def[
"nativetype"]);
727 unset($def[
"mdb2type"]);
730 $type = ($a_attributes[
"type"] !=
"")
731 ? $a_attributes[
"type"]
733 foreach ($def as $k => $v)
735 if ($k !=
"type" && !in_array($k, $this->allowed_attributes[$type]))
741 foreach ($a_attributes as $k => $v)
743 $check_array[$k] = $v;
747 $this->
raisePearError(
"ilDB Error: modifyTableColumn(".$a_table.
", ".$a_column.
")<br />".
752 if ($this->getDbType() ==
"oracle")
754 if ($def[
"notnull"] ==
true && ($a_attributes[
"notnull"] ==
true 755 || !isset($a_attributes[
"notnull"])))
757 unset($def[
"notnull"]);
758 unset($a_attributes[
"notnull"]);
760 if ($def[
"notnull"] ==
false && ($a_attributes[
"notnull"] ==
false 761 || !isset($a_attributes[
"notnull"])))
763 unset($def[
"notnull"]);
764 unset($a_attributes[
"notnull"]);
767 foreach ($a_attributes as $a => $v)
772 $a_attributes[
"definition"] = $def;
776 $a_column => $a_attributes
780 $r = $manager->alterTable($a_table, $changes,
false);
782 return $this->
handleError(
$r,
"modifyTableColumn(".$a_table.
")");
798 $this->
raisePearError(
"ilDB Error: renameTableColumn(".$a_table.
",".$a_column.
",".$a_new_column.
")<br />".
802 $manager = $this->db->loadModule(
'Manager');
803 $reverse = $this->db->loadModule(
'Reverse');
804 $def = $reverse->getTableFieldDefinition($a_table, $a_column);
806 $this->
handleError($def,
"renameTableColumn(".$a_table.
",".$a_column.
",".$a_new_column.
")");
808 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php"))
810 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
814 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
818 $best_alt = $analyzer->getBestDefinitionAlternative($def);
819 $def = $def[$best_alt];
820 unset($def[
"nativetype"]);
821 unset($def[
"mdb2type"]);
823 $f[
"definition"] = $def;
824 $f[
"name"] = $a_new_column;
832 $r = $manager->alterTable($a_table, $changes,
false);
834 return $this->
handleError(
$r,
"renameTableColumn(".$a_table.
",".$a_column.
",".$a_new_column.
")");
848 $this->
raisePearError(
"ilDB Error: renameTable(".$a_name.
",".$a_new_name.
")<br />".
852 $manager = $this->db->loadModule(
'Manager');
853 $r = $manager->alterTable($a_name,
array(
"name" => $a_new_name),
false);
861 return $this->
handleError(
$r,
"renameTable(".$a_name.
",".$a_new_name.
")");
873 $manager = $this->db->loadModule(
'Manager');
876 foreach ($a_fields as $f)
878 $fields[$f] =
array();
880 $definition =
array (
884 $r = $manager->createConstraint($a_table,
887 return $this->
handleError(
$r,
"addPrimaryKey(".$a_table.
")");
906 $manager = $this->db->loadModule(
'Manager');
908 $r = $manager->dropConstraint($a_table,
911 return $this->
handleError(
$r,
"dropPrimaryKey(".$a_table.
")");
921 function addIndex($a_table, $a_fields, $a_name =
"in", $a_fulltext =
false)
926 $manager = $this->db->loadModule(
'Manager');
931 $this->
raisePearError(
"ilDB Error: addIndex(".$a_table.
",".$a_name.
")<br />".
936 foreach ($a_fields as $f)
938 $fields[$f] =
array();
940 $definition =
array (
946 $r = $manager->createIndex($a_table, $this->
constraintName($a_table, $a_name), $definition);
983 $manager = $this->db->loadModule(
'Manager');
984 $reverse = $this->db->loadModule(
'Reverse');
987 foreach($manager->listTableIndexes($a_table) as $idx_name)
989 $def = $reverse->getTableIndexDefinition($a_table,$idx_name);
990 $idx_fields = array_keys((
array) $def[
'fields']);
992 if($idx_fields === $a_fields)
1009 $manager = $this->db->loadModule(
'Manager');
1010 $reverse = $this->db->loadModule(
'Reverse');
1013 foreach($manager->listTableIndexes($a_table) as $idx_name)
1015 $def = $reverse->getTableIndexDefinition($a_table,$idx_name);
1016 $idx_fields = array_keys((
array) $def[
'fields']);
1018 if($idx_fields === $a_fields)
1020 return $this->
dropIndex($a_table, $idx_name);
1037 $manager = $this->db->loadModule(
'Manager');
1041 $r = $manager->dropIndex($a_table, $this->
constraintName($a_table, $a_name));
1060 $manager = $this->db->loadModule(
'Manager');
1065 $this->
raisePearError(
"ilDB Error: addUniqueConstraint(".$a_table.
",".$a_name.
")<br />".
1070 foreach ($a_fields as $f)
1072 $fields[$f] =
array();
1074 $definition =
array (
1079 $r = $manager->createConstraint($a_table, $this->
constraintName($a_table, $a_name), $definition);
1081 return $this->
handleError(
$r,
"addUniqueConstraint(".$a_table.
")");
1093 $manager = $this->db->loadModule(
'Manager');
1095 $r = $manager->dropConstraint(
1099 return $this->
handleError(
$r,
"dropUniqueConstraint(".$a_table.
")");
1110 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php"))
1112 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
1116 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
1119 $cons = $analyzer->getConstraintsInformation($a_table);
1120 foreach ($cons as $c)
1122 if ($c[
"type"] ==
"unique" && count($a_fields) == count($c[
"fields"]))
1125 foreach ($a_fields as $f)
1127 if (!isset($c[
"fields"][$f]))
1146 $manager = $this->db->loadModule(
'Manager');
1148 $r = $manager->createSequence($a_table_name, $a_start);
1150 return $this->
handleError(
$r,
"createSequence(".$a_table_name.
")");
1159 $manager = $this->db->loadModule(
'Manager');
1161 $r = $manager->dropSequence($a_table_name);
1163 return $this->
handleError(
$r,
"dropSequence(".$a_table_name.
")");
1173 if (!preg_match (
"/^[a-z]+[_a-z0-9]*$/", $a_name))
1175 $this->error_str =
"Table name must only contain _a-z0-9 and must start with a-z.";
1181 $this->error_str =
"Invalid table name '".$a_name.
"' (Reserved Word).";
1185 if (strtolower(
substr($a_name, 0, 4)) ==
"sys_")
1187 $this->error_str =
"Invalid table name '".$a_name.
"'. Name must not start with 'sys_'.";
1191 if (strlen($a_name) > 22)
1193 $this->error_str =
"Invalid table name '".$a_name.
"'. Maximum table identifer length is 22 bytes.";
1207 foreach ($a_cols as $col => $def)
1244 if (!in_array($a_def[
"type"],
array(
"text",
"integer",
"float",
"date",
"time",
"timestamp",
"clob",
"blob")))
1246 switch ($a_def[
"type"])
1249 $this->error_str =
"Invalid column type '".$a_def[
"type"].
"'. Use integer(1) instead.";
1253 $this->error_str =
"Invalid column type '".$a_def[
"type"].
"'. Use float or integer instead.";
1257 $this->error_str =
"Invalid column type '".$a_def[
"type"].
"'. Allowed types are: ".
1258 "text, integer, float, date, time, timestamp, clob and blob.";
1265 foreach ($a_def as $k => $v)
1267 if ($k !=
"type" && !in_array($k, $allowed_attributes[$a_def[
"type"]]))
1269 $this->error_str =
"Attribute '".$k.
"' is not allowed for column type '".$a_def[
"type"].
"'.";
1275 switch ($a_def[
"type"])
1278 if ($a_def[
"length"] < 1 || $a_def[
"length"] > 4000)
1280 if (!$a_modify_mode || isset($a_def[
"length"]))
1282 $this->error_str =
"Invalid length '".$a_def[
"length"].
"' for type text.".
1283 " Length must be >=1 and <= 4000.";
1290 if (!in_array($a_def[
"length"],
array(1, 2, 3, 4, 8)))
1292 if (!$a_modify_mode || isset($a_def[
"length"]))
1294 $this->error_str =
"Invalid length '".$a_def[
"length"].
"' for type integer.".
1295 " Length must be 1, 2, 3, 4 or 8 (bytes).";
1299 if ($a_def[
"unsigned"])
1301 $this->error_str =
"Unsigned attribut must not be true for type integer.";
1317 if (!preg_match (
"/^[a-z]+[_a-z0-9]*$/", $a_name))
1319 $this->error_str =
"Invalid column name '".$a_name.
"'. Column name must only contain _a-z0-9 and must start with a-z.";
1325 $this->error_str =
"Invalid column name '".$a_name.
"' (Reserved Word).";
1329 if (strtolower(
substr($a_name, 0, 4)) ==
"sys_")
1331 $this->error_str =
"Invalid column name '".$a_name.
"'. Name must not start with 'sys_'.";
1335 if (strlen($a_name) > 30)
1337 $this->error_str =
"Invalid column name '".$a_name.
"'. Maximum column identifer length is 30 bytes.";
1351 if (!preg_match (
"/^[a-z]+[_a-z0-9]*$/", $a_name))
1353 $this->error_str =
"Invalid column name '".$a_name.
"'. Column name must only contain _a-z0-9 and must start with a-z.";
1359 $this->error_str =
"Invalid column name '".$a_name.
"' (Reserved Word).";
1363 if (strlen($a_name) > 3)
1365 $this->error_str =
"Invalid index name '".$a_name.
"'. Maximum index identifer length is 3 bytes.";
1384 return $a_constraint;
1393 require_once(
'./Services/Database/classes/PDO/FieldDefinition/class.ilDBPdoMySQLFieldDefinition.php');
1397 return $ilDBPdoMySQLFieldDefinition->isReserved($a_word);
1415 function query($sql, $a_handle_error =
true)
1419 if (is_object($ilBench))
1421 $ilBench->startDbBench($sql);
1423 $r = $this->db->query($sql);
1424 if (is_object($ilBench))
1426 $ilBench->stopDbBench();
1429 if ($a_handle_error)
1444 function queryF($a_query, $a_types, $a_values)
1446 if (!is_array($a_types) || !is_array($a_values) ||
1447 count($a_types) != count($a_values))
1449 $this->
raisePearError(
"ilDB::queryF: Types and values must be arrays of same size. ($a_query)");
1451 $quoted_values =
array();
1452 foreach($a_types as $k =>
$t)
1454 $quoted_values[] = $this->
quote($a_values[$k],
$t);
1456 $query = vsprintf($a_query, $quoted_values);
1470 if (!is_array($a_types) || !is_array($a_values) ||
1471 count($a_types) != count($a_values))
1473 $this->
raisePearError(
"ilDB::manipulateF: types and values must be arrays of same size. ($a_query)");
1475 $quoted_values =
array();
1476 foreach($a_types as $k =>
$t)
1478 $quoted_values[] = $this->
quote($a_values[$k],
$t);
1480 $query = vsprintf($a_query, $quoted_values);
1490 $pos1 = strpos(strtolower($sql),
"from ");
1494 $tablef =
substr($sql, $pos1+5);
1495 $pos2 = strpos(strtolower($tablef),
" ");
1498 $table =
substr($tablef, 0, $pos2);
1505 if (trim($table) !=
"")
1507 if (!is_array($this->ttt) || !in_array($table, $this->ttt))
1510 $this->ttt[] = $table;
1515 echo "<br><b>".$sql.
"</b>";
1524 $this->db->setLimit($a_limit, $a_offset);
1535 $r = $this->db->nextId($a_table_name,
false);
1554 if (is_object($ilBench))
1556 $ilBench->startDbBench($sql);
1558 $r = $this->db->exec($sql);
1559 if (is_object($ilBench))
1561 $ilBench->stopDbBench();
1575 function prepare($a_query, $a_types = null, $a_result_types = null)
1577 $res = $this->db->prepare($a_query, $a_types, $a_result_types);
1607 $res = $a_stmt->execute($a_data);
1623 $res = $this->db->extended->executeMultiple($a_stmt,$a_data);
1625 return $this->
handleError(
$res,
"executeMultiple(".$a_stmt->query.
")");
1637 $field_values =
array();
1638 $placeholders =
array();
1643 foreach ($a_columns as $k => $col)
1646 $placeholders[] =
"%s";
1647 $placeholders2[] =
":$k";
1651 if ($col[0] ==
'integer' && !is_null($col[1]))
1653 $col[1] = (int) $col[1];
1656 $values[] = $col[1];
1657 $field_values[$k] = $col[1];
1658 if ($col[0] ==
"blob" || $col[0] ==
"clob")
1666 $st = $this->db->prepare(
"INSERT INTO ".$a_table.
" (".implode($fields,
",").
") VALUES (".
1669 $this->
handleError($st,
"insert / prepare/execute(".$a_table.
")");
1671 $r = $st->execute($field_values);
1675 $this->
handleError(
$r,
"insert / prepare/execute(".$a_table.
")");
1680 $q =
"INSERT INTO ".$a_table.
" (".implode($fields,
",").
") VALUES (".
1681 implode($placeholders,
",").
")";
1695 function update($a_table, $a_columns, $a_where)
1698 $field_values =
array();
1699 $placeholders =
array();
1704 foreach ($a_columns as $k => $col)
1707 $placeholders[] =
"%s";
1708 $placeholders2[] =
":$k";
1712 if ($col[0] ==
'integer' && !is_null($col[1]))
1714 $col[1] = (int) $col[1];
1717 $values[] = $col[1];
1718 $field_values[$k] = $col[1];
1719 if ($col[0] ==
"blob" || $col[0] ==
"clob")
1728 $q =
"UPDATE ".$a_table.
" SET ";
1730 foreach ($fields as $k => $field)
1732 $q.= $lim.$field.
" = ".$placeholders2[$k];
1737 foreach ($a_where as $k => $col)
1739 $q.= $lim.$k.
" = ".$this->
quote($col[1], $col[0]);
1743 $r = $st->execute($field_values);
1746 $this->
handleError(
$r,
"update / prepare/execute(".$a_table.
")");
1751 foreach ($a_where as $k => $col)
1754 $values[] = $col[1];
1755 $field_values[$k] = $col;
1757 $q =
"UPDATE ".$a_table.
" SET ";
1759 foreach ($fields as $k => $field)
1761 $q.= $lim.$field.
" = ".$placeholders[$k];
1766 foreach ($a_where as $k => $col)
1768 $q.= $lim.$k.
" = %s";
1784 function replace($a_table, $a_pk_columns, $a_other_columns)
1787 $a_columns = array_merge($a_pk_columns, $a_other_columns);
1789 $field_values =
array();
1790 $placeholders =
array();
1795 foreach ($a_columns as $k => $col)
1798 $placeholders[] =
"%s";
1799 $placeholders2[] =
":$k";
1803 if ($col[0] ==
'integer' && !is_null($col[1]))
1805 $col[1] = (int) $col[1];
1808 $values[] = $col[1];
1809 $field_values[$k] = $col[1];
1810 if ($col[0] ==
"blob" || $col[0] ==
"clob")
1818 $st = $this->db->prepare(
"REPLACE INTO ".$a_table.
" (".implode($fields,
",").
") VALUES (".
1820 $this->
handleError($st,
"insert / prepare/execute(".$a_table.
")");
1821 $r = $st->execute($field_values);
1823 $this->
handleError(
$r,
"insert / prepare/execute(".$a_table.
")");
1828 $q =
"REPLACE INTO ".$a_table.
" (".implode($fields,
",").
") VALUES (".
1829 implode($placeholders,
",").
")";
1850 return $a_st->free();
1870 return $a_set->numRows();
1888 function in($a_field, $a_values, $negate =
false,
$a_type =
"")
1890 if (count($a_values) == 0)
1894 return $negate ?
' 1=1 ' :
' 1=2 ';
1899 $str = $a_field.(($negate) ?
" NOT" :
"").
" IN (?".str_repeat(
",?", count($a_values) - 1).
")";
1903 $str = $a_field.(($negate) ?
" NOT" :
"").
" IN (";
1905 foreach ($a_values as $v)
1907 $str.= $sep.$this->quote($v,
$a_type);
1921 if (!is_array($a_arr))
1927 $type_arr = array_fill(0, $a_cnt,
$a_type);
1931 $type_arr =
array();
1933 return array_merge($a_arr, $type_arr);
1957 if(!count($a_values))
1962 $concat =
' CONCAT(';
1964 foreach($a_values as $field_info)
1966 $val = $field_info[0];
1975 $concat .=
'COALESCE(';
1997 function substr($a_exp, $a_pos = 1, $a_len = -1)
2002 $lenstr =
", ".$a_len;
2004 return " SUBSTR(".$a_exp.
", ".$a_pos.$lenstr.
") ";
2015 return " UPPER(".$a_exp.
") ";
2026 return " LOWER(".$a_exp.
") ";
2036 public function locate($a_needle,$a_string,$a_start_pos = 1)
2038 $locate =
' LOCATE( ';
2039 $locate .= $a_needle;
2041 $locate .= $a_string;
2043 $locate .= $a_start_pos;
2054 function like($a_col,
$a_type, $a_value =
"?", $case_insensitive =
true)
2060 if ($a_value ==
"?")
2062 if ($case_insensitive)
2064 return "UPPER(".$a_col.
") LIKE(UPPER(?))";
2068 return $a_col .
" LIKE(?)";
2073 if ($case_insensitive)
2076 return " UPPER(".$a_col.
") LIKE(UPPER(".$this->
quote($a_value,
'text').
"))";
2081 return " ".$a_col.
" LIKE(".$this->
quote($a_value,
'text').
")";
2092 if (!$a_empty_or_null || $a_value !=
"")
2098 return "(".$a_col.
" = '' OR $a_col IS NULL)";
2107 if (!$a_empty_or_null)
2113 return "(".$a_col.
" <> ".$this->
quote($a_value,
$a_type).
" OR ".
2118 return "(".$a_col.
" <> '' AND $a_col IS NOT NULL)";
2130 return "FROM_UNIXTIME(".$a_expr.
")";
2138 return "UNIX_TIMESTAMP()";
2156 if (is_array($tables))
2158 if (in_array($a_table, $tables))
2176 $column_visibility =
false;
2177 $manager = $this->db->loadModule(
'Manager');
2178 $r = $manager->listTableFields($a_table);
2182 foreach(
$r as $field)
2184 if ($field == $a_column_name)
2186 $column_visibility =
true;
2191 return $column_visibility;
2203 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php"))
2205 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
2209 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
2212 $cons = $analyzer->getConstraintsInformation($a_table);
2213 foreach ($cons as $c)
2215 if ($c[
"type"] ==
"unique" && count($a_fields) == count($c[
"fields"]))
2218 foreach ($a_fields as $f)
2220 if (!isset($c[
"fields"][$f]))
2242 $manager = $this->db->loadModule(
'Manager');
2243 $r = $manager->listTables();
2263 if (is_array($sequences))
2265 if (in_array($a_sequence, $sequences))
2280 $manager = $this->db->loadModule(
'Manager');
2281 $r = $manager->listSequences();
2301 if ($a_query ==
"" && is_null(
$a_type))
2307 if(
$a_type ==
'integer' && !is_null($a_query))
2309 return (
int) $a_query;
2314 $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);
2317 return $this->db->quote($a_query,
$a_type);
2329 return $this->db->quoteIdentifier($a_identifier);
2344 if (!$this->db->supports(
'transactions'))
2346 $this->
raisePearError(
"ilDB::beginTransaction: Transactions are not supported.", $this->error_class->FATAL);
2348 $res = $this->db->beginTransaction();
2358 $res = $this->db->commit();
2368 $res = $this->db->rollback();
2379 abstract public function lockTables($a_tables);
2405 $res = $this->db->autoExecute($a_tablename,$a_fields,$a_mode,$a_where);
2421 $res = $this->db->lastInsertId();
2441 $set = $this->db->query($sql);
2464 $set = $this->
query($sql);
2465 $r = $set->fetchRow($mode);
2483 while (
$data = $query_result->fetch($fetch_mode)) {
2497 $this->sub_type = (
string)$a_value;
2536 return $this->db->getSequenceName($table_name);
2544 require_once(
'./Services/Database/classes/Atom/class.ilAtomQueryLock.php');
2556 $query_replaced = preg_replace(
2559 if (!empty($query_replaced)) {
2560 return $query_replaced;
2580 public function cast($a_field_name, $a_dest_type) {
2581 $manager = $this->db->loadModule(
'Manager');
2582 return $manager->getQueryUtils()->cast($a_field_name, $a_dest_type);
static getReservedWords()
Get reserved words.
raisePearError($a_message, $a_level="")
Raise an error.
prepare($a_query, $a_types=null, $a_result_types=null)
Prepare a query (SELECT) statement to be used with execute.
checkTableColumns($a_cols)
Check table columns definition.
numRows($a_set)
Fetch row as associative array from result set.
rollback()
Rollback a transaction.
createDatabase($a_name, $a_charset="utf8", $a_collation="")
Create database.
const PEAR_ERROR_CALLBACK
isFulltextIndex($a_table, $a_name)
Is index a fulltext index?
isError($data, $code=null)
Tell whether a value is a MDB2 error.
setDBUser($a_user)
Set database user.
sequenceExists($a_sequence)
Check, whether a given sequence exists.
checkColumnDefinition($a_def, $a_modify_mode=false)
Check whether a column definition is valid.
renameTableColumn($a_table, $a_column, $a_new_column)
Rename a table column Use this only on aleady "abstracted" tables.
tableExists($a_table)
Check, whether a given table exists.
query($sql, $a_handle_error=true)
Query.
nextId($a_table_name)
Get next ID for an index.
const MDB2_PORTABILITY_EMPTY_TO_NULL
Portability: convert empty values to null strings in data output by query*() and fetch*().
migrateAllTablesToEngine($engine=ilDBConstants::MYSQL_ENGINE_INNODB)
doesCollationSupportMB4Strings()
locate($a_needle, $a_string, $a_start_pos=1)
Create locate string.
replace($a_table, $a_pk_columns, $a_other_columns)
Replace into method.
beginTransaction()
Begin Transaction.
createTable($a_name, $a_definition_array, $a_drop_table=false, $a_ignore_erros=false)
Create a new table in the database.
checkIndexName($a_name)
Check whether an index name is valid.
like($a_col, $a_type, $a_value="?", $case_insensitive=true)
Like.
fetchObject($a_set)
Fetch row as object from result set.
getSubType()
Get sub type.
static setErrorHandling($mode=null, $options=null)
Sets how errors generated by this object should be handled.
uniqueConstraintExists($a_table, array $a_fields)
Checks if a unique constraint exists based on the fields of the unique constraint (not the name) ...
dropIndex($a_table, $a_name="in")
Drop an index from a table.
modifyTableColumn($a_table, $a_column, $a_attributes)
Modify a table column Use this only on aleady "abstracted" tables.
dropIndexByFields($a_table, $a_fields)
Drop index by field(s)
Add rich text string
The name of the decorator.
addFulltextIndex($a_table, $a_fields, $a_name="in")
Add fulltext index.
getDBPort()
Get database port.
addPrimaryKey($a_table, $a_fields)
Add a primary key to a table.
Class ilDatabaseException.
in($a_field, $a_values, $negate=false, $a_type="")
Get abstract in-clause for given array.
addUniqueConstraint($a_table, $a_fields, $a_name="con")
Add a unique constraint to a table.
doConnect()
Standard way to connect to db.
const MDB2_AUTOQUERY_SELECT
getLastInsertId()
Get last insert id.
equals($a_col, $a_value, $a_type, $a_empty_or_null=false)
Use this only on text fields.
setSubType($a_value)
Set sub type.
manipulateF($a_query, $a_types, $a_values)
Formatted manupulate (for DELETE, UPDATE, INSERT).
getDBName()
Get database name.
addIndex($table_name, $fields, $index_name='', $fulltext=false)
fetchAssoc($a_set)
Fetch row as associative array from result set.
dropTable($a_name, $a_error_if_not_existing=true)
Drop a table.
quote($a_query, $a_type=null)
Wrapper for quote method.
executeMultiple($a_stmt, $a_data)
Execute a query statement prepared by either prepare() or prepareManip() with multiple data arrays...
unixTimestamp()
Unix timestamp.
createSequence($a_table_name, $a_start=1)
Create a sequence for a table.
prepareManip($a_query, $a_types=null)
Prepare a data manipulation statement to be used with execute.
const MDB2_AUTOQUERY_DELETE
getDBHost()
Get database host.
initConnection()
Initialize the database connection.
setDBPort($a_port)
Set database port.
useSlave($a_val=true)
Use slave.
const MDB2_PREPARE_MANIP
These are just helper constants to more verbosely express parameters to prepare() ...
substr($a_exp, $a_pos=1, $a_len=-1)
Substring.
checkTableName($a_name)
Check whether a table name is valid.
if(!is_array($argv)) $options
getDBUser()
Get database user.
getRow($sql, $mode=ilDBConstants::FETCHMODE_OBJECT)
getRow.
& connect($dsn, $options=false)
Create a new MDB2 connection object and connect to the specified database.
free($a_st)
Free a statement / result set.
supportsSlave()
Supports slave.
cast($a_field_name, $a_dest_type)
quoteIdentifier($a_identifier, $check_option=false)
Quote table and field names.
checkColumnName($a_name)
Check whether a column name is valid.
fromUnixtime($a_expr, $a_to_text=true)
fromUnixtime (makes timestamp out of unix timestamp)
setDBName($a_name)
Set database name.
autoExecute($a_tablename, $a_fields, $a_mode=MDB2_AUTOQUERY_INSERT, $a_where=false)
Wrapper for Pear autoExecute.
setLimit($a_limit, $a_offset=0)
Set limit and offset for a query.
const MDB2_PORTABILITY_ALL
Portability: turn on all portability features.
const MYSQL_ENGINE_INNODB
supportsEngineMigration()
enableResultBuffering($a_status)
En/disable result buffering.
unlockTables()
Unlock tables locked by previous lock table calls.
execute($a_stmt, $a_data=null)
Execute a query statement prepared by either prepare() or prepareManip()
const MDB2_AUTOQUERY_UPDATE
concat(array $a_values, $a_allow_null=true)
Abstraction of SQL function CONCAT.
static isReservedWord($a_word)
Checks whether a word is a reserved word in one of the supported databases.
listTables()
Get all tables.
initHostConnection()
Initialize the host connection (no specific database)
handleError($a_res, $a_info="", $a_level="")
Handle MDB2 Errors.
loadMDB2Extensions()
load additional mdb2 extensions and set their constants
insert($a_table, $a_columns)
Convenient method for standard insert statements, example field array:
connectHost()
Sets up a host connection only (no specific database used).
connect($a_return_false_for_error=false)
Open the connection.
Create styles array
The data for the language used.
queryF($a_query, $a_types, $a_values)
Formatted query (for SELECTS).
Class ilDBPdoMySQLFieldDefinition.
logStatement($sql)
Helper function, should usually not be called.
initFromIniFile($tmpClientIniFile=null)
Init db parameters from ini file.
manipulate($sql)
Data manipulation.
constraintName($a_table, $a_constraint)
Determine contraint name by table name and constraint name.
sanitizeMB4StringIfNotSupported($query)
string to sanitize, all MB4-Characters like emojis will re replaced with ???string sanitized query ...
setDBPassword($a_password)
Set database password.
commit()
Commit a transaction.
dropPrimaryKey($a_table)
Drop a primary key from a table.
update($a_table, $a_columns, $a_where)
Convenient method for standard update statements, example field array:
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object's de...
getCreateTableOptions()
Get options for the create table statement.
listSequences()
Get all sequences.
dropSequence($a_table_name)
Drop a sequence for a table.
getHostDSN()
Should return a valid value, if host connections are possible (connectHost) to create a new database ...
getDBPassword()
Get database password.
This class gives all kind of DB information using the MDB2 manager and reverse module.
equalsNot($a_col, $a_value, $a_type, $a_empty_or_null=false)
Use this only on text fields.
addTableColumn($a_table, $a_column, $a_attributes)
Add table column Use this only on aleady "abstracted" tables.
getSequenceName($table_name)
dropUniqueConstraint($a_table, $a_name="con")
Drop a constraint from a table.
dropTableColumn($a_table, $a_column)
Drop table column Use this only on aleady "abstracted" tables.
getPrimaryKeyIdentifier()
Primary key identifier.
addTypesToArray($a_arr, $a_type, $a_cnt)
Adds a type x times to an array.
dropUniqueConstraintByFields($a_table, $a_fields)
Drop constraint by field(s)
checkColumn($a_col, $a_def)
Check column definition.
alterTable($a_name, $a_changes)
Alter a table in the database This method is DEPRECATED, see http://www.ilias.de/docu/goto.php?target=pg_25354_42&client_id=docu PLEASE USE THE SPECIALIZED METHODS OF THIS CLASS TO CHANGE THE DB SCHEMA.
setDBHost($a_host)
Set database host.
tableColumnExists($a_table, $a_column_name)
Checks for the existence of a table column.
fetchAll($query_result, $fetch_mode=ilDBConstants::FETCHMODE_ASSOC)
renameTable($a_name, $a_new_name)
Rename a table.
const MDB2_PORTABILITY_FIX_CASE
Portability: convert names of tables and fields to case defined in the "field_case" option when using...
const MDB2_AUTOQUERY_INSERT
Used by autoPrepare()
static isDbError($a_res)
Check error.
lockTables($a_tables)
Abstraction of lock table.
getDBVersion()
Get DB version.
indexExistsByFields($a_table, $a_fields)
Check if index exists.
dropFulltextIndex($a_table, $a_name)