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