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