From 95b66d19b2210b1f80fbe1b5b419f9f6526fbc08 Mon Sep 17 00:00:00 2001 From: Stephan van Diepen Date: Mon, 14 Jul 2014 11:06:47 +0200 Subject: [PATCH] Added MySQL support for Bigint. Conflicts: model/MySQLDatabase.php --- model/connect/MySQLSchemaManager.php | 16 ++++++++++++++++ model/fieldtypes/Bigint.php | 25 +++++++++++++++++++++++++ tests/model/DBFieldTest.php | 5 +++++ tests/model/DataObjectTest.php | 12 +++++++++++- 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 model/fieldtypes/Bigint.php diff --git a/model/connect/MySQLSchemaManager.php b/model/connect/MySQLSchemaManager.php index 8acae7a58..8bb0f4424 100644 --- a/model/connect/MySQLSchemaManager.php +++ b/model/connect/MySQLSchemaManager.php @@ -486,6 +486,22 @@ class MySQLSchemaManager extends DBSchemaManager { return "int(11) not null" . $this->defaultClause($values); } + /** + * Return a bigint type-formatted string + * + * @param array $values Contains a tokenised list of info about this data type + * @return string + */ + public function bigint($values) { + //For reference, this is what typically gets passed to this function: + //$parts=Array('datatype'=>'bigint', 'precision'=>20, 'null'=>'not null', 'default'=>$this->defaultVal, + // 'arrayValue'=>$this->arrayValue); + //$values=Array('type'=>'bigint', 'parts'=>$parts); + //DB::requireField($this->tableName, $this->name, $values); + + return 'bigint(20) not null' . $this->defaultClause($values); + } + /** * Return a datetime type-formatted string * For MySQL, we simply return the word 'datetime', no other parameters are necessary diff --git a/model/fieldtypes/Bigint.php b/model/fieldtypes/Bigint.php new file mode 100644 index 000000000..482d069ce --- /dev/null +++ b/model/fieldtypes/Bigint.php @@ -0,0 +1,25 @@ + 'bigint', + 'precision' => 8, + 'null' => 'not null', + 'default' => $this->defaultVal, + 'arrayValue' => $this->arrayValue + ); + + $values = array('type' => 'bigint', 'parts' => $parts); + DB::require_field($this->tableName, $this->name, $values); + } +} diff --git a/tests/model/DBFieldTest.php b/tests/model/DBFieldTest.php index 41d410f5e..68c071447 100644 --- a/tests/model/DBFieldTest.php +++ b/tests/model/DBFieldTest.php @@ -160,6 +160,11 @@ class DBFieldTest extends SapphireTest { $this->assertEquals("00:00:00", $time->getValue()); $time->setValue('00:00:00'); $this->assertEquals("00:00:00", $time->getValue()); + + /* BigInt behaviour */ + $bigInt = singleton('BigInt'); + $bigInt->setValue(PHP_INT_MAX); + $this->assertEquals(PHP_INT_MAX, $bigInt->getValue()); } public function testExists() { diff --git a/tests/model/DataObjectTest.php b/tests/model/DataObjectTest.php index 3ca70a887..add8c7ed2 100644 --- a/tests/model/DataObjectTest.php +++ b/tests/model/DataObjectTest.php @@ -1700,6 +1700,13 @@ class DataObjectTest extends SapphireTest { } + public function testBigIntField() { + $staff = new DataObjectTest_Staff(); + $staff->Salary = PHP_INT_MAX; + $staff->write(); + $this->assertEquals(PHP_INT_MAX, DataObjectTest_Staff::get()->byID($staff->ID)->Salary); + } + } class DataObjectTest_Player extends Member implements TestOnly { @@ -1913,11 +1920,14 @@ class DataObjectTest_EquipmentCompany extends DataObjectTest_Company implements class DataObjectTest_SubEquipmentCompany extends DataObjectTest_EquipmentCompany implements TestOnly { private static $db = array( - 'SubclassDatabaseField' => 'Varchar' + 'SubclassDatabaseField' => 'Varchar', ); } class DataObjectTest_Staff extends DataObject implements TestOnly { + private static $db = array( + 'Salary' => 'BigInt', + ); private static $has_one = array ( 'CurrentCompany' => 'DataObjectTest_Company', 'PreviousCompany' => 'DataObjectTest_Company'