1 package DBIx::Class::ResultSource::View;
6 use DBIx::Class::ResultSet;
7 use SQL::Translator::Parser::DBIx::Class;
8 use base qw/DBIx::Class/;
9 __PACKAGE__->load_components(qw/ResultSource/);
10 __PACKAGE__->mk_group_accessors(
11 'simple' => qw(is_virtual view_definition deploy_depends_on) );
15 DBIx::Class::ResultSource::View - ResultSource object representing a view
19 package MyDB::Schema::Result::Year2000CDs;
21 use base qw/DBIx::Class::Core/;
23 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
25 __PACKAGE__->table('year2000cds');
26 __PACKAGE__->result_source_instance->is_virtual(1);
27 __PACKAGE__->result_source_instance->view_definition(
28 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
30 __PACKAGE__->add_columns(
32 data_type => 'integer',
33 is_auto_increment => 1,
36 data_type => 'integer',
39 data_type => 'varchar',
46 View object that inherits from L<DBIx::Class::ResultSource>
48 This class extends ResultSource to add basic view support.
50 A view has a L</view_definition>, which contains a SQL query. The query can
51 only have parameters if L</is_virtual> is set to true. It may contain JOINs,
52 sub selects and any other SQL your database supports.
54 View definition SQL is deployed to your database on
55 L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
57 Deploying the view does B<not> translate it between different database
58 syntaxes, so be careful what you write in your view SQL.
60 Virtual views (L</is_virtual> true), are assumed to not
61 exist in your database as a real view. The L</view_definition> in this
62 case replaces the view name in a FROM clause in a subselect.
66 Having created the MyDB::Schema::Year2000CDs schema as shown in the SYNOPSIS
69 $2000_cds = $schema->resultset('Year2000CDs')
72 $count = $schema->resultset('Year2000CDs')
76 If you modified the schema to include a placeholder
78 __PACKAGE__->result_source_instance->view_definition(
79 "SELECT cdid, artist, title FROM cd WHERE year ='?'"
82 and ensuring you have is_virtual set to true:
84 __PACKAGE__->result_source_instance->is_virtual(1);
88 $2001_cds = $schema->resultset('Year2000CDs')
89 ->search({}, { bind => [2001] })
91 $count = $schema->resultset('Year2000CDs')
92 ->search({}, { bind => [2001] })
99 =item is_virtual set to false
101 $schema->resultset('Year2000CDs')->all();
103 SELECT cdid, artist, title FROM year2000cds me
105 =item is_virtual set to true
107 $schema->resultset('Year2000CDs')->all();
109 SELECT cdid, artist, title FROM
110 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
118 __PACKAGE__->result_source_instance->is_virtual(1);
120 Set to true for a virtual view, false or unset for a real
123 =head2 view_definition
125 __PACKAGE__->result_source_instance->view_definition(
126 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
129 An SQL query for your view. Will not be translated across database
132 =head2 deploy_depends_on
134 __PACKAGE__->result_source_instance->deploy_depends_on(
135 "MyDB::Schema::Result::Year","MyDB::Schema::Result::CD"
138 Specify the result classes that comprise this view. Pass this
141 =head1 OVERRIDDEN METHODS
145 The constructor. This is a private method, as only other DBIC modules
151 my ( $self, @args ) = @_;
152 my $new = $self->next::method(@args);
153 $new->{deploy_depends_on}
154 = { map { $_ => 1 } @{ ($new->{deploy_depends_on} || []) } };
156 print STDERR Dumper $new;
162 Returns the FROM entry for the table (i.e. the view name)
163 or the SQL as a subselect if this is a virtual view.
169 return \"(${\$self->view_definition})" if $self->is_virtual;
173 =head1 PRIVATE METHODS
175 =head2 deploy_depends_on
177 An internal method for the construction of a hashref of the view's
178 superclasses, e.g., the sources that comprise it.
186 Matt S. Trout <mst@shadowcatsystems.co.uk>
188 With Contributions from:
190 Guillermo Roditi E<lt>groditi@cpan.orgE<gt>
192 Jess Robinson <castaway@desert-island.me.uk>
194 Wallace Reis <wreis@cpan.org>
196 Amiri Barksdale <amiri@metalabel.com>
200 You may distribute this code under the same terms as Perl itself.