45 if (@is_file(
'../Services/Database/classes/class.ilDBAnalyzer.php')) {
46 include_once
'../Services/Database/classes/class.ilDBAnalyzer.php';
48 include_once
'./Services/Database/classes/class.ilDBAnalyzer.php';
59 $this->testmode = $a_testmode;
87 $a_table_name = strtolower($a_table_name);
92 $auto_inc_field = $this->analyzer->getAutoIncrementField($a_table_name);
95 $pk = $this->analyzer->getPrimaryKeyInformation($a_table_name);
98 $indices = $this->analyzer->getIndicesInformation($a_table_name);
101 $constraints = $this->analyzer->getConstraintsInformation($a_table_name);
104 $fields = $this->analyzer->getFieldInformation($a_table_name);
125 $this->
alterTable($a_table_name, $fields, $a_set_text_ts_fields_notnull_false, $pk);
127 $a_table_name = strtolower($a_table_name) .
"_copy";
157 if ($auto_inc_field !=
"") {
179 if ($nr_rec != $nr_rec2) {
180 throw new ilDatabaseException(
"ilMySQLAbstraction: Unexpected difference in table record number, table '" . $a_table_name .
"'."
181 .
" Before: " . ((
int)$nr_rec) .
", After: " . ((
int)$nr_rec2) .
".");
196 $st =
$ilDB->prepare(
"SELECT count(*) AS cnt FROM `" . $a_table_name .
"`");
211 $st = $this->
ilDBInterface->
prepareManip(
"REPLACE INTO abstraction_progress (table_name, step)" .
" VALUES (?,?)", array(
228 $fields = $this->analyzer->getFieldInformation($a_table);
230 foreach ($fields as $field => $def) {
231 if ($def[
"type"] ==
"text"
232 && ($def[
"length"] >= 1 && $def[
"length"] <= 4000)
234 $upfields[] = $field;
237 foreach ($upfields as $uf) {
238 $ilDB->query(
"UPDATE `" . $a_table .
"` SET `" . $uf .
"` = null WHERE `" . $uf .
"` = ''");
256 $fields = $this->analyzer->getFieldInformation($a_table);
258 foreach ($fields as $field => $def) {
259 if ($def[
"type"] ==
"timestamp") {
260 $upfields[] = $field;
263 foreach ($upfields as $uf) {
264 $ilDB->query(
"UPDATE `" . $a_table .
"` SET `" . $uf .
"` = null WHERE `" . $uf .
"` = '0000-00-00 00:00:00'");
269 foreach ($fields as $field => $def) {
270 if ($def[
"type"] ==
"date") {
271 $upfields[] = $field;
274 foreach ($upfields as $uf) {
275 $ilDB->query(
"UPDATE `" . $a_table .
"` SET `" . $uf .
"` = null WHERE `" . $uf .
"` = '0000-00-00'");
288 if ($a_table_name != strtolower($a_table_name)) {
291 mysql_query(
"ALTER TABLE `" . $a_table_name .
"` RENAME `" . strtolower($a_table_name) .
"xxx" .
"`");
292 mysql_query(
"ALTER TABLE `" . strtolower($a_table_name) .
"xxx" .
"` RENAME `" . strtolower($a_table_name) .
"`");
305 $result = mysql_query(
"SHOW COLUMNS FROM `" . $a_table_name .
"`");
307 if (
$row[
"Field"] != strtolower(
$row[
"Field"])) {
308 $ilDB->renameTableColumn($a_table_name,
$row[
"Field"], strtolower(
$row[
"Field"]));
321 if ($a_auto_inc_field !=
"") {
334 if ($a_pk[
"name"] !=
"") {
335 $this->
ilDBInterface->dropPrimaryKey($a_table, $a_pk[
"name"]);
347 if (is_array($a_indices)) {
348 foreach ($a_indices as $index) {
349 $this->
ilDBInterface->
query(
"ALTER TABLE `" . $a_table .
"` DROP INDEX `" . $index[
"name"] .
"`");
362 if (is_array($a_constraints)) {
363 foreach ($a_constraints as $c) {
364 if ($c[
"type"] ==
"unique") {
365 $this->
ilDBInterface->
query(
"ALTER TABLE `" . $a_table .
"` DROP INDEX `" . $c[
"name"] .
"`");
380 public function alterTable($a_table, $a_fields, $a_set_text_ts_fields_notnull_false =
true, $pk =
"") {
382 foreach ($a_fields as $field =>
$d) {
383 $def = $this->reverse->getTableFieldDefinition($a_table, $field);
385 $best_alt = $this->analyzer->getBestDefinitionAlternative($def);
386 $def = $def[$best_alt];
389 if (strtolower($def[
"nativetype"]) ==
"timestamp"
390 && strtolower($def[
"default"]) ==
"current_timestamp"
392 unset($def[
"default"]);
395 if (strtolower($def[
"type"]) ==
"float") {
396 unset($def[
"length"]);
400 foreach ($def as $k => $v) {
401 if (!in_array($k, array(
"type",
"default",
"notnull",
"length",
"unsigned",
"fixed" ))) {
407 if ($def[
"type"] ==
"decimal") {
408 $l_arr = explode(
",", $def[
"length"]);
409 $def[
"length"] = $l_arr[0];
413 if ($def[
"type"] ==
"float") {
414 unset($def[
"length"]);
418 if ($a_set_text_ts_fields_notnull_false
419 && ($def[
"type"] ==
"text"
420 || $def[
"type"] ==
"timestamp"
421 || $def[
"type"] ==
"date")
422 && (!is_array($pk) || !isset($field, $pk[
"fields"][$field]))
424 $def[
"notnull"] =
false;
428 if ($def[
"type"] ==
"integer") {
429 $def[
"unsigned"] =
false;
433 if ($def[
"type"] ==
"blob" || $def[
"type"] ==
"clob") {
434 $def[
"notnull"] =
false;
438 if (($def[
"type"] ==
"timestamp" && $def[
"default"] ==
"0000-00-00 00:00:00")
439 || ($def[
"type"] ==
"date" && $def[
"default"] ==
"0000-00-00")
441 unset($def[
"default"]);
445 foreach ($def as $k => $v) {
448 $def[
"definition"] = $a;
450 $n_fields[$field] = $def;
454 "change" => $n_fields,
458 $r = $this->manager->alterTable($a_table, $changes,
false);
460 $r = $this->manager->createTable(strtolower($a_table) .
"_copy", $n_fields);
472 if (is_array($a_pk[
"fields"])) {
474 foreach ($a_pk[
"fields"] as $f => $pos) {
475 $fields[] = strtolower($f);
489 if (is_array($a_indices)) {
492 foreach ($a_indices as $index) {
493 if (strlen($index[
"name"]) > 3) {
499 foreach ($a_indices as $index) {
500 if (is_array($index[
"fields"])) {
502 $index[
"name"] =
"i" . $cnt;
505 foreach ($index[
"fields"] as $f => $pos) {
506 $fields[] = strtolower($f);
523 if (is_array($a_constraints)) {
526 foreach ($a_constraints as $c) {
527 if (strlen($c[
"name"]) > 3) {
533 foreach ($a_constraints as $c) {
534 if (is_array($c[
"fields"])) {
536 $c[
"name"] =
"c" . $cnt;
539 foreach ($c[
"fields"] as $f => $pos) {
540 $fields[] = strtolower($f);
542 $this->
ilDBInterface->addUniqueConstraint($a_table, $fields, strtolower($c[
"name"]));
559 $indices = $this->analyzer->getIndicesInformation($a_table);
560 foreach ($indices as $index) {
561 if (strlen($index[
"name"]) > 3) {
567 foreach ($indices as $index) {
582 if ($a_auto_inc_field !=
"") {
583 $set = $this->
ilDBInterface->
query(
"SELECT MAX(`" . strtolower($a_auto_inc_field) .
"`) ma FROM `" . $a_table .
"`");
585 $next = $rec[
"ma"] + 1;
600 $fields = $this->analyzer->getFieldInformation($a_table);
601 foreach ($fields as $name => $def) {
602 if ($def[
"type"] ==
"clob" && $def[
"notnull"] ==
true) {
618 $fields = $this->analyzer->getFieldInformation($a_table);
619 foreach ($fields as $name => $def) {
620 if ($def[
"type"] ==
"timestamp"
621 && ($def[
"notnull"] ==
true || $def[
"default"] ==
"0000-00-00 00:00:00")
623 $nd = array(
"type" =>
"timestamp",
"notnull" =>
false );
624 if ($def[
"default"] ==
"0000-00-00 00:00:00") {
625 $nd[
"default"] =
null;
629 if ($def[
"type"] ==
"date"
630 && ($def[
"notnull"] ==
true || $def[
"default"] ==
"0000-00-00")
632 $nd = array(
"type" =>
"date",
"notnull" =>
false );
633 if ($def[
"default"] ==
"0000-00-00") {
634 $nd[
"default"] =
null;
for($col=0; $col< 50; $col++) $d
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.
execute($stmt, $data=array())
fetchAssoc($query_result)
addPrimaryKey($table_name, $primary_keys)
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)