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