Centralize all user-side rsrc calls to go through result_source()
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource / View.pm
CommitLineData
1d521afd 1package DBIx::Class::ResultSource::View;
2
3use strict;
4use warnings;
5
7f068248 6use base 'DBIx::Class::ResultSource';
1d521afd 7
030c27ad 8__PACKAGE__->mk_group_accessors(
e55d9d89 9 'simple' => qw(is_virtual view_definition deploy_depends_on) );
1d521afd 10
1d521afd 11=head1 NAME
12
00be2e0b 13DBIx::Class::ResultSource::View - ResultSource object representing a view
1d521afd 14
15=head1 SYNOPSIS
16
03460bef 17 package MyApp::Schema::Result::Year2000CDs;
00be2e0b 18
d88ecca6 19 use base qw/DBIx::Class::Core/;
00be2e0b 20
00be2e0b 21 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
22
23 __PACKAGE__->table('year2000cds');
e570488a 24 __PACKAGE__->result_source->is_virtual(1);
25 __PACKAGE__->result_source->view_definition(
00be2e0b 26 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
b4664250 27 );
28 __PACKAGE__->add_columns(
29 'cdid' => {
30 data_type => 'integer',
31 is_auto_increment => 1,
32 },
33 'artist' => {
34 data_type => 'integer',
35 },
36 'title' => {
37 data_type => 'varchar',
38 size => 100,
39 },
40 );
00be2e0b 41
1d521afd 42=head1 DESCRIPTION
43
00be2e0b 44View object that inherits from L<DBIx::Class::ResultSource>
45
b4664250 46This class extends ResultSource to add basic view support.
00be2e0b 47
b4664250 48A view has a L</view_definition>, which contains a SQL query. The query can
49only have parameters if L</is_virtual> is set to true. It may contain JOINs,
50sub selects and any other SQL your database supports.
00be2e0b 51
52View definition SQL is deployed to your database on
53L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
54
55Deploying the view does B<not> translate it between different database
56syntaxes, so be careful what you write in your view SQL.
57
66d2a14e 58Virtual views (L</is_virtual> true), are assumed to not
00be2e0b 59exist in your database as a real view. The L</view_definition> in this
60case replaces the view name in a FROM clause in a subselect.
61
b4664250 62=head1 EXAMPLES
63
03460bef 64Having created the MyApp::Schema::Year2000CDs schema as shown in the SYNOPSIS
b4664250 65above, you can then:
66
d296da9b 67 $y2000_cds = $schema->resultset('Year2000CDs')
68 ->search()
69 ->all();
70 $count = $schema->resultset('Year2000CDs')
71 ->search()
72 ->count();
b4664250 73
74If you modified the schema to include a placeholder
75
e570488a 76 __PACKAGE__->result_source->view_definition(
c584b60b 77 "SELECT cdid, artist, title FROM cd WHERE year = ?"
b4664250 78 );
79
80and ensuring you have is_virtual set to true:
81
e570488a 82 __PACKAGE__->result_source->is_virtual(1);
b4664250 83
84You could now say:
85
d296da9b 86 $y2001_cds = $schema->resultset('Year2000CDs')
87 ->search({}, { bind => [2001] })
88 ->all();
89 $count = $schema->resultset('Year2000CDs')
90 ->search({}, { bind => [2001] })
91 ->count();
b4664250 92
00be2e0b 93=head1 SQL EXAMPLES
94
95=over
96
66d2a14e 97=item is_virtual set to false
00be2e0b 98
99 $schema->resultset('Year2000CDs')->all();
100
101 SELECT cdid, artist, title FROM year2000cds me
102
66d2a14e 103=item is_virtual set to true
00be2e0b 104
105 $schema->resultset('Year2000CDs')->all();
106
8273e845 107 SELECT cdid, artist, title FROM
00be2e0b 108 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
109
110=back
1d521afd 111
112=head1 METHODS
113
9774f48b 114=head2 is_virtual
115
e570488a 116 __PACKAGE__->result_source->is_virtual(1);
00be2e0b 117
118Set to true for a virtual view, false or unset for a real
119database-based view.
120
121=head2 view_definition
122
e570488a 123 __PACKAGE__->result_source->view_definition(
00be2e0b 124 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
125 );
126
127An SQL query for your view. Will not be translated across database
128syntaxes.
129
8273e845 130=head2 deploy_depends_on
e55d9d89 131
e570488a 132 __PACKAGE__->result_source->deploy_depends_on(
03460bef 133 ["MyApp::Schema::Result::Year","MyApp::Schema::Result::CD"]
e55d9d89 134 );
135
41d90e35 136Specify the views (and only the views) that this view depends on.
0edbc8f0 137Pass this an array reference of fully qualified result classes.
00be2e0b 138
139=head1 OVERRIDDEN METHODS
9774f48b 140
1d521afd 141=head2 from
142
143Returns the FROM entry for the table (i.e. the view name)
00be2e0b 144or the SQL as a subselect if this is a virtual view.
1d521afd 145
146=cut
147
9774f48b 148sub from {
b8e0ecca 149 $_[0]->throw_exception('from() is not a setter method') if @_ > 1;
150 $_[0]->is_virtual
151 ? \( '(' . $_[0]->view_definition .')' )
152 : $_[0]->name
153 ;
9774f48b 154}
1d521afd 155
5f2aeb09 156=head1 OTHER METHODS
1d521afd 157
5f2aeb09 158=head2 new
1d521afd 159
5f2aeb09 160The constructor.
1d521afd 161
5f2aeb09 162=cut
1d521afd 163
7364d776 164sub new {
165 my ( $self, @args ) = @_;
166 my $new = $self->next::method(@args);
7c4ade2a 167 $new->{deploy_depends_on} =
168 { map { $_ => 1 }
169 @{ $new->{deploy_depends_on} || [] } }
170 unless ref $new->{deploy_depends_on} eq 'HASH';
7364d776 171 return $new;
172}
1d521afd 173
a2bd3796 174=head1 FURTHER QUESTIONS?
00be2e0b 175
a2bd3796 176Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
66d2a14e 177
a2bd3796 178=head1 COPYRIGHT AND LICENSE
1d521afd 179
a2bd3796 180This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
181by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
182redistribute it and/or modify it under the same terms as the
183L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
1d521afd 184
185=cut
186
a2bd3796 1871;