remove (groditi) instances
[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
00be2e0b 20 package MyDB::Schema::Year2000CDs;
21
22 use DBIx::Class::ResultSource::View;
23
24 __PACKAGE__->load_components('Core');
25 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
26
27 __PACKAGE__->table('year2000cds');
28 __PACKAGE__->result_source_instance->is_virtual(1);
29 __PACKAGE__->result_source_instance->view_definition(
30 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
31 );
32
1d521afd 33=head1 DESCRIPTION
34
00be2e0b 35View object that inherits from L<DBIx::Class::ResultSource>
36
37This class extends ResultSource to add basic view support.
38
39A view has a L</view_definition>, which contains an SQL query. The
40query cannot have parameters. It may contain JOINs, sub selects and
41any other SQL your database supports.
42
43View definition SQL is deployed to your database on
44L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
45
46Deploying the view does B<not> translate it between different database
47syntaxes, so be careful what you write in your view SQL.
48
49Virtual views (L</is_virtual> unset or false), are assumed to not
50exist in your database as a real view. The L</view_definition> in this
51case replaces the view name in a FROM clause in a subselect.
52
53=head1 SQL EXAMPLES
54
55=over
56
57=item is_virtual set to true
58
59 $schema->resultset('Year2000CDs')->all();
60
61 SELECT cdid, artist, title FROM year2000cds me
62
63=item is_virtual set to false
64
65 $schema->resultset('Year2000CDs')->all();
66
67 SELECT cdid, artist, title FROM
68 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
69
70=back
1d521afd 71
72=head1 METHODS
73
9774f48b 74=head2 is_virtual
75
00be2e0b 76 __PACKAGE__->result_source_instance->is_virtual(1);
77
78Set to true for a virtual view, false or unset for a real
79database-based view.
80
81=head2 view_definition
82
83 __PACKAGE__->result_source_instance->view_definition(
84 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
85 );
86
87An SQL query for your view. Will not be translated across database
88syntaxes.
89
90
91=head1 OVERRIDDEN METHODS
9774f48b 92
1d521afd 93=head2 from
94
95Returns the FROM entry for the table (i.e. the view name)
00be2e0b 96or the SQL as a subselect if this is a virtual view.
1d521afd 97
98=cut
99
9774f48b 100sub from {
101 my $self = shift;
102 return \"(${\$self->view_definition})" if $self->is_virtual;
103 return $self->name;
104}
1d521afd 105
1061;
107
108=head1 AUTHORS
109
110Matt S. Trout <mst@shadowcatsystems.co.uk>
111
112With Contributions from:
113
114Guillermo Roditi E<lt>groditi@cpan.orgE<gt>
115
00be2e0b 116Jess Robinson <castaway@desert-island.me.uk>
117
1d521afd 118=head1 LICENSE
119
120You may distribute this code under the same terms as Perl itself.
121
122=cut
123