From: stronk7 Date: Fri, 29 Sep 2006 23:12:40 +0000 (+0000) Subject: Enablig PHP generation of code for change_field_type() X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=4fae686bc245d68d4e8ec26002e370fb85d52670;p=moodle.git Enablig PHP generation of code for change_field_type() --- 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 88c0abba34..3312264036 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 @@ -120,7 +120,7 @@ class view_table_php extends XMLDBAction { $optionspacer . 'add_field', $optionspacer . 'drop_field', $optionspacer . 'rename_field (not imp!)', - $optionspacer . 'change_field_type (not imp!)', + $optionspacer . 'change_field_type', $optionspacer . 'change_field_precision', $optionspacer . 'change_field_unsigned', $optionspacer . 'change_field_notnull', @@ -201,6 +201,13 @@ class view_table_php extends XMLDBAction { $o.= $this->str['mustselectonefield']; } break; + case 'change_field_type': + if ($fieldkeyindexinitial == 'f') { //Only if we have got one field + $o.= s($this->change_field_type_php($structure, $tableparam, $fieldkeyindexparam)); + } else { + $o.= $this->str['mustselectonefield']; + } + break; case 'change_field_precision': if ($fieldkeyindexinitial == 'f') { //Only if we have got one field $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam)); @@ -417,6 +424,59 @@ class view_table_php extends XMLDBAction { return $result; } + /** + * This function will generate all the PHP code needed to + * change the type of one field using XMLDB objects and functions. + * Currently these conversions are supported: + * integer to char + * char to integer + * number to char + * char to number + * float to char + * char to float + * + * @param XMLDBStructure structure object containing all the info + * @param string table table name + * @param string field field name to change precision + */ + function change_field_type_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 type tip text + $type = $field->getXMLDBTypeName($field->getType()); + + /// Add the standard PHP header + $result .= XMLDB_PHP_HEADER; + + /// Add contents + $result .= XMLDB_LINEFEED; + $result .= ' /// Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . 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 type for field ' . $field->getName() . XMLDB_LINEFEED; + $result .= ' $status = $status && change_field_type($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 precision of one field using XMLDB objects and functions