47 if (@is_file(
'../Services/Database/classes/class.ilDBAnalyzer.php')) {
48 include_once
'../Services/Database/classes/class.ilDBAnalyzer.php';
50 include_once
'./Services/Database/classes/class.ilDBAnalyzer.php';
62 $this->testmode = $a_testmode;
92 $a_table_name = strtolower($a_table_name);
97 $auto_inc_field = $this->analyzer->getAutoIncrementField($a_table_name);
100 $pk = $this->analyzer->getPrimaryKeyInformation($a_table_name);
103 $indices = $this->analyzer->getIndicesInformation($a_table_name);
106 $constraints = $this->analyzer->getConstraintsInformation($a_table_name);
109 $fields = $this->analyzer->getFieldInformation($a_table_name);
130 $this->
alterTable($a_table_name, $fields, $a_set_text_ts_fields_notnull_false, $pk);
132 $a_table_name = strtolower($a_table_name) .
"_copy";
162 if ($auto_inc_field !=
"") {
184 if ($nr_rec != $nr_rec2) {
185 throw new ilDatabaseException(
"ilMySQLAbstraction: Unexpected difference in table record number, table '" . $a_table_name .
"'."
186 .
" Before: " . ((
int) $nr_rec) .
", After: " . ((
int) $nr_rec2) .
".");
202 $st =
$ilDB->prepare(
"SELECT count(*) AS cnt FROM `" . $a_table_name .
"`");
218 $st = $this->
ilDBInterface->
prepareManip(
"REPLACE INTO abstraction_progress (table_name, step)" .
" VALUES (?,?)", array(
236 $fields = $this->analyzer->getFieldInformation($a_table);
238 foreach ($fields as $field =>
$def) {
239 if (
$def[
"type"] ==
"text"
240 && (
$def[
"length"] >= 1 &&
$def[
"length"] <= 4000)
242 $upfields[] = $field;
245 foreach ($upfields as $uf) {
246 $ilDB->query(
"UPDATE `" . $a_table .
"` SET `" . $uf .
"` = null WHERE `" . $uf .
"` = ''");
265 $fields = $this->analyzer->getFieldInformation($a_table);
267 foreach ($fields as $field =>
$def) {
268 if (
$def[
"type"] ==
"timestamp") {
269 $upfields[] = $field;
272 foreach ($upfields as $uf) {
273 $ilDB->query(
"UPDATE `" . $a_table .
"` SET `" . $uf .
"` = null WHERE `" . $uf .
"` = '0000-00-00 00:00:00'");
278 foreach ($fields as $field =>
$def) {
279 if (
$def[
"type"] ==
"date") {
280 $upfields[] = $field;
283 foreach ($upfields as $uf) {
284 $ilDB->query(
"UPDATE `" . $a_table .
"` SET `" . $uf .
"` = null WHERE `" . $uf .
"` = '0000-00-00'");
298 if ($a_table_name != strtolower($a_table_name)) {
301 mysql_query(
"ALTER TABLE `" . $a_table_name .
"` RENAME `" . strtolower($a_table_name) .
"xxx" .
"`");
302 mysql_query(
"ALTER TABLE `" . strtolower($a_table_name) .
"xxx" .
"` RENAME `" . strtolower($a_table_name) .
"`");
316 $result = mysql_query(
"SHOW COLUMNS FROM `" . $a_table_name .
"`");
318 if (
$row[
"Field"] != strtolower(
$row[
"Field"])) {
319 $ilDB->renameTableColumn($a_table_name,
$row[
"Field"], strtolower(
$row[
"Field"]));
333 if ($a_auto_inc_field !=
"") {
347 if ($a_pk[
"name"] !=
"") {
348 $this->
ilDBInterface->dropPrimaryKey($a_table, $a_pk[
"name"]);
361 if (is_array($a_indices)) {
362 foreach ($a_indices as
$index) {
377 if (is_array($a_constraints)) {
378 foreach ($a_constraints as $c) {
379 if ($c[
"type"] ==
"unique") {
380 $this->
ilDBInterface->
query(
"ALTER TABLE `" . $a_table .
"` DROP INDEX `" . $c[
"name"] .
"`");
395 public function alterTable($a_table, $a_fields, $a_set_text_ts_fields_notnull_false =
true, $pk =
"")
398 foreach ($a_fields as $field =>
$d) {
399 $def = $this->reverse->getTableFieldDefinition($a_table, $field);
401 $best_alt = $this->analyzer->getBestDefinitionAlternative(
$def);
405 if (strtolower(
$def[
"nativetype"]) ==
"timestamp"
406 && strtolower(
$def[
"default"]) ==
"current_timestamp"
408 unset(
$def[
"default"]);
411 if (strtolower(
$def[
"type"]) ==
"float") {
412 unset(
$def[
"length"]);
416 foreach (
$def as $k => $v) {
417 if (!in_array($k, array(
"type",
"default",
"notnull",
"length",
"unsigned",
"fixed" ))) {
423 if (
$def[
"type"] ==
"decimal") {
424 $l_arr = explode(
",",
$def[
"length"]);
425 $def[
"length"] = $l_arr[0];
429 if (
$def[
"type"] ==
"float") {
430 unset(
$def[
"length"]);
434 if ($a_set_text_ts_fields_notnull_false
435 && (
$def[
"type"] ==
"text"
436 ||
$def[
"type"] ==
"timestamp"
437 ||
$def[
"type"] ==
"date")
438 && (!is_array($pk) || !isset($field, $pk[
"fields"][$field]))
440 $def[
"notnull"] =
false;
444 if (
$def[
"type"] ==
"integer") {
445 $def[
"unsigned"] =
false;
449 if (
$def[
"type"] ==
"blob" ||
$def[
"type"] ==
"clob") {
450 $def[
"notnull"] =
false;
454 if ((
$def[
"type"] ==
"timestamp" &&
$def[
"default"] ==
"0000-00-00 00:00:00")
455 || (
$def[
"type"] ==
"date" &&
$def[
"default"] ==
"0000-00-00")
457 unset(
$def[
"default"]);
461 foreach (
$def as $k => $v) {
464 $def[
"definition"] = $a;
466 $n_fields[$field] =
$def;
470 "change" => $n_fields,
474 $r = $this->manager->alterTable($a_table, $changes,
false);
476 $r = $this->manager->createTable(strtolower($a_table) .
"_copy", $n_fields);
489 if (is_array($a_pk[
"fields"])) {
491 foreach ($a_pk[
"fields"] as $f => $pos) {
492 $fields[] = strtolower($f);
507 if (is_array($a_indices)) {
510 foreach ($a_indices as
$index) {
511 if (strlen(
$index[
"name"]) > 3) {
517 foreach ($a_indices as
$index) {
518 if (is_array(
$index[
"fields"])) {
520 $index[
"name"] =
"i" . $cnt;
523 foreach (
$index[
"fields"] as $f => $pos) {
524 $fields[] = strtolower($f);
542 if (is_array($a_constraints)) {
545 foreach ($a_constraints as $c) {
546 if (strlen($c[
"name"]) > 3) {
552 foreach ($a_constraints as $c) {
553 if (is_array($c[
"fields"])) {
555 $c[
"name"] =
"c" . $cnt;
558 foreach ($c[
"fields"] as $f => $pos) {
559 $fields[] = strtolower($f);
561 $this->
ilDBInterface->addUniqueConstraint($a_table, $fields, strtolower($c[
"name"]));
579 $indices = $this->analyzer->getIndicesInformation($a_table);
580 foreach ($indices as
$index) {
581 if (strlen(
$index[
"name"]) > 3) {
587 foreach ($indices as
$index) {
603 if ($a_auto_inc_field !=
"") {
604 $set = $this->
ilDBInterface->
query(
"SELECT MAX(`" . strtolower($a_auto_inc_field) .
"`) ma FROM `" . $a_table .
"`");
606 $next = $rec[
"ma"] + 1;
622 $fields = $this->analyzer->getFieldInformation($a_table);
624 if (
$def[
"type"] ==
"clob" &&
$def[
"notnull"] ==
true) {
641 $fields = $this->analyzer->getFieldInformation($a_table);
643 if (
$def[
"type"] ==
"timestamp"
644 && (
$def[
"notnull"] ==
true ||
$def[
"default"] ==
"0000-00-00 00:00:00")
646 $nd = array(
"type" =>
"timestamp",
"notnull" =>
false );
647 if (
$def[
"default"] ==
"0000-00-00 00:00:00") {
648 $nd[
"default"] =
null;
652 if (
$def[
"type"] ==
"date"
653 && (
$def[
"notnull"] ==
true ||
$def[
"default"] ==
"0000-00-00")
655 $nd = array(
"type" =>
"date",
"notnull" =>
false );
656 if (
$def[
"default"] ==
"0000-00-00") {
657 $nd[
"default"] =
null;
An exception for terminatinating execution or to throw for unit testing.
This class gives all kind of DB information using the MDB2 manager and reverse module.
Class ilDatabaseException.
This class includes methods that help to abstract ILIAS 3.10.x MySQL tables for the use with MDB2 abs...
lowerCaseColumnNames($a_table_name)
lower case column names
fixClobNotNull($a_table)
This is only used on tables that have already been abstracted but missed the "full treatment".
addPrimaryKey($a_table, $a_pk)
__construct()
Constructor.
performAbstraction($a_table_name, $a_set_text_ts_fields_notnull_false=true)
Converts an existing (MySQL) ILIAS table in an abstract table.
fixIndexNames($a_table)
This is only used on tables that have already been abstracted but missed the "full treatment".
countRecords($a_table_name)
Check number of records before and after.
replaceEmptyDatesWithNull($a_table)
Replace empty dates with null.
removeConstraints($a_table, $a_constraints)
Remove Constraints.
removeIndices($a_table, $a_indices)
Remove Indices.
alterTable($a_table, $a_fields, $a_set_text_ts_fields_notnull_false=true, $pk="")
replaceEmptyStringsWithNull($a_table)
Replace empty strings with null values.
fixDatetimeValues($a_table)
This is only used on tables that have already been abstracted but missed the "full treatment".
lowerCaseTableName($a_table_name)
Lower case table and field names.
removePrimaryKey($a_table, $a_pk)
Remove primary key from table.
addConstraints($a_table, $a_constraints)
Add constraints.
addIndices($a_table, $a_indices)
Add indices.
storeStep($a_table, $a_step)
Store performed step.
removeAutoIncrement($a_table_name, $a_auto_inc_field)
Remove auto_increment attribute of a field.
addAutoIncrementSequence($a_table, $a_auto_inc_field)
Add autoincrement sequence.
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
execute($stmt, $data=array())
fetchAssoc($query_result)
addPrimaryKey($table_name, $primary_keys)
query($query)
Run a (read-only) Query on the database.
modifyTableColumn($table, $column, $attributes)
createSequence($table_name, $start=1)
prepareManip($a_query, $a_types=null)
dropIndex($a_table, $a_name="i1")
addIndex($table_name, $fields, $index_name='', $fulltext=false)
foreach($_POST as $key=> $value) $res