forked from GNUsocial/gnu-social
Some cleanup on detecting types
This commit is contained in:
parent
82e9a2eee8
commit
14e4f50f29
@ -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 ($type == 'char' || $type == 'varchar') {
|
||||
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 ($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') {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user