Improve support of enum data type

This commit is contained in:
bancer 2018-12-12 12:40:20 +01:00
parent acb0436cb6
commit cf6c8d511f
3 changed files with 33 additions and 37 deletions

View file

@ -420,6 +420,7 @@ class CakeSchema extends CakeObject {
$type = $value; $type = $value;
$value = array('type' => $type); $value = array('type' => $type);
} }
$value['type'] = addslashes($value['type']);
$col = "\t\t'{$field}' => array('type' => '" . $value['type'] . "', "; $col = "\t\t'{$field}' => array('type' => '" . $value['type'] . "', ";
unset($value['type']); unset($value['type']);
$col .= implode(', ', $this->_values($value)); $col .= implode(', ', $this->_values($value));
@ -624,17 +625,19 @@ class CakeSchema extends CakeObject {
if ($Obj->primaryKey === $name && !$hasPrimaryAlready && !isset($value['key'])) { if ($Obj->primaryKey === $name && !$hasPrimaryAlready && !isset($value['key'])) {
$value['key'] = 'primary'; $value['key'] = 'primary';
} }
if (!isset($db->columns[$value['type']])) { if (substr($value['type'], 0, 4) !== 'enum') {
trigger_error(__d('cake_dev', 'Schema generation error: invalid column type %s for %s.%s does not exist in DBO', $value['type'], $Obj->name, $name), E_USER_NOTICE); if (!isset($db->columns[$value['type']])) {
continue; trigger_error(__d('cake_dev', 'Schema generation error: invalid column type %s for %s.%s does not exist in DBO', $value['type'], $Obj->name, $name), E_USER_NOTICE);
} else { continue;
$defaultCol = $db->columns[$value['type']]; } else {
if (isset($defaultCol['limit']) && $defaultCol['limit'] == $value['length']) { $defaultCol = $db->columns[$value['type']];
unset($value['length']); if (isset($defaultCol['limit']) && $defaultCol['limit'] == $value['length']) {
} elseif (isset($defaultCol['length']) && $defaultCol['length'] == $value['length']) { unset($value['length']);
unset($value['length']); } elseif (isset($defaultCol['length']) && $defaultCol['length'] == $value['length']) {
unset($value['length']);
}
unset($value['limit']);
} }
unset($value['limit']);
} }
if (isset($value['default']) && ($value['default'] === '' || ($value['default'] === false && $value['type'] !== 'boolean'))) { if (isset($value['default']) && ($value['default'] === '' || ($value['default'] === false && $value['type'] !== 'boolean'))) {

View file

@ -120,6 +120,7 @@ class Mysql extends DboSource {
'primary_key' => array('name' => 'NOT NULL AUTO_INCREMENT'), 'primary_key' => array('name' => 'NOT NULL AUTO_INCREMENT'),
'string' => array('name' => 'varchar', 'limit' => '255'), 'string' => array('name' => 'varchar', 'limit' => '255'),
'text' => array('name' => 'text'), 'text' => array('name' => 'text'),
'enum' => array('name' => 'enum'),
'biginteger' => array('name' => 'bigint', 'limit' => '20'), 'biginteger' => array('name' => 'bigint', 'limit' => '20'),
'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'), 'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
'smallinteger' => array('name' => 'smallint', 'limit' => '6', 'formatter' => 'intval'), 'smallinteger' => array('name' => 'smallint', 'limit' => '6', 'formatter' => 'intval'),

View file

@ -3282,18 +3282,7 @@ class DboSource extends DataSource {
return (int)$length; return (int)$length;
} }
if (in_array($type, array('enum', 'set'))) { if (in_array($type, array('enum', 'set'))) {
$values = array_map(function ($value) { return null;
return trim(trim($value), '\'"');
}, explode(',', $length));
$maxLength = 0;
foreach ($values as $key => $enumValue) {
$tmpLength = strlen($enumValue);
if ($tmpLength > $maxLength) {
$maxLength = $tmpLength;
}
}
return $maxLength;
} }
return (int)$length; return (int)$length;
} }
@ -3511,25 +3500,28 @@ class DboSource extends DataSource {
return null; return null;
} }
if (!isset($this->columns[$type])) { if (!isset($this->columns[$type]) && substr($type, 0, 4) !== 'enum') {
trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING); trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING);
return null; return null;
} }
$real = $this->columns[$type]; if (substr($type, 0, 4) === 'enum') {
$out = $this->name($name) . ' ' . $real['name']; $out = $this->name($name) . ' ' . $type;
} else {
if (isset($column['length'])) { $real = $this->columns[$type];
$length = $column['length']; $out = $this->name($name) . ' ' . $real['name'];
} elseif (isset($column['limit'])) { if (isset($column['length'])) {
$length = $column['limit']; $length = $column['length'];
} elseif (isset($real['length'])) { } elseif (isset($column['limit'])) {
$length = $real['length']; $length = $column['limit'];
} elseif (isset($real['limit'])) { } elseif (isset($real['length'])) {
$length = $real['limit']; $length = $real['length'];
} } elseif (isset($real['limit'])) {
if (isset($length)) { $length = $real['limit'];
$out .= '(' . $length . ')'; }
if (isset($length)) {
$out .= '(' . $length . ')';
}
} }
if (($column['type'] === 'integer' || $column['type'] === 'float') && isset($column['default']) && $column['default'] === '') { if (($column['type'] === 'integer' || $column['type'] === 'float') && isset($column['default']) && $column['default'] === '') {