📄 xmldbgenerator.class.php
字号:
<?php // $Id: XMLDBGenerator.class.php,v 1.66 2007/10/10 05:25:25 nicolasconnault Exp $///////////////////////////////////////////////////////////////////////////// //// NOTICE OF COPYRIGHT //// //// Moodle - Modular Object-Oriented Dynamic Learning Environment //// http://moodle.com //// //// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //// //// This program is free software; you can redistribute it and/or modify //// it under the terms of the GNU General Public License as published by //// the Free Software Foundation; either version 2 of the License, or //// (at your option) any later version. //// //// This program is distributed in the hope that it will be useful, //// but WITHOUT ANY WARRANTY; without even the implied warranty of //// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //// GNU General Public License for more details: //// //// http://www.gnu.org/copyleft/gpl.html //// //////////////////////////////////////////////////////////////////////////////// This class represent the base generator class where all the/// needed functions to generate proper SQL are defined./// The rest of classes will inherit, by default, the same logic./// Functions will be overriden as needed to generate correct SQL.class XMLDBgenerator {/// Please, avoid editing this defaults in this base class!/// It could change the behaviour of the rest of generators/// that, by default, inherit this configuration./// To change any of them, do it in extended classes instead. var $quote_string = '"'; // String used to quote names var $quote_all = false; // To decide if we want to quote all the names or only the reserved ones var $statement_end = ';'; // String to be automatically added at the end of each statement var $integer_to_number = false; // To create all the integers as NUMBER(x) (also called DECIMAL, NUMERIC...) var $float_to_number = false; // To create all the floats as NUMBER(x) (also called DECIMAL, NUMERIC...) var $number_type = 'NUMERIC'; // Proper type for NUMBER(x) in this DB var $unsigned_allowed = true; // To define in the generator must handle unsigned information var $default_for_char = null; // To define the default to set for NOT NULLs CHARs without default (null=do nothing) var $drop_default_clause_required = false; //To specify if the generator must use some DEFAULT clause to drop defaults var $drop_default_clause = ''; //The DEFAULT clause required to drop defaults var $default_after_null = true; //To decide if the default clause of each field must go after the null clause var $specify_nulls = false; //To force the generator if NULL clauses must be specified. It shouldn't be necessary //but some mssql drivers require them or everything is created as NOT NULL :-( var $primary_key_name = null; //To force primary key names to one string (null=no force) var $primary_keys = true; // Does the generator build primary keys var $unique_keys = false; // Does the generator build unique keys var $foreign_keys = false; // Does the generator build foreign keys var $drop_primary_key = 'ALTER TABLE TABLENAME DROP CONSTRAINT KEYNAME'; // Template to drop PKs // with automatic replace for TABLENAME and KEYNAME var $drop_unique_key = 'ALTER TABLE TABLENAME DROP CONSTRAINT KEYNAME'; // Template to drop UKs // with automatic replace for TABLENAME and KEYNAME var $drop_foreign_key = 'ALTER TABLE TABLENAME DROP CONSTRAINT KEYNAME'; // Template to drop FKs // with automatic replace for TABLENAME and KEYNAME var $sequence_extra_code = true; //Does the generator need to add extra code to generate the sequence fields var $sequence_name = 'auto_increment'; //Particular name for inline sequences in this generator var $sequence_name_small = false; //Different name for small (4byte) sequences or false if same var $sequence_only = false; //To avoid to output the rest of the field specs, leaving only the name and the sequence_name variable var $enum_inline_code = true; //Does the generator need to add inline code in the column definition var $enum_extra_code = true; //Does the generator need to add extra code to generate code for the enums in the table var $add_table_comments = true; // Does the generator need to add code for table comments var $add_after_clause = false; // Does the generator need to add the after clause for fields var $prefix_on_names = true; //Does the generator need to prepend the prefix to all the key/index/sequence/trigger/check names var $names_max_length = 30; //Max length for key/index/sequence/trigger/check names (keep 30 for all!) var $concat_character = '||'; //Characters to be used as concatenation operator. If not defined //MySQL CONCAT function will be used var $rename_table_sql = 'ALTER TABLE OLDNAME RENAME TO NEWNAME'; //SQL sentence to rename one table, both //OLDNAME and NEWNAME are dinamically replaced var $rename_table_extra_code = false; //Does the generator need to add code after table rename var $drop_table_sql = 'DROP TABLE TABLENAME'; //SQL sentence to drop one table //TABLENAME is dinamically replaced var $drop_table_extra_code = false; //Does the generator need to add code after table drop var $alter_column_sql = 'ALTER TABLE TABLENAME ALTER COLUMN COLUMNSPECS'; //The SQL template to alter columns var $alter_column_skip_default = false; //The generator will skip the default clause on alter columns var $alter_column_skip_type = false; //The generator will skip the type clause on alter columns var $alter_column_skip_notnull = false; //The generator will skip the null/notnull clause on alter columns var $rename_column_sql = 'ALTER TABLE TABLENAME RENAME COLUMN OLDFIELDNAME TO NEWFIELDNAME'; ///TABLENAME, OLDFIELDNAME and NEWFIELDNAME are dianmically replaced var $rename_column_extra_code = false; //Does the generator need to add code after column rename var $drop_index_sql = 'DROP INDEX INDEXNAME'; //SQL sentence to drop one index //TABLENAME, INDEXNAME are dinamically replaced var $rename_index_sql = 'ALTER INDEX OLDINDEXNAME RENAME TO NEWINDEXNAME'; //SQL sentence to rename one index //TABLENAME, OLDINDEXNAME, NEWINDEXNAME are dinamically replaced var $rename_key_sql = 'ALTER TABLE TABLENAME CONSTRAINT OLDKEYNAME RENAME TO NEWKEYNAME'; //SQL sentence to rename one key //TABLENAME, OLDKEYNAME, NEWKEYNAME are dinamically replaced var $prefix; // Prefix to be used for all the DB objects var $reserved_words; // List of reserved words (in order to quote them properly) /** * Creates one new XMLDBGenerator */ function XMLDBgenerator() { global $CFG; $this->prefix = ''; $this->reserved_words = $this->getReservedWords(); }/// ALL THESE FUNCTION ARE SHARED BY ALL THE XMLDGenerator classes /** * Set the prefix */ function setPrefix($prefix) { if ($this->prefix_on_names) { // Only if we want prefix on names $this->prefix = $prefix; } } /** * Given one XMLDBTable, returns it's correct name, depending of all the parametrization * * @param XMLDBTable table whose name we want * @param boolean to specify if the name must be quoted (if reserved word, only!) * @return string the correct name of the table */ function getTableName($xmldb_table, $quoted = true) { $prefixtouse = $this->prefix; /// Determinate if this table must have prefix or no if (in_array($xmldb_table->getName(), $this->getTablesWithoutPrefix())) { $prefixtouse = ''; } /// Get the name $tablename = $prefixtouse . $xmldb_table->getName(); /// Apply quotes conditionally if ($quoted) { $tablename = $this->getEncQuoted($tablename); } return $tablename; } /** * Given one correct XMLDBTable, returns the SQL statements * to create it (inside one array) */ function getCreateTableSQL($xmldb_table) { $results = array(); //Array where all the sentences will be stored /// Table header $table = 'CREATE TABLE ' . $this->getTableName($xmldb_table) . ' ('; if (!$xmldb_fields = $xmldb_table->getFields()) { return $results; } /// Prevent tables without prefix to be duplicated (part of MDL-6614) if (in_array($xmldb_table->getName(), $this->getTablesWithoutPrefix()) && table_exists($xmldb_table)) { return $results; // false here would break the install, empty array is better ;-) } /// Add the fields, separated by commas foreach ($xmldb_fields as $xmldb_field) { $table .= "\n " . $this->getFieldSQL($xmldb_field); $table .= ','; } /// Add the keys, separated by commas if ($xmldb_keys = $xmldb_table->getKeys()) { foreach ($xmldb_keys as $xmldb_key) { if ($keytext = $this->getKeySQL($xmldb_table, $xmldb_key)) { $table .= "\nCONSTRAINT " . $keytext . ','; } /// If the key is XMLDB_KEY_FOREIGN_UNIQUE, create it as UNIQUE too if ($xmldb_key->getType() == XMLDB_KEY_FOREIGN_UNIQUE) { ///Duplicate the key $xmldb_key->setType(XMLDB_KEY_UNIQUE); if ($keytext = $this->getKeySQL($xmldb_table, $xmldb_key)) { $table .= "\nCONSTRAINT " . $keytext . ','; } } } } /// Add enum extra code if needed if ($this->enum_extra_code) { /// Iterate over fields looking for enums foreach ($xmldb_fields as $xmldb_field) { if ($xmldb_field->getEnum()) { $table .= "\n" . $this->getEnumExtraSQL($xmldb_table, $xmldb_field) . ','; } } } /// Table footer, trim the latest comma $table = trim($table,','); $table .= "\n)"; /// Add the CREATE TABLE to results $results[] = $table; /// Add comments if specified and it exists if ($this->add_table_comments && $xmldb_table->getComment()) { $comment = $this->getCommentSQL ($xmldb_table); /// Add the COMMENT to results $results = array_merge($results, $comment); } /// Add the indexes (each one, one statement) if ($xmldb_indexes = $xmldb_table->getIndexes()) { foreach ($xmldb_indexes as $xmldb_index) { ///Only process all this if the index doesn't exist in DB if (!index_exists($xmldb_table, $xmldb_index)) { if ($indextext = $this->getCreateIndexSQL($xmldb_table, $xmldb_index)) { $results = array_merge($results, $indextext); } } } } /// Also, add the indexes needed from keys, based on configuration (each one, one statement) if ($xmldb_keys = $xmldb_table->getKeys()) { foreach ($xmldb_keys as $xmldb_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 (!$this->getKeySQL($xmldb_table, $xmldb_key) || $xmldb_key->getType() == XMLDB_KEY_FOREIGN) { /// Create the interim index $index = new XMLDBIndex('anyname'); $index->setFields($xmldb_key->getFields()); ///Only process all this if the index doesn't exist in DB if (!index_exists($xmldb_table, $index)) { $createindex = false; //By default switch ($xmldb_key->getType()) { case XMLDB_KEY_UNIQUE: case XMLDB_KEY_FOREIGN_UNIQUE: $index->setUnique(true); $createindex = true; break; case XMLDB_KEY_FOREIGN: $index->setUnique(false); $createindex = true; break; } if ($createindex) { if ($indextext = $this->getCreateIndexSQL($xmldb_table, $index)) { /// Add the INDEX to the array $results = array_merge($results, $indextext); } } } } } } /// Add sequence extra code if needed if ($this->sequence_extra_code) { /// Iterate over fields looking for sequences foreach ($xmldb_fields as $xmldb_field) { if ($xmldb_field->getSequence()) { /// returns an array of statements needed to create one sequence $sequence_sentences = $this->getCreateSequenceSQL($xmldb_table, $xmldb_field); /// Add the SEQUENCE to the array $results = array_merge($results, $sequence_sentences); } } } return $results; } /** * Given one correct XMLDBIndex, returns the SQL statements * needed to create it (in array) */ function getCreateIndexSQL ($xmldb_table, $xmldb_index) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -