/** * Invoke method, every class will have its own * returns true/false on completion, setting both * errormsg and output as necessary */ function invoke() { parent::invoke(); $result = true; // Set own core attributes $this->does_generate = ACTION_NONE; //$this->does_generate = ACTION_GENERATE_HTML; // These are always here global $CFG, $XMLDB; // Do the job, setting result as needed // Get the dir containing the file $dirpath = required_param('dir', PARAM_PATH); $dirpath = $CFG->dirroot . $dirpath; // Get the correct dirs if (!empty($XMLDB->dbdirs)) { $dbdir =& $XMLDB->dbdirs[$dirpath]; } else { return false; } if (!empty($XMLDB->editeddirs)) { $editeddir =& $XMLDB->editeddirs[$dirpath]; $structure =& $editeddir->xml_file->getStructure(); } // If the changeme table exists, just get it and continue $changeme_exists = false; if ($tables =& $structure->getTables()) { if ($table =& $structure->getTable('changeme')) { $changeme_exists = true; } } if (!$changeme_exists) { // Lets create the table $field = new xmldb_field('id'); $field->setType(XMLDB_TYPE_INTEGER); $field->setLength(10); $field->setNotNull(true); $field->setUnsigned(true); $field->setSequence(true); $field->setLoaded(true); $field->setChanged(true); $key = new xmldb_key('primary'); $key->setType(XMLDB_KEY_PRIMARY); $key->setFields(array('id')); $key->setLoaded(true); $key->setChanged(true); $table = new xmldb_table('changeme'); $table->setComment('Default comment for the table, please edit me'); $table->addField($field); $table->addKey($key); // Finally, add the whole retrofitted table to the structure // in the place specified $structure->addTable($table); } // Launch postaction if exists (leave this here!) if ($this->getPostAction() && $result) { return $this->launch($this->getPostAction()); } // Return ok if arrived here return $result; }
/** * Given one correct xmldb_field, returns the complete SQL line to create it. * * @param xmldb_table $xmldb_table The table related to $xmldb_field. * @param xmldb_field $xmldb_field The instance of xmldb_field to create the SQL from. * @param string $skip_type_clause The type clause on alter columns, NULL by default. * @param string $skip_default_clause The default clause on alter columns, NULL by default. * @param string $skip_notnull_clause The null/notnull clause on alter columns, NULL by default. * @param string $specify_nulls_clause To force a specific null clause, NULL by default. * @param bool $specify_field_name Flag to specify fieldname in return. * @return string The field generating SQL statement. * @throws coding_exception Thrown when xmldb_field doesn't validate with the xmldb_table. */ public function getFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL, $specify_nulls_clause = NULL, $specify_field_name = true) { if ($error = $xmldb_field->validateDefinition($xmldb_table)) { throw new coding_exception($error); } $skip_type_clause = is_null($skip_type_clause) ? $this->alter_column_skip_type : $skip_type_clause; $skip_default_clause = is_null($skip_default_clause) ? $this->alter_column_skip_default : $skip_default_clause; $skip_notnull_clause = is_null($skip_notnull_clause) ? $this->alter_column_skip_notnull : $skip_notnull_clause; $specify_nulls_clause = is_null($specify_nulls_clause) ? $this->specify_nulls : $specify_nulls_clause; /// First of all, convert integers to numbers if defined if ($this->integer_to_number) { if ($xmldb_field->getType() == XMLDB_TYPE_INTEGER) { $xmldb_field->setType(XMLDB_TYPE_NUMBER); } } /// Same for floats if ($this->float_to_number) { if ($xmldb_field->getType() == XMLDB_TYPE_FLOAT) { $xmldb_field->setType(XMLDB_TYPE_NUMBER); } } $field = ''; // Let's accumulate the whole expression based on params and settings /// The name if ($specify_field_name) { $field .= $this->getEncQuoted($xmldb_field->getName()); } /// The type and length only if we don't want to skip it if (!$skip_type_clause) { /// The type and length $field .= ' ' . $this->getTypeSQL($xmldb_field->getType(), $xmldb_field->getLength(), $xmldb_field->getDecimals()); } /// The unsigned if supported if ($this->unsigned_allowed && ($xmldb_field->getType() == XMLDB_TYPE_INTEGER || $xmldb_field->getType() == XMLDB_TYPE_NUMBER || $xmldb_field->getType() == XMLDB_TYPE_FLOAT)) { if ($xmldb_field->getUnsigned()) { $field .= ' unsigned'; } } /// Calculate the not null clause $notnull = ''; /// Only if we don't want to skip it if (!$skip_notnull_clause) { if ($xmldb_field->getNotNull()) { $notnull = ' NOT NULL'; } else { if ($specify_nulls_clause) { $notnull = ' NULL'; } } } /// Calculate the default clause $default_clause = ''; if (!$skip_default_clause) { //Only if we don't want to skip it $default_clause = $this->getDefaultClause($xmldb_field); } /// Based on default_after_null, set both clauses properly if ($this->default_after_null) { $field .= $notnull . $default_clause; } else { $field .= $default_clause . $notnull; } /// The sequence if ($xmldb_field->getSequence()) { if ($xmldb_field->getLength() <= 9 && $this->sequence_name_small) { $sequencename = $this->sequence_name_small; } else { $sequencename = $this->sequence_name; } $field .= ' ' . $sequencename; if ($this->sequence_only) { /// We only want the field name and sequence name to be printed /// so, calculate it and return $sql = $this->getEncQuoted($xmldb_field->getName()) . ' ' . $sequencename; return $sql; } } return $field; }
/** * Invoke method, every class will have its own * returns true/false on completion, setting both * errormsg and output as necessary */ function invoke() { parent::invoke(); $result = true; /// Set own core attributes $this->does_generate = ACTION_NONE; //$this->does_generate = ACTION_GENERATE_HTML; /// These are always here global $CFG, $XMLDB; /// Do the job, setting result as needed if (!data_submitted()) { ///Basic prevention print_error('wrongcall', 'error'); } /// Get parameters $dirpath = required_param('dir', PARAM_PATH); $dirpath = $CFG->dirroot . $dirpath; $tableparam = strtolower(required_param('table', PARAM_PATH)); $fieldparam = strtolower(required_param('field', PARAM_PATH)); $name = substr(trim(strtolower(optional_param('name', $fieldparam, PARAM_PATH))), 0, 30); $comment = required_param('comment', PARAM_CLEAN); $comment = trim($comment); $type = required_param('type', PARAM_INT); $length = strtolower(optional_param('length', NULL, PARAM_ALPHANUM)); $decimals = optional_param('decimals', NULL, PARAM_INT); $unsigned = optional_param('unsigned', false, PARAM_BOOL); $notnull = optional_param('notnull', false, PARAM_BOOL); $sequence = optional_param('sequence', false, PARAM_BOOL); $default = optional_param('default', NULL, PARAM_PATH); $default = trim($default); $editeddir =& $XMLDB->editeddirs[$dirpath]; $structure =& $editeddir->xml_file->getStructure(); $table =& $structure->getTable($tableparam); $field =& $table->getField($fieldparam); $oldhash = $field->getHash(); $errors = array(); /// To store all the errors found /// Perform some automatic asumptions if ($sequence) { $unsigned = true; $notnull = true; $default = NULL; } if ($type != XMLDB_TYPE_NUMBER && $type != XMLDB_TYPE_FLOAT) { $decimals = NULL; } if ($type == XMLDB_TYPE_BINARY) { $default = NULL; } if ($default === '') { $default = NULL; } /// Perform some checks /// Check empty name if (empty($name)) { $errors[] = $this->str['fieldnameempty']; } /// Check incorrect name if ($name == 'changeme') { $errors[] = $this->str['incorrectfieldname']; } /// Check duplicate name if ($fieldparam != $name && $table->getField($name)) { $errors[] = $this->str['duplicatefieldname']; } /// Integer checks if ($type == XMLDB_TYPE_INTEGER) { if (!(is_numeric($length) && !empty($length) && intval($length) == floatval($length) && $length > 0 && $length <= 20)) { $errors[] = $this->str['integerincorrectlength']; } if (!(empty($default) || is_numeric($default) && !empty($default) && intval($default) == floatval($default))) { $errors[] = $this->str['defaultincorrect']; } } /// Number checks if ($type == XMLDB_TYPE_NUMBER) { if (!(is_numeric($length) && !empty($length) && intval($length) == floatval($length) && $length > 0 && $length <= 20)) { $errors[] = $this->str['numberincorrectlength']; } if (!(empty($decimals) || is_numeric($decimals) && !empty($decimals) && intval($decimals) == floatval($decimals) && $decimals >= 0 && $decimals < $length)) { $errors[] = $this->str['numberincorrectdecimals']; } if (!(empty($default) || is_numeric($default) && !empty($default))) { $errors[] = $this->str['defaultincorrect']; } } /// Float checks if ($type == XMLDB_TYPE_FLOAT) { if (!(empty($length) || is_numeric($length) && !empty($length) && intval($length) == floatval($length) && $length > 0 && $length <= 20)) { $errors[] = $this->str['floatincorrectlength']; } if (!(empty($decimals) || is_numeric($decimals) && !empty($decimals) && intval($decimals) == floatval($decimals) && $decimals >= 0 && $decimals < $length)) { $errors[] = $this->str['floatincorrectdecimals']; } if (!(empty($default) || is_numeric($default) && !empty($default))) { $errors[] = $this->str['defaultincorrect']; } } /// Char checks if ($type == XMLDB_TYPE_CHAR) { if (!(is_numeric($length) && !empty($length) && intval($length) == floatval($length) && $length > 0 && $length <= 255)) { $errors[] = $this->str['charincorrectlength']; } if ($default !== NULL && $default !== '') { if (substr($default, 0, 1) == "'" || substr($default, -1, 1) == "'") { $errors[] = $this->str['defaultincorrect']; } } } /// Text checks if ($type == XMLDB_TYPE_TEXT) { if ($length != 'small' && $length != 'medium' && $length != 'big') { $errors[] = $this->str['textincorrectlength']; } if ($default !== NULL && $default !== '') { if (substr($default, 0, 1) == "'" || substr($default, -1, 1) == "'") { $errors[] = $this->str['defaultincorrect']; } } } /// Binary checks if ($type == XMLDB_TYPE_BINARY) { if ($length != 'small' && $length != 'medium' && $length != 'big') { $errors[] = $this->str['binaryincorrectlength']; } } if (!empty($errors)) { $tempfield = new xmldb_field($name); $tempfield->setType($type); $tempfield->setLength($length); $tempfield->setDecimals($decimals); $tempfield->setUnsigned($unsigned); $tempfield->setNotNull($notnull); $tempfield->setSequence($sequence); $tempfield->setDefault($default); /// Prepare the output $site = get_site(); $navlinks = array(); $navlinks[] = array('name' => $this->str['administration'], 'link' => '../index.php', 'type' => 'misc'); $navlinks[] = array('name' => 'XMLDB', 'link' => 'index.php', 'type' => 'misc'); $navigation = build_navigation($navlinks); print_header("{$site->shortname}: XMLDB", "{$site->fullname}", $navigation); notice('<p>' . implode(', ', $errors) . '</p> <p>' . $tempfield->readableInfo() . '</p>', 'index.php?action=edit_field&field=' . $field->getName() . '&table=' . $table->getName() . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath))); die; /// re-die :-P } /// Continue if we aren't under errors if (empty($errors)) { /// If there is one name change, do it, changing the prev and next /// atributes of the adjacent fields if ($fieldparam != $name) { $field->setName($name); if ($field->getPrevious()) { $prev =& $table->getField($field->getPrevious()); $prev->setNext($name); $prev->setChanged(true); } if ($field->getNext()) { $next =& $table->getField($field->getNext()); $next->setPrevious($name); $next->setChanged(true); } } /// Set comment $field->setComment($comment); /// Set the rest of fields $field->setType($type); $field->setLength($length); $field->setDecimals($decimals); $field->setUnsigned($unsigned); $field->setNotNull($notnull); $field->setSequence($sequence); $field->setDefault($default); /// If the hash has changed from the old one, change the version /// and mark the structure as changed $field->calculateHash(true); if ($oldhash != $field->getHash()) { $field->setChanged(true); $table->setChanged(true); /// Recalculate the structure hash $structure->calculateHash(true); $structure->setVersion(userdate(time(), '%Y%m%d', 99, false)); /// Mark as changed $structure->setChanged(true); } /// Launch postaction if exists (leave this here!) if ($this->getPostAction() && $result) { return $this->launch($this->getPostAction()); } } /// Return ok if arrived here return $result; }
<pre> <?php require_once '../configpath.php'; require_once $CFG->dirroot . '/lib/xmldb/xmldb_object.php'; require_once $CFG->dirroot . '/lib/xmldb/xmldb_table.php'; $dbman = $DB->get_manager(); $problematic_table_list = array("block_ilp_plu_hte_ent" => 'value', "block_ilp_plu_are_ent" => 'value'); foreach ($problematic_table_list as $table => $field) { $table = new xmldb_table($table); $field = new xmldb_field($field); $field->setType(XMLDB_TYPE_TEXT); $dbman->change_field_type($table, $field); echo "{$table}.{$field} type is now 'text'\n"; } /* $table = new xmldb_table( 'block_ilp_plu_gradebooktracker_ent' ); $dropfield = new xmldb_field( 'review' ); if($dbman->field_exists( $table, $dropfield ) ){ echo "Found 'review': dropping 'review' from block_ilp_plu_gradebooktracker_ent\n"; $dbman->drop_field( $table, $dropfield ); } else{ echo "Didn't find 'review' in block_ilp_plu_gradebooktracker_ent\n"; } $addfield = new xmldb_field( 'entry_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED ) ; if( !$dbman->field_exists( $table, $addfield ) ){ echo "adding 'entry_id' to block_ilp_plu_gradebooktracker_ent\n"; $dbman->add_field( $table, $addfield );