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)
16 DBIx::Class::ResultSource::View - ResultSource object representing a view
20 package MyDB::Schema::Result::Year2000CDs;
22 use base qw/DBIx::Class/;
24 __PACKAGE__->load_components('Core');
25 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
27 __PACKAGE__->table('year2000cds');
28 __PACKAGE__->result_source_instance->is_virtual(1);
29 __PACKAGE__->result_source_instance->view_definition(
30 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
32 __PACKAGE__->add_columns(
34 data_type => 'integer',
35 is_auto_increment => 1,
38 data_type => 'integer',
41 data_type => 'varchar',
48 View object that inherits from L<DBIx::Class::ResultSource>
50 This class extends ResultSource to add basic view support.
52 A view has a L</view_definition>, which contains a SQL query. The query can
53 only have parameters if L</is_virtual> is set to true. It may contain JOINs,
54 sub selects and any other SQL your database supports.
56 View definition SQL is deployed to your database on
57 L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
59 Deploying the view does B<not> translate it between different database
60 syntaxes, so be careful what you write in your view SQL.
62 Virtual views (L</is_virtual> true), are assumed to not
63 exist in your database as a real view. The L</view_definition> in this
64 case replaces the view name in a FROM clause in a subselect.
68 Having created the MyDB::Schema::Year2000CDs schema as shown in the SYNOPSIS
71 $2000_cds = $schema->resultset('Year2000CDs')
74 $count = $schema->resultset('Year2000CDs')
78 If you modified the schema to include a placeholder
80 __PACKAGE__->result_source_instance->view_definition(
81 "SELECT cdid, artist, title FROM cd WHERE year ='?'"
84 and ensuring you have is_virtual set to true:
86 __PACKAGE__->result_source_instance->is_virtual(1);
90 $2001_cds = $schema->resultset('Year2000CDs')
91 ->search({}, { bind => [2001] })
93 $count = $schema->resultset('Year2000CDs')
94 ->search({}, { bind => [2001] })
101 =item is_virtual set to false
103 $schema->resultset('Year2000CDs')->all();
105 SELECT cdid, artist, title FROM year2000cds me
107 =item is_virtual set to true
109 $schema->resultset('Year2000CDs')->all();
111 SELECT cdid, artist, title FROM
112 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
120 __PACKAGE__->result_source_instance->is_virtual(1);
122 Set to true for a virtual view, false or unset for a real
125 =head2 view_definition
127 __PACKAGE__->result_source_instance->view_definition(
128 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
131 An SQL query for your view. Will not be translated across database
135 =head1 OVERRIDDEN METHODS
139 Returns the FROM entry for the table (i.e. the view name)
140 or the SQL as a subselect if this is a virtual view.
146 return \"(${\$self->view_definition})" if $self->is_virtual;
154 Matt S. Trout <mst@shadowcatsystems.co.uk>
156 With Contributions from:
158 Guillermo Roditi E<lt>groditi@cpan.orgE<gt>
160 Jess Robinson <castaway@desert-island.me.uk>
162 Wallace Reis <wreis@cpan.org>
166 You may distribute this code under the same terms as Perl itself.