Class::DBI::Plugin::PseudoColumns - an interface to use some pseudo columns
package Music::CD; use base 'Music::DBI'; Music::CD->table('cd'); Music::CD->columns(All => qw/cdid artist title year reldate properties/); use Class::DBI::Plugin::PseudoColumns; Music::CD->pseudo_columns(properties => qw/asin tag/); use Music::CD; my $cds = Music::CD->search(artist => 'Steve Vai'); while (my $cd = $cds->next) { if ($cd->title =~ /^Real\s+Illusions/i) { $cd->asin('B0007GADZO'); } $cd->tag(['rock', 'guitar', 'tricky play']); $cd->update; } my $bought_cd = Music::CD->create({ artist => 'Rolling Stones', title => 'A Bigger Bang', year => 2005, reldate => '2005-11-22', asin => 'B000BP86LE', tag => ['rock', 'blues', 'favorite'], }); $bought_cd->set( artist => 'The Rolling Stones', title => 'A Bigger Bang - Special Edition', ); $bought_cd->update;
This module provides an easy way to use pseudo column in your Class::DBI based table classes. The ``pseudo column'' means a kind of column that is including an optical hashref string (via Data::Dumper, by default). You can get and set with using the pseudo column accessors (same as always). But, you can't use the columns' name into your SQL, SQL interfaced methods, ORDER BY clause and GROUP BY clause, etc. This module is useful when you'd like to add an unimportant column without issuing ALTER TABLE, and when you'd like to have related multiple data without normalizing table.
Class::DBI
Data::Dumper
ORDER BY
GROUP BY
ALTER TABLE
You should create a huge size column into your table. like this:
CREATE TABLE cd ( cdid int UNSIGNED auto_increment, artist varchar(255), title varchar(255), year int, reldate date, properties text, # create for using pseudo column PRIMARY KEY (cdid) );
Almost same as usual.
package Music::CD; use base 'Music::DBI'; Music::CD->table('cd'); Music::CD->columns(All => qw/cdid artist title year reldate properties/);
You will be able to use pseudo column with only to use this module.
use
use Class::DBI::Plugin::PseudoColumns;
Music::CD->pseudo_columns(properties => qw/asin tag/);
This module provides following class methods.
create(\%data);
create method works almost same as Class::DBI::create() if there's some pseudo column in %data.
create
Class::DBI::create()
%data
set(column => value[, column2 => value2, ...]);
set method works almost same as Class::DBI::set() if there's some pseudo column in argument.
set
Class::DBI::set()
pseudo_columns([parent_colname => ('pseudo_column1'[, 'pseudo_column2' ...])]);
You can set a pseudo columns' name with parent column's name. ``pseudo_column1'' ... will provide to you each pseudo column's accessor.
if you want to take a list of pseudo columns, you can pass one argument to this method when you want to specify grouped parent column name.
my @p_columns = Music::CD->pseudo_columns('properties');
And if you want to take all columns list of pseudo columns, you don't need to pass any argument to this method.
my @all_p_columns = Music::CD->pseudo_columns();
serializer(parent_colname => \&serializer_sub);
You can set a specific serializing function for your pseudo columns.
use Storable (); __PACKAGE__->serializer(properties => \&Storable::nfreeze);
The default serializer is Data::Dumper::Dumper (when you don't specify).
Data::Dumper::Dumper
deserializer(parent_coluname => \&deserializer_sub);
You can set a specific deserializing function for your pseudo columns.
use Storable (); __PACKAGE__->deserializer(properties => \&Storable::thaw);
The default deserializer calls eval() (when you don't specify) for the dumped optical hashref string.
eval()
NOTE: The subref for serializer/deserializer must return a really ``storable'' string for your database. example of above works under a MySQL environment, but you have to change to use some another ``storable'' filter (like MIME::Base64) under SQLite environment (see t/05_serializer.t)
MIME::Base64
Koichi Taniguchi <taniguchi@livedoor.jp>
Copyright (c) 2006 Koichi Taniguchi. Japan. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Class::DBI, Data::Dumper
To install Class::DBI::Plugin::PseudoColumns, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::DBI::Plugin::PseudoColumns
CPAN shell
perl -MCPAN -e shell install Class::DBI::Plugin::PseudoColumns
For more information on module installation, please visit the detailed CPAN module installation guide.