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