From 49173d25a76b5561c3f90e880b43d7dcecfd7da2 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Thu, 2 Nov 2006 23:09:39 +0000 Subject: [PATCH] Ending fied/key/index PHP generators Merged from MOODLE_17_STABLE --- .../view_table_php/view_table_php.class.php | 65 ++++++++++++++++++- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/admin/xmldb/actions/view_table_php/view_table_php.class.php b/admin/xmldb/actions/view_table_php/view_table_php.class.php index 3312264036..a3b7ca4584 100644 --- a/admin/xmldb/actions/view_table_php/view_table_php.class.php +++ b/admin/xmldb/actions/view_table_php/view_table_php.class.php @@ -119,13 +119,12 @@ class view_table_php extends XMLDBAction { $commands = array('Fields', $optionspacer . 'add_field', $optionspacer . 'drop_field', - $optionspacer . 'rename_field (not imp!)', + $optionspacer . 'rename_field', $optionspacer . 'change_field_type', $optionspacer . 'change_field_precision', $optionspacer . 'change_field_unsigned', $optionspacer . 'change_field_notnull', - $optionspacer . 'change_field_sequence (not imp!)', - $optionspacer . 'change_field_enum (not imp!)', + $optionspacer . 'change_field_enum', $optionspacer . 'change_field_default', 'Keys', $optionspacer . 'add_key', @@ -229,6 +228,13 @@ class view_table_php extends XMLDBAction { $o.= $this->str['mustselectonefield']; } break; + case 'change_field_enum': + if ($fieldkeyindexinitial == 'f') { //Only if we have got one field + $o.= s($this->change_field_enum_php($structure, $tableparam, $fieldkeyindexparam)); + } else { + $o.= $this->str['mustselectonefield']; + } + break; case 'change_field_default': if ($fieldkeyindexinitial == 'f') { //Only if we have got one field $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam)); @@ -619,6 +625,53 @@ class view_table_php extends XMLDBAction { return $result; } + /** + * This function will generate all the PHP code needed to + * change the enum values (check constraint) of one field + * using XMLDB objects and functions + * + * @param XMLDBStructure structure object containing all the info + * @param string table table name + * @param string field field name to change its enum + */ + function change_field_enum_php($structure, $table, $field) { + + $result = ''; + /// Validate if we can do it + if (!$table = $structure->getTable($table)) { + return false; + } + if (!$field = $table->getField($field)) { + return false; + } + if ($table->getAllErrors()) { + return false; + } + + /// Calculate the enum tip text + $enum = $field->getEnum() ? implode(', ', $field->getEnumValues()) : 'none'; + + /// Add the standard PHP header + $result .= XMLDB_PHP_HEADER; + + /// Add contents + $result .= XMLDB_LINEFEED; + $result .= ' /// Changing list of values (enum) of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $enum . XMLDB_LINEFEED; + $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED; + $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED; + $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED; + + /// Launch the proper DDL + $result .= XMLDB_LINEFEED; + $result .= ' /// Launch change of list of values for field ' . $field->getName() . XMLDB_LINEFEED; + $result .= ' $status = $status && change_field_enum($table, $field);' . XMLDB_LINEFEED; + + /// Add standard PHP footer + $result .= XMLDB_PHP_FOOTER; + + return $result; + } + /** * This function will generate all the PHP code needed to * change the default of one field using XMLDB objects and functions @@ -776,6 +829,9 @@ class view_table_php extends XMLDBAction { return false; } + /// Prepend warning. This function isn't usable! + $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED; + /// Add the standard PHP header $result .= XMLDB_PHP_HEADER; @@ -908,6 +964,9 @@ class view_table_php extends XMLDBAction { return false; } + /// Prepend warning. This function isn't usable! + $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED; + /// Add the standard PHP header $result .= XMLDB_PHP_HEADER; -- 2.39.5