Merge branch 'MDL-32113-master-xmldbnumeric' of https://github.com/mudrd8mz/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2017-12-21 22:43:26 +01:00
commit 3e38589f77
9 changed files with 56 additions and 18 deletions

View File

@ -80,7 +80,7 @@ function transformForm(event) {
decimalsField.value = '';
break;
case '2': // XMLDB_TYPE_NUMBER
lengthTip.innerHTML = ' 1...20'; // Hardcoded xmldb_field::NUMBER_MAX_LENGTH, yes!
lengthTip.innerHTML = ' 1...38'; // Hardcoded xmldb_field::NUMBER_MAX_LENGTH, yes!
lengthField.disabled = false;
decimalsTip.innerHTML = ' 0...length or empty';
break;

View File

@ -48,6 +48,7 @@ class edit_field_save extends XMLDBAction {
'floatincorrectlength' => 'tool_xmldb',
'charincorrectlength' => 'tool_xmldb',
'numberincorrectdecimals' => 'tool_xmldb',
'numberincorrectwholepart' => 'tool_xmldb',
'floatincorrectdecimals' => 'tool_xmldb',
'defaultincorrect' => 'tool_xmldb',
'back' => 'tool_xmldb',
@ -158,6 +159,9 @@ class edit_field_save extends XMLDBAction {
$decimals < $length))) {
$errors[] = $this->str['numberincorrectdecimals'];
}
if (!empty($decimals) && ($length - $decimals > xmldb_field::INTEGER_MAX_LENGTH)) {
$errors[] = $this->str['numberincorrectwholepart'];
}
if (!(empty($default) || (is_numeric($default) &&
!empty($default)))) {
$errors[] = $this->str['defaultincorrect'];

View File

@ -165,6 +165,7 @@ $string['nowrongintsfound'] = 'No wrong integers have been found, your DB doesn\
$string['nowrongoraclesemanticsfound'] = 'No Oracle columns using BYTE semantics have been found, your DB doesn\'t need further actions.';
$string['numberincorrectdecimals'] = 'Incorrect number of decimals for number field';
$string['numberincorrectlength'] = 'Incorrect length for number field';
$string['numberincorrectwholepart'] = 'Too big whole number part for number field';
$string['pendingchanges'] = 'Note: You have performed changes to this file. They can be saved at any moment.';
$string['pendingchangescannotbesaved'] = 'There are changes in this file but they cannot be saved! Please verify that both the directory and the "install.xml" within it have write permissions for the web server.';
$string['pendingchangescannotbesavedreload'] = 'There are changes in this file but they cannot be saved! Please verify that both the directory and the "install.xml" within it have write permissions for the web server. Then reload this page and you should be able to save those changes.';

View File

@ -194,10 +194,6 @@ class mssql_sql_generator extends sql_generator {
case XMLDB_TYPE_NUMBER:
$dbtype = $this->number_type;
if (!empty($xmldb_length)) {
// 38 is the max allowed
if ($xmldb_length > 38) {
$xmldb_length = 38;
}
$dbtype .= '(' . $xmldb_length;
if (!empty($xmldb_decimals)) {
$dbtype .= ',' . $xmldb_decimals;

View File

@ -181,10 +181,6 @@ class oracle_sql_generator extends sql_generator {
case XMLDB_TYPE_FLOAT:
case XMLDB_TYPE_NUMBER:
$dbtype = $this->number_type;
// 38 is the max allowed
if ($xmldb_length > 38) {
$xmldb_length = 38;
}
if (!empty($xmldb_length)) {
$dbtype .= '(' . $xmldb_length;
if (!empty($xmldb_decimals)) {

View File

@ -52,6 +52,7 @@ class core_ddl_testcase extends database_driver_testcase {
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', null, null, null, null);
$table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, 66.6);
$table->add_field('bignum', XMLDB_TYPE_NUMBER, '38,18', null, null, null, 1234567890.1234);
$table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
@ -414,10 +415,10 @@ class core_ddl_testcase extends database_driver_testcase {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid decimal length.
// Invalid decimal length - max precision is 38 digits.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_NUMBER, '21,10', null, XMLDB_NOTNULL, null, null);
$table->add_field('num', XMLDB_TYPE_NUMBER, '39,19', null, XMLDB_NOTNULL, 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");
@ -430,7 +431,7 @@ class core_ddl_testcase extends database_driver_testcase {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid decimal decimals.
// Invalid decimal decimals - number of decimals can't be higher than total number of digits.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_NUMBER, '10,11', null, XMLDB_NOTNULL, null, null);
@ -446,6 +447,38 @@ class core_ddl_testcase extends database_driver_testcase {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid decimal whole number - the whole number part can't have more digits than integer fields.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_NUMBER, '38,17', null, XMLDB_NOTNULL, 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;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid decimal decimals - negative scale not supported.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('num', XMLDB_TYPE_NUMBER, '30,-5', null, XMLDB_NOTNULL, 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;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
} catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
// Invalid decimal default.
$table = new xmldb_table('test_table4');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);

View File

@ -10,6 +10,7 @@
<FIELD NAME="intro" TYPE="text" LENGTH="medium" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="avatar" TYPE="binary" LENGTH="medium" NOTNULL="false" UNSIGNED="false" SEQUENCE="false"/>
<FIELD NAME="grade" TYPE="number" LENGTH="20" DECIMALS="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="bignum" TYPE="number" LENGTH="38" DECIMALS="18" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="path" TYPE="char" LENGTH="255" NOTNULL="true"/>
</FIELDS>
<KEYS>

View File

@ -1,7 +1,14 @@
This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.
=== 3.5 ===
* The maximum supported precision (the total number of digits) for XMLDB_TYPE_NUMBER ("number") fields raised from 20 to
38 digits. Additionally, the whole number part (precision minus scale) must not be longer than the maximum length of
integer fields (20 digits). Note that PHP floats commonly support precision of roughly 15 digits only (MDL-32113).
=== 3.4 ===
* oauth2_client::request method has an extra parameter to specify the accept header for the response (MDL-60733)
* The following functions, previously used (exclusively) by upgrade steps are not available
anymore because of the upgrade cleanup performed for this version. See MDL-57432 for more info:

View File

@ -64,9 +64,9 @@ class xmldb_field extends xmldb_object {
const INTEGER_MAX_LENGTH = 20;
/**
* @const max length of decimals
* @const max length (precision, the total number of digits) of decimals
*/
const NUMBER_MAX_LENGTH = 20;
const NUMBER_MAX_LENGTH = 38;
/**
* @const max length of floats
@ -786,10 +786,6 @@ class xmldb_field extends xmldb_object {
case XMLDB_TYPE_NUMBER:
$maxlength = self::NUMBER_MAX_LENGTH;
if ($xmldb_table->getName() === 'question_numerical_units' and $name === 'multiplier') {
//TODO: remove after MDL-32113 is resolved
$maxlength = 40;
}
$length = $this->getLength();
if (!is_number($length) or $length <= 0 or $length > $maxlength) {
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid length';
@ -799,6 +795,10 @@ class xmldb_field extends xmldb_object {
if (!is_number($decimals) or $decimals < 0 or $decimals > $length) {
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid decimals';
}
if ($length - $decimals > self::INTEGER_MAX_LENGTH) {
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.
$this->getName().'" has too big whole number part';
}
$default = $this->getDefault();
if (!empty($default) and !is_numeric($default)) {
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid default';