--- admin/xmldb/actions/check_bigints/check_bigints.class.php
+++ admin/xmldb/actions/check_bigints/check_bigints.class.php
@@ -31,6 +31,36 @@
class check_bigints extends XMLDBAction {
+ function correct_type($length) {
+ global $CFG;
+
+ static $cache;
+
+ if (isset($cache[$length]))
+ return $cache[$length];
+
+ switch ($CFG->dbfamily) {
+ case 'mysql':
+ if (10 == $length)
+ return ($cache[$length] = 'bigint');
+
+ break;
+ case 'postgres':
+ if (1 <= $length && $length <= 4)
+ return ($cache[$length] = 'int2');
+
+ if (5 <= $length && $length <= 8)
+ return ($cache[$length] = 'int4');
+
+ if (10 <= $length && $length <= 20)
+ return ($cache[$length] = 'int8');
+
+ break;
+ }
+
+ return NULL;
+ }
+
/**
* Init method, every subclass will have its own
*/
@@ -84,18 +114,6 @@
/// Here we'll acummulate all the wrong fields found
$wrong_fields = array();
- /// Correct fields must be type bigint for MySQL and int8 for PostgreSQL
- switch ($CFG->dbfamily) {
- case 'mysql':
- $correct_type = 'bigint';
- break;
- case 'postgres':
- $correct_type = 'int8';
- break;
- default:
- $correct_type = NULL;
- }
-
/// Do the job, setting $result as needed
/// Get the confirmed to decide what to do
@@ -181,7 +199,7 @@
$o.='
';
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
@@ -189,6 +207,7 @@
continue;
}
/// To variable for better handling
+ $correct_type = $this->correct_type($xmldb_field->getLength());
$metacolumn = $metacolumns[$xmldb_field->getName()];
/// Going to check this field in DB
$o.=' - ' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
@@ -199,6 +218,7 @@
$obj = new object;
$obj->table = $xmldb_table;
$obj->field = $xmldb_field;
+ $obj->type = $correct_type; // store correct type until PostgreSQL better supports getAlterFieldSQL()
$wrong_fields[] = $obj;
} else {
$o.='' . $this->str['ok'] . '';
@@ -238,7 +258,7 @@
/// PostgreSQL (XMLDB implementation) is a bit, er... imperfect.
} else if ($CFG->dbfamily == 'postgres') {
$sqlarr = array('ALTER TABLE ' . $CFG->prefix . $xmldb_table->getName() .
- ' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE BIGINT;');
+ ' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE '. $obj->type .';');
}
$r.= '
- ' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['field'] . ': ' . $xmldb_field->getName() . '
';