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