]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Some cleanup on detecting types
authorBrion Vibber <brion@pobox.com>
Thu, 7 Oct 2010 23:49:49 +0000 (16:49 -0700)
committerBrion Vibber <brion@pobox.com>
Thu, 7 Oct 2010 23:49:49 +0000 (16:49 -0700)
lib/mysqlschema.php
lib/schema.php

index 9ff7932364fbdcffd4ca50973fa2a916192b24fe..d3b463c1039d0d4e0f0c1f2c99f1485918f91ec7 100644 (file)
@@ -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') {
index d20bced4a740e056deab2b183c921adf05c643c9..5868627ed74e7ff5bfb02bfbd024d249c3171d65 100644 (file)
@@ -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