From 2f6122cb0102175fd1e6cb7be4f3ddcb12f4f44e Mon Sep 17 00:00:00 2001 From: imsamurai Date: Sun, 17 Nov 2013 22:37:01 +0200 Subject: [PATCH] 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(); +}