1 package DBIx::Class::ResultSource::View;
6 use DBIx::Class::ResultSet;
8 use base qw/DBIx::Class/;
9 __PACKAGE__->load_components(qw/ResultSource/);
10 __PACKAGE__->mk_group_accessors(
11 'simple' => qw(is_virtual view_definition depends_on)
15 my $new = shift->next::method(@_);
16 $new->{depends_on} = { %{$new->{depends_on}||{}} };
22 DBIx::Class::ResultSource::View - ResultSource object representing a view
26 package MyDB::Schema::Result::Year2000CDs;
28 use base qw/DBIx::Class/;
30 __PACKAGE__->load_components('Core');
31 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
33 __PACKAGE__->table('year2000cds');
34 __PACKAGE__->result_source_instance->is_virtual(1);
35 __PACKAGE__->result_source_instance->view_definition(
36 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
38 __PACKAGE__->add_columns(
40 data_type => 'integer',
41 is_auto_increment => 1,
44 data_type => 'integer',
47 data_type => 'varchar',
54 View object that inherits from L<DBIx::Class::ResultSource>
56 This class extends ResultSource to add basic view support.
58 A view has a L</view_definition>, which contains a SQL query. The query can
59 only have parameters if L</is_virtual> is set to true. It may contain JOINs,
60 sub selects and any other SQL your database supports.
62 View definition SQL is deployed to your database on
63 L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
65 Deploying the view does B<not> translate it between different database
66 syntaxes, so be careful what you write in your view SQL.
68 Virtual views (L</is_virtual> true), are assumed to not
69 exist in your database as a real view. The L</view_definition> in this
70 case replaces the view name in a FROM clause in a subselect.
74 Having created the MyDB::Schema::Year2000CDs schema as shown in the SYNOPSIS
77 $2000_cds = $schema->resultset('Year2000CDs')
80 $count = $schema->resultset('Year2000CDs')
84 If you modified the schema to include a placeholder
86 __PACKAGE__->result_source_instance->view_definition(
87 "SELECT cdid, artist, title FROM cd WHERE year ='?'"
90 and ensuring you have is_virtual set to true:
92 __PACKAGE__->result_source_instance->is_virtual(1);
96 $2001_cds = $schema->resultset('Year2000CDs')
97 ->search({}, { bind => [2001] })
99 $count = $schema->resultset('Year2000CDs')
100 ->search({}, { bind => [2001] })
107 =item is_virtual set to false
109 $schema->resultset('Year2000CDs')->all();
111 SELECT cdid, artist, title FROM year2000cds me
113 =item is_virtual set to true
115 $schema->resultset('Year2000CDs')->all();
117 SELECT cdid, artist, title FROM
118 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
126 __PACKAGE__->result_source_instance->is_virtual(1);
128 Set to true for a virtual view, false or unset for a real
131 =head2 view_definition
133 __PACKAGE__->result_source_instance->view_definition(
134 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
137 An SQL query for your view. Will not be translated across database
141 =head1 OVERRIDDEN METHODS
145 Returns the FROM entry for the table (i.e. the view name)
146 or the SQL as a subselect if this is a virtual view.
152 return \"(${\$self->view_definition})" if $self->is_virtual;
160 Matt S. Trout <mst@shadowcatsystems.co.uk>
162 With Contributions from:
164 Guillermo Roditi E<lt>groditi@cpan.orgE<gt>
166 Jess Robinson <castaway@desert-island.me.uk>
168 Wallace Reis <wreis@cpan.org>
172 You may distribute this code under the same terms as Perl itself.