$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...
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') {