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