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