dependency for views
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource / View.pm
CommitLineData
1d521afd 1package DBIx::Class::ResultSource::View;
2
3use strict;
4use warnings;
5
6use DBIx::Class::ResultSet;
7
8use base qw/DBIx::Class/;
9__PACKAGE__->load_components(qw/ResultSource/);
030c27ad 10__PACKAGE__->mk_group_accessors(
943538a0 11 'simple' => qw(is_virtual view_definition depends_on)
030c27ad 12);
1d521afd 13
943538a0 14sub new {
15 my $new = shift->next::method(@_);
16 $new->{depends_on} = { %{$new->{depends_on}||{}} };
17 return $new;
18}
19
1d521afd 20=head1 NAME
21
00be2e0b 22DBIx::Class::ResultSource::View - ResultSource object representing a view
1d521afd 23
24=head1 SYNOPSIS
25
16ccb4fe 26 package MyDB::Schema::Result::Year2000CDs;
00be2e0b 27
b4664250 28 use base qw/DBIx::Class/;
00be2e0b 29
30 __PACKAGE__->load_components('Core');
31 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
32
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'"
b4664250 37 );
38 __PACKAGE__->add_columns(
39 'cdid' => {
40 data_type => 'integer',
41 is_auto_increment => 1,
42 },
43 'artist' => {
44 data_type => 'integer',
45 },
46 'title' => {
47 data_type => 'varchar',
48 size => 100,
49 },
50 );
00be2e0b 51
1d521afd 52=head1 DESCRIPTION
53
00be2e0b 54View object that inherits from L<DBIx::Class::ResultSource>
55
b4664250 56This class extends ResultSource to add basic view support.
00be2e0b 57
b4664250 58A view has a L</view_definition>, which contains a SQL query. The query can
59only have parameters if L</is_virtual> is set to true. It may contain JOINs,
60sub selects and any other SQL your database supports.
00be2e0b 61
62View definition SQL is deployed to your database on
63L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
64
65Deploying the view does B<not> translate it between different database
66syntaxes, so be careful what you write in your view SQL.
67
66d2a14e 68Virtual views (L</is_virtual> true), are assumed to not
00be2e0b 69exist in your database as a real view. The L</view_definition> in this
70case replaces the view name in a FROM clause in a subselect.
71
b4664250 72=head1 EXAMPLES
73
74Having created the MyDB::Schema::Year2000CDs schema as shown in the SYNOPSIS
75above, you can then:
76
77 $2000_cds = $schema->resultset('Year2000CDs')
78 ->search()
79 ->all();
80 $count = $schema->resultset('Year2000CDs')
81 ->search()
82 ->count();
83
84If you modified the schema to include a placeholder
85
86 __PACKAGE__->result_source_instance->view_definition(
87 "SELECT cdid, artist, title FROM cd WHERE year ='?'"
88 );
89
90and ensuring you have is_virtual set to true:
91
92 __PACKAGE__->result_source_instance->is_virtual(1);
93
94You could now say:
95
96 $2001_cds = $schema->resultset('Year2000CDs')
97 ->search({}, { bind => [2001] })
98 ->all();
99 $count = $schema->resultset('Year2000CDs')
100 ->search({}, { bind => [2001] })
101 ->count();
102
00be2e0b 103=head1 SQL EXAMPLES
104
105=over
106
66d2a14e 107=item is_virtual set to false
00be2e0b 108
109 $schema->resultset('Year2000CDs')->all();
110
111 SELECT cdid, artist, title FROM year2000cds me
112
66d2a14e 113=item is_virtual set to true
00be2e0b 114
115 $schema->resultset('Year2000CDs')->all();
116
117 SELECT cdid, artist, title FROM
118 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
119
120=back
1d521afd 121
122=head1 METHODS
123
9774f48b 124=head2 is_virtual
125
00be2e0b 126 __PACKAGE__->result_source_instance->is_virtual(1);
127
128Set to true for a virtual view, false or unset for a real
129database-based view.
130
131=head2 view_definition
132
133 __PACKAGE__->result_source_instance->view_definition(
134 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
135 );
136
137An SQL query for your view. Will not be translated across database
138syntaxes.
139
140
141=head1 OVERRIDDEN METHODS
9774f48b 142
1d521afd 143=head2 from
144
145Returns the FROM entry for the table (i.e. the view name)
00be2e0b 146or the SQL as a subselect if this is a virtual view.
1d521afd 147
148=cut
149
9774f48b 150sub from {
151 my $self = shift;
152 return \"(${\$self->view_definition})" if $self->is_virtual;
153 return $self->name;
154}
1d521afd 155
1561;
157
158=head1 AUTHORS
159
160Matt S. Trout <mst@shadowcatsystems.co.uk>
161
162With Contributions from:
163
164Guillermo Roditi E<lt>groditi@cpan.orgE<gt>
165
00be2e0b 166Jess Robinson <castaway@desert-island.me.uk>
167
66d2a14e 168Wallace Reis <wreis@cpan.org>
169
1d521afd 170=head1 LICENSE
171
172You may distribute this code under the same terms as Perl itself.
173
174=cut
175