Commit | Line | Data |
ad91060a |
1 | package Catalyst::Model::DBIC::Schema; |
2 | |
3 | use strict; |
14d912a2 |
4 | use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/; |
ad91060a |
5 | use NEXT; |
6 | use UNIVERSAL::require; |
7 | use Carp; |
7db6da78 |
8 | require DBIx::Class; |
ad91060a |
9 | |
045b3789 |
10 | our $VERSION = '0.14'; |
ad91060a |
11 | |
14d912a2 |
12 | __PACKAGE__->mk_classaccessor('composed_schema'); |
ad91060a |
13 | __PACKAGE__->mk_accessors('schema'); |
14 | |
15 | =head1 NAME |
16 | |
17 | Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class |
18 | |
19 | =head1 SYNOPSIS |
20 | |
aabc1d75 |
21 | Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema: |
22 | |
23 | =over |
24 | |
25 | =item 1. |
26 | |
27 | Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm: |
28 | |
29 | package MyApp::Schema::FilmDB; |
30 | use base qw/DBIx::Class::Schema/; |
31 | |
32 | __PACKAGE__->load_classes(qw/Actor Role/); |
33 | |
34 | =item 2. |
35 | |
36 | Create some classes for the tables in the database, for example an |
37 | Actor in MyApp/Schema/FilmDB/Actor.pm: |
38 | |
39 | package MyApp::Schema::FilmDB::Actor; |
40 | use base qw/DBIx::Class/ |
41 | |
42 | __PACKAGE__->load_components(qw/Core/); |
43 | __PACKAGE__->table('actor'); |
44 | |
45 | ... |
46 | |
47 | and a Role in MyApp/Schema/Role.pm: |
48 | |
49 | package MyApp::Schema::FilmDB::Role; |
50 | use base qw/DBIx::Class/ |
51 | |
52 | __PACKAGE__->load_components(qw/Core/); |
4a3e80e9 |
53 | __PACKAGE__->table('role'); |
aabc1d75 |
54 | |
55 | ... |
56 | |
57 | Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's |
58 | usable as a standalone module and you can test/run it without Catalyst. |
59 | |
60 | =item 3. |
61 | |
62 | To expose it to Catalyst as a model, you should create a DBIC Model in |
63 | MyApp/Model/FilmDB.pm: |
64 | |
65 | package MyApp::Model::FilmDB; |
66 | use base qw/Catalyst::Model::DBIC::Schema/; |
67 | |
68 | __PACKAGE__->config( |
69 | schema_class => 'MyApp::Schema::FilmDB', |
70 | connect_info => [ |
71 | "DBI:...", |
72 | "username", |
73 | "password", |
74 | {AutoCommit => 1} |
75 | ] |
76 | ); |
77 | |
78 | See below for a full list of the possible config parameters. |
79 | |
80 | =back |
81 | |
82 | Now you have a working Model, accessing your separate DBIC Schema. Which can |
83 | be used/accessed in the normal Catalyst manner, via $c->model(): |
84 | |
85 | my $actor = $c->model('FilmDB::Actor')->find(1); |
86 | |
87 | You can also use it to set up DBIC authentication with |
88 | Authentication::Store::DBIC in MyApp.pm: |
89 | |
90 | package MyApp; |
91 | |
92 | use Catalyst qw/... Authentication::Store::DBIC/; |
93 | |
94 | ... |
95 | |
96 | __PACKAGE__->config->{authentication}{dbic} = { |
97 | user_class => 'FilmDB::Actor', |
98 | user_field => 'name', |
99 | password_field => 'password' |
100 | } |
101 | |
102 | C<< $c->model() >> returns a L<DBIx::Class::ResultSet> for the source name |
103 | parameter passed. To find out more about which methods can be called on a |
104 | ResultSet, or how to add your own methods to it, please see the ResultSet |
105 | documentation in the L<DBIx::Class> distribution. |
106 | |
107 | Some examples are given below: |
108 | |
109 | # to access schema methods directly: |
110 | $c->model('FilmDB')->schema->source(...); |
111 | |
112 | # to access the source object, resultset, and class: |
113 | $c->model('FilmDB')->source(...); |
114 | $c->model('FilmDB')->resultset(...); |
115 | $c->model('FilmDB')->class(...); |
c12b7310 |
116 | |
117 | # For resultsets, there's an even quicker shortcut: |
150c864c |
118 | $c->model('FilmDB::Actor') |
aabc1d75 |
119 | # is the same as $c->model('FilmDB')->resultset('Actor') |
ad91060a |
120 | |
121 | # To get the composed schema for making new connections: |
aabc1d75 |
122 | my $newconn = $c->model('FilmDB')->composed_schema->connect(...); |
ad91060a |
123 | |
124 | # Or the same thing via a convenience shortcut: |
aabc1d75 |
125 | my $newconn = $c->model('FilmDB')->connect(...); |
ad91060a |
126 | |
127 | # or, if your schema works on different storage drivers: |
aabc1d75 |
128 | my $newconn = $c->model('FilmDB')->composed_schema->clone(); |
ad91060a |
129 | $newconn->storage_type('::LDAP'); |
f7a944f8 |
130 | $newconn->connection(...); |
ad91060a |
131 | |
132 | # and again, a convenience shortcut |
aabc1d75 |
133 | my $newconn = $c->model('FilmDB')->clone(); |
ad91060a |
134 | $newconn->storage_type('::LDAP'); |
f7a944f8 |
135 | $newconn->connection(...); |
ad91060a |
136 | |
137 | =head1 DESCRIPTION |
138 | |
7b39f3f0 |
139 | This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See |
140 | the documentation for L<Catalyst::Helper::Model::DBIC::Schema> and |
1aeb6e1e |
141 | L<Catalyst::Helper::Model::DBIC::SchemaLoader> for information |
142 | on generating these Models via Helper scripts. The latter of the two |
143 | will also generated a L<DBIx::Class::Schema::Loader>-based Schema class |
144 | for you. |
ad91060a |
145 | |
146 | =head1 CONFIG PARAMETERS |
147 | |
148 | =over 4 |
149 | |
150 | =item schema_class |
151 | |
152 | This is the classname of your L<DBIx::Class::Schema> Schema. It needs |
aabc1d75 |
153 | to be findable in C<@INC>, but it does not need to be inside the |
154 | C<Catalyst::Model::> namespace. This parameter is required. |
ad91060a |
155 | |
156 | =item connect_info |
157 | |
158 | This is an arrayref of connection parameters, which are specific to your |
7db6da78 |
159 | C<storage_type> (see your storage type documentation for more details). |
ad91060a |
160 | |
0f2fd2c0 |
161 | This is not required if C<schema_class> already has connection information |
d89e6c8a |
162 | defined inside itself (which isn't highly recommended, but can be done) |
0f2fd2c0 |
163 | |
7db6da78 |
164 | For L<DBIx::Class::Storage::DBI>, which is the only supported |
165 | C<storage_type> in L<DBIx::Class> at the time of this writing, the |
166 | parameters are your dsn, username, password, and connect options hashref. |
167 | |
168 | If you need to specify the L<DBIx::Class::Storage::DBI> specific parameter |
169 | C<on_connect_do>, or the related C<sql_maker> options C<limit_dialect>, |
170 | C<quote_char>, or C<name_sep>, you can place these options into a hashref |
171 | as the final element of the C<connect_info> arrayref. If in doubt, don't |
172 | specify these options. You would know it if you needed them. |
173 | |
174 | Examples: |
175 | |
176 | connect_info => [ 'dbi:Pg:dbname=mypgdb', 'postgres', '' ], |
aabc1d75 |
177 | |
7db6da78 |
178 | connect_info => [ |
179 | 'dbi:SQLite:dbname=foo.db', |
180 | { |
181 | on_connect_do => [ |
aabc1d75 |
182 | 'PRAGMA synchronous = OFF', |
7db6da78 |
183 | ], |
184 | } |
185 | ], |
aabc1d75 |
186 | |
7db6da78 |
187 | connect_info => [ |
188 | 'dbi:Pg:dbname=mypgdb', |
189 | 'postgres', |
190 | '', |
191 | { AutoCommit => 0 }, |
192 | { |
193 | on_connect_do => [ |
194 | 'some SQL statement', |
195 | 'another SQL statement', |
196 | ], |
197 | } |
198 | ], |
199 | |
ad91060a |
200 | =item storage_type |
201 | |
202 | Allows the use of a different C<storage_type> than what is set in your |
203 | C<schema_class> (which in turn defaults to C<::DBI> if not set in current |
b8427e0b |
204 | L<DBIx::Class>). Completely optional, and probably unnecessary for most |
205 | people until other storage backends become available for L<DBIx::Class>. |
ad91060a |
206 | |
207 | =back |
208 | |
209 | =head1 METHODS |
210 | |
211 | =over 4 |
212 | |
213 | =item new |
214 | |
215 | Instantiates the Model based on the above-documented ->config parameters. |
0f2fd2c0 |
216 | The only required parameter is C<schema_class>. C<connect_info> is |
217 | required in the case that C<schema_class> does not already have connection |
218 | information defined for it. |
ad91060a |
219 | |
220 | =item schema |
221 | |
222 | Accessor which returns the connected schema being used by the this model. |
aabc1d75 |
223 | There are direct shortcuts on the model class itself for |
c12b7310 |
224 | schema->resultset, schema->source, and schema->class. |
ad91060a |
225 | |
226 | =item composed_schema |
227 | |
228 | Accessor which returns the composed schema, which has no connection info, |
229 | which was used in constructing the C<schema> above. Useful for creating |
c12b7310 |
230 | new connections based on the same schema/model. There are direct shortcuts |
231 | from the model object for composed_schema->clone and composed_schema->connect |
ad91060a |
232 | |
233 | =item clone |
234 | |
235 | Shortcut for ->composed_schema->clone |
236 | |
237 | =item connect |
238 | |
239 | Shortcut for ->composed_schema->connect |
240 | |
c12b7310 |
241 | =item source |
242 | |
243 | Shortcut for ->schema->source |
244 | |
245 | =item class |
246 | |
247 | Shortcut for ->schema->class |
248 | |
249 | =item resultset |
250 | |
251 | Shortcut for ->schema->resultset |
252 | |
b8427e0b |
253 | =item storage |
254 | |
255 | Provides an accessor for the connected schema's storage object. |
256 | Used often for debugging and controlling transactions. |
257 | |
ad91060a |
258 | =back |
259 | |
260 | =cut |
261 | |
262 | sub new { |
46878f2e |
263 | my $self = shift->NEXT::new(@_); |
ad91060a |
264 | |
265 | my $class = ref($self); |
266 | my $model_name = $class; |
267 | $model_name =~ s/^[\w:]+::(?:Model|M):://; |
268 | |
0f2fd2c0 |
269 | croak "->config->{schema_class} must be defined for this model" |
270 | unless $self->{schema_class}; |
ad91060a |
271 | |
272 | my $schema_class = $self->{schema_class}; |
273 | |
1aeb6e1e |
274 | $schema_class->require |
275 | or croak "Cannot load schema class '$schema_class': $@"; |
ad91060a |
276 | |
0f2fd2c0 |
277 | if( !$self->{connect_info} ) { |
278 | if($schema_class->storage && $schema_class->storage->connect_info) { |
279 | $self->{connect_info} = $schema_class->storage->connect_info; |
280 | } |
281 | else { |
282 | croak "Either ->config->{connect_info} must be defined for $class" |
1aeb6e1e |
283 | . " or $schema_class must have connect info defined on it"; |
0f2fd2c0 |
284 | } |
285 | } |
286 | |
ad91060a |
287 | $self->composed_schema($schema_class->compose_namespace($class)); |
288 | $self->schema($self->composed_schema->clone); |
cd7e6e4f |
289 | |
290 | $self->schema->storage_type($self->{storage_type}) |
291 | if $self->{storage_type}; |
7db6da78 |
292 | |
293 | # XXX This is temporary, until DBIx::Class::Storage::DBI supports the |
294 | # same syntax and we switch our requisite to that version somewhere |
295 | # down the line. This syntax is already committed into DBIx::Class |
d89e6c8a |
296 | # -current branch post-0.06. |
7db6da78 |
297 | # At that time, this whole block can revert back to just being: |
298 | # $self->schema->connection(@{$self->{connect_info}}); |
299 | |
300 | my $connect_info = [ @{$self->{connect_info}} ]; |
301 | my ($on_connect_do, %sql_maker_opts); |
302 | if($DBIx::Class::VERSION < 0.069) { |
303 | my $used; |
304 | my $last_info = $self->{connect_info}->[-1]; |
305 | if(ref $last_info eq 'HASH') { |
306 | if($on_connect_do = $last_info->{on_connect_do}) { |
307 | $used = 1; |
308 | } |
309 | for my $sql_maker_opt (qw/limit_dialect quote_char name_sep/) { |
310 | if(my $opt_val = $last_info->{$sql_maker_opt}) { |
311 | $used = 1; |
312 | $sql_maker_opts{$sql_maker_opt} = $opt_val; |
313 | } |
4bd6775c |
314 | } |
7db6da78 |
315 | pop(@$connect_info) if $used; |
4bd6775c |
316 | } |
317 | } |
ad91060a |
318 | |
7db6da78 |
319 | $self->schema->connection(@$connect_info); |
320 | |
321 | if($DBIx::Class::VERSION < 0.069) { |
322 | $self->schema->storage->on_connect_do($on_connect_do) |
323 | if $on_connect_do; |
324 | foreach my $sql_maker_opt (keys %sql_maker_opts) { |
325 | $self->schema->storage->sql_maker->$sql_maker_opt( |
326 | $sql_maker_opts{$sql_maker_opt} |
327 | ); |
328 | } |
329 | } |
330 | |
331 | # XXX end of compatibility block referenced above |
332 | |
ad91060a |
333 | no strict 'refs'; |
334 | foreach my $moniker ($self->schema->sources) { |
0b2a7108 |
335 | my $classname = "${class}::$moniker"; |
7db6da78 |
336 | *{"${classname}::ACCEPT_CONTEXT"} = sub { |
ad91060a |
337 | shift; |
c12b7310 |
338 | shift->model($model_name)->resultset($moniker); |
ad91060a |
339 | } |
340 | } |
341 | |
342 | return $self; |
343 | } |
344 | |
ad91060a |
345 | sub clone { shift->composed_schema->clone(@_); } |
346 | |
ad91060a |
347 | sub connect { shift->composed_schema->connect(@_); } |
348 | |
b8427e0b |
349 | sub storage { shift->schema->storage(@_); } |
350 | |
ad91060a |
351 | =head1 SEE ALSO |
352 | |
7b39f3f0 |
353 | General Catalyst Stuff: |
354 | |
355 | L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>, |
356 | L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>, |
357 | |
358 | Stuff related to DBIC and this Model style: |
359 | |
360 | L<DBIx::Class>, L<DBIx::Class::Schema>, |
361 | L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>, |
1aeb6e1e |
362 | L<Catalyst::Helper::Model::DBIC::SchemaLoader> |
ad91060a |
363 | |
364 | =head1 AUTHOR |
365 | |
366 | Brandon L Black, C<blblack@gmail.com> |
367 | |
368 | =head1 COPYRIGHT |
369 | |
370 | This program is free software, you can redistribute it and/or modify it |
371 | under the same terms as Perl itself. |
372 | |
373 | =cut |
374 | |
375 | 1; |