File Coverage

blib/lib/Catalyst/Model/DBIC/Schema.pm
Criterion Covered Total %
statement 21 47 44.7
branch 0 10 0.0
condition 0 3 0.0
subroutine 7 12 58.3
pod 4 4 100.0
total 32 76 42.1


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