Browse Source

add `unsigned` property in `Model::describe`

imsamurai 12 years ago
parent
commit
2f6122cb01

+ 11 - 0
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];
@@ -783,6 +784,16 @@ class Mysql extends DboSource {
 	}
 
 /**
+ * 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
  *
  * @return string The schema name

+ 21 - 2
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);
 	}
@@ -3267,6 +3267,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()
  *
  * @return void

+ 60 - 0
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();
+}