MDL-32112 use constants for field and table limits

For now using the same JS hardcoding trick Eloy did the last time...
This commit is contained in:
Petr Skoda 2012-03-20 19:30:45 +01:00
parent 7fd31fe86c
commit 436b961381
5 changed files with 56 additions and 23 deletions

View File

@ -74,24 +74,24 @@ function transformForm(event) {
// Based on type, disable some items
switch (typeField.value) {
case '1': // XMLDB_TYPE_INTEGER
lengthTip.innerHTML = ' 1...20';
lengthTip.innerHTML = ' 1...20'; // Hardcoded xmldb_field::INTEGER_MAX_LENGTH, yes!
lengthField.disabled = false;
decimalsTip.innerHTML = '';
decimalsField.disabled = true;
decimalsField.value = '';
break;
case '2': // XMLDB_TYPE_NUMBER
lengthTip.innerHTML = ' 1...20';
lengthTip.innerHTML = ' 1...20'; // Hardcoded xmldb_field::NUMBER_MAX_LENGTH, yes!
lengthField.disabled = false;
decimalsTip.innerHTML = ' 0...length or empty';
break;
case '3': // XMLDB_TYPE_FLOAT
lengthTip.innerHTML = ' 1...20 or empty';
lengthTip.innerHTML = ' 1...20 or empty'; // Hardcoded xmldb_field::FLOAT_MAX_LENGTH, yes!
lengthField.disabled = false;
decimalsTip.innerHTML = ' 0...length or empty';
break;
case '4': // XMLDB_TYPE_CHAR
lengthTip.innerHTML = ' 1...1333'; // Hardcoded, yes!
lengthTip.innerHTML = ' 1...1333'; // Hardcoded xmldb_field::CHAR_MAX_LENGTH, yes!
lengthField.disabled = false;
decimalsTip.innerHTML = '';
decimalsField.disabled = true;

View File

@ -86,7 +86,7 @@ class edit_field_save extends XMLDBAction {
$tableparam = strtolower(required_param('table', PARAM_PATH));
$fieldparam = strtolower(required_param('field', PARAM_PATH));
$name = substr(trim(strtolower(optional_param('name', $fieldparam, PARAM_PATH))),0,30);
$name = substr(trim(strtolower(optional_param('name', $fieldparam, PARAM_PATH))),0,xmldb_field::NAME_MAX_LENGTH);
$comment = required_param('comment', PARAM_CLEAN);
$comment = trim($comment);
@ -138,7 +138,7 @@ class edit_field_save extends XMLDBAction {
// Integer checks
if ($type == XMLDB_TYPE_INTEGER) {
if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) &&
$length > 0 && $length <= 20)) {
$length > 0 && $length <= xmldb_field::INTEGER_MAX_LENGTH)) {
$errors[] = $this->str['integerincorrectlength'];
}
if (!(empty($default) || (is_numeric($default) &&
@ -150,7 +150,7 @@ class edit_field_save extends XMLDBAction {
// Number checks
if ($type == XMLDB_TYPE_NUMBER) {
if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) &&
$length > 0 && $length <= 20)) {
$length > 0 && $length <= xmldb_field::NUMBER_MAX_LENGTH)) {
$errors[] = $this->str['numberincorrectlength'];
}
if (!(empty($decimals) || (is_numeric($decimals) &&
@ -171,7 +171,7 @@ class edit_field_save extends XMLDBAction {
!empty($length) &&
intval($length)==floatval($length) &&
$length > 0 &&
$length <= 20))) {
$length <= xmldb_field::FLOAT_MAX_LENGTH))) {
$errors[] = $this->str['floatincorrectlength'];
}
if (!(empty($decimals) || (is_numeric($decimals) &&

View File

@ -77,7 +77,7 @@ class edit_table_save extends XMLDBAction {
$dirpath = $CFG->dirroot . $dirpath;
$tableparam = strtolower(required_param('table', PARAM_PATH));
$name = substr(trim(strtolower(required_param('name', PARAM_PATH))),0,28);
$name = substr(trim(strtolower(required_param('name', PARAM_PATH))),0,xmldb_table::NAME_MAX_LENGTH);
$comment = required_param('comment', PARAM_CLEAN);
$comment = $comment;

View File

@ -46,6 +46,30 @@ class xmldb_field extends xmldb_object {
*/
const CHAR_MAX_LENGTH = 1333;
/**
* @const maximum number of digits of integers
*/
const INTEGER_MAX_LENGTH = 20;
/**
* @const max length of decimals
*/
const NUMBER_MAX_LENGTH = 20;
/**
* @const max length of floats
*/
const FLOAT_MAX_LENGTH = 20;
/**
* Note:
* - Oracle has 30 chars limit for all names
*
* @const maximumn length of field names
*/
const NAME_MAX_LENGTH = 30;
/**
* Creates one new xmldb_field
*/
@ -705,9 +729,9 @@ class xmldb_field extends xmldb_object {
}
$name = $this->getName();
if (strlen($name) > 30) {
if (strlen($name) > self::NAME_MAX_LENGTH) {
return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name is too long.'
.' Limit is 30 chars.';
.' Limit is '.self::NAME_MAX_LENGTH.' chars.';
}
if (!preg_match('/^[a-z][a-z0-9_]*$/', $name)) {
return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name includes invalid characters.';
@ -716,50 +740,50 @@ class xmldb_field extends xmldb_object {
switch ($this->getType()) {
case XMLDB_TYPE_INTEGER:
$length = $this->getLength();
if (!is_number($length) or $length <= 0 or $length > 20) {
return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid length';
if (!is_number($length) or $length <= 0 or $length > self::INTEGER_MAX_LENGTH) {
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid length';
}
$default = $this->getDefault();
if (!empty($default) and !is_number($default)) {
return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid default';
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid default';
}
break;
case XMLDB_TYPE_NUMBER:
$maxlength = 20;
$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';
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid length';
}
$decimals = $this->getDecimals();
$decimals = empty($decimals) ? 0 : $decimals; // fix missing decimals
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';
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid decimals';
}
$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';
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid default';
}
break;
case XMLDB_TYPE_FLOAT:
$length = $this->getLength();
$length = empty($length) ? 6 : $length; // weird, it might be better to require something here...
if (!is_number($length) or $length <= 0 or $length > 20) {
return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid length';
if (!is_number($length) or $length <= 0 or $length > self::FLOAT_MAX_LENGTH) {
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid length';
}
$decimals = $this->getDecimals();
$decimals = empty($decimals) ? 0 : $decimals; // fix missing decimals
if (!is_number($decimals) or $decimals < 0 or $decimals > $length) {
return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid decimals';
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid decimals';
}
$default = $this->getDefault();
if (!empty($default) and !is_numeric($default)) {
return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid default';
return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid default';
}
break;

View File

@ -32,6 +32,15 @@ class xmldb_table extends xmldb_object {
var $keys;
var $indexes;
/**
* Note:
* - Oracle has 30 chars limit for all names,
* 2 chars are reserved for prefix.
*
* @const maximumn length of field names
*/
const NAME_MAX_LENGTH = 28;
/**
* Creates one new xmldb_table
*/
@ -671,7 +680,7 @@ class xmldb_table extends xmldb_object {
function validateDefinition(xmldb_table $xmldb_table=null) {
// table parameter is ignored
$name = $this->getName();
if (strlen($name) > 28) {
if (strlen($name) > self::NAME_MAX_LENGTH) {
return 'Invalid table name {'.$name.'}: name is too long. Limit is 28 chars.';
}
if (!preg_match('/^[a-z][a-z0-9_]*$/', $name)) {