Skip to content

Commit

Permalink
Added support for money in MySQL/MariaDB.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Sep 9, 2015
1 parent 9f48472 commit c66a0ba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions RedBeanPHP/QueryWriter/MySQL.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions testing/RedUNIT/Mysql/Setget.php
Expand Up @@ -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" );

Expand Down
23 changes: 23 additions & 0 deletions testing/RedUNIT/Mysql/Writer.php
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit c66a0ba

Please sign in to comment.