Commit | Line | Data |
1d521afd |
1 | package DBIx::Class::ResultSource::View; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use DBIx::Class::ResultSet; |
7c4ade2a |
7 | use Devel::Dwarn; |
bf5c3a3f |
8 | |
1d521afd |
9 | use base qw/DBIx::Class/; |
10 | __PACKAGE__->load_components(qw/ResultSource/); |
030c27ad |
11 | __PACKAGE__->mk_group_accessors( |
e55d9d89 |
12 | 'simple' => qw(is_virtual view_definition deploy_depends_on) ); |
1d521afd |
13 | |
1d521afd |
14 | =head1 NAME |
15 | |
00be2e0b |
16 | DBIx::Class::ResultSource::View - ResultSource object representing a view |
1d521afd |
17 | |
18 | =head1 SYNOPSIS |
19 | |
16ccb4fe |
20 | package MyDB::Schema::Result::Year2000CDs; |
00be2e0b |
21 | |
d88ecca6 |
22 | use base qw/DBIx::Class::Core/; |
00be2e0b |
23 | |
00be2e0b |
24 | __PACKAGE__->table_class('DBIx::Class::ResultSource::View'); |
25 | |
26 | __PACKAGE__->table('year2000cds'); |
27 | __PACKAGE__->result_source_instance->is_virtual(1); |
28 | __PACKAGE__->result_source_instance->view_definition( |
29 | "SELECT cdid, artist, title FROM cd WHERE year ='2000'" |
b4664250 |
30 | ); |
31 | __PACKAGE__->add_columns( |
32 | 'cdid' => { |
33 | data_type => 'integer', |
34 | is_auto_increment => 1, |
35 | }, |
36 | 'artist' => { |
37 | data_type => 'integer', |
38 | }, |
39 | 'title' => { |
40 | data_type => 'varchar', |
41 | size => 100, |
42 | }, |
43 | ); |
00be2e0b |
44 | |
1d521afd |
45 | =head1 DESCRIPTION |
46 | |
00be2e0b |
47 | View object that inherits from L<DBIx::Class::ResultSource> |
48 | |
b4664250 |
49 | This class extends ResultSource to add basic view support. |
00be2e0b |
50 | |
b4664250 |
51 | A view has a L</view_definition>, which contains a SQL query. The query can |
52 | only have parameters if L</is_virtual> is set to true. It may contain JOINs, |
53 | sub selects and any other SQL your database supports. |
00be2e0b |
54 | |
55 | View definition SQL is deployed to your database on |
56 | L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true. |
57 | |
58 | Deploying the view does B<not> translate it between different database |
59 | syntaxes, so be careful what you write in your view SQL. |
60 | |
66d2a14e |
61 | Virtual views (L</is_virtual> true), are assumed to not |
00be2e0b |
62 | exist in your database as a real view. The L</view_definition> in this |
63 | case replaces the view name in a FROM clause in a subselect. |
64 | |
b4664250 |
65 | =head1 EXAMPLES |
66 | |
67 | Having created the MyDB::Schema::Year2000CDs schema as shown in the SYNOPSIS |
68 | above, you can then: |
69 | |
70 | $2000_cds = $schema->resultset('Year2000CDs') |
71 | ->search() |
72 | ->all(); |
73 | $count = $schema->resultset('Year2000CDs') |
74 | ->search() |
75 | ->count(); |
76 | |
77 | If you modified the schema to include a placeholder |
78 | |
79 | __PACKAGE__->result_source_instance->view_definition( |
80 | "SELECT cdid, artist, title FROM cd WHERE year ='?'" |
81 | ); |
82 | |
83 | and ensuring you have is_virtual set to true: |
84 | |
85 | __PACKAGE__->result_source_instance->is_virtual(1); |
86 | |
87 | You could now say: |
88 | |
89 | $2001_cds = $schema->resultset('Year2000CDs') |
90 | ->search({}, { bind => [2001] }) |
91 | ->all(); |
92 | $count = $schema->resultset('Year2000CDs') |
93 | ->search({}, { bind => [2001] }) |
94 | ->count(); |
95 | |
00be2e0b |
96 | =head1 SQL EXAMPLES |
97 | |
98 | =over |
99 | |
66d2a14e |
100 | =item is_virtual set to false |
00be2e0b |
101 | |
102 | $schema->resultset('Year2000CDs')->all(); |
103 | |
104 | SELECT cdid, artist, title FROM year2000cds me |
105 | |
66d2a14e |
106 | =item is_virtual set to true |
00be2e0b |
107 | |
108 | $schema->resultset('Year2000CDs')->all(); |
109 | |
110 | SELECT cdid, artist, title FROM |
111 | (SELECT cdid, artist, title FROM cd WHERE year ='2000') me |
112 | |
113 | =back |
1d521afd |
114 | |
115 | =head1 METHODS |
116 | |
9774f48b |
117 | =head2 is_virtual |
118 | |
00be2e0b |
119 | __PACKAGE__->result_source_instance->is_virtual(1); |
120 | |
121 | Set to true for a virtual view, false or unset for a real |
122 | database-based view. |
123 | |
124 | =head2 view_definition |
125 | |
126 | __PACKAGE__->result_source_instance->view_definition( |
127 | "SELECT cdid, artist, title FROM cd WHERE year ='2000'" |
128 | ); |
129 | |
130 | An SQL query for your view. Will not be translated across database |
131 | syntaxes. |
132 | |
e55d9d89 |
133 | =head2 deploy_depends_on |
134 | |
135 | __PACKAGE__->result_source_instance->deploy_depends_on( |
7c4ade2a |
136 | ["Year","CD"] |
e55d9d89 |
137 | ); |
138 | |
41d90e35 |
139 | Specify the views (and only the views) that this view depends on. |
7c4ade2a |
140 | Pass this an array reference of source names. |
e55d9d89 |
141 | |
00be2e0b |
142 | =head1 OVERRIDDEN METHODS |
9774f48b |
143 | |
1d521afd |
144 | =head2 from |
145 | |
146 | Returns the FROM entry for the table (i.e. the view name) |
00be2e0b |
147 | or the SQL as a subselect if this is a virtual view. |
1d521afd |
148 | |
149 | =cut |
150 | |
9774f48b |
151 | sub from { |
e55d9d89 |
152 | my $self = shift; |
153 | return \"(${\$self->view_definition})" if $self->is_virtual; |
154 | return $self->name; |
9774f48b |
155 | } |
1d521afd |
156 | |
5f2aeb09 |
157 | =head1 OTHER METHODS |
158 | |
159 | =head2 new |
160 | |
161 | The constructor. |
162 | |
163 | =cut |
164 | |
7364d776 |
165 | sub 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 | } |
6ebf5cbb |
174 | |
1d521afd |
175 | 1; |
176 | |
177 | =head1 AUTHORS |
178 | |
7aaf4192 |
179 | See L<DBIx::Class/CONTRIBUTORS>. |
e55d9d89 |
180 | |
1d521afd |
181 | =head1 LICENSE |
182 | |
183 | You may distribute this code under the same terms as Perl itself. |
184 | |
185 | =cut |
186 | |