Commit | Line | Data |
ad91060a |
1 | package Catalyst::Model::DBIC::Schema; |
2 | |
0fbbc8d5 |
3 | use Moose; |
2201c2e4 |
4 | no warnings 'uninitialized'; |
018eb0e2 |
5 | |
2201c2e4 |
6 | our $VERSION = '0.24'; |
018eb0e2 |
7 | |
0f3de2c4 |
8 | use mro 'c3'; |
0fbbc8d5 |
9 | extends 'Catalyst::Model'; |
10 | with 'MooseX::Object::Pluggable'; |
11 | |
12 | use Carp::Clan '^Catalyst::Model::DBIC::Schema'; |
bfcd6e3d |
13 | use Data::Dumper; |
2201c2e4 |
14 | use DBIx::Class (); |
15 | use Scalar::Util 'reftype'; |
0fbbc8d5 |
16 | use MooseX::ClassAttribute; |
17 | use Moose::Autobox; |
ad91060a |
18 | |
0fbbc8d5 |
19 | use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfo SchemaClass/; |
20 | |
21 | use namespace::clean -except => 'meta'; |
f1613faa |
22 | |
ad91060a |
23 | =head1 NAME |
24 | |
25 | Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class |
26 | |
27 | =head1 SYNOPSIS |
28 | |
aabc1d75 |
29 | Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema: |
30 | |
31 | =over |
32 | |
33 | =item 1. |
34 | |
35 | Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm: |
36 | |
37 | package MyApp::Schema::FilmDB; |
38 | use base qw/DBIx::Class::Schema/; |
39 | |
40 | __PACKAGE__->load_classes(qw/Actor Role/); |
41 | |
42 | =item 2. |
43 | |
44 | Create some classes for the tables in the database, for example an |
45 | Actor in MyApp/Schema/FilmDB/Actor.pm: |
46 | |
47 | package MyApp::Schema::FilmDB::Actor; |
48 | use base qw/DBIx::Class/ |
07edc53e |
49 | |
aabc1d75 |
50 | __PACKAGE__->load_components(qw/Core/); |
51 | __PACKAGE__->table('actor'); |
07edc53e |
52 | |
aabc1d75 |
53 | ... |
54 | |
c8ae74f8 |
55 | and a Role in MyApp/Schema/FilmDB/Role.pm: |
aabc1d75 |
56 | |
57 | package MyApp::Schema::FilmDB::Role; |
58 | use base qw/DBIx::Class/ |
07edc53e |
59 | |
aabc1d75 |
60 | __PACKAGE__->load_components(qw/Core/); |
4a3e80e9 |
61 | __PACKAGE__->table('role'); |
07edc53e |
62 | |
aabc1d75 |
63 | ... |
64 | |
65 | Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's |
66 | usable as a standalone module and you can test/run it without Catalyst. |
67 | |
68 | =item 3. |
69 | |
70 | To expose it to Catalyst as a model, you should create a DBIC Model in |
71 | MyApp/Model/FilmDB.pm: |
72 | |
73 | package MyApp::Model::FilmDB; |
74 | use base qw/Catalyst::Model::DBIC::Schema/; |
07edc53e |
75 | |
aabc1d75 |
76 | __PACKAGE__->config( |
77 | schema_class => 'MyApp::Schema::FilmDB', |
2201c2e4 |
78 | connect_info => { |
79 | dsn => "DBI:...", |
80 | user => "username", |
81 | password => "password", |
82 | } |
aabc1d75 |
83 | ); |
84 | |
85 | See below for a full list of the possible config parameters. |
86 | |
87 | =back |
88 | |
6dec11b7 |
89 | Now you have a working Model which accesses your separate DBIC Schema. This can |
aabc1d75 |
90 | be used/accessed in the normal Catalyst manner, via $c->model(): |
91 | |
92 | my $actor = $c->model('FilmDB::Actor')->find(1); |
93 | |
94 | You can also use it to set up DBIC authentication with |
95 | Authentication::Store::DBIC in MyApp.pm: |
96 | |
97 | package MyApp; |
07edc53e |
98 | |
aabc1d75 |
99 | use Catalyst qw/... Authentication::Store::DBIC/; |
07edc53e |
100 | |
aabc1d75 |
101 | ... |
07edc53e |
102 | |
aabc1d75 |
103 | __PACKAGE__->config->{authentication}{dbic} = { |
104 | user_class => 'FilmDB::Actor', |
105 | user_field => 'name', |
106 | password_field => 'password' |
107 | } |
108 | |
d52bc376 |
109 | C<< $c->model('Schema::Source') >> returns a L<DBIx::Class::ResultSet> for |
110 | the source name parameter passed. To find out more about which methods can |
111 | be called on a ResultSet, or how to add your own methods to it, please see |
112 | the ResultSet documentation in the L<DBIx::Class> distribution. |
aabc1d75 |
113 | |
114 | Some examples are given below: |
115 | |
f1613faa |
116 | # to access schema methods directly: |
117 | $c->model('FilmDB')->schema->source(...); |
118 | |
119 | # to access the source object, resultset, and class: |
07edc53e |
120 | $c->model('FilmDB')->source(...); |
121 | $c->model('FilmDB')->resultset(...); |
122 | $c->model('FilmDB')->class(...); |
c12b7310 |
123 | |
07edc53e |
124 | # For resultsets, there's an even quicker shortcut: |
125 | $c->model('FilmDB::Actor') |
126 | # is the same as $c->model('FilmDB')->resultset('Actor') |
ad91060a |
127 | |
f1613faa |
128 | # To get the composed schema for making new connections: |
129 | my $newconn = $c->model('FilmDB')->composed_schema->connect(...); |
130 | |
131 | # Or the same thing via a convenience shortcut: |
132 | my $newconn = $c->model('FilmDB')->connect(...); |
133 | |
134 | # or, if your schema works on different storage drivers: |
135 | my $newconn = $c->model('FilmDB')->composed_schema->clone(); |
136 | $newconn->storage_type('::LDAP'); |
137 | $newconn->connection(...); |
138 | |
139 | # and again, a convenience shortcut |
140 | my $newconn = $c->model('FilmDB')->clone(); |
141 | $newconn->storage_type('::LDAP'); |
142 | $newconn->connection(...); |
143 | |
ad91060a |
144 | =head1 DESCRIPTION |
145 | |
7b39f3f0 |
146 | This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See |
ef91bcf9 |
147 | the documentation for L<Catalyst::Helper::Model::DBIC::Schema> for |
148 | information on generating these Models via Helper scripts. |
ad91060a |
149 | |
d52bc376 |
150 | When your Catalyst app starts up, a thin Model layer is created as an |
151 | interface to your DBIC Schema. It should be clearly noted that the model |
152 | object returned by C<< $c->model('FilmDB') >> is NOT itself a DBIC schema or |
153 | resultset object, but merely a wrapper proving L<methods|/METHODS> to access |
154 | the underlying schema. |
155 | |
156 | In addition to this model class, a shortcut class is generated for each |
157 | source in the schema, allowing easy and direct access to a resultset of the |
158 | corresponding type. These generated classes are even thinner than the model |
159 | class, providing no public methods but simply hooking into Catalyst's |
160 | model() accessor via the |
161 | L<ACCEPT_CONTEXT|Catalyst::Component/ACCEPT_CONTEXT> mechanism. The complete |
162 | contents of each generated class is roughly equivalent to the following: |
163 | |
164 | package MyApp::Model::FilmDB::Actor |
165 | sub ACCEPT_CONTEXT { |
166 | my ($self, $c) = @_; |
167 | $c->model('FilmDB')->resultset('Actor'); |
168 | } |
169 | |
170 | In short, there are three techniques available for obtaining a DBIC |
171 | resultset object: |
172 | |
173 | # the long way |
174 | my $rs = $c->model('FilmDB')->schema->resultset('Actor'); |
175 | |
176 | # using the shortcut method on the model object |
177 | my $rs = $c->model('FilmDB')->resultset('Actor'); |
178 | |
179 | # using the generated class directly |
180 | my $rs = $c->model('FilmDB::Actor'); |
181 | |
c082639a |
182 | In order to add methods to a DBIC resultset, you cannot simply add them to |
183 | the source (row, table) definition class; you must define a separate custom |
184 | resultset class. See L<DBIx::Class::Manual::Cookbook/"Predefined searches"> |
185 | for more info. |
186 | |
ad91060a |
187 | =head1 CONFIG PARAMETERS |
188 | |
189 | =over 4 |
190 | |
191 | =item schema_class |
192 | |
193 | This is the classname of your L<DBIx::Class::Schema> Schema. It needs |
aabc1d75 |
194 | to be findable in C<@INC>, but it does not need to be inside the |
195 | C<Catalyst::Model::> namespace. This parameter is required. |
ad91060a |
196 | |
197 | =item connect_info |
198 | |
199 | This is an arrayref of connection parameters, which are specific to your |
b9a72351 |
200 | C<storage_type> (see your storage type documentation for more details). |
201 | If you only need one parameter (e.g. the DSN), you can just pass a string |
202 | instead of an arrayref. |
ad91060a |
203 | |
0f2fd2c0 |
204 | This is not required if C<schema_class> already has connection information |
d89e6c8a |
205 | defined inside itself (which isn't highly recommended, but can be done) |
0f2fd2c0 |
206 | |
7db6da78 |
207 | For L<DBIx::Class::Storage::DBI>, which is the only supported |
208 | C<storage_type> in L<DBIx::Class> at the time of this writing, the |
209 | parameters are your dsn, username, password, and connect options hashref. |
210 | |
018eb0e2 |
211 | See L<DBIx::Class::Storage::DBI/connect_info> for a detailed explanation |
212 | of the arguments supported. |
7db6da78 |
213 | |
214 | Examples: |
215 | |
2201c2e4 |
216 | connect_info => { |
217 | dsn => 'dbi:Pg:dbname=mypgdb', |
218 | user => 'postgres', |
219 | password => '' |
220 | } |
07edc53e |
221 | |
2201c2e4 |
222 | connect_info => { |
223 | dsn => 'dbi:SQLite:dbname=foo.db', |
224 | on_connect_do => [ |
225 | 'PRAGMA synchronous = OFF', |
226 | ] |
227 | } |
07edc53e |
228 | |
2201c2e4 |
229 | connect_info => { |
230 | dsn => 'dbi:Pg:dbname=mypgdb', |
231 | user => 'postgres', |
232 | password => '', |
233 | pg_enable_utf8 => 1, |
234 | on_connect_do => [ |
235 | 'some SQL statement', |
236 | 'another SQL statement', |
237 | ], |
238 | } |
7db6da78 |
239 | |
8281c933 |
240 | Or using L<Config::General>: |
241 | |
242 | <Model::FilmDB> |
243 | schema_class MyApp::Schema::FilmDB |
8281c933 |
244 | <connect_info> |
2201c2e4 |
245 | dsn dbi:Pg:dbname=mypgdb |
246 | user postgres |
247 | password '' |
248 | auto_savepoint 1 |
8281c933 |
249 | on_connect_do some SQL statement |
250 | on_connect_do another SQL statement |
251 | </connect_info> |
252 | </Model::FilmDB> |
253 | |
254 | or |
255 | |
256 | <Model::FilmDB> |
257 | schema_class MyApp::Schema::FilmDB |
258 | connect_info dbi:SQLite:dbname=foo.db |
259 | </Model::FilmDB> |
260 | |
2201c2e4 |
261 | Or using L<YAML>: |
262 | |
263 | Model::MyDB: |
264 | schema_class: MyDB |
265 | connect_info: |
266 | dsn: dbi:Oracle:mydb |
267 | user: mtfnpy |
268 | password: mypass |
269 | LongReadLen: 1000000 |
270 | LongTruncOk: 1 |
271 | on_connect_do: [ "alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'" ] |
272 | cursor_class: 'DBIx::Class::Cursor::Cached' |
273 | |
274 | The old arrayref style with hashrefs for L<DBI> then L<DBIx::Class> options is also |
275 | supported: |
276 | |
277 | connect_info => [ |
278 | 'dbi:Pg:dbname=mypgdb', |
279 | 'postgres', |
280 | '', |
281 | { |
282 | pg_enable_utf8 => 1, |
283 | }, |
284 | { |
0fbbc8d5 |
285 | auto_savepoint => 1, |
2201c2e4 |
286 | on_connect_do => [ |
287 | 'some SQL statement', |
288 | 'another SQL statement', |
289 | ], |
290 | } |
291 | ] |
292 | |
0fbbc8d5 |
293 | =item roles |
294 | |
295 | Array of Roles to apply at BUILD time. Roles are relative to the |
296 | C<<MyApp::Model::DB::Role::> then C<<Catalyst::Model::DBIC::Schema::Role::>> |
297 | namespaces, unless prefixed with C<+> in which case they are taken to be a |
298 | fully qualified name. E.g.: |
2201c2e4 |
299 | |
0fbbc8d5 |
300 | roles Caching |
301 | roles +MyApp::DB::Role::Foo |
2201c2e4 |
302 | |
0fbbc8d5 |
303 | This is done using L<MooseX::Object::Pluggable>. |
2201c2e4 |
304 | |
0fbbc8d5 |
305 | A new instance is created at application time, so any consumed required |
306 | attributes, coercions and modifiers will work. |
2201c2e4 |
307 | |
0fbbc8d5 |
308 | Roles are applied before setup, schema and connection are set, and have a chance |
309 | to modify C<connect_info>. |
310 | |
311 | C<ref $self> will not be what you expect. |
312 | |
313 | WARNING: you cannot modify C<new>, modify C<setup> instead. |
314 | |
315 | Roles that come with the distribution: |
316 | |
317 | =over 4 |
2201c2e4 |
318 | |
0fbbc8d5 |
319 | =item L<Catalyst::Model::DBIC::Schema::Role::Caching> |
320 | |
321 | =back |
8281c933 |
322 | |
ad91060a |
323 | =item storage_type |
324 | |
325 | Allows the use of a different C<storage_type> than what is set in your |
326 | C<schema_class> (which in turn defaults to C<::DBI> if not set in current |
f1613faa |
327 | L<DBIx::Class>). Completely optional, and probably unnecessary for most |
328 | people until other storage backends become available for L<DBIx::Class>. |
ad91060a |
329 | |
330 | =back |
331 | |
332 | =head1 METHODS |
333 | |
334 | =over 4 |
335 | |
336 | =item new |
337 | |
338 | Instantiates the Model based on the above-documented ->config parameters. |
0f2fd2c0 |
339 | The only required parameter is C<schema_class>. C<connect_info> is |
340 | required in the case that C<schema_class> does not already have connection |
341 | information defined for it. |
ad91060a |
342 | |
f1613faa |
343 | =item schema |
344 | |
345 | Accessor which returns the connected schema being used by the this model. |
346 | There are direct shortcuts on the model class itself for |
347 | schema->resultset, schema->source, and schema->class. |
348 | |
349 | =item composed_schema |
350 | |
351 | Accessor which returns the composed schema, which has no connection info, |
352 | which was used in constructing the C<schema> above. Useful for creating |
353 | new connections based on the same schema/model. There are direct shortcuts |
354 | from the model object for composed_schema->clone and composed_schema->connect |
355 | |
356 | =item clone |
357 | |
358 | Shortcut for ->composed_schema->clone |
359 | |
360 | =item connect |
361 | |
362 | Shortcut for ->composed_schema->connect |
363 | |
364 | =item source |
c12b7310 |
365 | |
f1613faa |
366 | Shortcut for ->schema->source |
367 | |
368 | =item class |
369 | |
370 | Shortcut for ->schema->class |
371 | |
372 | =item resultset |
373 | |
374 | Shortcut for ->schema->resultset |
375 | |
376 | =item storage |
377 | |
378 | Provides an accessor for the connected schema's storage object. |
379 | Used often for debugging and controlling transactions. |
b8427e0b |
380 | |
ad91060a |
381 | =cut |
382 | |
2ff00e2b |
383 | class_has 'composed_schema' => (is => 'rw', isa => 'DBIx::Class::Schema'); |
0fbbc8d5 |
384 | |
2ff00e2b |
385 | has 'schema' => (is => 'rw', isa => 'DBIx::Class::Schema'); |
0fbbc8d5 |
386 | |
387 | has 'schema_class' => ( |
388 | is => 'ro', |
389 | isa => SchemaClass, |
390 | coerce => 1, |
391 | required => 1 |
392 | ); |
393 | |
2ff00e2b |
394 | has 'storage_type' => (is => 'ro', isa => 'Str'); |
0fbbc8d5 |
395 | |
396 | has 'connect_info' => (is => 'ro', isa => ConnectInfo, coerce => 1); |
397 | |
398 | # ref $self changes to anon after roles are applied, and _original_class_name is |
2ff00e2b |
399 | # broken in MX::O::P 0.0009 |
0fbbc8d5 |
400 | has '_class_name' => (is => 'ro', isa => 'ClassName', default => sub { |
401 | ref shift |
402 | }); |
403 | |
2ff00e2b |
404 | has 'model_name' => (is => 'ro', isa => 'Str', default => sub { |
0fbbc8d5 |
405 | my $self = shift; |
406 | |
2201c2e4 |
407 | my $class = ref $self; |
0fbbc8d5 |
408 | (my $model_name = $class) =~ s/^[\w:]+::(?:Model|M):://; |
2201c2e4 |
409 | |
0fbbc8d5 |
410 | $model_name |
411 | }); |
ad91060a |
412 | |
0fbbc8d5 |
413 | has 'roles' => (is => 'ro', isa => 'ArrayRef|Str'); |
ad91060a |
414 | |
0fbbc8d5 |
415 | sub BUILD { |
416 | my $self = shift; |
417 | my $class = ref $self; |
2201c2e4 |
418 | my $schema_class = $self->schema_class; |
ad91060a |
419 | |
2201c2e4 |
420 | if( !$self->connect_info ) { |
f1613faa |
421 | if($schema_class->storage && $schema_class->storage->connect_info) { |
2201c2e4 |
422 | $self->connect_info($schema_class->storage->connect_info); |
f1613faa |
423 | } |
424 | else { |
425 | croak "Either ->config->{connect_info} must be defined for $class" |
460e3ac8 |
426 | . " or $schema_class must have connect info defined on it." |
427 | . " Here's what we got:\n" |
f1613faa |
428 | . Dumper($self); |
429 | } |
7db6da78 |
430 | } |
431 | |
0fbbc8d5 |
432 | if (exists $self->connect_info->{cursor_class}) { |
433 | eval { Class::MOP::load_class($self->connect_info->{cursor_class}) } |
434 | or croak "invalid connect_info: Cannot load your cursor_class" |
435 | . " ".$self->connect_info->{cursor_class}.": $@"; |
436 | } |
437 | |
438 | $self->_plugin_ns('Role'); |
439 | |
440 | $self->load_plugins($self->roles->flatten) if $self->roles; |
441 | |
442 | $self->setup; |
443 | |
f1613faa |
444 | $self->composed_schema($schema_class->compose_namespace($class)); |
2201c2e4 |
445 | |
f1613faa |
446 | $self->schema($self->composed_schema->clone); |
447 | |
2201c2e4 |
448 | $self->schema->storage_type($self->storage_type) |
449 | if $self->storage_type; |
7db6da78 |
450 | |
2201c2e4 |
451 | $self->schema->connection($self->connect_info); |
452 | |
453 | $self->_install_rs_models; |
2201c2e4 |
454 | } |
455 | |
456 | sub clone { shift->composed_schema->clone(@_); } |
457 | |
458 | sub connect { shift->composed_schema->connect(@_); } |
459 | |
460 | sub storage { shift->schema->storage(@_); } |
461 | |
0fbbc8d5 |
462 | =item setup |
2201c2e4 |
463 | |
0fbbc8d5 |
464 | Called at C<<BUILD>> time, for modifying in roles/subclasses. |
2201c2e4 |
465 | |
466 | =cut |
467 | |
0fbbc8d5 |
468 | sub setup { 1 } |
2201c2e4 |
469 | |
0fbbc8d5 |
470 | =item ACCEPT_CONTEXT |
2201c2e4 |
471 | |
0fbbc8d5 |
472 | Point of extension for doing things at C<<$c->model>> time, returns the model |
473 | instance, see L<Catalyst::Manual::Intro> for more information. |
2201c2e4 |
474 | |
0fbbc8d5 |
475 | =cut |
2201c2e4 |
476 | |
0fbbc8d5 |
477 | sub ACCEPT_CONTEXT { shift } |
2201c2e4 |
478 | |
479 | sub _install_rs_models { |
480 | my $self = shift; |
0fbbc8d5 |
481 | my $class = $self->_class_name; |
2201c2e4 |
482 | |
ad91060a |
483 | no strict 'refs'; |
484 | foreach my $moniker ($self->schema->sources) { |
0b2a7108 |
485 | my $classname = "${class}::$moniker"; |
7db6da78 |
486 | *{"${classname}::ACCEPT_CONTEXT"} = sub { |
ad91060a |
487 | shift; |
2201c2e4 |
488 | shift->model($self->model_name)->resultset($moniker); |
ad91060a |
489 | } |
490 | } |
2201c2e4 |
491 | } |
ad91060a |
492 | |
0fbbc8d5 |
493 | __PACKAGE__->meta->make_immutable; |
2201c2e4 |
494 | |
495 | =back |
b8427e0b |
496 | |
ad91060a |
497 | =head1 SEE ALSO |
498 | |
7b39f3f0 |
499 | General Catalyst Stuff: |
500 | |
501 | L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>, |
502 | L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>, |
503 | |
504 | Stuff related to DBIC and this Model style: |
505 | |
506 | L<DBIx::Class>, L<DBIx::Class::Schema>, |
ef91bcf9 |
507 | L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema> |
ad91060a |
508 | |
509 | =head1 AUTHOR |
510 | |
511 | Brandon L Black, C<blblack@gmail.com> |
512 | |
2ff00e2b |
513 | Contributors: |
514 | |
515 | Rafael Kitover, C<<rkitover at cpan.org>> |
516 | |
ad91060a |
517 | =head1 COPYRIGHT |
518 | |
519 | This program is free software, you can redistribute it and/or modify it |
520 | under the same terms as Perl itself. |
521 | |
522 | =cut |
523 | |
524 | 1; |