Comprehensive diagnostic on incorrect ResultSource metadata use
[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
53View definition SQL is deployed to your database on
54L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
55
56Deploying the view does B<not> translate it between different database
57syntaxes, so be careful what you write in your view SQL.
58
66d2a14e 59Virtual views (L</is_virtual> true), are assumed to not
00be2e0b 60exist in your database as a real view. The L</view_definition> in this
61case replaces the view name in a FROM clause in a subselect.
62
b4664250 63=head1 EXAMPLES
64
03460bef 65Having created the MyApp::Schema::Year2000CDs schema as shown in the SYNOPSIS
b4664250 66above, you can then:
67
d296da9b 68 $y2000_cds = $schema->resultset('Year2000CDs')
69 ->search()
70 ->all();
71 $count = $schema->resultset('Year2000CDs')
72 ->search()
73 ->count();
b4664250 74
75If you modified the schema to include a placeholder
76
e570488a 77 __PACKAGE__->result_source->view_definition(
c584b60b 78 "SELECT cdid, artist, title FROM cd WHERE year = ?"
b4664250 79 );
80
81and ensuring you have is_virtual set to true:
82
e570488a 83 __PACKAGE__->result_source->is_virtual(1);
b4664250 84
85You could now say:
86
d296da9b 87 $y2001_cds = $schema->resultset('Year2000CDs')
88 ->search({}, { bind => [2001] })
89 ->all();
90 $count = $schema->resultset('Year2000CDs')
91 ->search({}, { bind => [2001] })
92 ->count();
b4664250 93
00be2e0b 94=head1 SQL EXAMPLES
95
96=over
97
66d2a14e 98=item is_virtual set to false
00be2e0b 99
100 $schema->resultset('Year2000CDs')->all();
101
102 SELECT cdid, artist, title FROM year2000cds me
103
66d2a14e 104=item is_virtual set to true
00be2e0b 105
106 $schema->resultset('Year2000CDs')->all();
107
8273e845 108 SELECT cdid, artist, title FROM
00be2e0b 109 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
110
111=back
1d521afd 112
113=head1 METHODS
114
9774f48b 115=head2 is_virtual
116
e570488a 117 __PACKAGE__->result_source->is_virtual(1);
00be2e0b 118
119Set to true for a virtual view, false or unset for a real
120database-based view.
121
122=head2 view_definition
123
e570488a 124 __PACKAGE__->result_source->view_definition(
00be2e0b 125 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
126 );
127
128An SQL query for your view. Will not be translated across database
129syntaxes.
130
8273e845 131=head2 deploy_depends_on
e55d9d89 132
e570488a 133 __PACKAGE__->result_source->deploy_depends_on(
03460bef 134 ["MyApp::Schema::Result::Year","MyApp::Schema::Result::CD"]
e55d9d89 135 );
136
41d90e35 137Specify the views (and only the views) that this view depends on.
0edbc8f0 138Pass this an array reference of fully qualified result classes.
00be2e0b 139
140=head1 OVERRIDDEN METHODS
9774f48b 141
1d521afd 142=head2 from
143
144Returns the FROM entry for the table (i.e. the view name)
00be2e0b 145or the SQL as a subselect if this is a virtual view.
1d521afd 146
147=cut
148
9774f48b 149sub from {
b8e0ecca 150 $_[0]->throw_exception('from() is not a setter method') if @_ > 1;
151 $_[0]->is_virtual
152 ? \( '(' . $_[0]->view_definition .')' )
153 : $_[0]->name
154 ;
9774f48b 155}
1d521afd 156
5f2aeb09 157=head1 OTHER METHODS
1d521afd 158
5f2aeb09 159=head2 new
1d521afd 160
5f2aeb09 161The constructor.
1d521afd 162
5f2aeb09 163=cut
1d521afd 164
7364d776 165sub new {
166 my ( $self, @args ) = @_;
167 my $new = $self->next::method(@args);
7c4ade2a 168 $new->{deploy_depends_on} =
169 { map { $_ => 1 }
170 @{ $new->{deploy_depends_on} || [] } }
171 unless ref $new->{deploy_depends_on} eq 'HASH';
7364d776 172 return $new;
173}
1d521afd 174
a2bd3796 175=head1 FURTHER QUESTIONS?
00be2e0b 176
a2bd3796 177Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
66d2a14e 178
a2bd3796 179=head1 COPYRIGHT AND LICENSE
1d521afd 180
a2bd3796 181This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
182by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
183redistribute it and/or modify it under the same terms as the
184L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
1d521afd 185
186=cut
187
a2bd3796 1881;