本文整理汇总了PHP中xmldb_table::getName方法的典型用法代码示例。如果您正苦于以下问题:PHP xmldb_table::getName方法的具体用法?PHP xmldb_table::getName怎么用?PHP xmldb_table::getName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xmldb_table
的用法示例。
在下文中一共展示了xmldb_table::getName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_reorder_rows
public function test_reorder_rows()
{
global $DB;
$dbman = $DB->get_manager();
$this->resetAfterTest();
$table = new xmldb_table('test_table');
$table->setComment("This is a test'n drop table. You can drop it safely");
$tablename = $table->getName();
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('otherid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('otherdata', XMLDB_TYPE_TEXT, 'big', null, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('unique', XMLDB_KEY_UNIQUE, array('otherid', 'sortorder'));
$dbman->create_table($table);
// Rows intentionally added in a slightly 'random' order.
// Note we are testing hat the otherid = 1 rows don't get messed up,
// as well as testing that the otherid = 2 rows are updated correctly.
$DB->insert_record($tablename, array('otherid' => 2, 'sortorder' => 1, 'otherdata' => 'To become 4'));
$DB->insert_record($tablename, array('otherid' => 2, 'sortorder' => 2, 'otherdata' => 'To become 1'));
$DB->insert_record($tablename, array('otherid' => 1, 'sortorder' => 1, 'otherdata' => 'Other 1'));
$DB->insert_record($tablename, array('otherid' => 1, 'sortorder' => 2, 'otherdata' => 'Other 2'));
$DB->insert_record($tablename, array('otherid' => 2, 'sortorder' => 3, 'otherdata' => 'To stay at 3'));
$DB->insert_record($tablename, array('otherid' => 2, 'sortorder' => 4, 'otherdata' => 'To become 2'));
update_field_with_unique_index($tablename, 'sortorder', array(1 => 4, 2 => 1, 3 => 3, 4 => 2), array('otherid' => 2));
$this->assertEquals(array(3 => (object) array('id' => 3, 'otherid' => 1, 'sortorder' => 1, 'otherdata' => 'Other 1'), 4 => (object) array('id' => 4, 'otherid' => 1, 'sortorder' => 2, 'otherdata' => 'Other 2')), $DB->get_records($tablename, array('otherid' => 1), 'sortorder'));
$this->assertEquals(array(2 => (object) array('id' => 2, 'otherid' => 2, 'sortorder' => 1, 'otherdata' => 'To become 1'), 6 => (object) array('id' => 6, 'otherid' => 2, 'sortorder' => 2, 'otherdata' => 'To become 2'), 5 => (object) array('id' => 5, 'otherid' => 2, 'sortorder' => 3, 'otherdata' => 'To stay at 3'), 1 => (object) array('id' => 1, 'otherid' => 2, 'sortorder' => 4, 'otherdata' => 'To become 4')), $DB->get_records($tablename, array('otherid' => 2), 'sortorder'));
}
示例2: setUp
public function setUp()
{
global $CFG, $DB, $UNITTEST;
if (isset($UNITTEST->func_test_db)) {
$this->tdb = $UNITTEST->func_test_db;
} else {
$this->tdb = $DB;
}
unset($CFG->xmldbreconstructprevnext);
// remove this unhack ;-)
$dbman = $this->tdb->get_manager();
$table = new xmldb_table('test_table0');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null);
$table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
$table->add_field('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null);
$table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
$table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null);
$table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null);
$table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('type-name', XMLDB_KEY_UNIQUE, array('type', 'name'));
$table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->add_index('rsstype', XMLDB_INDEX_UNIQUE, array('rsstype'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
// Second, smaller table
$table = new xmldb_table('test_table1');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '30', null, null, null, 'Moodle');
$table->add_field('secondname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
$table->add_field('intro', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
$table->add_field('avatar', XMLDB_TYPE_BINARY, 'medium', null, null, null, null);
$table->add_field('grade', XMLDB_TYPE_NUMBER, '20,10', null, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
// make sure no tables are present!
$this->tearDown();
}
示例3: check_table
protected function check_table(xmldb_table $xmldb_table, array $metacolumns)
{
global $DB;
$o = '';
$wrong_fields = array();
// Get and process XMLDB fields
if ($xmldb_fields = $xmldb_table->getFields()) {
$o .= '<ul>';
foreach ($xmldb_fields as $xmldb_field) {
// Get the type of the column, we only will process CHAR (VARCHAR2) ones
if ($xmldb_field->getType() != XMLDB_TYPE_CHAR) {
continue;
}
$o .= '<li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
// Get current semantic from dictionary, we only will process B (BYTE) ones
// suplying the SQL code to change them to C (CHAR) semantic
$params = array('table_name' => core_text::strtoupper($DB->get_prefix() . $xmldb_table->getName()), 'column_name' => core_text::strtoupper($xmldb_field->getName()), 'data_type' => 'VARCHAR2');
$currentsemantic = $DB->get_field_sql('
SELECT char_used
FROM user_tab_columns
WHERE table_name = :table_name
AND column_name = :column_name
AND data_type = :data_type', $params);
// If using byte semantics, we'll need to change them to char semantics
if ($currentsemantic == 'B') {
$info = '(' . $this->str['expected'] . " 'CHAR', " . $this->str['actual'] . " 'BYTE')";
$o .= '<font color="red">' . $this->str['wrong'] . " {$info}</font>";
// Add the wrong field to the list
$obj = new stdClass();
$obj->table = $xmldb_table;
$obj->field = $xmldb_field;
$wrong_fields[] = $obj;
} else {
$o .= '<font color="green">' . $this->str['ok'] . '</font>';
}
$o .= '</li>';
}
$o .= '</ul>';
}
return array($o, $wrong_fields);
}
示例4: getCreateTempTableSQL
/**
* Given one correct xmldb_table, returns the SQL statements
* to create temporary table (inside one array).
*
* @param xmldb_table $xmldb_table The xmldb_table object instance.
* @return array of sql statements
*/
public function getCreateTempTableSQL($xmldb_table)
{
// Do we know collation?
$collation = $this->mdb->get_dbcollation();
$this->temptables->add_temptable($xmldb_table->getName());
$sqlarr = parent::getCreateTableSQL($xmldb_table);
// Let's inject the extra MySQL tweaks.
foreach ($sqlarr as $i => $sql) {
if (strpos($sql, 'CREATE TABLE ') === 0) {
// We do not want the engine hack included in create table SQL.
$sqlarr[$i] = preg_replace('/^CREATE TABLE (.*)/s', 'CREATE TEMPORARY TABLE $1', $sql);
if ($collation) {
if (strpos($collation, 'utf8_') === 0) {
$sqlarr[$i] .= " DEFAULT CHARACTER SET utf8";
}
$sqlarr[$i] .= " DEFAULT COLLATE {$collation}";
}
}
}
return $sqlarr;
}
示例5: getAlterFieldSQL
/**
* Given one xmldb_table and one xmldb_field, return the SQL statements needed to alter the field in the table.
*
* PostgreSQL has some severe limits:
* - Any change of type or precision requires a new temporary column to be created, values to
* be transfered potentially casting them, to apply defaults if the column is not null and
* finally, to rename it
* - Changes in null/not null require the SET/DROP NOT NULL clause
* - Changes in default require the SET/DROP DEFAULT clause
*
* @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.
* @return string The field altering SQL statement.
*/
public function getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL)
{
$results = array();
// To store all the needed SQL commands
// Get the normal names of the table and field
$tablename = $xmldb_table->getName();
$fieldname = $xmldb_field->getName();
// Take a look to field metadata
$meta = $this->mdb->get_columns($tablename);
$metac = $meta[$xmldb_field->getName()];
$oldmetatype = $metac->meta_type;
$oldlength = $metac->max_length;
$olddecimals = empty($metac->scale) ? null : $metac->scale;
$oldnotnull = empty($metac->not_null) ? false : $metac->not_null;
$olddefault = empty($metac->has_default) ? null : $metac->default_value;
$typechanged = true;
//By default, assume that the column type has changed
$precisionchanged = true;
//By default, assume that the column precision has changed
$decimalchanged = true;
//By default, assume that the column decimal has changed
$defaultchanged = true;
//By default, assume that the column default has changed
$notnullchanged = true;
//By default, assume that the column notnull has changed
// Detect if we are changing the type of the column
if ($xmldb_field->getType() == XMLDB_TYPE_INTEGER && $oldmetatype == 'I' || $xmldb_field->getType() == XMLDB_TYPE_NUMBER && $oldmetatype == 'N' || $xmldb_field->getType() == XMLDB_TYPE_FLOAT && $oldmetatype == 'F' || $xmldb_field->getType() == XMLDB_TYPE_CHAR && $oldmetatype == 'C' || $xmldb_field->getType() == XMLDB_TYPE_TEXT && $oldmetatype == 'X' || $xmldb_field->getType() == XMLDB_TYPE_BINARY && $oldmetatype == 'B') {
$typechanged = false;
}
// Detect if we are changing the precision
if ($xmldb_field->getType() == XMLDB_TYPE_TEXT || $xmldb_field->getType() == XMLDB_TYPE_BINARY || $oldlength == -1 || $xmldb_field->getLength() == $oldlength) {
$precisionchanged = false;
}
// Detect if we are changing the decimals
if ($xmldb_field->getType() == XMLDB_TYPE_INTEGER || $xmldb_field->getType() == XMLDB_TYPE_CHAR || $xmldb_field->getType() == XMLDB_TYPE_TEXT || $xmldb_field->getType() == XMLDB_TYPE_BINARY || !$xmldb_field->getDecimals() || !$olddecimals || $xmldb_field->getDecimals() == $olddecimals) {
$decimalchanged = false;
}
// Detect if we are changing the default
if ($xmldb_field->getDefault() === null && $olddefault === null || $xmldb_field->getDefault() === $olddefault) {
$defaultchanged = false;
}
// Detect if we are changing the nullability
if ($xmldb_field->getNotnull() === $oldnotnull) {
$notnullchanged = false;
}
// Get the quoted name of the table and field
$tablename = $this->getTableName($xmldb_table);
$fieldname = $this->getEncQuoted($xmldb_field->getName());
// Decide if we have changed the column specs (type/precision/decimals)
$specschanged = $typechanged || $precisionchanged || $decimalchanged;
// if specs have changed, need to alter column
if ($specschanged) {
// Always drop any exiting default before alter column (some type changes can cause casting error in default for column)
if ($olddefault !== null) {
$results[] = 'ALTER TABLE ' . $tablename . ' ALTER COLUMN ' . $fieldname . ' DROP DEFAULT';
// Drop default clause
}
$alterstmt = 'ALTER TABLE ' . $tablename . ' ALTER COLUMN ' . $this->getEncQuoted($xmldb_field->getName()) . ' TYPE' . $this->getFieldSQL($xmldb_table, $xmldb_field, null, true, true, null, false);
// Some castings must be performed explicitly (mainly from text|char to numeric|integer)
if (($oldmetatype == 'C' || $oldmetatype == 'X') && ($xmldb_field->getType() == XMLDB_TYPE_NUMBER || $xmldb_field->getType() == XMLDB_TYPE_FLOAT)) {
$alterstmt .= ' USING CAST(' . $fieldname . ' AS NUMERIC)';
// from char or text to number or float
} else {
if (($oldmetatype == 'C' || $oldmetatype == 'X') && $xmldb_field->getType() == XMLDB_TYPE_INTEGER) {
$alterstmt .= ' USING CAST(CAST(' . $fieldname . ' AS NUMERIC) AS INTEGER)';
// From char to integer
}
}
$results[] = $alterstmt;
}
// If the default has changed or we have performed one change in specs
if ($defaultchanged || $specschanged) {
$default_clause = $this->getDefaultClause($xmldb_field);
if ($default_clause) {
$sql = 'ALTER TABLE ' . $tablename . ' ALTER COLUMN ' . $fieldname . ' SET' . $default_clause;
// Add default clause
$results[] = $sql;
} else {
if (!$specschanged) {
// Only drop default if we haven't performed one specs change
$results[] = 'ALTER TABLE ' . $tablename . ' ALTER COLUMN ' . $fieldname . ' DROP DEFAULT';
// Drop default clause
}
//.........这里部分代码省略.........
示例6: check_table
protected function check_table(xmldb_table $xmldb_table, array $metacolumns)
{
global $DB;
$dbman = $DB->get_manager();
$strictchecks = optional_param('strict', false, PARAM_BOOL);
$o = '';
$violatedkeys = array();
// Keys
if ($xmldb_keys = $xmldb_table->getKeys()) {
$o .= ' <ul>';
foreach ($xmldb_keys as $xmldb_key) {
// We are only interested in foreign keys.
if (!in_array($xmldb_key->getType(), array(XMLDB_KEY_FOREIGN, XMLDB_KEY_FOREIGN_UNIQUE))) {
continue;
}
$o .= ' <li>' . $this->str['key'] . ': ' . $xmldb_key->readableInfo() . ' ';
// Work out the SQL to find key violations.
$keyfields = $xmldb_key->getFields();
$reffields = $xmldb_key->getRefFields();
$joinconditions = array();
$nullnessconditions = array();
$params = array();
foreach ($keyfields as $i => $field) {
$joinconditions[] = 't1.' . $field . ' = t2.' . $reffields[$i];
$xmldb_field = $xmldb_table->getField($field);
$default = $xmldb_field->getDefault();
if (!$xmldb_field->getNotNull()) {
$nullnessconditions[] = 't1.' . $field . ' IS NOT NULL';
} else {
if (!$strictchecks && ($default == '0' || !$default)) {
// We have a default of 0 or '' or something like that.
// These generate a lot of false-positives, so ignore them
// for now.
$nullnessconditions[] = 't1.' . $field . ' <> ?';
$params[] = $xmldb_field->getDefault();
}
}
}
$nullnessconditions[] = 't2.id IS NULL';
$sql = 'SELECT count(1) FROM {' . $xmldb_table->getName() . '} t1 LEFT JOIN {' . $xmldb_key->getRefTable() . '} t2 ON ' . implode(' AND ', $joinconditions) . ' WHERE ' . implode(' AND ', $nullnessconditions);
// Check there are any problems in the database.
$violations = $DB->count_records_sql($sql, $params);
if ($violations == 0) {
$o .= '<font color="green">' . $this->str['ok'] . '</font>';
} else {
$o .= '<font color="red">' . $this->str['violations'] . '</font>';
// Add the missing index to the list
$violation = new stdClass();
$violation->table = $xmldb_table;
$violation->key = $xmldb_key;
$violation->numviolations = $violations;
$violation->numrows = $DB->count_records($xmldb_table->getName());
$violation->sql = str_replace('count(1)', '*', $sql);
if (!empty($params)) {
$violation->sqlparams = '(' . implode(', ', $params) . ')';
} else {
$violation->sqlparams = '';
}
$violatedkeys[] = $violation;
}
$o .= '</li>';
}
$o .= ' </ul>';
}
return array($o, $violatedkeys);
}
示例7: validateDefinition
/**
* Validates the index restrictions.
*
* The error message should not be localised because it is intended for developers,
* end users and admins should never see these problems!
*
* @param xmldb_table $xmldb_table optional when object is table
* @return string null if ok, error message if problem found
*/
function validateDefinition(xmldb_table $xmldb_table=null) {
if (!$xmldb_table) {
return 'Invalid xmldb_index->validateDefinition() call, $xmldb_table si required.';
}
$total = 0;
foreach ($this->getFields() as $fieldname) {
if (!$field = $xmldb_table->getField($fieldname)) {
// argh, we do not have the fields loaded yet, this should not happen during install
continue;
}
switch ($field->getType()) {
case XMLDB_TYPE_INTEGER:
$total += 8; // big int
break;
case XMLDB_TYPE_NUMBER:
$total += 12; // this is just a guess
break;
case XMLDB_TYPE_FLOAT:
$total += 8; // double precision
break;
case XMLDB_TYPE_CHAR:
if ($field->getLength() > self::INDEX_MAX_BYTES / 3) {
return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_CHAR field "'.$field->getName().'" can not be indexed because it is too long.'
.' Limit is '.(self::INDEX_MAX_BYTES/3).' chars.';
}
$total += ($field->getLength() * 3); // the most complex utf-8 chars have 3 bytes
break;
case XMLDB_TYPE_TEXT:
return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_TEXT field "'.$field->getName().'" can not be indexed';
break;
case XMLDB_TYPE_BINARY:
return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_BINARY field "'.$field->getName().'" can not be indexed';
break;
case XMLDB_TYPE_DATETIME:
$total += 8; // this is just a guess
break;
case XMLDB_TYPE_TIMESTAMP:
$total += 8; // this is just a guess
break;
}
}
if ($total > self::INDEX_COMPOSED_MAX_BYTES) {
return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: the composed index on fields "'.implode(',', $this->getFields()).'" is too long.'
.' Limit is '.self::INDEX_COMPOSED_MAX_BYTES.' bytes / '.(self::INDEX_COMPOSED_MAX_BYTES/3).' chars.';
}
return null;
}
示例8: getTableName
/**
* Given one xmldb_table, returns it's correct name, depending of all the parametrization
* Overridden to allow change of names in temp tables
*
* @param xmldb_table 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
*/
public function getTableName(xmldb_table $xmldb_table, $quoted = true)
{
/// Get the name, supporting special mssql names for temp tables
if ($this->temptables->is_temptable($xmldb_table->getName())) {
$tablename = $this->temptables->get_correct_name($xmldb_table->getName());
} else {
$tablename = $this->prefix . $xmldb_table->getName();
}
/// Apply quotes optionally
if ($quoted) {
$tablename = $this->getEncQuoted($tablename);
}
return $tablename;
}
示例9: getTableName
/**
* Given one xmldb_table, returns it's correct name, depending of all the parametrization
*
* @param xmldb_table 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
*/
public function getTableName(xmldb_table $xmldb_table, $quoted = true)
{
/// Get the name
$tablename = $this->prefix . $xmldb_table->getName();
/// Apply quotes optionally
if ($quoted) {
$tablename = $this->getEncQuoted($tablename);
}
return $tablename;
}
示例10: invoke
/**
* 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));
$name = substr(trim(strtolower(required_param('name', PARAM_PATH))), 0, 28);
$comment = required_param('comment', PARAM_CLEAN);
$comment = $comment;
$dbdir =& $XMLDB->dbdirs[$dirpath];
$editeddir =& $XMLDB->editeddirs[$dirpath];
$structure =& $editeddir->xml_file->getStructure();
$table =& $structure->getTable($tableparam);
$errors = array();
/// To store all the errors found
/// Perform some checks
/// Check empty name
if (empty($name)) {
$errors[] = $this->str['tablenameempty'];
}
/// Check incorrect name
if ($name == 'changeme') {
$errors[] = $this->str['incorrecttablename'];
}
/// Check duplicatename
if ($tableparam != $name && $structure->getTable($name)) {
$errors[] = $this->str['duplicatetablename'];
}
if (!empty($errors)) {
$temptable = new xmldb_table($name);
/// Prepare the output
$o = '<p>' . implode(', ', $errors) . '</p>
<p>' . $temptable->getName() . '</p>';
$o .= '<a href="index.php?action=edit_table&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
$this->output = $o;
/// Continue if we aren't under errors
} else {
if (empty($errors)) {
/// If there is one name change, do it, changing the prev and next
/// atributes of the adjacent tables
if ($tableparam != $name) {
$table->setName($name);
if ($table->getPrevious()) {
$prev =& $structure->getTable($table->getPrevious());
$prev->setNext($name);
$prev->setChanged(true);
}
if ($table->getNext()) {
$next =& $structure->getTable($table->getNext());
$next->setPrevious($name);
$next->setChanged(true);
}
/// Table has changed
$table->setChanged(true);
}
/// Set comment
if ($table->getComment() != $comment) {
$table->setComment($comment);
/// Table has changed
$table->setChanged(true);
}
/// Recalculate the hash
$structure->calculateHash(true);
/// If the hash has changed from the original one, change the version
/// and mark the structure as changed
$origstructure =& $dbdir->xml_file->getStructure();
if ($structure->getHash() != $origstructure->getHash()) {
$structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
$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;
}
示例11: getAlterFieldSQL
/**
* Given one xmldb_table and one xmldb_field, return the SQL statements needed to alter the field in the table.
*
* @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.
* @return string The field altering SQL statement.
*/
public function getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL)
{
$results = array();
// To store all the needed SQL commands
// Get the quoted name of the table and field
$tablename = $xmldb_table->getName();
$fieldname = $xmldb_field->getName();
// Take a look to field metadata
$meta = $this->mdb->get_columns($tablename);
$metac = $meta[$fieldname];
$oldmetatype = $metac->meta_type;
$oldlength = $metac->max_length;
$olddecimals = empty($metac->scale) ? null : $metac->scale;
$oldnotnull = empty($metac->not_null) ? false : $metac->not_null;
//$olddefault = empty($metac->has_default) ? null : strtok($metac->default_value, ':');
$typechanged = true;
//By default, assume that the column type has changed
$lengthchanged = true;
//By default, assume that the column length has changed
// Detect if we are changing the type of the column
if ($xmldb_field->getType() == XMLDB_TYPE_INTEGER && $oldmetatype == 'I' || $xmldb_field->getType() == XMLDB_TYPE_NUMBER && $oldmetatype == 'N' || $xmldb_field->getType() == XMLDB_TYPE_FLOAT && $oldmetatype == 'F' || $xmldb_field->getType() == XMLDB_TYPE_CHAR && $oldmetatype == 'C' || $xmldb_field->getType() == XMLDB_TYPE_TEXT && $oldmetatype == 'X' || $xmldb_field->getType() == XMLDB_TYPE_BINARY && $oldmetatype == 'B') {
$typechanged = false;
}
// If the new field (and old) specs are for integer, let's be a bit more specific differentiating
// types of integers. Else, some combinations can cause things like MDL-21868
if ($xmldb_field->getType() == XMLDB_TYPE_INTEGER && $oldmetatype == 'I') {
if ($xmldb_field->getLength() > 9) {
// Convert our new lenghts to detailed meta types
$newmssqlinttype = 'I8';
} else {
if ($xmldb_field->getLength() > 4) {
$newmssqlinttype = 'I';
} else {
$newmssqlinttype = 'I2';
}
}
if ($metac->type == 'bigint') {
// Convert current DB type to detailed meta type (our metatype is not enough!)
$oldmssqlinttype = 'I8';
} else {
if ($metac->type == 'smallint') {
$oldmssqlinttype = 'I2';
} else {
$oldmssqlinttype = 'I';
}
}
if ($newmssqlinttype != $oldmssqlinttype) {
// Compare new and old meta types
$typechanged = true;
// Change in meta type means change in type at all effects
}
}
// Detect if we are changing the length of the column, not always necessary to drop defaults
// if only the length changes, but it's safe to do it always
if ($xmldb_field->getLength() == $oldlength) {
$lengthchanged = false;
}
// If type or length have changed drop the default if exists
if ($typechanged || $lengthchanged) {
$results = $this->getDropDefaultSQL($xmldb_table, $xmldb_field);
}
// Some changes of type require multiple alter statements, because mssql lacks direct implicit cast between such types
// Here it is the matrix: http://msdn.microsoft.com/en-us/library/ms187928(SQL.90).aspx
// Going to store such intermediate alters in array of objects, storing all the info needed
$multiple_alter_stmt = array();
$targettype = $xmldb_field->getType();
if ($targettype == XMLDB_TYPE_TEXT && $oldmetatype == 'I') {
// integer to text
$multiple_alter_stmt[0] = new stdClass();
// needs conversion to varchar
$multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
$multiple_alter_stmt[0]->length = 255;
} else {
if ($targettype == XMLDB_TYPE_TEXT && $oldmetatype == 'N') {
// decimal to text
$multiple_alter_stmt[0] = new stdClass();
// needs conversion to varchar
$multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
$multiple_alter_stmt[0]->length = 255;
} else {
if ($targettype == XMLDB_TYPE_TEXT && $oldmetatype == 'F') {
// float to text
$multiple_alter_stmt[0] = new stdClass();
// needs conversion to varchar
$multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
$multiple_alter_stmt[0]->length = 255;
} else {
if ($targettype == XMLDB_TYPE_INTEGER && $oldmetatype == 'X') {
// text to integer
$multiple_alter_stmt[0] = new stdClass();
//.........这里部分代码省略.........
示例12: test_create_table
/**
* Test behaviour of create_table()
*/
public function test_create_table() {
$DB = $this->tdb; // do not use global $DB!
$dbman = $this->tdb->get_manager();
// create table
$table = $this->tables['test_table1'];
$dbman->create_table($table);
$this->assertTrue($dbman->table_exists($table));
// basic get_tables() test
$tables = $DB->get_tables();
$this->assertTrue(array_key_exists('test_table1', $tables));
// basic get_columns() tests
$columns = $DB->get_columns('test_table1');
$this->assertEqual($columns['id']->meta_type, 'R');
$this->assertEqual($columns['course']->meta_type, 'I');
$this->assertEqual($columns['name']->meta_type, 'C');
$this->assertEqual($columns['secondname']->meta_type, 'C');
$this->assertEqual($columns['thirdname']->meta_type, 'C');
$this->assertEqual($columns['intro']->meta_type, 'X');
$this->assertEqual($columns['avatar']->meta_type, 'B');
$this->assertEqual($columns['grade']->meta_type, 'N');
$this->assertEqual($columns['percentfloat']->meta_type, 'N');
$this->assertEqual($columns['userid']->meta_type, 'I');
// some defaults
$this->assertTrue($columns['course']->has_default);
$this->assertEqual($columns['course']->default_value, 0);
$this->assertTrue($columns['name']->has_default);
$this->assertEqual($columns['name']->default_value, 'Moodle');
$this->assertTrue($columns['secondname']->has_default);
$this->assertEqual($columns['secondname']->default_value, '');
$this->assertTrue($columns['thirdname']->has_default);
$this->assertEqual($columns['thirdname']->default_value, '');
$this->assertTrue($columns['percentfloat']->has_default);
$this->assertEqual($columns['percentfloat']->default_value, 99.9);
$this->assertTrue($columns['userid']->has_default);
$this->assertEqual($columns['userid']->default_value, 0);
// basic get_indexes() test
$indexes = $DB->get_indexes('test_table1');
$courseindex = reset($indexes);
$this->assertEqual($courseindex['unique'], 1);
$this->assertEqual($courseindex['columns'][0], 'course');
// check sequence returns 1 for first insert
$rec = (object)array(
'course' => 10,
'secondname' => 'not important',
'intro' => 'not important');
$this->assertIdentical($DB->insert_record('test_table1', $rec), 1);
// check defined defaults are working ok
$dbrec = $DB->get_record('test_table1', array('id' => 1));
$this->assertEqual($dbrec->name, 'Moodle');
$this->assertEqual($dbrec->thirdname, '');
// check exceptions if multiple R columns
$table = new xmldb_table ('test_table2');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('rid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('primaryx', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (Exception $e) {
$this->assertTrue($e instanceof ddl_exception);
}
// check exceptions missing primary key on R column
$table = new xmldb_table ('test_table2');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (Exception $e) {
$this->assertTrue($e instanceof ddl_exception);
}
}
示例13: getCreateTriggerSQL
/**
* Returns the code needed to create one trigger for the xmldb_table and xmldb_field passed
*
* @param xmldb_table $xmldb_table The xmldb_table object instance.
* @param xmldb_field $xmldb_field The xmldb_field object instance.
* @param string $sequence_name
* @return array Array of SQL statements to create the sequence.
*/
public function getCreateTriggerSQL($xmldb_table, $xmldb_field, $sequence_name)
{
$trigger_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'trg');
$trigger = "CREATE TRIGGER " . $trigger_name;
$trigger .= "\n BEFORE INSERT";
$trigger .= "\nON " . $this->getTableName($xmldb_table);
$trigger .= "\n FOR EACH ROW";
$trigger .= "\nBEGIN";
$trigger .= "\n IF :new." . $this->getEncQuoted($xmldb_field->getName()) . ' IS NULL THEN';
$trigger .= "\n SELECT " . $sequence_name . '.nextval INTO :new.' . $this->getEncQuoted($xmldb_field->getName()) . " FROM dual;";
$trigger .= "\n END IF;";
$trigger .= "\nEND;";
return array($trigger);
}
示例14: test_create_table
/**
* Test behaviour of create_table()
*/
public function test_create_table()
{
$DB = $this->tdb;
// do not use global $DB!
$dbman = $this->tdb->get_manager();
// create table
$table = $this->tables['test_table1'];
$dbman->create_table($table);
$this->assertTrue($dbman->table_exists($table));
// basic get_tables() test
$tables = $DB->get_tables();
$this->assertTrue(array_key_exists('test_table1', $tables));
// basic get_columns() tests
$columns = $DB->get_columns('test_table1');
$this->assertEquals($columns['id']->meta_type, 'R');
$this->assertEquals($columns['course']->meta_type, 'I');
$this->assertEquals($columns['name']->meta_type, 'C');
$this->assertEquals($columns['secondname']->meta_type, 'C');
$this->assertEquals($columns['thirdname']->meta_type, 'C');
$this->assertEquals($columns['intro']->meta_type, 'X');
$this->assertEquals($columns['avatar']->meta_type, 'B');
$this->assertEquals($columns['grade']->meta_type, 'N');
$this->assertEquals($columns['percentfloat']->meta_type, 'N');
$this->assertEquals($columns['userid']->meta_type, 'I');
// some defaults
$this->assertTrue($columns['course']->has_default);
$this->assertEquals($columns['course']->default_value, 0);
$this->assertTrue($columns['name']->has_default);
$this->assertEquals($columns['name']->default_value, 'Moodle');
$this->assertTrue($columns['secondname']->has_default);
$this->assertEquals($columns['secondname']->default_value, '');
$this->assertTrue($columns['thirdname']->has_default);
$this->assertEquals($columns['thirdname']->default_value, '');
$this->assertTrue($columns['percentfloat']->has_default);
$this->assertEquals($columns['percentfloat']->default_value, 99.90000000000001);
$this->assertTrue($columns['userid']->has_default);
$this->assertEquals($columns['userid']->default_value, 0);
// basic get_indexes() test
$indexes = $DB->get_indexes('test_table1');
$courseindex = reset($indexes);
$this->assertEquals($courseindex['unique'], 1);
$this->assertEquals($courseindex['columns'][0], 'course');
// check sequence returns 1 for first insert
$rec = (object) array('course' => 10, 'secondname' => 'not important', 'intro' => 'not important');
$this->assertSame($DB->insert_record('test_table1', $rec), 1);
// check defined defaults are working ok
$dbrec = $DB->get_record('test_table1', array('id' => 1));
$this->assertEquals($dbrec->name, 'Moodle');
$this->assertEquals($dbrec->thirdname, '');
// check exceptions if multiple R columns
$table = new xmldb_table('test_table2');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('rid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('primaryx', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (Exception $e) {
$this->assertTrue($e instanceof ddl_exception);
}
// check exceptions missing primary key on R column
$table = new xmldb_table('test_table2');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (Exception $e) {
$this->assertTrue($e instanceof ddl_exception);
}
// long table name names - the largest allowed
$table = new xmldb_table('test_table0123456789_____xyz');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
$dbman->create_table($table);
$this->assertTrue($dbman->table_exists($table));
$dbman->drop_table($table);
// table name is too long
$table = new xmldb_table('test_table0123456789_____xyz9');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (Exception $e) {
//.........这里部分代码省略.........
示例15: addTable
/**
* Add one table to the structure, allowing to specify the desired order
* If it's not specified, then the table is added at the end.
* @param xmldb_table $table
* @param mixed $after
*/
public function addTable($table, $after = null)
{
// Calculate the previous and next tables
$prevtable = null;
$nexttable = null;
if (!$after) {
if ($this->tables) {
end($this->tables);
$prevtable = $this->tables[key($this->tables)];
}
} else {
$prevtable = $this->getTable($after);
}
if ($prevtable && $prevtable->getNext()) {
$nexttable = $this->getTable($prevtable->getNext());
}
// Set current table previous and next attributes
if ($prevtable) {
$table->setPrevious($prevtable->getName());
$prevtable->setNext($table->getName());
}
if ($nexttable) {
$table->setNext($nexttable->getName());
$nexttable->setPrevious($table->getName());
}
// Some more attributes
$table->setLoaded(true);
$table->setChanged(true);
// Add the new table
$this->tables[] = $table;
// Reorder the whole structure
$this->orderTables($this->tables);
// Recalculate the hash
$this->calculateHash(true);
// We have one new table, so the structure has changed
$this->setVersion(userdate(time(), '%Y%m%d', 99, false));
$this->setChanged(true);
}