/** * Add one key to the table, allowing to specify the desired order * If it's not specified, then the key is added at the end * @param xmldb_key $key * @param xmldb_object $after */ public function addKey($key, $after = null) { // Detect duplicates first if ($this->getKey($key->getName())) { throw new coding_exception('Duplicate key ' . $key->getName() . ' specified in table ' . $this->getName()); } // Make sure there are no indexes with the key column specs because they would collide. $newfields = $key->getFields(); $allindexes = $this->getIndexes(); foreach ($allindexes as $index) { $fields = $index->getFields(); if ($fields === $newfields) { throw new coding_exception('Index ' . $index->getName() . ' collides with key' . $key->getName() . ' specified in table ' . $this->getName()); } } // Calculate the previous and next keys $prevkey = null; $nextkey = null; if (!$after) { $allkeys = $this->getKeys(); if (!empty($allkeys)) { end($allkeys); $prevkey = $allkeys[key($allkeys)]; } } else { $prevkey = $this->getKey($after); } if ($prevkey && $prevkey->getNext()) { $nextkey = $this->getKey($prevkey->getNext()); } // Set current key previous and next attributes if ($prevkey) { $key->setPrevious($prevkey->getName()); $prevkey->setNext($key->getName()); } if ($nextkey) { $key->setNext($nextkey->getName()); $nextkey->setPrevious($key->getName()); } // Some more attributes $key->setLoaded(true); $key->setChanged(true); // Add the new key $this->keys[] = $key; // Reorder the keys $this->orderKeys($this->keys); // Recalculate the hash $this->calculateHash(true); // We have one new field, so the table has changed $this->setChanged(true); }
/** * 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->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; }
/** * Add one key to the table, allowing to specify the desired order * If it's not specified, then the key is added at the end * @param xmldb_key $key * @param xmldb_object $after */ public function addKey($key, $after=null) { // Detect duplicates first if ($this->getKey($key->getName())) { throw new coding_exception('Duplicate key '.$key->getName().' specified in table '.$this->getName()); } // Calculate the previous and next keys $prevkey = null; $nextkey = null; if (!$after) { $allkeys = $this->getKeys(); if (!empty($allkeys)) { end($allkeys); $prevkey = $allkeys[key($allkeys)]; } } else { $prevkey = $this->getKey($after); } if ($prevkey && $prevkey->getNext()) { $nextkey = $this->getKey($prevkey->getNext()); } // Set current key previous and next attributes if ($prevkey) { $key->setPrevious($prevkey->getName()); $prevkey->setNext($key->getName()); } if ($nextkey) { $key->setNext($nextkey->getName()); $nextkey->setPrevious($key->getName()); } // Some more attributes $key->setLoaded(true); $key->setChanged(true); // Add the new key $this->keys[] = $key; // Reorder the keys $this->orderKeys($this->keys); // Recalculate the hash $this->calculateHash(true); // We have one new field, so the table has changed $this->setChanged(true); }