📄 xmldbgenerator.class.php
字号:
* Given one XMLDBTable and one XMLDBField, return the SQL statements needded to modify the enum of the field in the table */ function getModifyEnumSQL($xmldb_table, $xmldb_field) { $results = array(); /// Get the quoted name of the table and field $tablename = $this->getTableName($xmldb_table); $fieldname = $this->getEncQuoted($xmldb_field->getName()); /// Decide if we are going to create or to drop the enum (based exclusively in the values passed!) if (!$xmldb_field->getEnum()) { $results = $this->getDropEnumSQL($xmldb_table, $xmldb_field); //Drop } else { $results = $this->getCreateEnumSQL($xmldb_table, $xmldb_field); //Create/modify } return $results; } /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to modify the default of the field in the table */ function getModifyDefaultSQL($xmldb_table, $xmldb_field) { $results = array(); /// Get the quoted name of the table and field $tablename = $this->getTableName($xmldb_table); $fieldname = $this->getEncQuoted($xmldb_field->getName()); /// Decide if we are going to create/modify or to drop the default if ($xmldb_field->getDefault() === null) { $results = $this->getDropDefaultSQL($xmldb_table, $xmldb_field); //Drop } else { $results = $this->getCreateDefaultSQL($xmldb_table, $xmldb_field); //Create/modify } return $results; } /** * Given one correct XMLDBField and the new name, returns the SQL statements * to rename it (inside one array) */ function getRenameFieldSQL($xmldb_table, $xmldb_field, $newname) { $results = array(); //Array where all the sentences will be stored /// Although this is checked in ddllib - rename_field() - double check /// that we aren't trying to rename one "id" field. Although it could be /// implemented (if adding the necessary code to rename sequences, defaults, /// triggers... and so on under each getRenameFieldExtraSQL() function, it's /// better to forbide it, mainly because this field is the default PK and /// in the future, a lot of FKs can be pointing here. So, this field, more /// or less, must be considered inmutable! if ($xmldb_field->getName() == 'id') { return array(); } $rename = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->rename_column_sql); $rename = str_replace('OLDFIELDNAME', $this->getEncQuoted($xmldb_field->getName()), $rename); $rename = str_replace('NEWFIELDNAME', $this->getEncQuoted($newname), $rename); $results[] = $rename; /// Call to getRenameFieldExtraSQL() if $rename_column_extra_code is enabled (will add some required sentences) if ($this->rename_column_extra_code) { $extra_sentences = $this->getRenameFieldExtraSQL($xmldb_table, $xmldb_field, $newname); $results = array_merge($results, $extra_sentences); } return $results; } /** * Given one XMLDBTable and one XMLDBKey, return the SQL statements needded to add the key to the table * note that undelying indexes will be added as parametrised by $xxxx_keys and $xxxx_index parameters */ function getAddKeySQL($xmldb_table, $xmldb_key) { $results = array(); /// Just use the CreateKeySQL function if ($keyclause = $this->getKeySQL($xmldb_table, $xmldb_key)) { $key = 'ALTER TABLE ' . $this->getTableName($xmldb_table) . ' ADD CONSTRAINT ' . $keyclause; $results[] = $key; } /// If we aren't creating the keys OR if the key is XMLDB_KEY_FOREIGN (not underlying index generated /// automatically by the RDBMS) create the underlying (created by us) index (if doesn't exists) if (!$keyclause || $xmldb_key->getType() == XMLDB_KEY_FOREIGN) { /// Only if they don't exist if ($xmldb_key->getType() == XMLDB_KEY_FOREIGN) { ///Calculate type of index based on type ok key $indextype = XMLDB_INDEX_NOTUNIQUE; } else { $indextype = XMLDB_INDEX_UNIQUE; } $xmldb_index = new XMLDBIndex('anyname'); $xmldb_index->setAttributes($indextype, $xmldb_key->getFields()); if (!index_exists($xmldb_table, $xmldb_index)) { $results = array_merge($results, $this->getAddIndexSQL($xmldb_table, $xmldb_index)); } } /// If the key is XMLDB_KEY_FOREIGN_UNIQUE, create it as UNIQUE too if ($xmldb_key->getType() == XMLDB_KEY_FOREIGN_UNIQUE && $this->unique_keys) { ///Duplicate the key $xmldb_key->setType(XMLDB_KEY_UNIQUE); $results = array_merge($results, $this->getAddKeySQL($xmldb_table, $xmldb_key)); } /// Return results return $results; } /** * Given one XMLDBTable and one XMLDBIndex, return the SQL statements needded to drop the index from the table */ function getDropKeySQL($xmldb_table, $xmldb_key) { $results = array(); /// Get the key name (note that this doesn't introspect DB, so could cause some problems sometimes!) /// TODO: We'll need to overwrite the whole getDropKeySQL() method inside each DB to do the proper queries /// against the dictionary or require ADOdb to support it or change the find_key_name() method to /// perform DB introspection directly. But, for now, as we aren't going to enable referential integrity /// it won't be a problem at all $dbkeyname = find_key_name($xmldb_table, $xmldb_key); /// Only if such type of key generation is enabled $dropkey = false; switch ($xmldb_key->getType()) { case XMLDB_KEY_PRIMARY: if ($this->primary_keys) { $template = $this->drop_primary_key; $dropkey = true; } break; case XMLDB_KEY_UNIQUE: if ($this->unique_keys) { $template = $this->drop_unique_key; $dropkey = true; } break; case XMLDB_KEY_FOREIGN_UNIQUE: case XMLDB_KEY_FOREIGN: if ($this->foreign_keys) { $template = $this->drop_foreign_key; $dropkey = true; } break; } /// If we have decided to drop the key, let's do it if ($dropkey) { /// Replace TABLENAME, CONSTRAINTTYPE and KEYNAME as needed $dropsql = str_replace('TABLENAME', $this->getTableName($xmldb_table), $template); $dropsql = str_replace('KEYNAME', $dbkeyname, $dropsql); $results[] = $dropsql; } /// If we aren't dropping the keys OR if the key is XMLDB_KEY_FOREIGN (not underlying index generated /// automatically by the RDBMS) drop the underlying (created by us) index (if exists) if (!$dropkey || $xmldb_key->getType() == XMLDB_KEY_FOREIGN) { /// Only if they exist $xmldb_index = new XMLDBIndex('anyname'); $xmldb_index->setAttributes(XMLDB_INDEX_UNIQUE, $xmldb_key->getFields()); if (index_exists($xmldb_table, $xmldb_index)) { $results = array_merge($results, $this->getDropIndexSQL($xmldb_table, $xmldb_index)); } } /// If the key is XMLDB_KEY_FOREIGN_UNIQUE, drop the UNIQUE too if ($xmldb_key->getType() == XMLDB_KEY_FOREIGN_UNIQUE && $this->unique_keys) { ///Duplicate the key $xmldb_key->setType(XMLDB_KEY_UNIQUE); $results = array_merge($results, $this->getDropKeySQL($xmldb_table, $xmldb_key)); } /// Return results return $results; } /** * Given one XMLDBTable and one XMLDBKey, return the SQL statements needded to rename the key in the table * Experimental! Shouldn't be used at all! */ function getRenameKeySQL($xmldb_table, $xmldb_key, $newname) { $results = array(); /// Get the real key name $dbkeyname = find_key_name($xmldb_table, $xmldb_key); /// Check we are really generating this type of keys if (($xmldb_key->getType() == XMLDB_KEY_PRIMARY && !$this->primary_keys) || ($xmldb_key->getType() == XMLDB_KEY_UNIQUE && !$this->unique_keys) || ($xmldb_key->getType() == XMLDB_KEY_FOREIGN && !$this->foreign_keys) || ($xmldb_key->getType() == XMLDB_KEY_FOREIGN_UNIQUE && !$this->unique_keys && !$this->foreign_keys)) { /// We aren't generating this type of keys, delegate to child indexes $xmldb_index = new XMLDBIndex($xmldb_key->getName()); $xmldb_index->setFields($xmldb_key->getFields()); return $this->getRenameIndexSQL($xmldb_table, $xmldb_index, $newname); } /// Arrived here so we are working with keys, lets rename them /// Replace TABLENAME and KEYNAME as needed $renamesql = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->rename_key_sql); $renamesql = str_replace('OLDKEYNAME', $dbkeyname, $renamesql); $renamesql = str_replace('NEWKEYNAME', $newname, $renamesql); /// Some DB doesn't support key renaming so this can be empty if ($renamesql) { $results[] = $renamesql; } return $results; } /** * Given one XMLDBTable and one XMLDBIndex, return the SQL statements needded to add the index to the table */ function getAddIndexSQL($xmldb_table, $xmldb_index) { /// Just use the CreateIndexSQL function return $this->getCreateIndexSQL($xmldb_table, $xmldb_index); } /** * Given one XMLDBTable and one XMLDBIndex, return the SQL statements needded to drop the index from the table */ function getDropIndexSQL($xmldb_table, $xmldb_index) { $results = array(); /// Get the real index name $dbindexname = find_index_name($xmldb_table, $xmldb_index); /// Replace TABLENAME and INDEXNAME as needed $dropsql = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->drop_index_sql); $dropsql = str_replace('INDEXNAME', $dbindexname, $dropsql); $results[] = $dropsql; return $results; } /** * Given one XMLDBTable and one XMLDBIndex, return the SQL statements needded to rename the index in the table * Experimental! Shouldn't be used at all! */ function getRenameIndexSQL($xmldb_table, $xmldb_index, $newname) { $results = array(); /// Get the real index name $dbindexname = find_index_name($xmldb_table, $xmldb_index); /// Replace TABLENAME and INDEXNAME as needed $renamesql = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->rename_index_sql); $renamesql = str_replace('OLDINDEXNAME', $dbindexname, $renamesql); $renamesql = str_replace('NEWINDEXNAME', $newname, $renamesql); /// Some DB doesn't support index renaming (MySQL) so this can be empty if ($renamesql) { $results[] = $renamesql; } return $results; } /** * Given three strings (table name, list of fields (comma separated) and suffix), * create the proper object name quoting it if necessary. * * IMPORTANT: This function must be used to CALCULATE NAMES of objects TO BE CREATED, * NEVER TO GUESS NAMES of EXISTING objects!!! */ function getNameForObject($tablename, $fields, $suffix='') { $name = ''; /// Implement one basic cache to avoid object name duplication /// and to speed up repeated queries for the same objects if (!isset($used_names)) { static $used_names = array(); } /// If this exact object has been requested, return it if (array_key_exists($tablename.'-'.$fields.'-'.$suffix, $used_names)) { return $used_names[$tablename.'-'.$fields.'-'.$suffix]; } /// Use standard naming. See http://docs.moodle.org/en/XMLDB_key_and_index_naming $tablearr = explode ('_', $tablename); foreach ($tablearr as $table) { $name .= substr(trim($table),0,4); } $name .= '_'; $fieldsarr = explode (',', $fields); foreach ($fieldsarr as $field) { $name .= substr(trim($field),0,3); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -