mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 04:52:36 +02:00
MDL-32112 validate integer definition in sql_generator
This commit is contained in:
parent
f9090b195b
commit
720d605e61
@ -402,6 +402,38 @@ class ddl_test extends UnitTestCase {
|
||||
$this->assertIdentical(get_class($e), 'coding_exception');
|
||||
}
|
||||
|
||||
// Invalid integer length
|
||||
$table = new xmldb_table('test_table4');
|
||||
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
|
||||
$table->add_field('course', XMLDB_TYPE_INTEGER, '21', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
|
||||
$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 (Exception $e) {
|
||||
$this->assertIdentical(get_class($e), 'coding_exception');
|
||||
}
|
||||
|
||||
// Invalid integer default
|
||||
$table = new xmldb_table('test_table4');
|
||||
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
|
||||
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'x');
|
||||
$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 (Exception $e) {
|
||||
$this->assertIdentical(get_class($e), 'coding_exception');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -715,6 +715,14 @@ 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';
|
||||
}
|
||||
$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';
|
||||
}
|
||||
break;
|
||||
|
||||
case XMLDB_TYPE_NUMBER:
|
||||
|
Loading…
x
Reference in New Issue
Block a user