Tatsuhiko Miyagawa > Class-DBI-View > Class::DBI::View

Download:
Class-DBI-View-0.07.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 0.07   Source  

NAME ^

Class::DBI::View - Virtual table for Class::DBI

SYNOPSIS ^

  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

DESCRIPTION ^

Class::DBI::View is a Class::DBI wrapper to make virtual VIEWs.

METHODS ^

import
  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'.

setup_view
  $class->setup_view($sql [, %opt ]);

Setups virtual VIEW for $class. $sql should be a raw SQL statement to build the VIEW.

%opt can be any of these:

cache_for_session

Caches temporary table per database connection. Only valid for TemporaryTable implementation.

  # creates tmp table once per session
  __PACKAGE__->setup_view($sql, cache_for_session => 1);

TIPS AND TRAPS ^

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:

  __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

NOTES ^

AUTHOR ^

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.

SEE ALSO ^

Class::DBI

syntax highlighting: