diff options
-rw-r--r-- | lib/mysqlschema.php | 61 | ||||
-rw-r--r-- | lib/schema.php | 7 |
2 files changed, 59 insertions, 9 deletions
diff --git a/lib/mysqlschema.php b/lib/mysqlschema.php index 9ff793236..d3b463c10 100644 --- a/lib/mysqlschema.php +++ b/lib/mysqlschema.php @@ -94,23 +94,37 @@ class MysqlSchema extends Schema $name = $row['COLUMN_NAME']; $field = array(); - if ($row['DATA_TYPE'] !== null) { - $field['type'] = $row['DATA_TYPE']; + // warning -- 'unsigned' attr on numbers isn't given in DATA_TYPE and friends. + // It is stuck in on COLUMN_TYPE though (eg 'bigint(20) unsigned') + list($type, $size) = $this->reverseMapType($row['DATA_TYPE']); + $field['type'] = $type; + if ($size !== null) { + $field['size'] = $size; } - if ($row['CHARACTER_MAXIMUM_LENGTH'] !== null) { - $field['length'] = intval($row['CHARACTER_MAXIMUM_LENGTH']); - } - if ($row['NUMERIC_PRECISION'] !== null) { - $field['precision'] = intval($row['NUMERIC_PRECISION']); + + if ($type == 'char' || $type == 'varchar') { + if ($row['CHARACTER_MAXIMUM_LENGTH'] !== null) { + $field['length'] = intval($row['CHARACTER_MAXIMUM_LENGTH']); + } } - if ($row['NUMERIC_SCALE'] !== null) { - $field['scale'] = intval($row['NUMERIC_SCALE']); + if ($type == 'numeric') { + // Other int types may report these values, but they're irrelevant. + // Just ignore them! + if ($row['NUMERIC_PRECISION'] !== null) { + $field['precision'] = intval($row['NUMERIC_PRECISION']); + } + if ($row['NUMERIC_SCALE'] !== null) { + $field['scale'] = intval($row['NUMERIC_SCALE']); + } } if ($row['IS_NULLABLE'] == 'NO') { $field['not null'] = true; } if ($row['COLUMN_DEFAULT'] !== null) { $field['default'] = $row['COLUMN_DEFAULT']; + if ($this->isNumericType($type)) { + $field['default'] = intval($field['default']); + } } if ($row['COLUMN_KEY'] !== null) { // We'll need to look up key info... @@ -562,6 +576,35 @@ class MysqlSchema extends Schema return $type; } + /** + * Map a MySQL native type back to an independent type + size + * + * @param string $type + * @return array ($type, $size) -- $size may be null + */ + protected function reverseMapType($type) + { + $type = strtolower($type); + $map = array( + 'decimal' => array('numeric', null), + 'tinyint' => array('int', 'tiny'), + 'smallint' => array('int', 'small'), + 'mediumint' => array('int', 'medium'), + 'bigint' => array('int', 'big'), + 'tinyblob' => array('blob', 'tiny'), + 'mediumblob' => array('blob', 'medium'), + 'longblob' => array('blob', 'long'), + 'tinytext' => array('text', 'tiny'), + 'mediumtext' => array('text', 'medium'), + 'longtext' => array('text', 'long'), + ); + if (isset($map[$type])) { + return $map[$type]; + } else { + return array($type, null); + } + } + function typeAndSize($column) { if ($column['type'] == 'enum') { diff --git a/lib/schema.php b/lib/schema.php index d20bced4a..5868627ed 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -583,6 +583,13 @@ class Schema return $table; } + + function isNumericType($type) + { + $type = strtolower($type); + $known = array('int', 'serial', 'numeric'); + return in_array($type, $known); + } } class SchemaTableMissingException extends Exception |