📄 xmldbgenerator.class.php
字号:
$unique = ''; $suffix = 'ix'; if ($xmldb_index->getUnique()) { $unique = ' UNIQUE'; $suffix = 'uix'; } $index = 'CREATE' . $unique . ' INDEX '; $index .= $this->getNameForObject($xmldb_table->getName(), implode(', ', $xmldb_index->getFields()), $suffix); $index .= ' ON ' . $this->getTableName($xmldb_table); $index .= ' (' . implode(', ', $this->getEncQuoted($xmldb_index->getFields())) . ')'; return array($index); } /** * Given one correct XMLDBField, returns the complete SQL line to create it */ function getFieldSQL($xmldb_field, $skip_type_clause = false, $skip_default_clause = false, $skip_notnull_clause = false) { /// 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); } } /// The 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 (if the field isn't enum) if (!$xmldb_field->getEnum() || $this->enum_inline_code == false) { $field .= ' ' . $this->getTypeSQL($xmldb_field->getType(), $xmldb_field->getLength(), $xmldb_field->getDecimals()); } else { /// call to custom function $field .= ' ' . $this->getEnumSQL($xmldb_field); } } /// 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 ($this->specify_nulls) { $notnull = ' NULL'; } } } /// Calculate the default clause if (!$skip_default_clause) { //Only if we don't want to skip it $default = $this->getDefaultClause($xmldb_field); } else { $default = ''; } /// Based on default_after_null, set both clauses properly if ($this->default_after_null) { $field .= $notnull . $default; } else { $field .= $default . $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 return $this->getEncQuoted($xmldb_field->getName()) . ' ' . $sequencename; } } return $field; } /** * Given one correct XMLDBKey, returns its specs */ function getKeySQL ($xmldb_table, $xmldb_key) { $key = ''; switch ($xmldb_key->getType()) { case XMLDB_KEY_PRIMARY: if ($this->primary_keys) { if ($this->primary_key_name !== null) { $key = $this->getEncQuoted($this->primary_key_name); } else { $key = $this->getNameForObject($xmldb_table->getName(), implode(', ', $xmldb_key->getFields()), 'pk'); } $key .= ' PRIMARY KEY (' . implode(', ', $this->getEncQuoted($xmldb_key->getFields())) . ')'; } break; case XMLDB_KEY_UNIQUE: if ($this->unique_keys) { $key = $this->getNameForObject($xmldb_table->getName(), implode(', ', $xmldb_key->getFields()), 'uk'); $key .= ' UNIQUE (' . implode(', ', $this->getEncQuoted($xmldb_key->getFields())) . ')'; } break; case XMLDB_KEY_FOREIGN: case XMLDB_KEY_FOREIGN_UNIQUE: if ($this->foreign_keys) { $key = $this->getNameForObject($xmldb_table->getName(), implode(', ', $xmldb_key->getFields()), 'fk'); $key .= ' FOREIGN KEY (' . implode(', ', $this->getEncQuoted($xmldb_key->getFields())) . ')'; $key .= ' REFERENCES ' . $this->getEncQuoted($this->prefix . $xmldb_key->getRefTable()); $key .= ' (' . implode(', ', $this->getEncQuoted($xmldb_key->getRefFields())) . ')'; } break; } return $key; } /** * Give one XMLDBField, returns the correct "default value" for the current configuration */ function getDefaultValue ($xmldb_field) { $default = null; if ($xmldb_field->getDefault() !== NULL) { if ($xmldb_field->getType() == XMLDB_TYPE_CHAR || $xmldb_field->getType() == XMLDB_TYPE_TEXT) { $default = "'" . addslashes($xmldb_field->getDefault()) . "'"; } else { $default = $xmldb_field->getDefault(); } } else { /// We force default '' for not null char columns without proper default /// some day this should be out! if ($this->default_for_char !== NULL && $xmldb_field->getType() == XMLDB_TYPE_CHAR && $xmldb_field->getNotNull()) { $default = "'" . $this->default_for_char . "'"; } else { /// If the DB requires to explicity define some clause to drop one default, do it here /// never applying defaults to TEXT and BINARY fields if ($this->drop_default_clause_required && $xmldb_field->getType() != XMLDB_TYPE_TEXT && $xmldb_field->getType() != XMLDB_TYPE_BINARY && !$xmldb_field->getNotNull()) { $default = $this->drop_default_clause; } } } return $default; } /** * Given one XMLDBField, returns the correct "default clause" for the current configuration */ function getDefaultClause ($xmldb_field) { $defaultvalue = $this->getDefaultValue ($xmldb_field); if ($defaultvalue !== null) { return ' DEFAULT ' . $defaultvalue; } else { return null; } } /** * Given one correct XMLDBTable and the new name, returns the SQL statements * to rename it (inside one array) */ function getRenameTableSQL($xmldb_table, $newname) { $results = array(); //Array where all the sentences will be stored $newt = new XMLDBTable($newname); //Temporal table for name calculations $rename = str_replace('OLDNAME', $this->getTableName($xmldb_table), $this->rename_table_sql); $rename = str_replace('NEWNAME', $this->getTableName($newt), $rename); $results[] = $rename; /// Call to getRenameTableExtraSQL() if $rename_table_extra_code is enabled. It will add sequence regeneration code. if ($this->rename_table_extra_code) { $extra_sentences = $this->getRenameTableExtraSQL($xmldb_table, $newname); $results = array_merge($results, $extra_sentences); } return $results; } /** * Given one correct XMLDBTable and the new name, returns the SQL statements * to drop it (inside one array) */ function getDropTableSQL($xmldb_table) { $results = array(); //Array where all the sentences will be stored $drop = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->drop_table_sql); $results[] = $drop; /// call to getDropTableExtraSQL() if $drop_table_extra_code is enabled. It will add sequence/trigger drop code. if ($this->drop_table_extra_code) { $extra_sentences = $this->getDropTableExtraSQL($xmldb_table); $results = array_merge($results, $extra_sentences); } return $results; } /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to add the field to the table */ function getAddFieldSQL($xmldb_table, $xmldb_field) { $results = array(); /// Get the quoted name of the table and field $tablename = $this->getTableName($xmldb_table); /// Build the standard alter table add $altertable = 'ALTER TABLE ' . $tablename . ' ADD ' . $this->getFieldSQL($xmldb_field, $this->alter_column_skip_type, $this->alter_column_skip_default, $this->alter_column_skip_notnull); /// Add the after clause if necesary if ($this->add_after_clause && $xmldb_field->getPrevious()) { $altertable .= ' after ' . $this->getEncQuoted($xmldb_field->getPrevious()); } $results[] = $altertable; /// If the DB has extra enum code if ($this->enum_extra_code) { /// If it's enum add the extra code if ($xmldb_field->getEnum()) { $results[] = 'ALTER TABLE ' . $tablename . ' ADD ' . $this->getEnumExtraSQL($xmldb_table, $xmldb_field); } } return $results; } /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop the field from the table */ function getDropFieldSQL($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()); /// Build the standard alter table drop $results[] = 'ALTER TABLE ' . $tablename . ' DROP COLUMN ' . $fieldname; return $results; } /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to alter the field in the table */ function getAlterFieldSQL($xmldb_table, $xmldb_field) { $results = array(); /// Always specify NULLs in alter fields because we can change not nulls to nulls $this->specify_nulls = true; /// Get the quoted name of the table and field $tablename = $this->getTableName($xmldb_table); $fieldname = $this->getEncQuoted($xmldb_field->getName()); /// Build de alter sentence using the alter_column_sql template $alter = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->alter_column_sql); $alter = str_replace('COLUMNSPECS', $this->getFieldSQL($xmldb_field, $this->alter_column_skip_type, $this->alter_column_skip_default, $this->alter_column_skip_notnull), $alter); /// Add the after clause if necesary if ($this->add_after_clause && $xmldb_field->getPrevious()) { $alter .= ' after ' . $this->getEncQuoted($xmldb_field->getPrevious()); } /// Build the standard alter table modify $results[] = $alter; return $results; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -