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