📄 test.class.php
字号:
$test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_type($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field type (number2char)'] = $test; } /// 12th test. Change the type of one column from varchar to float if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('grade'); $field->setAttributes(XMLDB_TYPE_FLOAT, '20,10', XMLDB_UNSIGNED, null, null, null, null, null); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_type($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field type (char2float)'] = $test; } /// 13th test. Change the type of one column from float to char if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('grade'); $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, 'test'); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_type($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field type (float2char)'] = $test; } /// 14th test. Change the type of one column from char to number if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('grade'); $field->setAttributes(XMLDB_TYPE_NUMBER, '20,10', XMLDB_UNSIGNED, null, null, null, null, null); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_type($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field type (char2number)'] = $test; } /// 15th test. Change the precision of one text field if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('intro'); $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_precision($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field precision (text)'] = $test; } /// 16th test. Change the precision of one char field if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('secondname'); $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_precision($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field precision (char)'] = $test; } /// 17th test. Change the precision of one numeric field if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('grade'); $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null, null, null); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_precision($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field precision (number)'] = $test; } /// 18th test. Change the precision of one integer field to a smaller one if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('course'); $field->setAttributes(XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_precision($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field precision (integer) to smaller one'] = $test; } /// 19th test. Change the sign of one numeric field to unsigned if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('grade'); $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', XMLDB_UNSIGNED, null, null, null, null, null); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_unsigned($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field sign (unsigned)'] = $test; } /// 20th test. Change the sign of one numeric field to signed if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('grade'); $field->setAttributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null, null, null); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_unsigned($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field sign (signed)'] = $test; } /// 21th test. Change the nullability of one char field to not null if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('name'); $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'Moodle'); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_notnull($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field nullability (not null)'] = $test; } /// 22th test. Change the nullability of one char field to null if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('name'); $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle'); $test->sql = $table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_notnull($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['change field nullability (null)'] = $test; } /// 23th test. Dropping the default of one field if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('name'); $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null); $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_default($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['drop field default of NULL field'] = $test; } /// 24th test. Creating the default for one field if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('name'); $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle'); $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_default($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['add field default of NULL field'] = $test; } /// 25th test. Creating the default for one field if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('secondname'); $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, 'Moodle2'); $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_default($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['add field default of NOT NULL field'] = $test; } /// 26th test. Dropping the default of one NOT NULL field if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('secondname'); $field->setAttributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null); $test->sql = $table->getModifyDefaultSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = change_field_default($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['drop field default of NOT NULL field'] = $test; } /// 27th test. Adding one unique index to the table if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $index = new XMLDBIndex('secondname'); $index->setAttributes(XMLDB_INDEX_UNIQUE, array('name', 'secondname', 'grade')); $test->sql = $table->getAddIndexSQL($CFG->dbtype, $CFG->prefix, $index, true); $test->status = add_index($table, $index, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['add unique index'] = $test; } /// 28th test. Adding one not unique index to the table if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $index = new XMLDBIndex('secondname'); $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name')); $test->sql = $table->getAddIndexSQL($CFG->dbtype, $CFG->prefix, $index, true); $test->status = add_index($table, $index, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['add not unique index'] = $test; } /// 29th test. Re-add the same index than previous test. Check find_index_name() works. if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $index = new XMLDBIndex('secondname'); $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('name', 'course')); if ($indexfound = find_index_name($table, $index)) { $test->status = true; $test->sql = array(); } else { $test->status = true; $test->error = 'Index not found!'; $test->sql = array();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -