From 2fcb4c3c6cbdd5e95ef5927d3aa38a6f869d565f Mon Sep 17 00:00:00 2001 From: imsamurai Date: Thu, 14 Nov 2013 01:11:30 +0200 Subject: [PATCH 01/16] - change check to strict for `options` of `$fieldParameters` - add `types` parameter and strict check if it present in `$fieldParameters` (if it present and not contain column type field parameter will be skipped) - add `noVal` parameter to `$fieldParameters` if it present and not empty value of this parameter from column will be ignored - add `unsigned` column type for integer, float and biginteger. If it set to `true` an 'UNSIGNED' will be add in sql column part, if not set or set not to `true` this parameter will be skipped --- lib/Cake/Model/Datasource/Database/Mysql.php | 8 +- lib/Cake/Model/Datasource/DboSource.php | 8 +- .../Model/Datasource/Database/MysqlTest.php | 94 +++++++++++++++++++ 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index 33b44c558..05853669e 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -86,7 +86,13 @@ class Mysql extends DboSource { public $fieldParameters = array( 'charset' => array('value' => 'CHARACTER SET', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'beforeDefault'), 'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => ' ', 'column' => 'Collation', 'position' => 'beforeDefault'), - 'comment' => array('value' => 'COMMENT', 'quote' => true, 'join' => ' ', 'column' => 'Comment', 'position' => 'afterDefault') + 'comment' => array('value' => 'COMMENT', 'quote' => true, 'join' => ' ', 'column' => 'Comment', 'position' => 'afterDefault'), + 'unsigned' => array( + 'value' => 'UNSIGNED', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'afterDefault', + 'noVal' => true, + 'options' => array(true), + 'types' => array('integer', 'float', 'biginteger') + ) ); /** diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 80988b2e2..8aee313b6 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -3142,14 +3142,18 @@ class DboSource extends DataSource { protected function _buildFieldParameters($columnString, $columnData, $position) { foreach ($this->fieldParameters as $paramName => $value) { if (isset($columnData[$paramName]) && $value['position'] == $position) { - if (isset($value['options']) && !in_array($columnData[$paramName], $value['options'])) { + if (isset($value['options']) && !in_array($columnData[$paramName], $value['options'], true)) { continue; } + if (isset($value['types']) && !in_array($columnData['type'], $value['types'], true)) { + continue; + } + $val = $columnData[$paramName]; if ($value['quote']) { $val = $this->value($val); } - $columnString .= ' ' . $value['value'] . $value['join'] . $val; + $columnString .= ' ' . $value['value'] . (empty($value['noVal']) ? $value['join'] . $val : ''); } } return $columnString; diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 95ecaa152..4bac968f2 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3154,6 +3154,100 @@ class MysqlTest extends CakeTestCase { ); $this->Dbo->buildColumn($data); } + + +/** + * testBuildColumn3 method + * + * @param array $data Column data + * @param string $expected Expected sql part + * + * @return void + * + * @dataProvider buildColumn3Provider + */ + public function testBuildColumn3($data, $expected) { + $restore = $this->Dbo->columns; + $this->Dbo->columns = array('string' => 1, 'integer' => 1, 'float' => 1, 'biginteger' => 1, 'any' => 1); + + $result = $this->Dbo->buildColumn($data); + $this->assertEquals($expected, $result); + + $this->Dbo->columns = $restore; + } + +/** + * Data provider testBuildColumn3 method + * + * @return array + */ + public function buildColumn3Provider() { + return array( + //set #0 + array( + array( + 'name' => 'testName', + 'type' => 'integer', + 'unsigned' => true + ), + '`testName` UNSIGNED' + ), + //set #1 + array( + array( + 'name' => 'testName', + 'type' => 'biginteger', + 'unsigned' => true + ), + '`testName` UNSIGNED' + ), + //set #2 + array( + array( + 'name' => 'testName', + 'type' => 'float', + 'unsigned' => true + ), + '`testName` UNSIGNED' + ), + //set #3 + array( + array( + 'name' => 'testName', + 'type' => 'string', + 'unsigned' => true + ), + '`testName` ' + ), + //set #4 + array( + array( + 'name' => 'testName', + 'type' => 'any', + 'unsigned' => true + ), + '`testName` ' + ), + //set #5 + array( + array( + 'name' => 'testName', + 'type' => 'any', + 'unsigned' => false + ), + '`testName` ' + ), + //set #6 + array( + array( + 'name' => 'testName', + 'type' => 'integer', + 'unsigned' => false + ), + '`testName` ' + ) + ); + } /** * test hasAny() From 5a40944a6f22c96b84ee1e5916323ce05ee20bf7 Mon Sep 17 00:00:00 2001 From: imsamurai Date: Thu, 14 Nov 2013 01:20:21 +0200 Subject: [PATCH 02/16] fix test name testBuildColumn3 to testBuildColumnUnsigned --- .../Test/Case/Model/Datasource/Database/MysqlTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 4bac968f2..1d166d1d1 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3157,16 +3157,16 @@ class MysqlTest extends CakeTestCase { /** - * testBuildColumn3 method + * Test `unsigned` field parameter * * @param array $data Column data * @param string $expected Expected sql part * * @return void * - * @dataProvider buildColumn3Provider + * @dataProvider buildColumnUnsignedProvider */ - public function testBuildColumn3($data, $expected) { + public function testBuildColumnUnsigned($data, $expected) { $restore = $this->Dbo->columns; $this->Dbo->columns = array('string' => 1, 'integer' => 1, 'float' => 1, 'biginteger' => 1, 'any' => 1); @@ -3177,11 +3177,11 @@ class MysqlTest extends CakeTestCase { } /** - * Data provider testBuildColumn3 method + * Data provider testBuildColumnUnsigned method * * @return array */ - public function buildColumn3Provider() { + public function buildColumnUnsignedProvider() { return array( //set #0 array( From e35823e72a7ae04fa497ba82c7333752a6d6055c Mon Sep 17 00:00:00 2001 From: imsamurai Date: Thu, 14 Nov 2013 01:33:11 +0200 Subject: [PATCH 03/16] fix tests, add numeric type for unsigned --- lib/Cake/Model/Datasource/Database/Mysql.php | 2 +- .../Model/Datasource/Database/MysqlTest.php | 38 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index 05853669e..547488012 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -91,7 +91,7 @@ class Mysql extends DboSource { 'value' => 'UNSIGNED', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'afterDefault', 'noVal' => true, 'options' => array(true), - 'types' => array('integer', 'float', 'biginteger') + 'types' => array('integer', 'float', 'biginteger', 'numeric') ) ); diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 1d166d1d1..2da1707ad 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3166,14 +3166,9 @@ class MysqlTest extends CakeTestCase { * * @dataProvider buildColumnUnsignedProvider */ - public function testBuildColumnUnsigned($data, $expected) { - $restore = $this->Dbo->columns; - $this->Dbo->columns = array('string' => 1, 'integer' => 1, 'float' => 1, 'biginteger' => 1, 'any' => 1); - + public function testBuildColumnUnsigned($data, $expected) {//debug($this->Dbo->columns); $result = $this->Dbo->buildColumn($data); $this->assertEquals($expected, $result); - - $this->Dbo->columns = $restore; } /** @@ -3188,18 +3183,20 @@ class MysqlTest extends CakeTestCase { array( 'name' => 'testName', 'type' => 'integer', + 'length' => 11, 'unsigned' => true ), - '`testName` UNSIGNED' + '`testName` int(11) UNSIGNED' ), //set #1 array( array( 'name' => 'testName', 'type' => 'biginteger', + 'length' => 20, 'unsigned' => true ), - '`testName` UNSIGNED' + '`testName` bigint(20) UNSIGNED' ), //set #2 array( @@ -3208,43 +3205,54 @@ class MysqlTest extends CakeTestCase { 'type' => 'float', 'unsigned' => true ), - '`testName` UNSIGNED' + '`testName` float UNSIGNED' ), //set #3 array( array( 'name' => 'testName', 'type' => 'string', + 'length' => 255, 'unsigned' => true ), - '`testName` ' + '`testName` varchar(255)' ), //set #4 array( array( 'name' => 'testName', - 'type' => 'any', + 'type' => 'date', 'unsigned' => true ), - '`testName` ' + '`testName` date' ), //set #5 array( array( 'name' => 'testName', - 'type' => 'any', + 'type' => 'date', 'unsigned' => false ), - '`testName` ' + '`testName` date' ), //set #6 array( array( 'name' => 'testName', 'type' => 'integer', + 'length' => 11, 'unsigned' => false ), - '`testName` ' + '`testName` int(11)' + ), + //set #7 + array( + array( + 'name' => 'testName', + 'type' => 'numeric', + 'unsigned' => true + ), + '`testName` decimal UNSIGNED' ) ); } From fd64d952b5f7a9696d5cb4dd073e7806ccfbf519 Mon Sep 17 00:00:00 2001 From: imsamurai Date: Thu, 14 Nov 2013 01:36:27 +0200 Subject: [PATCH 04/16] change position from afterDefault to beforeDefault during to wrong order, add test --- lib/Cake/Model/Datasource/Database/Mysql.php | 2 +- .../Test/Case/Model/Datasource/Database/MysqlTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index 547488012..34252256b 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -88,7 +88,7 @@ class Mysql extends DboSource { 'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => ' ', 'column' => 'Collation', 'position' => 'beforeDefault'), 'comment' => array('value' => 'COMMENT', 'quote' => true, 'join' => ' ', 'column' => 'Comment', 'position' => 'afterDefault'), 'unsigned' => array( - 'value' => 'UNSIGNED', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'afterDefault', + 'value' => 'UNSIGNED', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'beforeDefault', 'noVal' => true, 'options' => array(true), 'types' => array('integer', 'float', 'biginteger', 'numeric') diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 2da1707ad..43c38094f 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3253,6 +3253,16 @@ class MysqlTest extends CakeTestCase { 'unsigned' => true ), '`testName` decimal UNSIGNED' + ), + //set #7 + array( + array( + 'name' => 'testName', + 'type' => 'numeric', + 'unsigned' => true, + 'default' => 1 + ), + '`testName` decimal UNSIGNED DEFAULT 1' ) ); } From 87fef897379c3b08c0fe452e12062d086eae93dd Mon Sep 17 00:00:00 2001 From: imsamurai Date: Thu, 14 Nov 2013 02:19:31 +0200 Subject: [PATCH 05/16] fix phpcs errors --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 43c38094f..23581a806 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3155,7 +3155,6 @@ class MysqlTest extends CakeTestCase { $this->Dbo->buildColumn($data); } - /** * Test `unsigned` field parameter * From 7bc8dac4531ab2132e43aeb51075746a58c7ca45 Mon Sep 17 00:00:00 2001 From: imsamurai Date: Thu, 14 Nov 2013 02:19:41 +0200 Subject: [PATCH 06/16] fix phpcs errors --- lib/Cake/Model/Datasource/DboSource.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 8aee313b6..5b003825f 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -3148,7 +3148,6 @@ class DboSource extends DataSource { if (isset($value['types']) && !in_array($columnData['type'], $value['types'], true)) { continue; } - $val = $columnData[$paramName]; if ($value['quote']) { $val = $this->value($val); From 15805e668efd06ff123b5875638072c8870568a9 Mon Sep 17 00:00:00 2001 From: imsamurai Date: Thu, 14 Nov 2013 02:39:16 +0200 Subject: [PATCH 07/16] fix phpcs errors --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 23581a806..4a76ddbe3 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3154,7 +3154,7 @@ class MysqlTest extends CakeTestCase { ); $this->Dbo->buildColumn($data); } - + /** * Test `unsigned` field parameter * @@ -3169,7 +3169,7 @@ class MysqlTest extends CakeTestCase { $result = $this->Dbo->buildColumn($data); $this->assertEquals($expected, $result); } - + /** * Data provider testBuildColumnUnsigned method * From f1a2c1a75c0424f750f358e7f3770ecfc6927aea Mon Sep 17 00:00:00 2001 From: imsamurai Date: Thu, 14 Nov 2013 10:56:51 +0200 Subject: [PATCH 08/16] add decimal type for unsigned --- lib/Cake/Model/Datasource/Database/Mysql.php | 2 +- .../Test/Case/Model/Datasource/Database/MysqlTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index 34252256b..26f5affea 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -91,7 +91,7 @@ class Mysql extends DboSource { 'value' => 'UNSIGNED', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'beforeDefault', 'noVal' => true, 'options' => array(true), - 'types' => array('integer', 'float', 'biginteger', 'numeric') + 'types' => array('integer', 'float', 'biginteger', 'numeric', 'decimal') ) ); diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 4a76ddbe3..d29a402af 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3262,6 +3262,15 @@ class MysqlTest extends CakeTestCase { 'default' => 1 ), '`testName` decimal UNSIGNED DEFAULT 1' + ), + //set #8 + array( + array( + 'name' => 'testName', + 'type' => 'decimal', + 'unsigned' => true + ), + '`testName` decimal UNSIGNED' ) ); } From 00fb663f9045eac6c183ed885f50c0ae19c0744a Mon Sep 17 00:00:00 2001 From: imsamurai Date: Sun, 17 Nov 2013 21:23:13 +0200 Subject: [PATCH 09/16] remove `numeric` from `unsigned` config --- lib/Cake/Model/Datasource/Database/Mysql.php | 2 +- .../Case/Model/Datasource/Database/MysqlTest.php | 13 ++----------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index 2124a62d1..af65ecdf5 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -91,7 +91,7 @@ class Mysql extends DboSource { 'value' => 'UNSIGNED', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'beforeDefault', 'noVal' => true, 'options' => array(true), - 'types' => array('integer', 'float', 'biginteger', 'numeric', 'decimal') + 'types' => array('integer', 'float', 'decimal', 'biginteger') ) ); diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index d29a402af..509bfe5a1 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3248,7 +3248,7 @@ class MysqlTest extends CakeTestCase { array( array( 'name' => 'testName', - 'type' => 'numeric', + 'type' => 'decimal', 'unsigned' => true ), '`testName` decimal UNSIGNED' @@ -3257,20 +3257,11 @@ class MysqlTest extends CakeTestCase { array( array( 'name' => 'testName', - 'type' => 'numeric', + 'type' => 'decimal', 'unsigned' => true, 'default' => 1 ), '`testName` decimal UNSIGNED DEFAULT 1' - ), - //set #8 - array( - array( - 'name' => 'testName', - 'type' => 'decimal', - 'unsigned' => true - ), - '`testName` decimal UNSIGNED' ) ); } From 2f6122cb0102175fd1e6cb7be4f3ddcb12f4f44e Mon Sep 17 00:00:00 2001 From: imsamurai Date: Sun, 17 Nov 2013 22:37:01 +0200 Subject: [PATCH 10/16] add `unsigned` property in `Model::describe` --- lib/Cake/Model/Datasource/Database/Mysql.php | 11 ++++ .../Model/Datasource/Database/MysqlTest.php | 23 ++++++- lib/Cake/Test/Fixture/UnsignedFixture.php | 60 +++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 lib/Cake/Test/Fixture/UnsignedFixture.php diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index af65ecdf5..ce058f88e 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -348,6 +348,7 @@ class Mysql extends DboSource { 'null' => ($column->Null === 'YES' ? true : false), 'default' => $column->Default, 'length' => $this->length($column->Type), + 'unsigned' => $this->unsigned($column->Type) ); if (!empty($column->Key) && isset($this->index[$column->Key])) { $fields[$column->Field]['key'] = $this->index[$column->Key]; @@ -782,6 +783,16 @@ class Mysql extends DboSource { return 'text'; } +/** + * Check if column type is unsigned + * + * @param string $real Real database-layer column type (i.e. "varchar(255)") + * @return bool True if column is unsigned, false otherwise + */ + public function unsigned($real) { + return strpos(strtolower($real), 'unsigned') !== false; + } + /** * Gets the schema name * diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 509bfe5a1..6a93b09d5 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -47,7 +47,7 @@ class MysqlTest extends CakeTestCase { public $fixtures = array( 'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment', 'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test', - 'core.binary_test', 'core.inno' + 'core.binary_test', 'core.inno', 'core.unsigned' ); /** @@ -3165,7 +3165,7 @@ class MysqlTest extends CakeTestCase { * * @dataProvider buildColumnUnsignedProvider */ - public function testBuildColumnUnsigned($data, $expected) {//debug($this->Dbo->columns); + public function testBuildColumnUnsigned($data, $expected) { $result = $this->Dbo->buildColumn($data); $this->assertEquals($expected, $result); } @@ -3266,6 +3266,25 @@ class MysqlTest extends CakeTestCase { ); } +/** + * Test getting `unsigned` field parameter from DB + * + * @return void + */ + public function testSchemaUnsigned() { + $this->loadFixtures('Unsigned'); + $Model = ClassRegistry::init('Model'); + $Model->setSource('unsigned'); + $types = $this->Dbo->fieldParameters['unsigned']['types']; + $schema = $Model->schema(); + foreach ($types as $type) { + $this->assertArrayHasKey('unsigned', $schema['u'.$type]); + $this->assertTrue($schema['u'.$type]['unsigned']); + $this->assertArrayHasKey('unsigned', $schema[$type]); + $this->assertFalse($schema[$type]['unsigned']); + } + } + /** * test hasAny() * diff --git a/lib/Cake/Test/Fixture/UnsignedFixture.php b/lib/Cake/Test/Fixture/UnsignedFixture.php new file mode 100644 index 000000000..1264ac9ae --- /dev/null +++ b/lib/Cake/Test/Fixture/UnsignedFixture.php @@ -0,0 +1,60 @@ + + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests + * @package Cake.Test.Fixture + * @since CakePHP(tm) v 1.2.0.4667 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ + +/** + * Short description for class. + * + * @package Cake.Test.Fixture + */ +class UnsignedFixture extends CakeTestFixture { + +/** + * table property + * + * @var array + */ + public $table = 'unsigned'; + +/** + * fields property + * + * @var array + */ + public $fields = array( + 'uinteger' => array('type' => 'integer', 'null' => '', 'default' => '1', 'length' => '8', 'key' => 'primary', 'unsigned' => true), + 'integer' => array('type' => 'integer', 'length' => '8', 'unsigned' => false), + 'udecimal' => array('type' => 'decimal', 'length' => '4', 'unsigned' => true), + 'decimal' => array('type' => 'decimal', 'length' => '4'), + 'biginteger' => array('type' => 'biginteger', 'length' => '20', 'default' => 3), + 'ubiginteger' => array('type' => 'biginteger', 'length' => '20', 'default' => 3, 'unsigned' => true), + 'float' => array('type' => 'float', 'length' => '4'), + 'ufloat' => array('type' => 'float', 'length' => '4', 'unsigned' => true), + 'tableParameters' => array( + 'engine' => 'MyISAM' + ) + ); + +/** + * records property + * + * @var array + */ + public $records = array(); +} From 8bcfe452da2a68b5896eca2cdcbee4a772287062 Mon Sep 17 00:00:00 2001 From: imsamurai Date: Sun, 17 Nov 2013 23:10:53 +0200 Subject: [PATCH 11/16] not add `unsigned` to not numeric fields, fix broken test, add new test --- lib/Cake/Model/Datasource/Database/Mysql.php | 6 ++++-- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 1 + lib/Cake/Test/Case/Model/ModelIntegrationTest.php | 3 ++- lib/Cake/Test/Fixture/UnsignedFixture.php | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index ce058f88e..571e5872c 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -347,9 +347,11 @@ class Mysql extends DboSource { 'type' => $this->column($column->Type), 'null' => ($column->Null === 'YES' ? true : false), 'default' => $column->Default, - 'length' => $this->length($column->Type), - 'unsigned' => $this->unsigned($column->Type) + 'length' => $this->length($column->Type) ); + if (in_array($fields[$column->Field]['type'], $this->fieldParameters['unsigned']['types'], true)) { + $fields[$column->Field]['unsigned'] = $this->unsigned($column->Type); + } if (!empty($column->Key) && isset($this->index[$column->Key])) { $fields[$column->Field]['key'] = $this->index[$column->Key]; } diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 6a93b09d5..b425740a3 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3283,6 +3283,7 @@ class MysqlTest extends CakeTestCase { $this->assertArrayHasKey('unsigned', $schema[$type]); $this->assertFalse($schema[$type]['unsigned']); } + $this->assertArrayNotHasKey('unsigned', $schema['string']); } /** diff --git a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php index 4f2e81f06..5f78af1d0 100644 --- a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php +++ b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php @@ -2216,7 +2216,8 @@ class ModelIntegrationTest extends BaseModelTest { 'null' => false, 'default' => null, 'length' => $intLength, - 'key' => 'primary' + 'key' => 'primary', + 'unsigned' => false ), 'user' => array( 'type' => 'string', diff --git a/lib/Cake/Test/Fixture/UnsignedFixture.php b/lib/Cake/Test/Fixture/UnsignedFixture.php index 1264ac9ae..e328de7dd 100644 --- a/lib/Cake/Test/Fixture/UnsignedFixture.php +++ b/lib/Cake/Test/Fixture/UnsignedFixture.php @@ -46,6 +46,7 @@ class UnsignedFixture extends CakeTestFixture { 'ubiginteger' => array('type' => 'biginteger', 'length' => '20', 'default' => 3, 'unsigned' => true), 'float' => array('type' => 'float', 'length' => '4'), 'ufloat' => array('type' => 'float', 'length' => '4', 'unsigned' => true), + 'string' => array('type' => 'string', 'length' => '4'), 'tableParameters' => array( 'engine' => 'MyISAM' ) From 0286e4120d5f6fc119cefec5692ce1b6cb7cf1cf Mon Sep 17 00:00:00 2001 From: imsamurai Date: Sun, 17 Nov 2013 23:12:51 +0200 Subject: [PATCH 12/16] fix code style --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index b425740a3..150fe5d77 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3278,8 +3278,8 @@ class MysqlTest extends CakeTestCase { $types = $this->Dbo->fieldParameters['unsigned']['types']; $schema = $Model->schema(); foreach ($types as $type) { - $this->assertArrayHasKey('unsigned', $schema['u'.$type]); - $this->assertTrue($schema['u'.$type]['unsigned']); + $this->assertArrayHasKey('unsigned', $schema['u' . $type]); + $this->assertTrue($schema['u' . $type]['unsigned']); $this->assertArrayHasKey('unsigned', $schema[$type]); $this->assertFalse($schema[$type]['unsigned']); } From 20dc92291edc7f9b3932a10dc263dfc43c3368ad Mon Sep 17 00:00:00 2001 From: imsamurai Date: Sun, 17 Nov 2013 23:34:22 +0200 Subject: [PATCH 13/16] fix broken test --- lib/Cake/Test/Case/Model/ModelIntegrationTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php index 5f78af1d0..7beeec025 100644 --- a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php +++ b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php @@ -2204,7 +2204,7 @@ class ModelIntegrationTest extends BaseModelTest { } else { $intLength = 11; } - foreach (array('collate', 'charset', 'comment') as $type) { + foreach (array('collate', 'charset', 'comment', 'unsigned') as $type) { foreach ($result as $i => $r) { unset($result[$i][$type]); } @@ -2216,8 +2216,7 @@ class ModelIntegrationTest extends BaseModelTest { 'null' => false, 'default' => null, 'length' => $intLength, - 'key' => 'primary', - 'unsigned' => false + 'key' => 'primary' ), 'user' => array( 'type' => 'string', From 1313b81913c21669d1619b9e3952144eb3cd4d7d Mon Sep 17 00:00:00 2001 From: imsamurai Date: Tue, 19 Nov 2013 10:48:51 +0200 Subject: [PATCH 14/16] fix `since` phpdoc tag for fixture --- lib/Cake/Test/Fixture/UnsignedFixture.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Fixture/UnsignedFixture.php b/lib/Cake/Test/Fixture/UnsignedFixture.php index e328de7dd..1c8eb2f28 100644 --- a/lib/Cake/Test/Fixture/UnsignedFixture.php +++ b/lib/Cake/Test/Fixture/UnsignedFixture.php @@ -14,7 +14,7 @@ * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests * @package Cake.Test.Fixture - * @since CakePHP(tm) v 1.2.0.4667 + * @since CakePHP(tm) v 2.5.0 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ From f817beb8b6576784111d266e64d0cf42df6f7852 Mon Sep 17 00:00:00 2001 From: imsamurai Date: Tue, 19 Nov 2013 10:55:44 +0200 Subject: [PATCH 15/16] change method `public unsigned` to `protected _unsigned` --- lib/Cake/Model/Datasource/Database/Mysql.php | 22 ++++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index 571e5872c..2030362b4 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -350,7 +350,7 @@ class Mysql extends DboSource { 'length' => $this->length($column->Type) ); if (in_array($fields[$column->Field]['type'], $this->fieldParameters['unsigned']['types'], true)) { - $fields[$column->Field]['unsigned'] = $this->unsigned($column->Type); + $fields[$column->Field]['unsigned'] = $this->_unsigned($column->Type); } if (!empty($column->Key) && isset($this->index[$column->Key])) { $fields[$column->Field]['key'] = $this->index[$column->Key]; @@ -785,16 +785,6 @@ class Mysql extends DboSource { return 'text'; } -/** - * Check if column type is unsigned - * - * @param string $real Real database-layer column type (i.e. "varchar(255)") - * @return bool True if column is unsigned, false otherwise - */ - public function unsigned($real) { - return strpos(strtolower($real), 'unsigned') !== false; - } - /** * Gets the schema name * @@ -813,4 +803,14 @@ class Mysql extends DboSource { return $this->useNestedTransactions && version_compare($this->getVersion(), '4.1', '>='); } +/** + * Check if column type is unsigned + * + * @param string $real Real database-layer column type (i.e. "varchar(255)") + * @return bool True if column is unsigned, false otherwise + */ + protected function _unsigned($real) { + return strpos(strtolower($real), 'unsigned') !== false; + } + } From 62a53d26bbe5607f9b0d24f5ea9f5194e86af5fe Mon Sep 17 00:00:00 2001 From: imsamurai Date: Fri, 22 Nov 2013 16:17:25 +0200 Subject: [PATCH 16/16] fix typo --- lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index 150fe5d77..8b40b1986 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -3253,7 +3253,7 @@ class MysqlTest extends CakeTestCase { ), '`testName` decimal UNSIGNED' ), - //set #7 + //set #8 array( array( 'name' => 'testName',