Commit | Line | Data |
ad91060a |
1 | package Catalyst::Model::DBIC::Schema; |
2 | |
3 | use strict; |
f1613faa |
4 | use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/; |
ad91060a |
5 | use NEXT; |
6 | use UNIVERSAL::require; |
7 | use Carp; |
bfcd6e3d |
8 | use Data::Dumper; |
7db6da78 |
9 | require DBIx::Class; |
ad91060a |
10 | |
6d5cb6fa |
11 | our $VERSION = '0.18'; |
ad91060a |
12 | |
f1613faa |
13 | __PACKAGE__->mk_classaccessor('composed_schema'); |
14 | __PACKAGE__->mk_accessors('schema'); |
15 | |
ad91060a |
16 | =head1 NAME |
17 | |
18 | Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class |
19 | |
20 | =head1 SYNOPSIS |
21 | |
aabc1d75 |
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/ |
07edc53e |
42 | |
aabc1d75 |
43 | __PACKAGE__->load_components(qw/Core/); |
44 | __PACKAGE__->table('actor'); |
07edc53e |
45 | |
aabc1d75 |
46 | ... |
47 | |
48 | and 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 | |
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/; |
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 | |
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; |
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 | |
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 | |
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 |
140 | This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See |
ef91bcf9 |
141 | the documentation for L<Catalyst::Helper::Model::DBIC::Schema> for |
142 | information on generating these Models via Helper scripts. |
ad91060a |
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 |
aabc1d75 |
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. |
ad91060a |
153 | |
154 | =item connect_info |
155 | |
156 | This is an arrayref of connection parameters, which are specific to your |
7db6da78 |
157 | C<storage_type> (see your storage type documentation for more details). |
ad91060a |
158 | |
0f2fd2c0 |
159 | This is not required if C<schema_class> already has connection information |
d89e6c8a |
160 | defined inside itself (which isn't highly recommended, but can be done) |
0f2fd2c0 |
161 | |
7db6da78 |
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 | |
f1613faa |
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. |
7db6da78 |
171 | |
172 | Examples: |
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 | |
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 |
f1613faa |
202 | L<DBIx::Class>). Completely optional, and probably unnecessary for most |
203 | people 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 | |
213 | Instantiates the Model based on the above-documented ->config parameters. |
0f2fd2c0 |
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. |
ad91060a |
217 | |
f1613faa |
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 |
c12b7310 |
240 | |
f1613faa |
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. |
b8427e0b |
255 | |
ad91060a |
256 | =back |
257 | |
258 | =cut |
259 | |
260 | sub 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 |
307 | sub clone { shift->composed_schema->clone(@_); } |
308 | |
309 | sub connect { shift->composed_schema->connect(@_); } |
310 | |
311 | sub storage { shift->schema->storage(@_); } |
b8427e0b |
312 | |
ad91060a |
313 | =head1 SEE ALSO |
314 | |
7b39f3f0 |
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>, |
ef91bcf9 |
323 | L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema> |
ad91060a |
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; |