diff --git a/src/Pluf/DB.php b/src/Pluf/DB.php index 2b6398a..166dd85 100644 --- a/src/Pluf/DB.php +++ b/src/Pluf/DB.php @@ -122,6 +122,8 @@ function Pluf_DB_defaultTypecast() array('Pluf_DB_IdentityFromDb', 'Pluf_DB_IdentityToDb'), 'Pluf_DB_Field_Serialized' => array('Pluf_DB_SerializedFromDb', 'Pluf_DB_SerializedToDb'), + 'Pluf_DB_Field_Compressed' => + array('Pluf_DB_CompressedFromDb', 'Pluf_DB_CompressedToDb'), ); } @@ -159,13 +161,6 @@ function Pluf_DB_SerializedFromDb($val) return $val; } -/** - * Identity function. - * - * @param mixed Value. - * @param object Database handler. - * @return string Ready to use for SQL. - */ function Pluf_DB_SerializedToDb($val, $db) { if (is_null($val)) { @@ -174,6 +169,16 @@ function Pluf_DB_SerializedToDb($val, $db) return $db->esc(serialize($val)); } +function Pluf_DB_CompressedFromDb($val) +{ + return ($val) ? gzinflate($val) : $val; +} + +function Pluf_DB_CompressedToDb($val, $db) +{ + return (is_null($val)) ? 'NULL' : $db->esc(gzdeflate($val, 9)); +} + function Pluf_DB_BooleanFromDb($val) { if ($val) { return true; diff --git a/src/Pluf/DB/Field/Compressed.php b/src/Pluf/DB/Field/Compressed.php new file mode 100644 index 0000000..8c5d166 --- /dev/null +++ b/src/Pluf/DB/Field/Compressed.php @@ -0,0 +1,42 @@ +_a['verbose'] = 'compressed'; + $this->_a['table'] = 'compressed'; + $this->_a['model'] = __CLASS__; + $this->_a['cols'] = array( + // It is mandatory to have an "id" column. + 'id' => + array( + 'type' => 'Pluf_DB_Field_Sequence', + //It is automatically added. + 'blank' => true, + ), + 'compressed' => + array( + 'type' => 'Pluf_DB_Field_Compressed', + ), + ); + } +} + + +class Pluf_Tests_Model_CompressedField extends UnitTestCase { + + function __construct() + { + parent::__construct('Test the compressed field.'); + } + + function testCreate() + { + $db = Pluf::db(); + $schema = new Pluf_DB_Schema($db); + $m = new Pluf_Tests_Model_CompressedField_Model(); + $schema->model = $m; + $schema->createTables(); + $m->compressed = 'Youplaboum'; + $m->create(); + $this->assertEqual(1, $m->id); + $m = new Pluf_Tests_Model_CompressedField_Model(1); + $this->assertEqual('Youplaboum', $m->compressed); + $schema->dropTables(); + } +} \ No newline at end of file