26 $this->manager = $ilDB->db->loadModule(
'Manager');
27 $this->reverse = $ilDB->db->loadModule(
'Reverse');
28 if (is_file(
'../Services/Database/classes/class.ilDBAnalyzer.php'))
30 include_once
'../Services/Database/classes/class.ilDBAnalyzer.php';
34 include_once
'./Services/Database/classes/class.ilDBAnalyzer.php';
47 $this->testmode = $a_testmode;
57 return $this->testmode;
79 $a_table_name = strtolower($a_table_name);
84 $auto_inc_field = $this->analyzer->getAutoIncrementField($a_table_name);
87 $pk = $this->analyzer->getPrimaryKeyInformation($a_table_name);
90 $indices = $this->analyzer->getIndicesInformation($a_table_name);
93 $constraints = $this->analyzer->getConstraintsInformation($a_table_name);
96 $fields = $this->analyzer->getFieldInformation($a_table_name);
118 $this->
alterTable($a_table_name, $fields, $a_set_text_ts_fields_notnull_false, $pk);
121 $a_table_name = strtolower($a_table_name).
"_copy";
157 if ($auto_inc_field !=
"")
184 if ($nr_rec != $nr_rec2)
186 die(
"ilMySQLAbstraction: Unexpected difference in table record number, table '".$a_table_name.
"'.".
187 " Before: ".((int) $nr_rec).
", After: ".((
int) $nr_rec2).
".");
200 $st = $ilDB->prepare(
"SELECT count(*) AS cnt FROM `".$a_table_name.
"`");
201 $res = $ilDB->execute($st);
202 $rec = $ilDB->fetchAssoc(
$res);
212 $st = $this->il_db->prepareManip(
"REPLACE INTO abstraction_progress (table_name, step)".
213 " VALUES (?,?)", array(
"text",
"integer"));
214 $this->il_db->execute($st, array($a_table, $a_step));
224 $fields = $this->analyzer->getFieldInformation($a_table);
226 foreach ($fields as $field => $def)
228 if ($def[
"type"] ==
"text" &&
229 ($def[
"length"] >= 1 && $def[
"length"] <= 4000))
231 $upfields[] = $field;
234 foreach ($upfields as $uf)
236 $ilDB->query(
"UPDATE `".$a_table.
"` SET `".$uf.
"` = null WHERE `".$uf.
"` = ''");
247 if (!$this->il_db->tableExists($a_table))
252 $fields = $this->analyzer->getFieldInformation($a_table);
254 foreach ($fields as $field => $def)
256 if ($def[
"type"] ==
"timestamp")
258 $upfields[] = $field;
261 foreach ($upfields as $uf)
263 $ilDB->query(
"UPDATE `".$a_table.
"` SET `".$uf.
"` = null WHERE `".$uf.
"` = '0000-00-00 00:00:00'");
268 foreach ($fields as $field => $def)
270 if ($def[
"type"] ==
"date")
272 $upfields[] = $field;
275 foreach ($upfields as $uf)
277 $ilDB->query(
"UPDATE `".$a_table.
"` SET `".$uf.
"` = null WHERE `".$uf.
"` = '0000-00-00'");
292 if ($a_table_name != strtolower($a_table_name))
296 mysql_query(
"ALTER TABLE `".$a_table_name.
"` RENAME `".strtolower($a_table_name).
"xxx".
"`");
297 mysql_query(
"ALTER TABLE `".strtolower($a_table_name).
"xxx".
"` RENAME `".strtolower($a_table_name).
"`");
308 $result = mysql_query(
"SHOW COLUMNS FROM `".$a_table_name.
"`");
311 if (
$row[
"Field"] != strtolower(
$row[
"Field"]))
313 $ilDB->renameTableColumn($a_table_name,
$row[
"Field"], strtolower(
$row[
"Field"]));
327 if ($a_auto_inc_field !=
"")
329 $this->il_db->modifyTableColumn($a_table_name, $a_auto_inc_field, array());
341 if ($a_pk[
"name"] !=
"")
343 $this->il_db->dropPrimaryKey($a_table, $a_pk[
"name"]);
355 if (is_array($a_indices))
357 foreach($a_indices as $index)
359 $this->il_db->query(
"ALTER TABLE `".$a_table.
"` DROP INDEX `".$index[
"name"].
"`");
372 if (is_array($a_constraints))
374 foreach($a_constraints as $c)
376 if ($c[
"type"] ==
"unique")
378 $this->il_db->query(
"ALTER TABLE `".$a_table.
"` DROP INDEX `".$c[
"name"].
"`");
391 function alterTable($a_table, $a_fields, $a_set_text_ts_fields_notnull_false =
true, $pk =
"")
394 foreach ($a_fields as $field =>
$d)
396 $def = $this->reverse->getTableFieldDefinition($a_table, $field);
397 $this->il_db->handleError($def);
398 $best_alt = $this->analyzer->getBestDefinitionAlternative($def);
399 $def = $def[$best_alt];
402 if (strtolower($def[
"nativetype"]) ==
"timestamp" &&
403 strtolower($def[
"default"]) ==
"current_timestamp")
405 unset($def[
"default"]);
408 if (strtolower($def[
"type"]) ==
"float")
410 unset($def[
"length"]);
414 foreach ($def as $k => $v)
416 if (!in_array($k, array(
"type",
"default",
"notnull",
"length",
"unsigned",
"fixed")))
423 if ($def[
"type"] ==
"decimal")
425 $l_arr = explode(
",",$def[
"length"]);
426 $def[
"length"] = $l_arr[0];
430 if ($def[
"type"] ==
"float")
432 unset($def[
"length"]);
436 if ($a_set_text_ts_fields_notnull_false && ($def[
"type"] ==
"text" ||
437 $def[
"type"] ==
"timestamp" || $def[
"type"] ==
"date") &&
438 (!is_array($pk) || !isset($field,$pk[
"fields"][$field])))
440 $def[
"notnull"] =
false;
444 if ($def[
"type"] ==
"integer")
446 $def[
"unsigned"] =
false;
450 if ($def[
"type"] ==
"blob" || $def[
"type"] ==
"clob")
452 $def[
"notnull"] =
false;
456 if (($def[
"type"] ==
"timestamp" && $def[
"default"] ==
"0000-00-00 00:00:00") ||
457 ($def[
"type"] ==
"date" && $def[
"default"] ==
"0000-00-00"))
459 unset($def[
"default"]);
463 foreach ($def as $k => $v)
467 $def[
"definition"] = $a;
469 $n_fields[$field] = $def;
473 "change" => $n_fields
478 $r = $this->manager->alterTable($a_table, $changes,
false);
482 $r = $this->manager->createTable(strtolower($a_table).
"_copy", $n_fields);
504 if (is_array($a_pk[
"fields"]))
507 foreach ($a_pk[
"fields"] as
$f => $pos)
509 $fields[] = strtolower(
$f);
511 $this->il_db->addPrimaryKey($a_table, $fields);
523 if (is_array($a_indices))
527 foreach ($a_indices as $index)
529 if (strlen($index[
"name"]) > 3)
536 foreach ($a_indices as $index)
538 if (is_array($index[
"fields"]))
542 $index[
"name"] =
"i".$cnt;
545 foreach ($index[
"fields"] as
$f => $pos)
547 $fields[] = strtolower(
$f);
549 $this->il_db->addIndex($a_table, $fields, strtolower($index[
"name"]), $index[
"fulltext"]);
564 if (is_array($a_constraints))
568 foreach ($a_constraints as $c)
570 if (strlen($c[
"name"]) > 3)
577 foreach ($a_constraints as $c)
579 if (is_array($c[
"fields"]))
583 $c[
"name"] =
"c".$cnt;
586 foreach ($c[
"fields"] as
$f => $pos)
588 $fields[] = strtolower(
$f);
590 $this->il_db->addUniqueConstraint($a_table, $fields, strtolower($c[
"name"]));
603 if (!$this->il_db->tableExists($a_table))
608 $indices = $this->analyzer->getIndicesInformation($a_table);
609 foreach ($indices as $index)
611 if (strlen($index[
"name"]) > 3)
619 foreach($indices as $index)
621 $this->il_db->dropIndex($a_table, $index[
"name"]);
635 if ($a_auto_inc_field !=
"")
637 $set = $this->il_db->query(
"SELECT MAX(`".strtolower($a_auto_inc_field).
"`) ma FROM `".$a_table.
"`");
638 $rec = $this->il_db->fetchAssoc($set);
639 $next = $rec[
"ma"] + 1;
640 $this->il_db->createSequence($a_table, $next);
650 if (!$this->il_db->tableExists($a_table))
655 $fields = $this->analyzer->getFieldInformation($a_table);
656 foreach ($fields as
$name => $def)
658 if ($def[
"type"] ==
"clob" && $def[
"notnull"] ==
true)
660 $this->il_db->modifyTableColumn($a_table,
$name, array(
"type" =>
"clob",
"notnull" =>
false));
671 if (!$this->il_db->tableExists($a_table))
676 $fields = $this->analyzer->getFieldInformation($a_table);
677 foreach ($fields as
$name => $def)
679 if ($def[
"type"] ==
"timestamp" &&
680 ($def[
"notnull"] ==
true || $def[
"default"] ==
"0000-00-00 00:00:00"))
682 $nd = array(
"type" =>
"timestamp",
"notnull" =>
false);
683 if ($def[
"default"] ==
"0000-00-00 00:00:00")
685 $nd[
"default"] = null;
687 $this->il_db->modifyTableColumn($a_table,
$name, $nd);
689 if ($def[
"type"] ==
"date" &&
690 ($def[
"notnull"] ==
true || $def[
"default"] ==
"0000-00-00"))
692 $nd = array(
"type" =>
"date",
"notnull" =>
false);
693 if ($def[
"default"] ==
"0000-00-00")
695 $nd[
"default"] = null;
697 $this->il_db->modifyTableColumn($a_table,
$name, $nd);