📄 test.class.php
字号:
<?php // $Id: test.class.php,v 1.39.2.1 2008/05/18 23:34:50 stronk7 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 will perform one full test of all the available DDL/// functions under your DBclass test extends XMLDBAction { /** * Init method, every subclass will have its own */ function init() { parent::init(); /// Set own custom attributes /// Get needed strings $this->loadStrings(array( 'back' => 'xmldb' )); } /** * 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, $db; /// ADD YOUR CODE HERE require_once ($CFG->libdir . '/ddllib.php'); /// Where all the tests will be stored $tests = array(); /// The back to edit table button $b = ' <p class="centerpara buttons">'; $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>'; $b .= '</p>'; $o = $b; /// Silenty drop any previous test tables $table = new XMLDBTable('testtable'); if (table_exists($table)) { $status = drop_table($table, true, false); } $table = new XMLDBTable ('anothertest'); if (table_exists($table)) { $status = drop_table($table, true, false); } $table = new XMLDBTable ('newnameforthetable'); if (table_exists($table)) { $status = drop_table($table, true, false); } /// 1st test. Complete table creation. $table = new XMLDBTable('testtable'); $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general'); $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null); $table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null); $table->addFieldInfo('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1'); $table->addFieldInfo('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null); $table->addFieldInfo('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null); $table->addFieldInfo('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); $table->addKeyInfo('type-name', XMLDB_KEY_UNIQUE, array('type', 'name')); $table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course')); $table->addIndexInfo('rsstype', XMLDB_INDEX_UNIQUE, array('rsstype')); $table->setComment("This is a test'n drop table. You can drop it safely"); /// Get SQL code and execute it $test = new stdClass; $test->sql = $table->getCreateTableSQL($CFG->dbtype, $CFG->prefix, true); $test->status = create_table($table, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['create table'] = $test; /// 2nd test. drop table if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $test->sql = $table->getDropTableSQL($CFG->dbtype, $CFG->prefix, true); $test->status = drop_table($table, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['drop table'] = $test; } /// 3rd test. creating another, smaller table if ($test->status) { $table = new XMLDBTable ('anothertest'); $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle'); $table->addFieldInfo('secondname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('avatar', XMLDB_TYPE_BINARY, 'medium', null, null, null, null, null, null); $table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,10', null, null, null, null, null); $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); /// Get SQL code and execute it $test = new stdClass; $test->sql = $table->getCreateTableSQL($CFG->dbtype, $CFG->prefix, true); $test->status = create_table($table, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['create table - 2'] = $test; } /// Insert two records to do the work with real data $rec->course = 1; $rec->name = 'Martin'; $rec->secondname = 'Dougiamas'; $rec->intro = 'The creator of Moodle'; $rec->grade = 10.0001; insert_record('anothertest', $rec); $rec->course = 2; $rec->name = 'Eloy'; $rec->secondname = 'Lafuente'; $rec->intro = 'One poor developer'; $rec->grade = 9.99; insert_record('anothertest', $rec); /// 4th test. Adding one complex enum field if ($test->status) { /// Create a new field with complex specs (enums are good candidates) $field = new XMLDBField('type'); $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course'); /// Get SQL code and execute it $test = new stdClass; $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = add_field($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['add enum field'] = $test; } /// 5th test. Dropping one complex enum field if ($test->status) { /// Create a new field with complex specs (enums are good candidates) $test = new stdClass; $test->sql = $table->getDropFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = drop_field($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['drop enum field'] = $test; } /// 6th test. Adding one complex enum field if ($test->status) { /// Create a new field with complex specs (enums are good candidates) $field = new XMLDBField('type'); $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course'); /// Get SQL code and execute it $test = new stdClass; $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = add_field($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['add enum field again'] = $test; } /// 7th test. Adding one numeric field if ($test->status) { /// Create a new field (numeric) $field = new XMLDBField('onenumber'); $field->setAttributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0, 'type'); /// Get SQL code and execute it $test = new stdClass; $test->sql = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = add_field($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['add numeric field'] = $test; } /// 8th test. Dropping one complex enum field if ($test->status) { /// Create a new field with complex specs (enums are good candidates) $field = new XMLDBField('type'); $test = new stdClass; $test->sql = $table->getDropFieldSQL($CFG->dbtype, $CFG->prefix, $field, true); $test->status = drop_field($table, $field, false, false); if (!$test->status) { $test->error = $db->ErrorMsg(); } $tests['drop enum field again'] = $test; } /// 9th test. Change the type of one column from integer to varchar if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('course'); $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, '0'); $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 (int2char)'] = $test; } /// 10th test. Change the type of one column from varchar to integer if ($test->status) { /// Get SQL code and execute it $test = new stdClass; $field = new XMLDBField('course'); $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $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 (char2int)'] = $test; } /// 11th test. Change the type of one column from number to varchar 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'n drop");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -