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