NAME
DBIx::Class::FrozenColumns - Store virtual columns inside another column.
SYNOPSIS
package Artist;
__PACKAGE__->load_components(qw/FrozenColumns Core/);
__PACKAGE__->add_columns(qw/name description frozen/);
__PACKAGE__->add_frozen_columns(
frozen => qw/biography url img50x50 img100x100/
);
$artist->url('http://cpan.org');
$artist->get_column('url');
$artist->get_dirty_columns; # 'url' and 'frozen' are dirty
$artist->update; #updates column 'frozen' (using Storable::freeze)
$artistRS->create({
name => 'theodor bastard',
img50x50 => '50x50.gif',
}); #that's ok. 'img50x50' will be stored in 'frozen'
my @artists = $artistRS->search({
name => '.....',
url => 'http://cpan.org',
}); # Error! no such column 'url'
package Artist;
__PACKAGE__->add_frozen_columns(
biography => qw/childhood adolescence youth present/,
);
$artist->childhood('bla-bla-bla');
$artist->update; #Updates column 'frozen'.
DESCRIPTION
This module allows you to store multiple columns in one. This is useful when you want to store dynamic number of columns in database or you just don't know what columns will be stored there. Or when you can't (or don't want) to alter your tables in database.
Module allows you to transparently use this columns as if they were normal columns in your table. With one obvious restriction: you cannot search rows in a table and therefore you cannot add relationships using these columns (search is needed to build reverse relationship).
Module handles its own dirty column management and will not update the parent field unless any columns is changed.
Note: The component needs to be loaded before Core and plugin 'Ordered'. If you get an error like 'no such column: <frozencolumn>' while updating a row then try to move this module more closer to the start of the load_components list.
Also note that frozen column IS NOT a real column of your result class. This impose some restrictions on use of this columns such as searching, adding relationships, has_column, get_columns, etc. See "EXTENDED METHODS" for the list of method that will work with frozen columns (as will methods that use it).
Module unpacks frozen columns only once when you first accessing it and packs when you call update.
You can also create frozen columns in another frozen column any level deep. The only restriction is that they all use the same storing mechanism.
METHODS
add_frozen_columns
__PACKAGE__->add_frozen_columns ($data_column, @columns)
__PACKAGE__->add_frozen_columns ($hashref)
Adds frozen @columns to your result source class. These columns will be stored in $data_column using Storable freeze/thaw algorithm. If $hashref is specified instead, then below params is expected in it: data_column - same as $data_column columns - same as @columns type - class with custom mechanism of storing/restoring frozen cols See below for more information about "Custom frozen class".
add_dumped_columns ($data_column, @columns)
Same as "add_frozen_columns" but uses Data::Dumper mechanism.
add_json_columns ($data_column, @columns)
Same as "add_frozen_columns" but uses JSON::XS mechanism.
frozen_columns
Returns hash of frozen columns where keys are the names of fcolumns and values are hashes with the following properties:
'type' - type of fcolumn (frozen or dumped or some custom). 'column' - parent column where fcolumn is stored.
frozen_columns_list
Returns list of names of all frozen columns registered with the result source.
EXTENDED METHODS
new
Accepts initial values for frozen columns.
$artistRS->new({img50x50 => '50x50.gif'});
get_column
get_columns
Returns DBIC's get_columns with frozen columns hash. IMPORTANT: until $row is not in storage this method will return basic get_columns result without frozen columns. This is needed for correct work of insert method.
store_column
set_column
get_dirty_columns
Returns real and frozen dirty columns. Note that changing frozen column will result in marking at least 2 columns as dirty.
has_column_loaded
Returns true if data_column of frozen column has loaded.
is_column_changed
is_changed
update
insert
Custom frozen class
Such a class must be derived from 'DBIx::Class::FrozenColumns::Base' and is responsible for fetching and storing frozen columns to/from a real database column. The corresponding methods are 'recover' and 'stringify'.
The best explanation is an expamle:
package DBIx::Class::FrozenColumns::Frozen;
use base qw/DBIx::Class::FrozenColumns::Base/;
use strict;
use Storable qw/freeze thaw/;
sub stringify {
freeze(shift);
}
sub recover {
my ($this, $dataref) = @_;
my $data = defined $$dataref ? eval {thaw($$dataref)} || {} : {};
bless ($data, ref $this || $this);
}
Information actually stored in database can be used by any other programs as a simple hash (possibly containing another hashes like itself).
CAVEATS
You cannot search rows in a table using frozen columns
You cannot add relationships using frozen columns
SEE ALSO
AUTHOR
Pronin Oleg <syber@cpan.org>
LICENSE
You may distribute this code under the same terms as Perl itself.