201ac29191ac1a9400cb6670646e90fb9bd8fcf1
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema.pm
1 package Catalyst::Model::DBIC::Schema;
2
3 use strict;
4 use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
5 use NEXT;
6 use UNIVERSAL::require;
7 use UNIVERSAL qw/ can /;
8 use Carp;
9
10 our $VERSION = '0.03';
11
12 __PACKAGE__->mk_classaccessor('composed_schema');
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
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
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')
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');
59     $newconn->connection(...);
60
61     # and again, a convenience shortcut
62     my $newconn = $c->model('Foo')->clone();
63     $newconn->storage_type('::LDAP');
64     $newconn->connection(...);
65
66 =head1 DESCRIPTION
67
68 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models.  See
69 the documentation for L<Catalyst::Helper::Model::DBIC::Schema> and
70 L<Catalyst::Helper::Model::DBIC::SchemaInlineLoader> for information
71 on generating these Models via Helper scripts.
72
73 =head1 CONFIG PARAMETERS
74
75 =over 4
76
77 =item schema_class
78
79 This is the classname of your L<DBIx::Class::Schema> Schema.  It needs
80 to be findable in C<@INC>, but it does not need to be underneath
81 C<Catalyst::Model::>.  This parameter is required.
82
83 =item connect_info
84
85 This is an arrayref of connection parameters, which are specific to your
86 C<storage_type>.  For C<::DBI>, which is the only supported C<storage_type>
87 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
88 dsn, username, password, and connect options hashref.
89
90 This is not required if C<schema_class> already has connection information
91 defined in itself (which would be the case for a Schema defined by
92 L<DBIx::Class::Schema::Loader>, for instance).
93
94 =item storage_type
95
96 Allows the use of a different C<storage_type> than what is set in your
97 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
98 L<DBIx::Class>).  Completely optional, and probably unneccesary for most
99 people, 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
109 Instantiates the Model based on the above-documented ->config parameters.
110 The only required parameter is C<schema_class>.  C<connect_info> is
111 required in the case that C<schema_class> does not already have connection
112 information defined for it.
113
114 =item schema
115
116 Accessor which returns the connected schema being used by the this model.
117 There are already direct shortcuts on the model class itself for
118 schema->resultset, schema->source, and schema->class.
119
120 =item composed_schema
121
122 Accessor which returns the composed schema, which has no connection info,
123 which was used in constructing the C<schema> above.  Useful for creating
124 new connections based on the same schema/model.  There are direct shortcuts
125 from the model object for composed_schema->clone and composed_schema->connect
126
127 =item clone
128
129 Shortcut for ->composed_schema->clone
130
131 =item connect
132
133 Shortcut for ->composed_schema->connect
134
135 =item source
136
137 Shortcut for ->schema->source
138
139 =item class
140
141 Shortcut for ->schema->class
142
143 =item resultset
144
145 Shortcut for ->schema->resultset
146
147 =back
148
149 =cut
150
151 sub new {
152     my $self = shift->NEXT::new(@_);
153     
154     my $class = ref($self);
155     my $model_name = $class;
156     $model_name =~ s/^[\w:]+::(?:Model|M):://;
157
158     croak "->config->{schema_class} must be defined for this model"
159         unless $self->{schema_class};
160
161     my $schema_class = $self->{schema_class};
162
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     }
168
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
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};
182     $self->schema->connection(@{$self->{connect_info}});
183
184     no strict 'refs';
185     foreach my $moniker ($self->schema->sources) {
186         *{"${class}::${moniker}::ACCEPT_CONTEXT"} = sub {
187             shift;
188             shift->model($model_name)->resultset($moniker);
189         }
190     }
191
192     return $self;
193 }
194
195 sub clone { shift->composed_schema->clone(@_); }
196
197 sub connect { shift->composed_schema->connect(@_); }
198
199 =head1 SEE ALSO
200
201 General Catalyst Stuff:
202
203 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
204 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
205
206 Stuff related to DBIC and this Model style:
207
208 L<DBIx::Class>, L<DBIx::Class::Schema>,
209 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
210 L<Catalyst::Helper::Model::DBIC::SchemaInlineLoader>
211
212 =head1 AUTHOR
213
214 Brandon L Black, C<blblack@gmail.com>
215
216 =head1 COPYRIGHT
217
218 This program is free software, you can redistribute it and/or modify it
219 under the same terms as Perl itself.
220
221 =cut
222
223 1;