Skip to content

Commit

Permalink
add unsigned property in Model::describe
Browse files Browse the repository at this point in the history
  • Loading branch information
imsamurai committed Nov 17, 2013
1 parent 00fb663 commit 2f6122c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -348,6 +348,7 @@ public function describe($model) {
'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];
Expand Down Expand Up @@ -782,6 +783,16 @@ public function column($real) {
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
*
Expand Down
23 changes: 21 additions & 2 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -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'
);

/**
Expand Down Expand Up @@ -3165,7 +3165,7 @@ public function testBuildColumnBadType() {
*
* @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);
}
Expand Down Expand Up @@ -3266,6 +3266,25 @@ public function buildColumnUnsignedProvider() {
);
}

/**
* 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()
*
Expand Down
60 changes: 60 additions & 0 deletions lib/Cake/Test/Fixture/UnsignedFixture.php
@@ -0,0 +1,60 @@
<?php
/**
* Short description for file.
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* 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();
}

0 comments on commit 2f6122c

Please sign in to comment.