remove dist files generated by M::B, add on_connect_do support from Alexander Hartmai...
[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 Carp;
8
9 our $VERSION = '0.10';
10
11 __PACKAGE__->mk_classaccessor('composed_schema');
12 __PACKAGE__->mk_accessors('schema');
13
14 =head1 NAME
15
16 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
17
18 =head1 SYNOPSIS
19
20     package MyApp::Model::Foo;
21     use strict;
22     use base 'Catalyst::Model::DBIC::Schema';
23
24     __PACKAGE__->config(
25         schema_class    => 'Foo::SchemaClass',
26         connect_info    => [ 'dbi:Pg:dbname=foodb',
27                              'postgres',
28                              '',
29                              { AutoCommit => 1 },
30                            ],
31         on_connect_do   => [ 'sql statement 1', 'sql statement 2' ],
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::SchemaLoader> for information
71 on generating these Models via Helper scripts.  The latter of the two
72 will also generated a L<DBIx::Class::Schema::Loader>-based Schema class
73 for you.
74
75 =head1 CONFIG PARAMETERS
76
77 =over 4
78
79 =item schema_class
80
81 This is the classname of your L<DBIx::Class::Schema> Schema.  It needs
82 to be findable in C<@INC>, but it does not need to be underneath
83 C<Catalyst::Model::>.  This parameter is required.
84
85 =item connect_info
86
87 This is an arrayref of connection parameters, which are specific to your
88 C<storage_type>.  For C<::DBI>, which is the only supported C<storage_type>
89 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
90 dsn, username, password, and connect options hashref.
91
92 This is not required if C<schema_class> already has connection information
93 defined in itself (which would be the case for a Schema defined by
94 L<DBIx::Class::Schema::Loader>, for instance).
95
96 =item on_connect_do
97
98 This is an arrayref of sql statements, which are executed on every connect.
99 May not be a valid/useful argument with non-DBI-based Storages.
100
101 =item storage_type
102
103 Allows the use of a different C<storage_type> than what is set in your
104 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
105 L<DBIx::Class>).  Completely optional, and probably unnecessary for most
106 people until other storage backends become available for L<DBIx::Class>.
107
108 =back
109
110 =head1 METHODS
111
112 =over 4
113
114 =item new
115
116 Instantiates the Model based on the above-documented ->config parameters.
117 The only required parameter is C<schema_class>.  C<connect_info> is
118 required in the case that C<schema_class> does not already have connection
119 information defined for it.
120
121 =item schema
122
123 Accessor which returns the connected schema being used by the this model.
124 There are already direct shortcuts on the model class itself for
125 schema->resultset, schema->source, and schema->class.
126
127 =item composed_schema
128
129 Accessor which returns the composed schema, which has no connection info,
130 which was used in constructing the C<schema> above.  Useful for creating
131 new connections based on the same schema/model.  There are direct shortcuts
132 from the model object for composed_schema->clone and composed_schema->connect
133
134 =item clone
135
136 Shortcut for ->composed_schema->clone
137
138 =item connect
139
140 Shortcut for ->composed_schema->connect
141
142 =item source
143
144 Shortcut for ->schema->source
145
146 =item class
147
148 Shortcut for ->schema->class
149
150 =item resultset
151
152 Shortcut for ->schema->resultset
153
154 =item storage
155
156 Provides an accessor for the connected schema's storage object.
157 Used often for debugging and controlling transactions.
158
159 =back
160
161 =cut
162
163 sub new {
164     my $self = shift->NEXT::new(@_);
165     
166     my $class = ref($self);
167     my $model_name = $class;
168     $model_name =~ s/^[\w:]+::(?:Model|M):://;
169
170     croak "->config->{schema_class} must be defined for this model"
171         unless $self->{schema_class};
172
173     my $schema_class = $self->{schema_class};
174
175     $schema_class->require
176         or croak "Cannot load schema class '$schema_class': $@";
177
178     if( !$self->{connect_info} ) {
179         if($schema_class->storage && $schema_class->storage->connect_info) {
180             $self->{connect_info} = $schema_class->storage->connect_info;
181         }
182         else {
183             croak "Either ->config->{connect_info} must be defined for $class"
184                   . " or $schema_class must have connect info defined on it";
185         }
186     }
187
188     $self->composed_schema($schema_class->compose_namespace($class));
189     $self->schema($self->composed_schema->clone);
190
191     $self->schema->storage_type($self->{storage_type})
192         if $self->{storage_type};
193     $self->schema->connection(@{$self->{connect_info}});
194     $self->schema->storage->on_connect_do($self->{on_connect_do})
195         if $self->{on_connect_do};
196
197     no strict 'refs';
198     foreach my $moniker ($self->schema->sources) {
199         my $classname = "${class}::$moniker";
200         *{"${classname}::ACCEPT_CONTEXT"} = sub {
201             shift;
202             shift->model($model_name)->resultset($moniker);
203         }
204     }
205
206     return $self;
207 }
208
209 sub clone { shift->composed_schema->clone(@_); }
210
211 sub connect { shift->composed_schema->connect(@_); }
212
213 sub storage { shift->schema->storage(@_); }
214
215 =head1 SEE ALSO
216
217 General Catalyst Stuff:
218
219 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
220 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
221
222 Stuff related to DBIC and this Model style:
223
224 L<DBIx::Class>, L<DBIx::Class::Schema>,
225 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
226 L<Catalyst::Helper::Model::DBIC::SchemaLoader>
227
228 =head1 AUTHOR
229
230 Brandon L Black, C<blblack@gmail.com>
231
232 =head1 COPYRIGHT
233
234 This program is free software, you can redistribute it and/or modify it
235 under the same terms as Perl itself.
236
237 =cut
238
239 1;