Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'w41_MDL-35691_m24_intsizes' of git://github.com/skodak/…

…moodle
  • Loading branch information...
commit f374702e75b6caa0947be92c88bbb0d87a751384 2 parents 0b85584 + 2b7fa4c
@stronk7 stronk7 authored
View
37 admin/tool/xmldb/actions/check_bigints/check_bigints.class.php
@@ -29,9 +29,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class check_bigints extends XMLDBCheckAction {
- private $correct_type;
- private $dbfamily;
-
/**
* Init method, every subclass will have its own
*/
@@ -51,19 +48,6 @@ function init() {
'nowrongintsfound' => 'tool_xmldb',
'yeswrongintsfound' => 'tool_xmldb',
));
-
- // Correct fields must be type bigint for MySQL and int8 for PostgreSQL
- $this->dbfamily = $DB->get_dbfamily();
- switch ($this->dbfamily) {
- case 'mysql':
- $this->correct_type = 'bigint';
- break;
- case 'postgres':
- $this->correct_type = 'int8';
- break;
- default:
- $this->correct_type = NULL;
- }
}
protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
@@ -75,19 +59,24 @@ protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
$o.=' <ul>';
foreach ($xmldb_fields as $xmldb_field) {
// If the field isn't integer(10), skip
- if ($xmldb_field->getType() != XMLDB_TYPE_INTEGER || $xmldb_field->getLength() != 10) {
+ if ($xmldb_field->getType() != XMLDB_TYPE_INTEGER) {
continue;
}
// If the metadata for that column doesn't exist, skip
if (!isset($metacolumns[$xmldb_field->getName()])) {
continue;
}
+ $minlength = $xmldb_field->getLength();
+ if ($minlength > 18) {
+ // Anything above 18 is borked, just ignore it here.
+ $minlength = 18;
+ }
// To variable for better handling
$metacolumn = $metacolumns[$xmldb_field->getName()];
// Going to check this field in DB
$o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
// Detect if the physical field is wrong
- if ($metacolumn->type != $this->correct_type) {
+ if (($metacolumn->meta_type != 'I' and $metacolumn->meta_type != 'R') or $metacolumn->max_length < $minlength) {
$o.='<font color="red">' . $this->str['wrong'] . '</font>';
// Add the wrong field to the list
$obj = new stdClass();
@@ -124,17 +113,7 @@ protected function display_results(array $wrong_fields) {
foreach ($wrong_fields as $obj) {
$xmldb_table = $obj->table;
$xmldb_field = $obj->field;
- // MySQL directly supports this
-
-// TODO: move this hack to generators!!
-
- if ($this->dbfamily == 'mysql') {
- $sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
- // PostgreSQL (XMLDB implementation) is a bit, er... imperfect.
- } else if ($this->dbfamily == 'postgres') {
- $sqlarr = array('ALTER TABLE ' . $DB->get_prefix() . $xmldb_table->getName() .
- ' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE BIGINT;');
- }
+ $sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['field'] . ': ' . $xmldb_field->getName() . '</li>';
// Add to output if we have sentences
View
2  admin/tool/xmldb/lang/en/tool_xmldb.php
@@ -101,7 +101,7 @@
$string['gotolastused'] = 'Go to last used file';
$string['change'] = 'Change';
$string['charincorrectlength'] = 'Incorrect length for char field';
-$string['checkbigints'] = 'Check bigints';
+$string['checkbigints'] = 'Check integers';
$string['check_bigints'] = 'Look for incorrect DB integers';
$string['checkdefaults'] = 'Check defaults';
$string['check_defaults'] = 'Look for inconsistent default values';
View
8 lib/db/install.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20120926" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20121002" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
@@ -140,7 +140,7 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="course"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="criteriatype"/>
- <FIELD NAME="criteriatype" TYPE="int" LENGTH="20" NOTNULL="false" SEQUENCE="false" COMMENT="The criteria type we are aggregating, or NULL if complete course aggregation" PREVIOUS="course" NEXT="method"/>
+ <FIELD NAME="criteriatype" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The criteria type we are aggregating, or NULL if complete course aggregation" PREVIOUS="course" NEXT="method"/>
<FIELD NAME="method" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="1 = all, 2 = any, 3 = fraction, 4 = unit" PREVIOUS="criteriatype" NEXT="value"/>
<FIELD NAME="value" TYPE="number" LENGTH="10" NOTNULL="false" SEQUENCE="false" DECIMALS="5" COMMENT="NULL = all/any, 0..1 for method 'fraction', &gt; 0 for method 'unit'" PREVIOUS="method"/>
</FIELDS>
@@ -157,7 +157,7 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="course"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="criteriatype"/>
- <FIELD NAME="criteriatype" TYPE="int" LENGTH="20" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Type of criteria" PREVIOUS="course" NEXT="module"/>
+ <FIELD NAME="criteriatype" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Type of criteria" PREVIOUS="course" NEXT="module"/>
<FIELD NAME="module" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="Type of module (if using module criteria type)" PREVIOUS="criteriatype" NEXT="moduleinstance"/>
<FIELD NAME="moduleinstance" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Module instance id (if using module criteria type)" PREVIOUS="module" NEXT="courseinstance"/>
<FIELD NAME="courseinstance" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Course instance id (if using course criteria type)" PREVIOUS="moduleinstance" NEXT="enrolperiod"/>
@@ -2598,7 +2598,7 @@
<FIELD NAME="externalserviceid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" PREVIOUS="userid" NEXT="sid"/>
<FIELD NAME="sid" TYPE="char" LENGTH="128" NOTNULL="false" SEQUENCE="false" COMMENT="link to browser or emulated session" PREVIOUS="externalserviceid" NEXT="contextid"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="context id where in token valid" PREVIOUS="sid" NEXT="creatorid"/>
- <FIELD NAME="creatorid" TYPE="int" LENGTH="20" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="user id of the token creator (useful to know when the administrator created a token and so display the token to a specific administrator)" PREVIOUS="contextid" NEXT="iprestriction"/>
+ <FIELD NAME="creatorid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="user id of the token creator (useful to know when the administrator created a token and so display the token to a specific administrator)" PREVIOUS="contextid" NEXT="iprestriction"/>
<FIELD NAME="iprestriction" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="ip restriction" PREVIOUS="creatorid" NEXT="validuntil"/>
<FIELD NAME="validuntil" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="timestampt - valid until data" PREVIOUS="iprestriction" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="created timestamp" PREVIOUS="validuntil" NEXT="lastaccess"/>
View
32 lib/dml/mysqli_native_moodle_database.php
@@ -570,7 +570,18 @@ public function get_columns($table, $usecache=true) {
} else if (preg_match('/([a-z]*int[a-z]*)\((\d+)\)/i', $rawcolumn->column_type, $matches)) {
$rawcolumn->data_type = $matches[1];
- $rawcolumn->character_maximum_length = $matches[2];
+ // Return number of decimals, not bytes here.
+ if ($matches[2] >= 8) {
+ $rawcolumn->max_length = 18;
+ } else if ($matches[2] >= 4) {
+ $rawcolumn->max_length = 9;
+ } else if ($matches[2] >= 2) {
+ $rawcolumn->max_length = 4;
+ } else if ($matches[2] >= 1) {
+ $rawcolumn->max_length = 2;
+ } else {
+ $rawcolumn->max_length = 0;
+ }
} else if (preg_match('/(decimal)\((\d+),(\d+)\)/i', $rawcolumn->column_type, $matches)) {
$rawcolumn->data_type = $matches[1];
@@ -631,7 +642,26 @@ private function get_column_info(stdClass $rawcolumn) {
$info->meta_type = 'R';
$info->unique = true;
}
+ // Return number of decimals, not bytes here.
$info->max_length = $rawcolumn->numeric_precision;
+ if (preg_match('/([a-z]*int[a-z]*)\((\d+)\)/i', $rawcolumn->column_type, $matches)) {
+ if ($matches[2] >= 8) {
+ $maxlength = 18;
+ } else if ($matches[2] >= 4) {
+ $maxlength = 9;
+ } else if ($matches[2] >= 2) {
+ $maxlength = 4;
+ } else if ($matches[2] >= 1) {
+ $maxlength = 2;
+ } else {
+ $maxlength = 0;
+ }
+ // It is possible that display precision is different from storage type length,
+ // always use the smaller value to make sure our data fits.
+ if ($maxlength < $info->max_length) {
+ $info->max_length = $maxlength;
+ }
+ }
$info->unsigned = (stripos($rawcolumn->column_type, 'unsigned') !== false);
$info->auto_increment= (strpos($rawcolumn->extra, 'auto_increment') !== false);
View
13 lib/dml/pgsql_native_moodle_database.php
@@ -463,7 +463,18 @@ public function get_columns($table, $usecache=true) {
$info->auto_increment= false;
$info->has_default = ($rawcolumn->atthasdef === 't');
}
- $info->max_length = $matches[1];
+ // Return number of decimals, not bytes here.
+ if ($matches[1] >= 8) {
+ $info->max_length = 18;
+ } else if ($matches[1] >= 4) {
+ $info->max_length = 9;
+ } else if ($matches[1] >= 2) {
+ $info->max_length = 4;
+ } else if ($matches[1] >= 1) {
+ $info->max_length = 2;
+ } else {
+ $info->max_length = 0;
+ }
$info->scale = null;
$info->not_null = ($rawcolumn->attnotnull === 't');
if ($info->has_default) {
View
20 lib/dml/tests/dml_test.php
@@ -677,6 +677,17 @@ public function test_get_columns() {
$table->add_field('negativedfltint', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '-1');
$table->add_field('negativedfltnumber', XMLDB_TYPE_NUMBER, '10', null, XMLDB_NOTNULL, null, '-2');
$table->add_field('negativedfltfloat', XMLDB_TYPE_FLOAT, '10', null, XMLDB_NOTNULL, null, '-3');
+ $table->add_field('someint1', XMLDB_TYPE_INTEGER, '1', null, null, null, '0');
+ $table->add_field('someint2', XMLDB_TYPE_INTEGER, '2', null, null, null, '0');
+ $table->add_field('someint3', XMLDB_TYPE_INTEGER, '3', null, null, null, '0');
+ $table->add_field('someint4', XMLDB_TYPE_INTEGER, '4', null, null, null, '0');
+ $table->add_field('someint5', XMLDB_TYPE_INTEGER, '5', null, null, null, '0');
+ $table->add_field('someint6', XMLDB_TYPE_INTEGER, '6', null, null, null, '0');
+ $table->add_field('someint7', XMLDB_TYPE_INTEGER, '7', null, null, null, '0');
+ $table->add_field('someint8', XMLDB_TYPE_INTEGER, '8', null, null, null, '0');
+ $table->add_field('someint9', XMLDB_TYPE_INTEGER, '9', null, null, null, '0');
+ $table->add_field('someint10', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
+ $table->add_field('someint18', XMLDB_TYPE_INTEGER, '18', null, null, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
@@ -698,6 +709,15 @@ public function test_get_columns() {
$this->assertEquals(0, $field->default_value);
$this->assertTrue($field->not_null);
+ for($i=1;$i<=10;$i++) {
+ $field = $columns['someint'.$i];
+ $this->assertEquals('I', $field->meta_type);
+ $this->assertGreaterThanOrEqual($i, $field->max_length);
+ }
+ $field = $columns['someint18'];
+ $this->assertEquals('I', $field->meta_type);
+ $this->assertGreaterThanOrEqual(18, $field->max_length);
+
$field = $columns['name'];
$this->assertEquals('C', $field->meta_type);
$this->assertFalse($field->auto_increment);
Please sign in to comment.
Something went wrong with that request. Please try again.