Skip to content

Commit

Permalink
introduce new tinyint, smallint types for smaller storage requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbarre committed Mar 5, 2017
1 parent 216ae0e commit 3810199
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 128 deletions.
46 changes: 8 additions & 38 deletions lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -117,10 +117,8 @@ class Mysql extends DboSource {
'text' => array('name' => 'text'),
'biginteger' => array('name' => 'bigint', 'limit' => '20'),
'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
'integer/4' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
'integer/3' => array('name' => 'mediumint', 'limit' => '9', 'formatter' => 'intval'),
'integer/2' => array('name' => 'smallint', 'limit' => '6', 'formatter' => 'intval'),
'integer/1' => array('name' => 'tinyint', 'limit' => '4', 'formatter' => 'intval'),
'smallint' => array('name' => 'smallint', 'limit' => '6', 'formatter' => 'intval'),
'tinyint' => 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'),
Expand Down Expand Up @@ -373,10 +371,6 @@ public function describe($model) {
$fields[$column->Field]['charset'] = $charset;
}
}
$storage = $this->storageRequirement($column->Type);
if (is_numeric($storage)) {
$fields[$column->Field]['storage'] = $storage;
}
}
$this->_cacheDescription($key, $fields);
$cols->closeCursor();
Expand Down Expand Up @@ -791,6 +785,12 @@ public function column($real) {
if (strpos($col, 'bigint') !== false || $col === 'bigint') {
return 'biginteger';
}
if (strpos($col, 'tinyint') !== false) {
return 'tinyint';
}
if (strpos($col, 'smallint') !== false) {
return 'smallint';
}
if (strpos($col, 'int') !== false) {
return 'integer';
}
Expand Down Expand Up @@ -818,36 +818,6 @@ public function column($real) {
return 'text';
}

/**
* Gets the storage requirement of a database-native column description, or null if unknown or N/A
*
* @param string $real Real database-layer column type (i.e. "varchar(255)")
* @return mixed An integer representing the storage requirement of the column in bytes, or null if unknown or N/A.
*/
public function storageRequirement($real) {
if (is_array($real)) {
$col = $real['name'];
} else {
$col = $real;
if (strpos($col, '(') !== false) {
list($col, $vals) = explode('(', $col);
}
}

// Base on Storage Requirements for Numeric Types
// https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html
if ($col === 'tinyint') {
return 1;
}
if ($col === 'smallint') {
return 2;
}
if ($col === 'mediumint') {
return 3;
}
return null;
}

/**
* {@inheritDoc}
*/
Expand Down
6 changes: 6 additions & 0 deletions lib/Cake/Model/Datasource/Database/Postgres.php
Expand Up @@ -51,13 +51,17 @@ class Postgres extends DboSource {
/**
* Columns
*
* @link https://www.postgresql.org/docs/9.6/static/datatype.html PostgreSQL Data Types
*
* @var array
*/
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'),
'smallint' => array('name' => 'smallint', 'formatter' => 'intval'),
'tinyint' => array('name' => 'smallint', 'formatter' => 'intval'),
'biginteger' => array('name' => 'bigint', 'limit' => '20'),
'float' => array('name' => 'float', 'formatter' => 'floatval'),
'decimal' => array('name' => 'decimal', 'formatter' => 'floatval'),
Expand Down Expand Up @@ -701,6 +705,8 @@ public function column($real) {
return 'time';
case ($col === 'bigint'):
return 'biginteger';
case ($col === 'smallint'):
return 'smallint';
case (strpos($col, 'int') !== false && $col !== 'interval'):
return 'integer';
case (strpos($col, 'char') !== false):
Expand Down
4 changes: 4 additions & 0 deletions lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -63,13 +63,17 @@ class Sqlite extends DboSource {
/**
* SQLite3 column definition
*
* @link https://www.sqlite.org/datatype3.html Datatypes In SQLite Version 3
*
* @var array
*/
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'),
'smallint' => array('name' => 'integer', 'limit' => null, 'formatter' => 'intval'),
'tinyint' => array('name' => 'integer', 'limit' => null, 'formatter' => 'intval'),
'biginteger' => array('name' => 'bigint', 'limit' => 20),
'float' => array('name' => 'float', 'formatter' => 'floatval'),
'decimal' => array('name' => 'decimal', 'formatter' => 'floatval'),
Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -84,13 +84,17 @@ class Sqlserver extends DboSource {
/**
* MS SQL column definition
*
* @link https://msdn.microsoft.com/en-us/library/ms187752.aspx SQL Server Data Types
*
* @var array
*/
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'),
'smallint' => array('name' => 'smallint', 'formatter' => 'intval'),
'tinyint' => array('name' => 'tinyint', 'formatter' => 'intval'),
'biginteger' => array('name' => 'bigint'),
'numeric' => array('name' => 'decimal', 'formatter' => 'floatval'),
'decimal' => array('name' => 'decimal', 'formatter' => 'floatval'),
Expand Down Expand Up @@ -435,6 +439,12 @@ public function column($real) {
if (strpos($col, 'bigint') !== false) {
return 'biginteger';
}
if (strpos($col, 'smallint') !== false) {
return 'smallint';
}
if (strpos($col, 'tinyint') !== false) {
return 'tinyint';
}
if (strpos($col, 'int') !== false) {
return 'integer';
}
Expand Down
24 changes: 5 additions & 19 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -3246,16 +3246,6 @@ public function length($real) {
return (int)$length;
}

/**
* Gets the storage requirement of a database-native column description, or null if unknown or N/A
*
* @param string $real Real database-layer column type (i.e. "varchar(255)")
* @return mixed An integer representing the storage requirement of the column in bytes, or null if unknown or N/A.
*/
public function storageRequirement($real) {
return null;
}

/**
* Translates between PHP boolean values and Database (faked) boolean values
*
Expand Down Expand Up @@ -3469,16 +3459,12 @@ public function buildColumn($column) {
return null;
}

// if a storage requirement in bytes was set, try it first (i.e. 'integer/4' before 'integer')
if (isset($storage) && isset($this->columns[$type . '/' . $storage])) {
$real = $this->columns[$type . '/' . $storage];
} else {
if (!isset($this->columns[$type])) {
trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING);
return null;
}
$real = $this->columns[$type];
if (!isset($this->columns[$type])) {
trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING);
return null;
}

$real = $this->columns[$type];
$out = $this->name($name) . ' ' . $real['name'];

if (isset($column['length'])) {
Expand Down
79 changes: 11 additions & 68 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -201,32 +201,32 @@ public function testScientificNotation() {
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'
));

$result = $this->model->schema();
$this->assertEquals('boolean', $result['bool']['type']);
$this->assertEquals('integer', $result['small_int']['type']);
$this->assertEquals('tinyint', $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));
Expand Down Expand Up @@ -389,36 +389,6 @@ public function testBuildColumn() {
$expected = '`testName` CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL';
$this->assertEquals($expected, $result);
$this->Dbo->columns = $restore;

$data = array(
'name' => 'testName',
'type' => 'integer',
'storage' => 1
);
$result = $this->Dbo->buildColumn($data);
$expected = '`testName` tinyint(4)';
$this->assertEquals($expected, $result);
$this->Dbo->columns = $restore;

$data = array(
'name' => 'testName',
'type' => 'integer',
'storage' => 2
);
$result = $this->Dbo->buildColumn($data);
$expected = '`testName` smallint(6)';
$this->assertEquals($expected, $result);
$this->Dbo->columns = $restore;

$data = array(
'name' => 'testName',
'type' => 'integer',
'storage' => 3
);
$result = $this->Dbo->buildColumn($data);
$expected = '`testName` mediumint(9)';
$this->assertEquals($expected, $result);
$this->Dbo->columns = $restore;
}

/**
Expand Down Expand Up @@ -556,16 +526,12 @@ public function testColumn() {
$expected = 'boolean';
$this->assertEquals($expected, $result);

$result = $this->Dbo->column('tinyint(4)');
$expected = 'integer';
$result = $this->Dbo->column('tinyint');
$expected = 'tinyint';
$this->assertEquals($expected, $result);

$result = $this->Dbo->column('smallint');
$expected = 'integer';
$this->assertEquals($expected, $result);

$result = $this->Dbo->column('mediumint');
$expected = 'integer';
$expected = 'smallint';
$this->assertEquals($expected, $result);

$result = $this->Dbo->column('boolean');
Expand Down Expand Up @@ -3134,29 +3100,6 @@ public function testLength() {
$this->assertSame($expected, $result);
}

/**
* test storageRequirement method
*
* @return void
*/
public function testStorageRequirement() {
$result = $this->Dbo->storageRequirement('varchar(255)');
$expected = null;
$this->assertSame($expected, $result);

$result = $this->Dbo->storageRequirement('mediumint');
$expected = 3;
$this->assertSame($expected, $result);

$result = $this->Dbo->storageRequirement('smallint');
$expected = 2;
$this->assertSame($expected, $result);

$result = $this->Dbo->storageRequirement('tinyint');
$expected = 1;
$this->assertSame($expected, $result);
}

/**
* testBuildIndex method
*
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php
Expand Up @@ -304,9 +304,9 @@ public function testColumnParsing() {
$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('smallint', $this->Dbo2->column('smallint'));
}

/**
Expand Down
22 changes: 22 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php
Expand Up @@ -263,6 +263,28 @@ public function testBuildColumn() {
$expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
$this->assertEquals($expected, $result);

$data = array(
'name' => 'testName',
'type' => 'smallint',
'length' => 6,
'default' => 6,
'null' => false,
);
$result = $this->Dbo->buildColumn($data);
$expected = '"testName" integer(6) DEFAULT 6 NOT NULL';
$this->assertEquals($expected, $result);

$data = array(
'name' => 'testName',
'type' => 'tinyint',
'length' => 4,
'default' => 4,
'null' => false,
);
$result = $this->Dbo->buildColumn($data);
$expected = '"testName" integer(4) DEFAULT 4 NOT NULL';
$this->assertEquals($expected, $result);

$data = array(
'name' => 'huge',
'type' => 'biginteger',
Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php
Expand Up @@ -532,6 +532,16 @@ public function testBuildColumn() {
$expected = '[client_id] int NULL';
$this->assertEquals($expected, $result);

$column = array('type' => 'smallint', 'name' => 'client_id');
$result = $this->db->buildColumn($column);
$expected = '[client_id] smallint NULL';
$this->assertEquals($expected, $result);

$column = array('type' => 'tinyint', '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';
Expand Down

0 comments on commit 3810199

Please sign in to comment.