File Coverage

blib/lib/DBIx/Class/ResultSource/View.pm
Criterion Covered Total %
statement 16 17 94.1
branch 6 8 75.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 29 32 90.6


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