From 8d8f4b5c5da3470058ea1848565e87f3c7cae4fc Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 30 Aug 2012 14:26:17 +0100 Subject: [PATCH] Add bigint support for Sqlite. * Update SQLite for biginteger. * Update CakeSchema tests for integration purposes. --- lib/Cake/Model/Datasource/Database/Sqlite.php | 16 ++++- lib/Cake/Test/Case/Model/CakeSchemaTest.php | 1 + .../Model/Datasource/Database/SqliteTest.php | 58 ++++++++++++++++++- lib/Cake/Test/Fixture/DatatypeFixture.php | 3 +- 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Sqlite.php b/lib/Cake/Model/Datasource/Database/Sqlite.php index 63ed603919c..5d3bb4f3ef2 100644 --- a/lib/Cake/Model/Datasource/Database/Sqlite.php +++ b/lib/Cake/Model/Datasource/Database/Sqlite.php @@ -70,6 +70,7 @@ class Sqlite extends DboSource { 'string' => array('name' => 'varchar', 'limit' => '255'), 'text' => array('name' => 'text'), 'integer' => array('name' => 'integer', 'limit' => null, 'formatter' => 'intval'), + 'biginteger' => array('name' => 'bigint', 'limit' => 20), 'float' => array('name' => 'float', 'formatter' => 'floatval'), 'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'), @@ -248,9 +249,22 @@ public function column($real) { $limit = null; @list($col, $limit) = explode('(', $col); - if (in_array($col, array('text', 'integer', 'float', 'boolean', 'timestamp', 'date', 'datetime', 'time'))) { + $standard = array( + 'text', + 'integer', + 'float', + 'boolean', + 'timestamp', + 'date', + 'datetime', + 'time' + ); + if (in_array($col, $standard)) { return $col; } + if ($col === 'bigint') { + return 'biginteger'; + } if (strpos($col, 'char') !== false) { return 'string'; } diff --git a/lib/Cake/Test/Case/Model/CakeSchemaTest.php b/lib/Cake/Test/Case/Model/CakeSchemaTest.php index 47effcc1461..34c782e58fc 100644 --- a/lib/Cake/Test/Case/Model/CakeSchemaTest.php +++ b/lib/Cake/Test/Case/Model/CakeSchemaTest.php @@ -198,6 +198,7 @@ class TestAppSchema extends CakeSchema { public $datatypes = array( 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), 'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => ''), + 'huge_int' => array('type' => 'biginteger'), 'bool' => array('type' => 'boolean', 'null' => false, 'default' => false), 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), 'tableParameters' => array() diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php index f590cc7a018..007f2a883e7 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php @@ -77,7 +77,7 @@ class SqliteTest extends CakeTestCase { * * @var object */ - public $fixtures = array('core.user', 'core.uuid'); + public $fixtures = array('core.user', 'core.uuid', 'core.datatype'); /** * Actual DB connection used in testing @@ -253,6 +253,16 @@ public function testBuildColumn() { $result = $this->Dbo->buildColumn($data); $expected = '"testName" integer(10) DEFAULT 10 NOT NULL'; $this->assertEquals($expected, $result); + + $data = array( + 'name' => 'huge', + 'type' => 'biginteger', + 'length' => 20, + 'null' => false, + ); + $result = $this->Dbo->buildColumn($data); + $expected = '"huge" bigint(20) NOT NULL'; + $this->assertEquals($expected, $result); } /** @@ -262,7 +272,11 @@ public function testBuildColumn() { */ public function testDescribe() { $this->loadFixtures('User'); - $Model = new Model(array('name' => 'User', 'ds' => 'test', 'table' => 'users')); + $Model = new Model(array( + 'name' => 'User', + 'ds' => 'test', + 'table' => 'users' + )); $this->Dbo->cacheSources = true; Configure::write('Cache.disable', false); @@ -310,6 +324,46 @@ public function testDescribe() { $this->assertEquals($expected, $result); } +/** + * Test that datatypes are reflected + * + * @return void + */ + public function testDatatypes() { + $this->loadFixtures('Datatype'); + $Model = new Model(array( + 'name' => 'Datatype', + 'ds' => 'test', + 'table' => 'datatypes' + )); + $result = $this->Dbo->describe($Model); + $expected = array( + 'id' => array( + 'type' => 'integer', + 'null' => false, + 'default' => 0, + 'key' => 'primary' + ), + 'float_field' => array( + 'type' => 'float', + 'length' => '5,2', + 'null' => false, + 'default' => null + ), + 'huge_int' => array( + 'type' => 'bigint', + 'length' => '20', + 'null' => true, + 'default' => null + ), + 'bool' => array( + 'type' => 'boolean', + 'null' => false, + 'default' => false + ), + ); + } + /** * test that describe does not corrupt UUID primary keys * diff --git a/lib/Cake/Test/Fixture/DatatypeFixture.php b/lib/Cake/Test/Fixture/DatatypeFixture.php index 0a9f2e067c9..5fb360e64c1 100644 --- a/lib/Cake/Test/Fixture/DatatypeFixture.php +++ b/lib/Cake/Test/Fixture/DatatypeFixture.php @@ -39,6 +39,7 @@ class DatatypeFixture extends CakeTestFixture { public $fields = array( 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), 'float_field' => array('type' => 'float', 'length' => '5,2', 'null' => false, 'default' => null), + 'huge_int' => array('type' => 'biginteger'), 'bool' => array('type' => 'boolean', 'null' => false, 'default' => false), ); @@ -48,6 +49,6 @@ class DatatypeFixture extends CakeTestFixture { * @var array */ public $records = array( - array('id' => 1, 'float_field' => 42.23, 'bool' => 0), + array('id' => 1, 'float_field' => 42.23, 'huge_int' => '123456789123456789123456789', 'bool' => 0), ); }