Skip to content

Commit

Permalink
MDL-32113 xmldb: Support numeric fields with precision up to 38 digits
Browse files Browse the repository at this point in the history
The patch increases the maximum supported precision (total number of
digits) for numeric (decimal) fields to 38 digits (current limit on
Oracle and MSSQL).

Additionally, we add our own limit for the whole number part of numeric
fields so they are no longer than integer fields (20 digits). This is to
make it easier to eventually convert from one field type to another.
Note that PHP floats commonly support precision of roughly 15 digits
anyway.
  • Loading branch information
mudrd8mz committed Dec 21, 2017
1 parent 457eaef commit 521252d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion admin/tool/xmldb/actions/edit_field/edit_field.js
Expand Up @@ -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;
Expand Down
Expand Up @@ -48,6 +48,7 @@ function init() {
'floatincorrectlength' => 'tool_xmldb',
'charincorrectlength' => 'tool_xmldb',
'numberincorrectdecimals' => 'tool_xmldb',
'numberincorrectwholepart' => 'tool_xmldb',
'floatincorrectdecimals' => 'tool_xmldb',
'defaultincorrect' => 'tool_xmldb',
'back' => 'tool_xmldb',
Expand Down Expand Up @@ -158,6 +159,9 @@ function invoke() {
$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'];
Expand Down
1 change: 1 addition & 0 deletions admin/tool/xmldb/lang/en/tool_xmldb.php
Expand Up @@ -165,6 +165,7 @@
$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.';
Expand Down
7 changes: 7 additions & 0 deletions lib/upgrade.txt
@@ -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:
Expand Down
8 changes: 6 additions & 2 deletions lib/xmldb/xmldb_field.php
Expand Up @@ -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
Expand Down Expand Up @@ -795,6 +795,10 @@ public function validateDefinition(xmldb_table $xmldb_table=null) {
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';
Expand Down

0 comments on commit 521252d

Please sign in to comment.