mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #12808 from bancer/2.x-issue-12804-generate-shell-enum
Improve support of enum data type
This commit is contained in:
commit
0471d7a9ca
6 changed files with 77 additions and 47 deletions
|
@ -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,6 +625,7 @@ 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 (substr($value['type'], 0, 4) !== 'enum') {
|
||||||
if (!isset($db->columns[$value['type']])) {
|
if (!isset($db->columns[$value['type']])) {
|
||||||
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);
|
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);
|
||||||
continue;
|
continue;
|
||||||
|
@ -636,6 +638,7 @@ class CakeSchema extends CakeObject {
|
||||||
}
|
}
|
||||||
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'))) {
|
||||||
unset($value['default']);
|
unset($value['default']);
|
||||||
|
|
|
@ -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'),
|
||||||
|
|
|
@ -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,14 +3500,16 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (substr($type, 0, 4) === 'enum') {
|
||||||
|
$out = $this->name($name) . ' ' . $type;
|
||||||
|
} else {
|
||||||
$real = $this->columns[$type];
|
$real = $this->columns[$type];
|
||||||
$out = $this->name($name) . ' ' . $real['name'];
|
$out = $this->name($name) . ' ' . $real['name'];
|
||||||
|
|
||||||
if (isset($column['length'])) {
|
if (isset($column['length'])) {
|
||||||
$length = $column['length'];
|
$length = $column['length'];
|
||||||
} elseif (isset($column['limit'])) {
|
} elseif (isset($column['limit'])) {
|
||||||
|
@ -3531,6 +3522,7 @@ class DboSource extends DataSource {
|
||||||
if (isset($length)) {
|
if (isset($length)) {
|
||||||
$out .= '(' . $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'] === '') {
|
||||||
$column['default'] = null;
|
$column['default'] = null;
|
||||||
|
|
|
@ -64,6 +64,13 @@ class MyAppSchema extends CakeSchema {
|
||||||
'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
|
'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
|
||||||
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||||
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
||||||
|
'status' => array(
|
||||||
|
'type' => 'enum(\'active\',\'deleted\')',
|
||||||
|
'null' => false,
|
||||||
|
'default' => 'active',
|
||||||
|
'collate' => 'utf8_unicode_ci',
|
||||||
|
'charset' => 'utf8',
|
||||||
|
),
|
||||||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -809,6 +816,14 @@ class CakeSchemaTest extends CakeTestCase {
|
||||||
'posts' => array(
|
'posts' => array(
|
||||||
'add' => array(
|
'add' => array(
|
||||||
'summary' => array('type' => 'text', 'null' => true, 'after' => 'body'),
|
'summary' => array('type' => 'text', 'null' => true, 'after' => 'body'),
|
||||||
|
'status' => array(
|
||||||
|
'type' => 'enum(\'active\',\'deleted\')',
|
||||||
|
'null' => false,
|
||||||
|
'default' => 'active',
|
||||||
|
'collate' => 'utf8_unicode_ci',
|
||||||
|
'charset' => 'utf8',
|
||||||
|
'after' => 'updated',
|
||||||
|
),
|
||||||
),
|
),
|
||||||
'drop' => array(
|
'drop' => array(
|
||||||
'tableParameters' => array(),
|
'tableParameters' => array(),
|
||||||
|
|
|
@ -3080,14 +3080,6 @@ SQL;
|
||||||
$expected = '5,2';
|
$expected = '5,2';
|
||||||
$this->assertSame($expected, $result);
|
$this->assertSame($expected, $result);
|
||||||
|
|
||||||
$result = $this->Dbo->length("enum('test','me','now')");
|
|
||||||
$expected = 4;
|
|
||||||
$this->assertSame($expected, $result);
|
|
||||||
|
|
||||||
$result = $this->Dbo->length("set('a','b','cd')");
|
|
||||||
$expected = 2;
|
|
||||||
$this->assertSame($expected, $result);
|
|
||||||
|
|
||||||
$result = $this->Dbo->length(false);
|
$result = $this->Dbo->length(false);
|
||||||
$this->assertNull($result);
|
$this->assertNull($result);
|
||||||
|
|
||||||
|
@ -3100,6 +3092,26 @@ SQL;
|
||||||
$this->assertSame($expected, $result);
|
$this->assertSame($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the length of enum column.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testLengthEnum() {
|
||||||
|
$result = $this->Dbo->length("enum('test','me','now')");
|
||||||
|
$this->assertNull($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the length of set column.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testLengthSet() {
|
||||||
|
$result = $this->Dbo->length("set('a','b','cd')");
|
||||||
|
$this->assertNull($result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* testBuildIndex method
|
* testBuildIndex method
|
||||||
*
|
*
|
||||||
|
|
|
@ -2177,12 +2177,19 @@ class DboSourceTest extends CakeTestCase {
|
||||||
|
|
||||||
$result = $this->db->length('decimal(20,3)');
|
$result = $this->db->length('decimal(20,3)');
|
||||||
$this->assertEquals('20,3', $result);
|
$this->assertEquals('20,3', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test length parsing of enum column.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testLengthEnum() {
|
||||||
$result = $this->db->length('enum("one", "longer")');
|
$result = $this->db->length('enum("one", "longer")');
|
||||||
$this->assertEquals(6, $result);
|
$this->assertNull($result);
|
||||||
|
|
||||||
$result = $this->db->length("enum('One Value','ANOTHER ... VALUE ...')");
|
$result = $this->db->length("enum('One Value','ANOTHER ... VALUE ...')");
|
||||||
$this->assertEquals(21, $result);
|
$this->assertNull($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue