Class::DBI::View - Virtual table for Class::DBI
package CD::Music::SalesRanking; use base qw(CD::DBI); # your Class::DBI base class use Class::DBI::View qw(TemporaryTable); __PACKAGE__->columns(All => qw(id count)); __PACKAGE__->setup_view(<<SQL); SELECT cd_id AS id, COUNT(*) AS count FROM cd_sales GROUP BY cd_id ORDER BY count LIMIT 1, 10 SQL
Class::DBI::View is a Class::DBI wrapper to make virtual VIEWs.
use Class::DBI::View qw(TemporaryTable); use Class::DBI::View qw(SubQuery); use Class::DBI::View qw(Having);
When use()ing this module, you should supply which strategy (implmentation) you use to create virtual view, which is one of 'TemporaryTable', 'SubQuery' or 'Having'.
$class->setup_view($sql [, %opt ]);
Setups virtual VIEW for $class. $sql should be a raw SQL statement to build the VIEW.
$class
$sql
%opt can be any of these:
%opt
Caches temporary table per database connection. Only valid for TemporaryTable implementation.
TemporaryTable
# creates tmp table once per session __PACKAGE__->setup_view($sql, cache_for_session => 1);
You know Class::DBI's retrieve method wants value for primary key. What if your view doesn't have primary column? Quick solution would be making primary column by combining some columns like:
retrieve
__PACKAGE__->columns(All => qw(id acc_id orgname sub_id productname)); __PACKAGE__->setup_view( <<SQL ); SELECT CONCAT(a.acc_id, '.', a.subs_id) AS id, a.acc_id, a.orgname, s.sub_id, s.productname FROM accounts a, subscriptions s WHERE a.acc_id = s.acc_id SQL
Currently update/delete/insert-related methods (like create) are not supported. Supporting it would make things too complicated IMHO. So only SELECT-related methods (search etc.) would be enough. (Patches are welcome, off course)
create
search
Tatsuhiko Miyagawa <miyagawa@bulknews.net> with feedbacks from:
Dominic Mitchell E<lt>dom@semantico.comE<gt> Tim Bunce E<lt>Tim.bunce@pobox.comE<gt>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Class::DBI
To install Class::DBI::View, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::DBI::View
CPAN shell
perl -MCPAN -e shell install Class::DBI::View
For more information on module installation, please visit the detailed CPAN module installation guide.