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