From c66a0badb3be418b9dcc01bd02489a84bc067886 Mon Sep 17 00:00:00 2001 From: Gabor de Mooij Date: Wed, 9 Sep 2015 23:04:17 +0200 Subject: [PATCH] Added support for money in MySQL/MariaDB. --- RedBeanPHP/QueryWriter/MySQL.php | 5 +++++ testing/RedUNIT/Mysql/Setget.php | 5 +++++ testing/RedUNIT/Mysql/Writer.php | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/RedBeanPHP/QueryWriter/MySQL.php b/RedBeanPHP/QueryWriter/MySQL.php index 1d9392aaf..9a3d9e3ab 100755 --- a/RedBeanPHP/QueryWriter/MySQL.php +++ b/RedBeanPHP/QueryWriter/MySQL.php @@ -39,6 +39,7 @@ class MySQL extends AQueryWriter implements QueryWriter const C_DATATYPE_SPECIAL_POINT = 90; const C_DATATYPE_SPECIAL_LINESTRING = 91; const C_DATATYPE_SPECIAL_POLYGON = 92; + const C_DATATYPE_SPECIAL_MONEY = 93; const C_DATATYPE_SPECIFIED = 99; @@ -113,6 +114,7 @@ public function __construct( Adapter $adapter ) MySQL::C_DATATYPE_SPECIAL_POINT => ' POINT ', MySQL::C_DATATYPE_SPECIAL_LINESTRING => ' LINESTRING ', MySQL::C_DATATYPE_SPECIAL_POLYGON => ' POLYGON ', + MySQL::C_DATATYPE_SPECIAL_MONEY => ' DECIMAL(10,2) ' ); $this->sqltype_typeno = array(); @@ -184,6 +186,9 @@ public function scanType( $value, $flagSpecial = FALSE ) if ( $value === INF ) return MySQL::C_DATATYPE_TEXT7; if ( $flagSpecial ) { + if ( preg_match( '/^\d+\.\d{2}$/', $value ) ) { + return MySQL::C_DATATYPE_SPECIAL_MONEY; + } if ( preg_match( '/^\d{4}\-\d\d-\d\d$/', $value ) ) { return MySQL::C_DATATYPE_SPECIAL_DATE; } diff --git a/testing/RedUNIT/Mysql/Setget.php b/testing/RedUNIT/Mysql/Setget.php index 11c642e1b..1c7240622 100644 --- a/testing/RedUNIT/Mysql/Setget.php +++ b/testing/RedUNIT/Mysql/Setget.php @@ -48,6 +48,11 @@ public function testNumbers() asrt( setget( "1.0" ), "1" ); asrt( setget( 1.0 ), "1" ); + asrt( setget( "3.20" ), "3.20" ); + asrt( setget( "13.20" ), "13.20" ); + asrt( setget( "134.20" ), "134.20" ); + asrt( setget( 3.21 ), '3.21' ); + asrt( setget( "0.12345678" ), "0.12345678" ); asrt( setget( 0.12345678 ), "0.12345678" ); diff --git a/testing/RedUNIT/Mysql/Writer.php b/testing/RedUNIT/Mysql/Writer.php index 3bf271fd4..597f619a1 100644 --- a/testing/RedUNIT/Mysql/Writer.php +++ b/testing/RedUNIT/Mysql/Writer.php @@ -179,6 +179,10 @@ public function testScanningAndCoding() asrt( $writer->scanType( "2001-10-10 10:00:00" ), MySQL::C_DATATYPE_TEXT7 ); + asrt( $writer->scanType( "1.23", TRUE ), MySQL::C_DATATYPE_SPECIAL_MONEY ); + asrt( $writer->scanType( "12.23", TRUE ), MySQL::C_DATATYPE_SPECIAL_MONEY ); + asrt( $writer->scanType( "124.23", TRUE ), MySQL::C_DATATYPE_SPECIAL_MONEY ); + asrt( $writer->scanType( str_repeat( "lorem ipsum", 100 ) ), MySQL::C_DATATYPE_TEXT16 ); $writer->widenColumn( "testtable", "c1", MySQL::C_DATATYPE_UINT32 ); @@ -544,6 +548,25 @@ public function testTypesDates() asrt( $cols['date'], 'date' ); } + /** + * Test money types. + * + * @return void + */ + public function testTypesMon() + { + $bean = R::dispense( 'bean' ); + + $bean->amount = '22.99'; + + R::store( $bean ); + + $cols = R::getColumns( 'bean' ); + + asrt( $cols['amount'], 'decimal(10,2)' ); + } + + /** * Date-time *