Merge pull request #10347 from sebastienbarre/storage_req

Add new `tinyint` and `smallint` abstract schema types to support smaller storage requirements
This commit is contained in:
Mark Story 2017-03-12 12:27:45 -04:00 committed by GitHub
commit cd249b0120
15 changed files with 179 additions and 30 deletions

View file

@ -335,6 +335,9 @@ class FixtureTask extends BakeTask {
}
$insert = '';
switch ($fieldInfo['type']) {
case 'tinyinteger':
case 'smallinteger':
case 'biginteger':
case 'integer':
case 'float':
$insert = $i + 1;

View file

@ -450,6 +450,10 @@ class ModelTask extends BakeTask {
$guess = $methods['notBlank'];
} elseif ($metaData['type'] === 'integer') {
$guess = $methods['numeric'];
} elseif ($metaData['type'] === 'smallinteger') {
$guess = $methods['numeric'];
} elseif ($metaData['type'] === 'tinyinteger') {
$guess = $methods['numeric'];
} elseif ($metaData['type'] === 'float') {
$guess = $methods['numeric'];
} elseif ($metaData['type'] === 'boolean') {

View file

@ -87,10 +87,14 @@ 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' => 'beforeDefault',
'value' => 'UNSIGNED',
'quote' => false,
'join' => ' ',
'column' => false,
'position' => 'beforeDefault',
'noVal' => true,
'options' => array(true),
'types' => array('integer', 'float', 'decimal', 'biginteger')
'types' => array('integer', 'smallinteger', 'tinyinteger', 'float', 'decimal', 'biginteger')
)
);
@ -110,6 +114,7 @@ class Mysql extends DboSource {
* MySQL column definition
*
* @var array
* @link https://dev.mysql.com/doc/refman/5.7/en/data-types.html MySQL Data Types
*/
public $columns = array(
'primary_key' => array('name' => 'NOT NULL AUTO_INCREMENT'),
@ -117,6 +122,8 @@ class Mysql extends DboSource {
'text' => array('name' => 'text'),
'biginteger' => array('name' => 'bigint', 'limit' => '20'),
'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
'smallinteger' => array('name' => 'smallint', 'limit' => '6', 'formatter' => 'intval'),
'tinyinteger' => array('name' => 'tinyint', 'limit' => '4', 'formatter' => 'intval'),
'float' => array('name' => 'float', 'formatter' => 'floatval'),
'decimal' => array('name' => 'decimal', 'formatter' => 'floatval'),
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
@ -783,6 +790,12 @@ class Mysql extends DboSource {
if (strpos($col, 'bigint') !== false || $col === 'bigint') {
return 'biginteger';
}
if (strpos($col, 'tinyint') !== false) {
return 'tinyinteger';
}
if (strpos($col, 'smallint') !== false) {
return 'smallinteger';
}
if (strpos($col, 'int') !== false) {
return 'integer';
}

View file

@ -52,12 +52,15 @@ class Postgres extends DboSource {
* Columns
*
* @var array
* @link https://www.postgresql.org/docs/9.6/static/datatype.html PostgreSQL Data Types
*/
public $columns = array(
'primary_key' => array('name' => 'serial NOT NULL'),
'string' => array('name' => 'varchar', 'limit' => '255'),
'text' => array('name' => 'text'),
'integer' => array('name' => 'integer', 'formatter' => 'intval'),
'smallinteger' => array('name' => 'smallint', 'formatter' => 'intval'),
'tinyinteger' => array('name' => 'smallint', 'formatter' => 'intval'),
'biginteger' => array('name' => 'bigint', 'limit' => '20'),
'float' => array('name' => 'float', 'formatter' => 'floatval'),
'decimal' => array('name' => 'decimal', 'formatter' => 'floatval'),
@ -701,6 +704,8 @@ class Postgres extends DboSource {
return 'time';
case ($col === 'bigint'):
return 'biginteger';
case ($col === 'smallint'):
return 'smallinteger';
case (strpos($col, 'int') !== false && $col !== 'interval'):
return 'integer';
case (strpos($col, 'char') !== false):

View file

@ -64,12 +64,15 @@ class Sqlite extends DboSource {
* SQLite3 column definition
*
* @var array
* @link https://www.sqlite.org/datatype3.html Datatypes In SQLite Version 3
*/
public $columns = array(
'primary_key' => array('name' => 'integer primary key autoincrement'),
'string' => array('name' => 'varchar', 'limit' => '255'),
'text' => array('name' => 'text'),
'integer' => array('name' => 'integer', 'limit' => null, 'formatter' => 'intval'),
'smallinteger' => array('name' => 'smallint', 'limit' => null, 'formatter' => 'intval'),
'tinyinteger' => array('name' => 'tinyint', 'limit' => null, 'formatter' => 'intval'),
'biginteger' => array('name' => 'bigint', 'limit' => 20),
'float' => array('name' => 'float', 'formatter' => 'floatval'),
'decimal' => array('name' => 'decimal', 'formatter' => 'floatval'),
@ -272,6 +275,12 @@ class Sqlite extends DboSource {
if (in_array($col, $standard)) {
return $col;
}
if ($col === 'tinyint') {
return 'tinyinteger';
}
if ($col === 'smallint') {
return 'smallinteger';
}
if ($col === 'bigint') {
return 'biginteger';
}

View file

@ -85,12 +85,15 @@ class Sqlserver extends DboSource {
* MS SQL column definition
*
* @var array
* @link https://msdn.microsoft.com/en-us/library/ms187752.aspx SQL Server Data Types
*/
public $columns = array(
'primary_key' => array('name' => 'IDENTITY (1, 1) NOT NULL'),
'string' => array('name' => 'nvarchar', 'limit' => '255'),
'text' => array('name' => 'nvarchar', 'limit' => 'MAX'),
'integer' => array('name' => 'int', 'formatter' => 'intval'),
'smallinteger' => array('name' => 'smallint', 'formatter' => 'intval'),
'tinyinteger' => array('name' => 'tinyint', 'formatter' => 'intval'),
'biginteger' => array('name' => 'bigint'),
'numeric' => array('name' => 'decimal', 'formatter' => 'floatval'),
'decimal' => array('name' => 'decimal', 'formatter' => 'floatval'),
@ -435,6 +438,12 @@ class Sqlserver extends DboSource {
if (strpos($col, 'bigint') !== false) {
return 'biginteger';
}
if (strpos($col, 'smallint') !== false) {
return 'smallinteger';
}
if (strpos($col, 'tinyint') !== false) {
return 'tinyinteger';
}
if (strpos($col, 'int') !== false) {
return 'integer';
}

View file

@ -470,17 +470,20 @@ class FixtureTaskTest extends CakeTestCase {
}
/**
* test record generation with float and binary types
* test record generation with various integer, float and binary types
*
* @return void
*/
public function testRecordGenerationForBinaryAndFloat() {
public function testRecordGenerationForBinaryFloatAndIntegerTypes() {
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$result = $this->Task->bake('Article', 'datatypes');
$this->assertContains("'float_field' => 1", $result);
$this->assertContains("'bool' => 1", $result);
$this->assertContains("'tiny_int' => 1", $result);
$this->assertContains("'small_int' => 1", $result);
$this->assertContains("'huge_int' => 1", $result);
$result = $this->Task->bake('Article', 'binary_tests');
$this->assertContains("'data' => 'Lorem ipsum dolor sit amet'", $result);

View file

@ -173,6 +173,9 @@ class TestAppSchema extends CakeSchema {
'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => ''),
'decimal_field' => array('type' => 'decimal', 'length' => '6,3', 'default' => '0.000'),
'huge_int' => array('type' => 'biginteger'),
'normal_int' => array('type' => 'integer'),
'small_int' => array('type' => 'smallinteger'),
'tiny_int' => array('type' => 'tinyinteger'),
'bool' => array('type' => 'boolean', 'null' => false, 'default' => false),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
'tableParameters' => array()

View file

@ -201,7 +201,7 @@ class MysqlTest extends CakeTestCase {
public function testTinyintCasting() {
$this->Dbo->cacheSources = false;
$tableName = 'tinyint_' . uniqid();
$this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
$this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), tiny_int tinyint(2), primary key(id));');
$this->model = new CakeTestModel(array(
'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test'
@ -209,24 +209,24 @@ class MysqlTest extends CakeTestCase {
$result = $this->model->schema();
$this->assertEquals('boolean', $result['bool']['type']);
$this->assertEquals('integer', $result['small_int']['type']);
$this->assertEquals('tinyinteger', $result['tiny_int']['type']);
$this->assertTrue((bool)$this->model->save(array('bool' => 5, 'small_int' => 5)));
$this->assertTrue((bool)$this->model->save(array('bool' => 5, 'tiny_int' => 5)));
$result = $this->model->find('first');
$this->assertTrue($result['Tinyint']['bool']);
$this->assertSame($result['Tinyint']['small_int'], '5');
$this->assertSame($result['Tinyint']['tiny_int'], '5');
$this->model->deleteAll(true);
$this->assertTrue((bool)$this->model->save(array('bool' => 0, 'small_int' => 100)));
$this->assertTrue((bool)$this->model->save(array('bool' => 0, 'tiny_int' => 100)));
$result = $this->model->find('first');
$this->assertFalse($result['Tinyint']['bool']);
$this->assertSame($result['Tinyint']['small_int'], '100');
$this->assertSame($result['Tinyint']['tiny_int'], '100');
$this->model->deleteAll(true);
$this->assertTrue((bool)$this->model->save(array('bool' => true, 'small_int' => 0)));
$this->assertTrue((bool)$this->model->save(array('bool' => true, 'tiny_int' => 0)));
$result = $this->model->find('first');
$this->assertTrue($result['Tinyint']['bool']);
$this->assertSame($result['Tinyint']['small_int'], '0');
$this->assertSame($result['Tinyint']['tiny_int'], '0');
$this->model->deleteAll(true);
$this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
@ -526,6 +526,14 @@ class MysqlTest extends CakeTestCase {
$expected = 'boolean';
$this->assertEquals($expected, $result);
$result = $this->Dbo->column('tinyint');
$expected = 'tinyinteger';
$this->assertEquals($expected, $result);
$result = $this->Dbo->column('smallint');
$expected = 'smallinteger';
$this->assertEquals($expected, $result);
$result = $this->Dbo->column('boolean');
$expected = 'boolean';
$this->assertEquals($expected, $result);
@ -3283,7 +3291,7 @@ SQL;
*/
public function buildColumnUnsignedProvider() {
return array(
//set #0
// unsigned int
array(
array(
'name' => 'testName',
@ -3293,7 +3301,7 @@ SQL;
),
'`testName` int(11) UNSIGNED'
),
//set #1
// unsigned bigint
array(
array(
'name' => 'testName',
@ -3303,7 +3311,7 @@ SQL;
),
'`testName` bigint(20) UNSIGNED'
),
//set #2
// unsigned float
array(
array(
'name' => 'testName',
@ -3312,7 +3320,7 @@ SQL;
),
'`testName` float UNSIGNED'
),
//set #3
// varchar
array(
array(
'name' => 'testName',
@ -3322,7 +3330,7 @@ SQL;
),
'`testName` varchar(255)'
),
//set #4
// date unsigned
array(
array(
'name' => 'testName',
@ -3331,7 +3339,7 @@ SQL;
),
'`testName` date'
),
//set #5
// date
array(
array(
'name' => 'testName',
@ -3340,7 +3348,7 @@ SQL;
),
'`testName` date'
),
//set #6
// integer with length
array(
array(
'name' => 'testName',
@ -3350,7 +3358,7 @@ SQL;
),
'`testName` int(11)'
),
//set #7
// unsigned decimal
array(
array(
'name' => 'testName',
@ -3359,7 +3367,7 @@ SQL;
),
'`testName` decimal UNSIGNED'
),
//set #8
// decimal with default
array(
array(
'name' => 'testName',
@ -3368,6 +3376,26 @@ SQL;
'default' => 1
),
'`testName` decimal UNSIGNED DEFAULT 1'
),
// smallinteger
array(
array(
'name' => 'testName',
'type' => 'smallinteger',
'length' => 6,
'unsigned' => true
),
'`testName` smallint(6) UNSIGNED'
),
// tinyinteger
array(
array(
'name' => 'testName',
'type' => 'tinyinteger',
'length' => 4,
'unsigned' => true
),
'`testName` tinyint(4) UNSIGNED'
)
);
}

View file

@ -304,9 +304,9 @@ class PostgresTest extends CakeTestCase {
$this->assertEquals('float', $this->Dbo2->column('double precision'));
$this->assertEquals('uuid', $this->Dbo2->column('uuid'));
$result = $this->Dbo2->column('bigint');
$expected = 'biginteger';
$this->assertEquals($expected, $result);
$this->assertEquals('biginteger', $this->Dbo2->column('bigint'));
$this->assertEquals('integer', $this->Dbo2->column('integer'));
$this->assertEquals('smallinteger', $this->Dbo2->column('smallint'));
}
/**

View file

@ -263,6 +263,28 @@ class SqliteTest extends CakeTestCase {
$expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
$this->assertEquals($expected, $result);
$data = array(
'name' => 'testName',
'type' => 'smallinteger',
'length' => 6,
'default' => 6,
'null' => false,
);
$result = $this->Dbo->buildColumn($data);
$expected = '"testName" smallint(6) DEFAULT 6 NOT NULL';
$this->assertEquals($expected, $result);
$data = array(
'name' => 'testName',
'type' => 'tinyinteger',
'length' => 4,
'default' => 4,
'null' => false,
);
$result = $this->Dbo->buildColumn($data);
$expected = '"testName" tinyint(4) DEFAULT 4 NOT NULL';
$this->assertEquals($expected, $result);
$data = array(
'name' => 'huge',
'type' => 'biginteger',
@ -383,6 +405,24 @@ class SqliteTest extends CakeTestCase {
'default' => null,
'length' => 20,
),
'normal_int' => array(
'type' => 'integer',
'null' => true,
'default' => null,
'length' => null
),
'small_int' => array(
'type' => 'smallinteger',
'null' => true,
'default' => null,
'length' => null
),
'tiny_int' => array(
'type' => 'tinyinteger',
'null' => true,
'default' => null,
'length' => null
),
'bool' => array(
'type' => 'boolean',
'null' => false,

View file

@ -532,6 +532,16 @@ class SqlserverTest extends CakeTestCase {
$expected = '[client_id] int NULL';
$this->assertEquals($expected, $result);
$column = array('type' => 'smallinteger', 'name' => 'client_id');
$result = $this->db->buildColumn($column);
$expected = '[client_id] smallint NULL';
$this->assertEquals($expected, $result);
$column = array('type' => 'tinyinteger', 'name' => 'client_id');
$result = $this->db->buildColumn($column);
$expected = '[client_id] tinyint NULL';
$this->assertEquals($expected, $result);
$column = array('type' => 'string', 'name' => 'name');
$result = $this->db->buildColumn($column);
$expected = '[name] nvarchar(255) NULL';

View file

@ -33,6 +33,9 @@ class DatatypeFixture extends CakeTestFixture {
'float_field' => array('type' => 'float', 'length' => '5,2', 'null' => false, 'default' => null),
'decimal_field' => array('type' => 'decimal', 'length' => '6,3', 'default' => '0.000'),
'huge_int' => array('type' => 'biginteger'),
'normal_int' => array('type' => 'integer'),
'small_int' => array('type' => 'smallinteger'),
'tiny_int' => array('type' => 'tinyinteger'),
'bool' => array('type' => 'boolean', 'null' => false, 'default' => false),
);
@ -42,6 +45,14 @@ class DatatypeFixture extends CakeTestFixture {
* @var array
*/
public $records = array(
array('id' => 1, 'float_field' => 42.23, 'huge_int' => '1234567891234567891', 'bool' => 0),
array(
'id' => 1,
'float_field' => 42.23,
'huge_int' => '9223372036854775807',
'normal_int' => 2147483647,
'small_int' => 32767,
'tiny_int' => 127,
'bool' => 0
),
);
}

View file

@ -40,6 +40,10 @@ class UnsignedFixture extends CakeTestFixture {
public $fields = array(
'uinteger' => array('type' => 'integer', 'null' => '', 'default' => '1', 'length' => '8', 'key' => 'primary', 'unsigned' => true),
'integer' => array('type' => 'integer', 'length' => '8', 'unsigned' => false),
'usmallinteger' => array('type' => 'smallinteger', 'unsigned' => true),
'smallinteger' => array('type' => 'smallinteger', 'unsigned' => false),
'utinyinteger' => array('type' => 'tinyinteger', 'unsigned' => true),
'tinyinteger' => array('type' => 'tinyinteger', 'unsigned' => false),
'udecimal' => array('type' => 'decimal', 'length' => '4', 'unsigned' => true),
'decimal' => array('type' => 'decimal', 'length' => '4'),
'biginteger' => array('type' => 'biginteger', 'length' => '20', 'default' => 3),

View file

@ -1236,11 +1236,18 @@ class FormHelper extends AppHelper {
$type = $fieldDef['type'];
$primaryKey = $this->fieldset[$modelKey]['key'];
$map = array(
'string' => 'text', 'datetime' => 'datetime',
'boolean' => 'checkbox', 'timestamp' => 'datetime',
'text' => 'textarea', 'time' => 'time',
'date' => 'date', 'float' => 'number',
'integer' => 'number', 'decimal' => 'number',
'string' => 'text',
'datetime' => 'datetime',
'boolean' => 'checkbox',
'timestamp' => 'datetime',
'text' => 'textarea',
'time' => 'time',
'date' => 'date',
'float' => 'number',
'integer' => 'number',
'smallinteger' => 'number',
'tinyinteger' => 'number',
'decimal' => 'number',
'binary' => 'file'
);