Skip to content

Commit

Permalink
Add support Postgres JSON columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Jan 23, 2016
1 parent 621b14a commit 92908de
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions RedBeanPHP/QueryWriter/PostgreSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PostgreSQL extends AQueryWriter implements QueryWriter
const C_DATATYPE_SPECIAL_MONEY = 93;
const C_DATATYPE_SPECIAL_POLYGON = 94;
const C_DATATYPE_SPECIAL_MONEY2 = 95; //Numbers only money, i.e. fixed point numeric
const C_DATATYPE_SPECIAL_JSON = 96;
const C_DATATYPE_SPECIFIED = 99;

/**
Expand Down Expand Up @@ -139,6 +140,7 @@ public function __construct( Adapter $adapter )
self::C_DATATYPE_SPECIAL_MONEY => ' money ',
self::C_DATATYPE_SPECIAL_MONEY2 => ' numeric(10,2) ',
self::C_DATATYPE_SPECIAL_POLYGON => ' polygon ',
self::C_DATATYPE_SPECIAL_JSON => ' json ',
);

$this->sqltype_typeno = array();
Expand Down Expand Up @@ -237,6 +239,10 @@ public function scanType( $value, $flagSpecial = FALSE )
if ( preg_match( '/^-?\d+\.\d{2}$/', $value ) ) {
return PostgreSQL::C_DATATYPE_SPECIAL_MONEY2;
}

if ( is_array( @json_decode( $value, TRUE ) ) ) {
return PostgreSQL::C_DATATYPE_SPECIAL_JSON;
}
}

if ( is_float( $value ) ) return self::C_DATATYPE_DOUBLE;
Expand Down
18 changes: 18 additions & 0 deletions testing/RedUNIT/Postgres/Setget.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
*/
class Setget extends Postgres
{
/**
* Test support for Postgres JSON columns.
*/
public function testJSON()
{
asrt( setget( json_encode( array( 'can' => 'store json?' ) ) ), json_encode( array( 'can' => 'store json?' ) ) );
R::nuke();
$bean = R::dispense( 'blob' );
$bean->content = json_encode( array( 'hello' => 'world' ) );
R::store( $bean );
$columns = R::inspect( 'blob' );
asrt( $columns['content'], 'json' );
$bean = R::load( 'blob', $bean->id );
asrt( $bean->content, json_encode( array( 'hello' => 'world' ) ) );
$data = R::getCell( 'SELECT json_extract_path(content, \'hello\') FROM blob LIMIT 1' );
asrt( $data, '"world"' );
}

/**
* Test numbers.
*
Expand Down
10 changes: 10 additions & 0 deletions testing/RedUNIT/Postgres/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public function testScanningAndCoding()

asrt( $writer->scanType( str_repeat( "lorem ipsum", 100 ) ), PostgreSQL::C_DATATYPE_TEXT );

asrt( $writer->scanType( json_encode( array( 1, 2 ) ), TRUE ), PostgreSQL::C_DATATYPE_SPECIAL_JSON );

asrt( $writer->scanType( json_encode( array( 'hello' => 'json', 'hello' => array( 'objects' ) ) ), TRUE ), PostgreSQL::C_DATATYPE_SPECIAL_JSON );

$writer->widenColumn( "testtable", "c1", PostgreSQL::C_DATATYPE_TEXT );

$cols = $writer->getColumns( "testtable" );
Expand All @@ -118,6 +122,12 @@ public function testScanningAndCoding()

asrt( $writer->code( $cols['special'], FALSE ), PostgreSQL::C_DATATYPE_SPECIFIED );

$writer->addColumn( "testtable", "special3", PostgreSQL::C_DATATYPE_SPECIAL_JSON );

$cols = $writer->getColumns( "testtable" );

asrt( $writer->code( $cols['special3'], TRUE ), PostgreSQL::C_DATATYPE_SPECIAL_JSON );

//$id = $writer->insertRecord("testtable", array("c1"), array(array("lorem ipsum")));

$id = $writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "lorem ipsum" ) ) );
Expand Down

0 comments on commit 92908de

Please sign in to comment.