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