bump to 0.03, added new helper to support automatic inline ::Schema::Loader
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema.pm
CommitLineData
ad91060a 1package Catalyst::Model::DBIC::Schema;
2
3use strict;
14d912a2 4use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
ad91060a 5use NEXT;
6use UNIVERSAL::require;
7b39f3f0 7use UNIVERSAL qw/ can /;
ad91060a 8use Carp;
9
7b39f3f0 10our $VERSION = '0.03';
ad91060a 11
14d912a2 12__PACKAGE__->mk_classaccessor('composed_schema');
ad91060a 13__PACKAGE__->mk_accessors('schema');
14
15=head1 NAME
16
17Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
18
19=head1 SYNOPSIS
20
21 package MyApp::Model::Foo;
22 use strict;
23 use base 'Catalyst::Model::DBIC::Schema';
24
25 __PACKAGE__->config(
26 schema_class => 'Foo::SchemaClass',
27 connect_info => [ 'dbi:Pg:dbname=foodb',
28 'postgres',
29 '',
30 { AutoCommit => 1 },
31 ],
32 );
33
34 1;
35
36 # In controller code:
37
38 # ->schema To access schema methods:
39 $c->model('Foo')->schema->source(...);
40
c12b7310 41 # certain ->schema methods (source, resultset, class) have shortcuts
42 $c->model('Foo')->source(...);
43 $c->model('Foo')->resultset(...);
44 $c->model('Foo')->class(...);
45
46 # For resultsets, there's an even quicker shortcut:
47 $c->model('Foo::Bar')
48 # is the same as $c->model('Foo')->resultset('Bar')
ad91060a 49
50 # To get the composed schema for making new connections:
51 my $newconn = $c->model('Foo')->composed_schema->connect(...);
52
53 # Or the same thing via a convenience shortcut:
54 my $newconn = $c->model('Foo')->connect(...);
55
56 # or, if your schema works on different storage drivers:
57 my $newconn = $c->model('Foo')->composed_schema->clone();
58 $newconn->storage_type('::LDAP');
f7a944f8 59 $newconn->connection(...);
ad91060a 60
61 # and again, a convenience shortcut
62 my $newconn = $c->model('Foo')->clone();
63 $newconn->storage_type('::LDAP');
f7a944f8 64 $newconn->connection(...);
ad91060a 65
66=head1 DESCRIPTION
67
7b39f3f0 68This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
69the documentation for L<Catalyst::Helper::Model::DBIC::Schema> and
70L<Catalyst::Helper::Model::DBIC::SchemaInlineLoader> for information
71on generating these Models via Helper scripts.
ad91060a 72
73=head1 CONFIG PARAMETERS
74
75=over 4
76
77=item schema_class
78
79This is the classname of your L<DBIx::Class::Schema> Schema. It needs
80to be findable in C<@INC>, but it does not need to be underneath
0f2fd2c0 81C<Catalyst::Model::>. This parameter is required.
ad91060a 82
83=item connect_info
84
85This is an arrayref of connection parameters, which are specific to your
86C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
87in L<DBIx::Class> at the time of this writing, the 4 parameters are your
88dsn, username, password, and connect options hashref.
89
0f2fd2c0 90This is not required if C<schema_class> already has connection information
91defined in itself (which would be the case for a Schema defined by
92L<DBIx::Class::Schema::Loader>, for instance).
93
ad91060a 94=item storage_type
95
96Allows the use of a different C<storage_type> than what is set in your
97C<schema_class> (which in turn defaults to C<::DBI> if not set in current
98L<DBIx::Class>). Completely optional, and probably unneccesary for most
99people, until other storage backends become available for L<DBIx::Class>.
100
101=back
102
103=head1 METHODS
104
105=over 4
106
107=item new
108
109Instantiates the Model based on the above-documented ->config parameters.
0f2fd2c0 110The only required parameter is C<schema_class>. C<connect_info> is
111required in the case that C<schema_class> does not already have connection
112information defined for it.
ad91060a 113
114=item schema
115
116Accessor which returns the connected schema being used by the this model.
c12b7310 117There are already direct shortcuts on the model class itself for
118schema->resultset, schema->source, and schema->class.
ad91060a 119
120=item composed_schema
121
122Accessor which returns the composed schema, which has no connection info,
123which was used in constructing the C<schema> above. Useful for creating
c12b7310 124new connections based on the same schema/model. There are direct shortcuts
125from the model object for composed_schema->clone and composed_schema->connect
ad91060a 126
127=item clone
128
129Shortcut for ->composed_schema->clone
130
131=item connect
132
133Shortcut for ->composed_schema->connect
134
c12b7310 135=item source
136
137Shortcut for ->schema->source
138
139=item class
140
141Shortcut for ->schema->class
142
143=item resultset
144
145Shortcut for ->schema->resultset
146
ad91060a 147=back
148
149=cut
150
151sub new {
46878f2e 152 my $self = shift->NEXT::new(@_);
ad91060a 153
154 my $class = ref($self);
155 my $model_name = $class;
156 $model_name =~ s/^[\w:]+::(?:Model|M):://;
157
0f2fd2c0 158 croak "->config->{schema_class} must be defined for this model"
159 unless $self->{schema_class};
ad91060a 160
161 my $schema_class = $self->{schema_class};
162
7b39f3f0 163 # don't require if the class is already loaded...
164 if( !can($schema_class, 'source') ) {
165 $schema_class->require
166 or croak "Cannot load schema class '$schema_class': $@";
167 }
ad91060a 168
0f2fd2c0 169 if( !$self->{connect_info} ) {
170 if($schema_class->storage && $schema_class->storage->connect_info) {
171 $self->{connect_info} = $schema_class->storage->connect_info;
172 }
173 else {
174 croak "Either ->config->{connect_info} must be defined for $class"
175 . " or $schema_class must have connection defined on it";
176 }
177 }
178
ad91060a 179 $self->composed_schema($schema_class->compose_namespace($class));
180 $self->schema($self->composed_schema->clone);
181 $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
46878f2e 182 $self->schema->connection(@{$self->{connect_info}});
ad91060a 183
184 no strict 'refs';
185 foreach my $moniker ($self->schema->sources) {
186 *{"${class}::${moniker}::ACCEPT_CONTEXT"} = sub {
187 shift;
c12b7310 188 shift->model($model_name)->resultset($moniker);
ad91060a 189 }
190 }
191
192 return $self;
193}
194
ad91060a 195sub clone { shift->composed_schema->clone(@_); }
196
ad91060a 197sub connect { shift->composed_schema->connect(@_); }
198
199=head1 SEE ALSO
200
7b39f3f0 201General Catalyst Stuff:
202
203L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
204L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
205
206Stuff related to DBIC and this Model style:
207
208L<DBIx::Class>, L<DBIx::Class::Schema>,
209L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
210L<Catalyst::Helper::Model::DBIC::SchemaInlineLoader>
ad91060a 211
212=head1 AUTHOR
213
214Brandon L Black, C<blblack@gmail.com>
215
216=head1 COPYRIGHT
217
218This program is free software, you can redistribute it and/or modify it
219under the same terms as Perl itself.
220
221=cut
222
2231;