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 public function getDSN();
204 $this->db->setOption(
'result_buffering', $a_status);
213 global $ilClientIniFile;
216 if (is_object($tmpClientIniFile)) {
217 $clientIniFile = $tmpClientIniFile;
219 $clientIniFile = $ilClientIniFile;
222 if (is_object($clientIniFile)) {
223 $this->
setDBUser($clientIniFile ->readVariable(
"db",
"user"));
224 $this->
setDBHost($clientIniFile ->readVariable(
"db",
"host"));
225 $this->
setDBPort($clientIniFile ->readVariable(
"db",
"port"));
226 $this->
setDBPassword($clientIniFile ->readVariable(
"db",
"pass"));
227 $this->
setDBName($clientIniFile ->readVariable(
"db",
"name"));
234 public function connect($a_return_false_for_error =
false)
241 if ($this->
getDSN() ==
"") {
274 array(
"use_transactions" =>
true)
283 $this->db->disconnect();
326 array(
"use_transactions" =>
true)
337 $this->db->setOption(
"portability", $this->db->getOption(
"portability") - $cur);
340 $this->db->setOption(
"portability", $this->db->getOption(
"portability") - $cur);
414 $this->use_slave = $a_val;
428 if ($a_level ==
"") {
429 $a_level = $this->error_class->FATAL;
440 $stack = $e->getTraceAsString();
443 if (is_object($ilLog)) {
449 throw new ilDatabaseException(
"ilDB Error: " . $a_info .
"<br />" . $a_res->getMessage() .
"<br />" . $a_res->getUserInfo() .
"<br />" 462 if ($a_level ==
"") {
463 $a_level = $this->error_class->FATAL;
477 $this->db->loadModule(
'Extended');
502 if ($a_collation !=
"") {
503 $sql =
"CREATE DATABASE " . $a_name .
504 " CHARACTER SET " . $a_charset .
505 " COLLATE " . $a_collation;
507 $sql =
"CREATE DATABASE " . $a_name .
508 " CHARACTER SET " . $a_charset;
511 return $this->
query($sql,
false);
525 $a_drop_table =
false,
526 $a_ignore_erros =
false 530 $this->
raisePearError(
"ilDB Error: createTable(" . $a_name .
")<br />" .
536 $this->
raisePearError(
"ilDB Error: createTable(" . $a_name .
")<br />" .
546 $manager = $this->db->loadModule(
'Manager');
547 $r = $manager->createTable($a_name, $a_definition_array,
$options);
549 return $this->
handleError(
$r,
"createTable(" . $a_name .
")");
568 public function dropTable($a_name, $a_error_if_not_existing =
true)
570 if (!$a_error_if_not_existing) {
572 if (!in_array($a_name, $tables)) {
577 $manager = $this->db->loadModule(
'Manager');
581 $constraints = $manager->listTableConstraints($a_name);
582 $this->
handleError($constraints,
"dropTable(" . $a_name .
"), listTableConstraints");
583 foreach ($constraints as $c) {
584 if (
substr($c, 0, 4) !=
"sys_") {
585 $r = $manager->dropConstraint($a_name, $c);
586 $this->
handleError(
$r,
"dropTable(" . $a_name .
"), dropConstraint");
591 $indexes = $manager->listTableIndexes($a_name);
592 $this->
handleError($indexes,
"dropTable(" . $a_name .
"), listTableIndexes");
593 foreach ($indexes as
$i) {
594 $r = $manager->dropIndex($a_name, $i);
595 $this->
handleError(
$r,
"dropTable(" . $a_name .
"), dropIndex");
600 $seqs = $manager->listSequences();
601 if (in_array($a_name, $seqs)) {
602 $r = $manager->dropSequence($a_name);
603 $this->
handleError(
$r,
"dropTable(" . $a_name .
"), dropSequence");
607 $r = $manager->dropTable($a_name);
619 if ($a_options ==
"") {
620 $a_options =
array();
623 $manager = $this->db->loadModule(
'Manager');
624 $r = $manager->alterTable($a_name, $a_changes,
false);
626 return $this->
handleError(
$r,
"alterTable(" . $a_name .
")");
639 $manager = $this->db->loadModule(
'Manager');
642 $this->
raisePearError(
"ilDB Error: addTableColumn(" . $a_table .
", " . $a_column .
")<br />" .
646 $this->
raisePearError(
"ilDB Error: addTableColumn(" . $a_table .
", " . $a_column .
")<br />" .
652 $a_column => $a_attributes
656 $r = $manager->alterTable($a_table, $changes,
false);
658 return $this->
handleError(
$r,
"addTableColumn(" . $a_table .
", " . $a_column .
")");
670 $manager = $this->db->loadModule(
'Manager');
678 $r = $manager->alterTable($a_table, $changes,
false);
680 return $this->
handleError(
$r,
"dropTableColumn(" . $a_table .
", " . $a_column .
")");
693 $manager = $this->db->loadModule(
'Manager');
694 $reverse = $this->db->loadModule(
'Reverse');
695 $def = $reverse->getTableFieldDefinition($a_table, $a_column);
699 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php")) {
700 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
702 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
705 $best_alt = $analyzer->getBestDefinitionAlternative(
$def);
707 unset(
$def[
"nativetype"]);
708 unset(
$def[
"mdb2type"]);
711 $type = ($a_attributes[
"type"] !=
"")
712 ? $a_attributes[
"type"]
714 foreach (
$def as $k => $v) {
715 if ($k !=
"type" && !in_array($k, $this->allowed_attributes[
$type])) {
720 foreach ($a_attributes as $k => $v) {
721 $check_array[$k] = $v;
724 $this->
raisePearError(
"ilDB Error: modifyTableColumn(" . $a_table .
", " . $a_column .
")<br />" .
729 if ($this->getDbType() ==
"oracle") {
730 if (
$def[
"notnull"] ==
true && ($a_attributes[
"notnull"] ==
true 731 || !isset($a_attributes[
"notnull"]))) {
732 unset(
$def[
"notnull"]);
733 unset($a_attributes[
"notnull"]);
735 if (
$def[
"notnull"] ==
false && ($a_attributes[
"notnull"] ==
false 736 || !isset($a_attributes[
"notnull"]))) {
737 unset(
$def[
"notnull"]);
738 unset($a_attributes[
"notnull"]);
741 foreach ($a_attributes as $a => $v) {
745 $a_attributes[
"definition"] =
$def;
749 $a_column => $a_attributes
753 $r = $manager->alterTable($a_table, $changes,
false);
755 return $this->
handleError(
$r,
"modifyTableColumn(" . $a_table .
")");
770 $this->
raisePearError(
"ilDB Error: renameTableColumn(" . $a_table .
"," . $a_column .
"," . $a_new_column .
")<br />" .
774 $manager = $this->db->loadModule(
'Manager');
775 $reverse = $this->db->loadModule(
'Reverse');
776 $def = $reverse->getTableFieldDefinition($a_table, $a_column);
778 $this->
handleError(
$def,
"renameTableColumn(" . $a_table .
"," . $a_column .
"," . $a_new_column .
")");
780 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php")) {
781 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
783 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
787 $best_alt = $analyzer->getBestDefinitionAlternative(
$def);
789 unset(
$def[
"nativetype"]);
790 unset(
$def[
"mdb2type"]);
792 $f[
"definition"] =
$def;
793 $f[
"name"] = $a_new_column;
801 $r = $manager->alterTable($a_table, $changes,
false);
803 return $this->
handleError(
$r,
"renameTableColumn(" . $a_table .
"," . $a_column .
"," . $a_new_column .
")");
816 $this->
raisePearError(
"ilDB Error: renameTable(" . $a_name .
"," . $a_new_name .
")<br />" .
820 $manager = $this->db->loadModule(
'Manager');
821 $r = $manager->alterTable($a_name,
array(
"name" => $a_new_name),
false);
829 return $this->
handleError(
$r,
"renameTable(" . $a_name .
"," . $a_new_name .
")");
841 $manager = $this->db->loadModule(
'Manager');
844 foreach ($a_fields as $f) {
845 $fields[$f] =
array();
851 $r = $manager->createConstraint(
857 return $this->
handleError(
$r,
"addPrimaryKey(" . $a_table .
")");
876 $manager = $this->db->loadModule(
'Manager');
878 $r = $manager->dropConstraint(
884 return $this->
handleError(
$r,
"dropPrimaryKey(" . $a_table .
")");
894 public function addIndex($a_table, $a_fields, $a_name =
"in", $a_fulltext =
false)
899 $manager = $this->db->loadModule(
'Manager');
903 $this->
raisePearError(
"ilDB Error: addIndex(" . $a_table .
"," . $a_name .
")<br />" .
908 foreach ($a_fields as $f) {
909 $fields[$f] =
array();
916 $r = $manager->createIndex($a_table, $this->
constraintName($a_table, $a_name), $definition);
950 $manager = $this->db->loadModule(
'Manager');
951 $reverse = $this->db->loadModule(
'Reverse');
953 foreach ($manager->listTableIndexes($a_table) as $idx_name) {
954 $def = $reverse->getTableIndexDefinition($a_table, $idx_name);
955 $idx_fields = array_keys((
array)
$def[
'fields']);
957 if ($idx_fields === $a_fields) {
973 $manager = $this->db->loadModule(
'Manager');
974 $reverse = $this->db->loadModule(
'Reverse');
976 foreach ($manager->listTableIndexes($a_table) as $idx_name) {
977 $def = $reverse->getTableIndexDefinition($a_table, $idx_name);
978 $idx_fields = array_keys((
array)
$def[
'fields']);
980 if ($idx_fields === $a_fields) {
981 return $this->
dropIndex($a_table, $idx_name);
997 $manager = $this->db->loadModule(
'Manager');
1000 $r = $manager->dropIndex($a_table, $this->
constraintName($a_table, $a_name));
1005 return $this->
handleError(
$r,
"dropIndex(" . $a_table .
")");
1017 $manager = $this->db->loadModule(
'Manager');
1021 $this->
raisePearError(
"ilDB Error: addUniqueConstraint(" . $a_table .
"," . $a_name .
")<br />" .
1026 foreach ($a_fields as $f) {
1027 $fields[$f] =
array();
1029 $definition =
array(
1034 $r = $manager->createConstraint($a_table, $this->
constraintName($a_table, $a_name), $definition);
1036 return $this->
handleError(
$r,
"addUniqueConstraint(" . $a_table .
")");
1048 $manager = $this->db->loadModule(
'Manager');
1050 $r = $manager->dropConstraint(
1056 return $this->
handleError(
$r,
"dropUniqueConstraint(" . $a_table .
")");
1067 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php")) {
1068 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
1070 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
1073 $cons = $analyzer->getConstraintsInformation($a_table);
1074 foreach ($cons as $c) {
1075 if ($c[
"type"] ==
"unique" && count($a_fields) == count($c[
"fields"])) {
1077 foreach ($a_fields as $f) {
1078 if (!isset($c[
"fields"][$f])) {
1095 $manager = $this->db->loadModule(
'Manager');
1097 $r = $manager->createSequence($a_table_name, $a_start);
1099 return $this->
handleError(
$r,
"createSequence(" . $a_table_name .
")");
1108 $manager = $this->db->loadModule(
'Manager');
1110 $r = $manager->dropSequence($a_table_name);
1112 return $this->
handleError(
$r,
"dropSequence(" . $a_table_name .
")");
1122 if (!preg_match(
"/^[a-z]+[_a-z0-9]*$/", $a_name)) {
1123 $this->error_str =
"Table name must only contain _a-z0-9 and must start with a-z.";
1128 $this->error_str =
"Invalid table name '" . $a_name .
"' (Reserved Word).";
1132 if (strtolower(
substr($a_name, 0, 4)) ==
"sys_") {
1133 $this->error_str =
"Invalid table name '" . $a_name .
"'. Name must not start with 'sys_'.";
1137 if (strlen($a_name) > 22) {
1138 $this->error_str =
"Invalid table name '" . $a_name .
"'. Maximum table identifer length is 22 bytes.";
1152 foreach ($a_cols as $col =>
$def) {
1185 if (!in_array($a_def[
"type"],
array(
"text",
"integer",
"float",
"date",
"time",
"timestamp",
"clob",
"blob"))) {
1186 switch ($a_def[
"type"]) {
1188 $this->error_str =
"Invalid column type '" . $a_def[
"type"] .
"'. Use integer(1) instead.";
1192 $this->error_str =
"Invalid column type '" . $a_def[
"type"] .
"'. Use float or integer instead.";
1196 $this->error_str =
"Invalid column type '" . $a_def[
"type"] .
"'. Allowed types are: " .
1197 "text, integer, float, date, time, timestamp, clob and blob.";
1204 foreach ($a_def as $k => $v) {
1206 $this->error_str =
"Attribute '" . $k .
"' is not allowed for column type '" . $a_def[
"type"] .
"'.";
1212 switch ($a_def[
"type"]) {
1214 if ($a_def[
"length"] < 1 || $a_def[
"length"] > 4000) {
1215 if (!$a_modify_mode || isset($a_def[
"length"])) {
1216 $this->error_str =
"Invalid length '" . $a_def[
"length"] .
"' for type text." .
1217 " Length must be >=1 and <= 4000.";
1224 if (!in_array($a_def[
"length"],
array(1, 2, 3, 4, 8))) {
1225 if (!$a_modify_mode || isset($a_def[
"length"])) {
1226 $this->error_str =
"Invalid length '" . $a_def[
"length"] .
"' for type integer." .
1227 " Length must be 1, 2, 3, 4 or 8 (bytes).";
1231 if ($a_def[
"unsigned"]) {
1232 $this->error_str =
"Unsigned attribut must not be true for type integer.";
1248 if (!preg_match(
"/^[a-z]+[_a-z0-9]*$/", $a_name)) {
1249 $this->error_str =
"Invalid column name '" . $a_name .
"'. Column name must only contain _a-z0-9 and must start with a-z.";
1254 $this->error_str =
"Invalid column name '" . $a_name .
"' (Reserved Word).";
1258 if (strtolower(
substr($a_name, 0, 4)) ==
"sys_") {
1259 $this->error_str =
"Invalid column name '" . $a_name .
"'. Name must not start with 'sys_'.";
1263 if (strlen($a_name) > 30) {
1264 $this->error_str =
"Invalid column name '" . $a_name .
"'. Maximum column identifer length is 30 bytes.";
1278 if (!preg_match(
"/^[a-z]+[_a-z0-9]*$/", $a_name)) {
1279 $this->error_str =
"Invalid column name '" . $a_name .
"'. Column name must only contain _a-z0-9 and must start with a-z.";
1284 $this->error_str =
"Invalid column name '" . $a_name .
"' (Reserved Word).";
1288 if (strlen($a_name) > 3) {
1289 $this->error_str =
"Invalid index name '" . $a_name .
"'. Maximum index identifer length is 3 bytes.";
1308 return $a_constraint;
1317 require_once(
'./Services/Database/classes/PDO/FieldDefinition/class.ilDBPdoMySQLFieldDefinition.php');
1321 return $ilDBPdoMySQLFieldDefinition->isReserved($a_word);
1339 public function query($sql, $a_handle_error =
true)
1343 if (is_object($ilBench)) {
1344 $ilBench->startDbBench($sql);
1346 $r = $this->db->query($sql);
1347 if (is_object($ilBench)) {
1348 $ilBench->stopDbBench();
1351 if ($a_handle_error) {
1365 public function queryF($a_query, $a_types, $a_values)
1367 if (!is_array($a_types) || !is_array($a_values) ||
1368 count($a_types) != count($a_values)) {
1369 $this->
raisePearError(
"ilDB::queryF: Types and values must be arrays of same size. ($a_query)");
1371 $quoted_values =
array();
1372 foreach ($a_types as $k =>
$t) {
1373 $quoted_values[] = $this->
quote($a_values[$k],
$t);
1375 $query = vsprintf($a_query, $quoted_values);
1389 if (!is_array($a_types) || !is_array($a_values) ||
1390 count($a_types) != count($a_values)) {
1391 $this->
raisePearError(
"ilDB::manipulateF: types and values must be arrays of same size. ($a_query)");
1393 $quoted_values =
array();
1394 foreach ($a_types as $k =>
$t) {
1395 $quoted_values[] = $this->
quote($a_values[$k],
$t);
1397 $query = vsprintf($a_query, $quoted_values);
1407 $pos1 = strpos(strtolower($sql),
"from ");
1410 $tablef =
substr($sql, $pos1+5);
1411 $pos2 = strpos(strtolower($tablef),
" ");
1418 if (trim(
$table) !=
"") {
1419 if (!is_array($this->ttt) || !in_array(
$table, $this->ttt)) {
1424 echo
"<br><b>" . $sql .
"</b>";
1433 $this->db->setLimit($a_limit, $a_offset);
1444 $r = $this->db->nextId($a_table_name,
false);
1446 return $this->
handleError(
$r,
"nextId(" . $a_table_name .
")");
1463 if (is_object($ilBench)) {
1464 $ilBench->startDbBench($sql);
1466 $r = $this->db->exec($sql);
1467 if (is_object($ilBench)) {
1468 $ilBench->stopDbBench();
1482 public function prepare($a_query, $a_types = null, $a_result_types = null)
1484 $res = $this->db->prepare($a_query, $a_types, $a_result_types);
1514 $res = $a_stmt->execute($a_data);
1530 $res = $this->db->extended->executeMultiple($a_stmt, $a_data);
1532 return $this->
handleError(
$res,
"executeMultiple(" . $a_stmt->query .
")");
1544 $field_values =
array();
1545 $placeholders =
array();
1550 foreach ($a_columns as $k => $col) {
1552 $placeholders[] =
"%s";
1553 $placeholders2[] =
":$k";
1557 if ($col[0] ==
'integer' && !is_null($col[1])) {
1558 $col[1] = (int) $col[1];
1561 $values[] = $col[1];
1562 $field_values[$k] = $col[1];
1563 if ($col[0] ==
"blob" || $col[0] ==
"clob") {
1569 $st = $this->db->prepare(
"INSERT INTO " . $a_table .
" (" . implode($fields,
",") .
") VALUES (" .
1572 $this->
handleError($st,
"insert / prepare/execute(" . $a_table .
")");
1574 $r = $st->execute($field_values);
1578 $this->
handleError(
$r,
"insert / prepare/execute(" . $a_table .
")");
1581 $q =
"INSERT INTO " . $a_table .
" (" . implode($fields,
",") .
") VALUES (" .
1582 implode($placeholders,
",") .
")";
1596 public function update($a_table, $a_columns, $a_where)
1599 $field_values =
array();
1600 $placeholders =
array();
1605 foreach ($a_columns as $k => $col) {
1607 $placeholders[] =
"%s";
1608 $placeholders2[] =
":$k";
1612 if ($col[0] ==
'integer' && !is_null($col[1])) {
1613 $col[1] = (int) $col[1];
1616 $values[] = $col[1];
1617 $field_values[$k] = $col[1];
1618 if ($col[0] ==
"blob" || $col[0] ==
"clob") {
1625 $q =
"UPDATE " . $a_table .
" SET ";
1627 foreach ($fields as $k => $field) {
1628 $q.= $lim . $field .
" = " . $placeholders2[$k];
1633 foreach ($a_where as $k => $col) {
1634 $q.= $lim . $k .
" = " . $this->
quote($col[1], $col[0]);
1638 $r = $st->execute($field_values);
1641 $this->
handleError(
$r,
"update / prepare/execute(" . $a_table .
")");
1644 foreach ($a_where as $k => $col) {
1646 $values[] = $col[1];
1647 $field_values[$k] = $col;
1649 $q =
"UPDATE " . $a_table .
" SET ";
1651 foreach ($fields as $k => $field) {
1652 $q.= $lim . $field .
" = " . $placeholders[$k];
1657 foreach ($a_where as $k => $col) {
1658 $q.= $lim . $k .
" = %s";
1674 public function replace($a_table, $a_pk_columns, $a_other_columns)
1677 $a_columns = array_merge($a_pk_columns, $a_other_columns);
1679 $field_values =
array();
1680 $placeholders =
array();
1685 foreach ($a_columns as $k => $col) {
1687 $placeholders[] =
"%s";
1688 $placeholders2[] =
":$k";
1692 if ($col[0] ==
'integer' && !is_null($col[1])) {
1693 $col[1] = (int) $col[1];
1696 $values[] = $col[1];
1697 $field_values[$k] = $col[1];
1698 if ($col[0] ==
"blob" || $col[0] ==
"clob") {
1704 $st = $this->db->prepare(
"REPLACE INTO " . $a_table .
" (" . implode($fields,
",") .
") VALUES (" .
1706 $this->
handleError($st,
"insert / prepare/execute(" . $a_table .
")");
1707 $r = $st->execute($field_values);
1709 $this->
handleError(
$r,
"insert / prepare/execute(" . $a_table .
")");
1712 $q =
"REPLACE INTO " . $a_table .
" (" . implode($fields,
",") .
") VALUES (" .
1713 implode($placeholders,
",") .
")";
1734 return $a_st->free();
1754 return $a_set->numRows();
1772 public function in($a_field, $a_values, $negate =
false,
$a_type =
"")
1774 if (count($a_values) == 0) {
1777 return $negate ?
' 1=1 ' :
' 1=2 ';
1781 $str = $a_field . (($negate) ?
" NOT" :
"") .
" IN (?" . str_repeat(
",?", count($a_values) - 1) .
")";
1783 $str = $a_field . (($negate) ?
" NOT" :
"") .
" IN (";
1785 foreach ($a_values as $v) {
1800 if (!is_array($a_arr)) {
1804 $type_arr = array_fill(0, $a_cnt,
$a_type);
1806 $type_arr =
array();
1808 return array_merge($a_arr, $type_arr);
1832 if (!count($a_values)) {
1836 $concat =
' CONCAT(';
1838 foreach ($a_values as $field_info) {
1839 $val = $field_info[0];
1845 if ($a_allow_null) {
1846 $concat .=
'COALESCE(';
1850 if ($a_allow_null) {
1867 public function substr($a_exp, $a_pos = 1, $a_len = -1)
1871 $lenstr =
", " . $a_len;
1873 return " SUBSTR(" . $a_exp .
", " . $a_pos . $lenstr .
") ";
1884 return " UPPER(" . $a_exp .
") ";
1895 return " LOWER(" . $a_exp .
") ";
1905 public function locate($a_needle, $a_string, $a_start_pos = 1)
1907 $locate =
' LOCATE( ';
1908 $locate .= $a_needle;
1910 $locate .= $a_string;
1912 $locate .= $a_start_pos;
1923 public function like($a_col,
$a_type, $a_value =
"?", $case_insensitive =
true)
1925 if (!in_array(
$a_type,
array(
"text",
"clob",
"blob"))) {
1928 if ($a_value ==
"?") {
1929 if ($case_insensitive) {
1930 return "UPPER(" . $a_col .
") LIKE(UPPER(?))";
1932 return $a_col .
" LIKE(?)";
1935 if ($case_insensitive) {
1937 return " UPPER(" . $a_col .
") LIKE(UPPER(" . $this->
quote($a_value,
'text') .
"))";
1940 return " " . $a_col .
" LIKE(" . $this->
quote($a_value,
'text') .
")";
1951 if (!$a_empty_or_null || $a_value !=
"") {
1952 return $a_col .
" = " . $this->
quote($a_value,
$a_type);
1954 return "(" . $a_col .
" = '' OR $a_col IS NULL)";
1963 if (!$a_empty_or_null) {
1964 return $a_col .
" <> " . $this->
quote($a_value,
$a_type);
1966 if ($a_value !=
"") {
1967 return "(" . $a_col .
" <> " . $this->
quote($a_value,
$a_type) .
" OR " .
1968 $a_col .
" IS NULL)";
1970 return "(" . $a_col .
" <> '' AND $a_col IS NOT NULL)";
1982 return "FROM_UNIXTIME(" . $a_expr .
")";
1990 return "UNIX_TIMESTAMP()";
2008 if (is_array($tables)) {
2009 if (in_array($a_table, $tables)) {
2025 $column_visibility =
false;
2026 $manager = $this->db->loadModule(
'Manager');
2027 $r = $manager->listTableFields($a_table);
2030 foreach (
$r as $field) {
2031 if ($field == $a_column_name) {
2032 $column_visibility =
true;
2037 return $column_visibility;
2049 if (is_file(
"./Services/Database/classes/class.ilDBAnalyzer.php")) {
2050 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
2052 include_once(
"../Services/Database/classes/class.ilDBAnalyzer.php");
2055 $cons = $analyzer->getConstraintsInformation($a_table);
2056 foreach ($cons as $c) {
2057 if ($c[
"type"] ==
"unique" && count($a_fields) == count($c[
"fields"])) {
2059 foreach ($a_fields as $f) {
2060 if (!isset($c[
"fields"][$f])) {
2080 $manager = $this->db->loadModule(
'Manager');
2081 $r = $manager->listTables();
2100 if (is_array($sequences)) {
2101 if (in_array($a_sequence, $sequences)) {
2115 $manager = $this->db->loadModule(
'Manager');
2116 $r = $manager->listSequences();
2135 if ($a_query ==
"" && is_null(
$a_type)) {
2140 if (
$a_type ==
'integer' && !is_null($a_query)) {
2141 return (
int) $a_query;
2145 $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);
2148 return $this->db->quote($a_query,
$a_type);
2160 return $this->db->quoteIdentifier($a_identifier);
2175 if (!$this->db->supports(
'transactions')) {
2176 $this->
raisePearError(
"ilDB::beginTransaction: Transactions are not supported.", $this->error_class->FATAL);
2178 $res = $this->db->beginTransaction();
2188 $res = $this->db->commit();
2198 $res = $this->db->rollback();
2209 abstract public function lockTables($a_tables);
2235 $res = $this->db->autoExecute($a_tablename, $a_fields, $a_mode, $a_where);
2251 $res = $this->db->lastInsertId();
2270 $set = $this->db->query($sql);
2292 $set = $this->
query($sql);
2293 $r = $set->fetchRow($mode);
2312 while (
$data = $query_result->fetch($fetch_mode)) {
2326 $this->sub_type = (
string) $a_value;
2366 return $this->db->getSequenceName($table_name);
2375 require_once(
'./Services/Database/classes/Atom/class.ilAtomQueryLock.php');
2387 $query_replaced = preg_replace(
2388 '/[\x{10000}-\x{10FFFF}]/u',
2392 if (!empty($query_replaced)) {
2393 return $query_replaced;
2413 public function cast($a_field_name, $a_dest_type)
2415 $manager = $this->db->loadModule(
'Manager');
2416 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.
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)
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.
foreach($_POST as $key=> $value) $res
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.
createTable( $a_name, $a_definition_array, $a_drop_table=false, $a_ignore_erros=false)
Create a new table in the database.
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.
if(empty($password)) $table
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.
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
dropFulltextIndex($a_table, $a_name)