version bumped, added kwalitee, removed old "first release" notice from pod
[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
3d8cdccc 9our $VERSION = '0.10';
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
7b39f3f0 67This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
68the documentation for L<Catalyst::Helper::Model::DBIC::Schema> and
1aeb6e1e 69L<Catalyst::Helper::Model::DBIC::SchemaLoader> for information
70on generating these Models via Helper scripts. The latter of the two
71will also generated a L<DBIx::Class::Schema::Loader>-based Schema class
72for you.
ad91060a 73
74=head1 CONFIG PARAMETERS
75
76=over 4
77
78=item schema_class
79
80This is the classname of your L<DBIx::Class::Schema> Schema. It needs
81to be findable in C<@INC>, but it does not need to be underneath
0f2fd2c0 82C<Catalyst::Model::>. This parameter is required.
ad91060a 83
84=item connect_info
85
86This is an arrayref of connection parameters, which are specific to your
87C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
88in L<DBIx::Class> at the time of this writing, the 4 parameters are your
89dsn, username, password, and connect options hashref.
90
0f2fd2c0 91This is not required if C<schema_class> already has connection information
92defined in itself (which would be the case for a Schema defined by
93L<DBIx::Class::Schema::Loader>, for instance).
94
ad91060a 95=item storage_type
96
97Allows the use of a different C<storage_type> than what is set in your
98C<schema_class> (which in turn defaults to C<::DBI> if not set in current
b8427e0b 99L<DBIx::Class>). Completely optional, and probably unnecessary for most
100people until other storage backends become available for L<DBIx::Class>.
ad91060a 101
102=back
103
104=head1 METHODS
105
106=over 4
107
108=item new
109
110Instantiates the Model based on the above-documented ->config parameters.
0f2fd2c0 111The only required parameter is C<schema_class>. C<connect_info> is
112required in the case that C<schema_class> does not already have connection
113information defined for it.
ad91060a 114
115=item schema
116
117Accessor which returns the connected schema being used by the this model.
c12b7310 118There are already direct shortcuts on the model class itself for
119schema->resultset, schema->source, and schema->class.
ad91060a 120
121=item composed_schema
122
123Accessor which returns the composed schema, which has no connection info,
124which was used in constructing the C<schema> above. Useful for creating
c12b7310 125new connections based on the same schema/model. There are direct shortcuts
126from the model object for composed_schema->clone and composed_schema->connect
ad91060a 127
128=item clone
129
130Shortcut for ->composed_schema->clone
131
132=item connect
133
134Shortcut for ->composed_schema->connect
135
c12b7310 136=item source
137
138Shortcut for ->schema->source
139
140=item class
141
142Shortcut for ->schema->class
143
144=item resultset
145
146Shortcut for ->schema->resultset
147
b8427e0b 148=item storage
149
150Provides an accessor for the connected schema's storage object.
151Used often for debugging and controlling transactions.
152
ad91060a 153=back
154
155=cut
156
157sub new {
46878f2e 158 my $self = shift->NEXT::new(@_);
ad91060a 159
160 my $class = ref($self);
161 my $model_name = $class;
162 $model_name =~ s/^[\w:]+::(?:Model|M):://;
163
0f2fd2c0 164 croak "->config->{schema_class} must be defined for this model"
165 unless $self->{schema_class};
ad91060a 166
167 my $schema_class = $self->{schema_class};
168
1aeb6e1e 169 $schema_class->require
170 or croak "Cannot load schema class '$schema_class': $@";
ad91060a 171
0f2fd2c0 172 if( !$self->{connect_info} ) {
173 if($schema_class->storage && $schema_class->storage->connect_info) {
174 $self->{connect_info} = $schema_class->storage->connect_info;
175 }
176 else {
177 croak "Either ->config->{connect_info} must be defined for $class"
1aeb6e1e 178 . " or $schema_class must have connect info defined on it";
0f2fd2c0 179 }
180 }
181
ad91060a 182 $self->composed_schema($schema_class->compose_namespace($class));
183 $self->schema($self->composed_schema->clone);
184 $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
46878f2e 185 $self->schema->connection(@{$self->{connect_info}});
ad91060a 186
187 no strict 'refs';
188 foreach my $moniker ($self->schema->sources) {
0b2a7108 189 my $classname = "${class}::$moniker";
0b2a7108 190 *{"${classname}::ACCEPT_CONTEXT"} = sub {
ad91060a 191 shift;
c12b7310 192 shift->model($model_name)->resultset($moniker);
ad91060a 193 }
194 }
195
196 return $self;
197}
198
ad91060a 199sub clone { shift->composed_schema->clone(@_); }
200
ad91060a 201sub connect { shift->composed_schema->connect(@_); }
202
b8427e0b 203sub storage { shift->schema->storage(@_); }
204
ad91060a 205=head1 SEE ALSO
206
7b39f3f0 207General Catalyst Stuff:
208
209L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
210L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
211
212Stuff related to DBIC and this Model style:
213
214L<DBIx::Class>, L<DBIx::Class::Schema>,
215L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
1aeb6e1e 216L<Catalyst::Helper::Model::DBIC::SchemaLoader>
ad91060a 217
218=head1 AUTHOR
219
220Brandon L Black, C<blblack@gmail.com>
221
222=head1 COPYRIGHT
223
224This program is free software, you can redistribute it and/or modify it
225under the same terms as Perl itself.
226
227=cut
228
2291;