summaryrefslogtreecommitdiff
path: root/lib/mysqlschema.php
diff options
context:
space:
mode:
authorBrion Vibber <brion@pobox.com>2010-10-07 16:49:49 -0700
committerBrion Vibber <brion@pobox.com>2010-10-07 16:49:49 -0700
commit14e4f50f2999e0bf160ec90a5b26b29b48fcf776 (patch)
treee74bd20b19376b2f61e89930233d27a714481fe2 /lib/mysqlschema.php
parent82e9a2eee8ca7b71f6ab81f6c787997e01b75492 (diff)
Some cleanup on detecting types
Diffstat (limited to 'lib/mysqlschema.php')
-rw-r--r--lib/mysqlschema.php61
1 files changed, 52 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') {