Commit | Line | Data |
ad91060a |
1 | package Catalyst::Helper::Model::DBIC::Schema; |
2 | |
3 | use strict; |
0f2fd2c0 |
4 | use warnings; |
018eb0e2 |
5 | |
6 | our $VERSION = '0.20'; |
7 | |
0f2fd2c0 |
8 | use Carp; |
781c6876 |
9 | use UNIVERSAL::require; |
ad91060a |
10 | |
11 | =head1 NAME |
12 | |
13 | Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models |
14 | |
15 | =head1 SYNOPSIS |
16 | |
6116daed |
17 | script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ] |
0f2fd2c0 |
18 | |
d89e6c8a |
19 | =head1 DESCRIPTION |
20 | |
21 | Helper for the DBIC Schema Models. |
22 | |
23 | =head2 Arguments: |
24 | |
018eb0e2 |
25 | C<CatalystModelName> is the short name for the Catalyst Model class |
26 | being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>). |
6116daed |
27 | |
018eb0e2 |
28 | C<MyApp::SchemaClass> is the fully qualified classname of your Schema, |
6116daed |
29 | which might or might not yet exist. Note that you should have a good |
30 | reason to create this under a new global namespace, otherwise use an |
31 | existing top level namespace for your schema class. |
32 | |
018eb0e2 |
33 | C<create=dynamic> instructs this Helper to generate the named Schema |
6116daed |
34 | class for you, basing it on L<DBIx::Class::Schema::Loader> (which |
35 | means the table information will always be dynamically loaded at |
36 | runtime from the database). |
37 | |
018eb0e2 |
38 | C<create=static> instructs this Helper to generate the named Schema |
6116daed |
39 | class for you, using L<DBIx::Class::Schema::Loader> in "one shot" |
40 | mode to create a standard, manually-defined L<DBIx::Class::Schema> |
41 | setup, based on what the Loader sees in your database at this moment. |
42 | A Schema/Model pair generated this way will not require |
43 | L<DBIx::Class::Schema::Loader> at runtime, and will not automatically |
44 | adapt itself to changes in your database structure. You can edit |
45 | the generated classes by hand to refine them. |
46 | |
018eb0e2 |
47 | C<connect_info> arguments are the same as what |
6116daed |
48 | DBIx::Class::Schema::connect expects, and are storage_type-specific. |
49 | For DBI-based storage, these arguments are the dsn, username, |
50 | password, and connect options, respectively. These are optional for |
51 | existing Schemas, but required if you use either of the C<create=> |
52 | options. |
d89e6c8a |
53 | |
54 | Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>. |
0b2a7108 |
55 | |
56 | =head1 TYPICAL EXAMPLES |
57 | |
d89e6c8a |
58 | # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema, |
59 | # and a Model which references it: |
6116daed |
60 | script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass |
0b2a7108 |
61 | |
d89e6c8a |
62 | # Create a dynamic DBIx::Class::Schema::Loader-based Schema, |
63 | # and a Model which references it: |
6116daed |
64 | script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass |
ad91060a |
65 | |
d89e6c8a |
66 | # Reference an existing Schema of any kind, and provide some connection information for ->config: |
6116daed |
67 | script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass dbi:mysql:foodb myuname mypass |
ad91060a |
68 | |
d89e6c8a |
69 | # Same, but don't supply connect information yet (you'll need to do this |
70 | # in your app config, or [not recommended] in the schema itself). |
71 | script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass |
ad91060a |
72 | |
018eb0e2 |
73 | =head1 METHODS |
ad91060a |
74 | |
018eb0e2 |
75 | =head2 mk_compclass |
ad91060a |
76 | |
77 | =cut |
78 | |
79 | sub mk_compclass { |
0b2a7108 |
80 | my ( $self, $helper, $schema_class, @connect_info) = @_; |
0f2fd2c0 |
81 | |
d89e6c8a |
82 | $helper->{schema_class} = $schema_class |
592cd3ae |
83 | or croak "Must supply schema class name"; |
d89e6c8a |
84 | |
85 | my $create = ''; |
86 | if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) { |
87 | $create = $1; |
88 | shift @connect_info; |
89 | } |
0f2fd2c0 |
90 | |
0b2a7108 |
91 | if(@connect_info) { |
0f2fd2c0 |
92 | $helper->{setup_connect_info} = 1; |
a0bc9b1d |
93 | my @helper_connect_info = @connect_info; |
94 | for(@helper_connect_info) { |
0b2a7108 |
95 | $_ = qq{'$_'} if $_ !~ /^\s*[[{]/; |
96 | } |
a0bc9b1d |
97 | $helper->{connect_info} = \@helper_connect_info; |
0f2fd2c0 |
98 | } |
99 | |
d89e6c8a |
100 | if($create eq 'dynamic') { |
101 | my @schema_parts = split(/\:\:/, $helper->{schema_class}); |
102 | my $schema_file_part = pop @schema_parts; |
103 | |
104 | my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib', @schema_parts ); |
105 | my $schema_file = File::Spec->catfile( $schema_dir, $schema_file_part . '.pm' ); |
106 | |
107 | $helper->mk_dir($schema_dir); |
108 | $helper->render_file( 'schemaclass', $schema_file ); |
109 | } |
110 | elsif($create eq 'static') { |
592cd3ae |
111 | my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib' ); |
112 | DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at') |
113 | or croak "Cannot load DBIx::Class::Schema::Loader: $@"; |
114 | |
115 | my @loader_connect_info = @connect_info; |
116 | my $num = 6; # argument number on the commandline for "dbi:..." |
117 | for(@loader_connect_info) { |
118 | if(/^\s*[[{]/) { |
119 | $_ = eval "$_"; |
120 | croak "Perl syntax error in commandline argument $num: $@" if $@; |
121 | } |
122 | $num++; |
123 | } |
124 | |
125 | make_schema_at( |
126 | $schema_class, |
127 | { relationships => 1 }, |
128 | \@loader_connect_info, |
129 | ); |
d89e6c8a |
130 | } |
202d09c8 |
131 | |
132 | my $file = $helper->{file}; |
133 | $helper->render_file( 'compclass', $file ); |
ad91060a |
134 | } |
135 | |
136 | =head1 SEE ALSO |
137 | |
7b39f3f0 |
138 | General Catalyst Stuff: |
139 | |
ad91060a |
140 | L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>, |
7b39f3f0 |
141 | L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>, |
142 | |
143 | Stuff related to DBIC and this Model style: |
144 | |
145 | L<DBIx::Class>, L<DBIx::Class::Schema>, |
ef91bcf9 |
146 | L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema> |
ad91060a |
147 | |
148 | =head1 AUTHOR |
149 | |
150 | Brandon L Black, C<blblack@gmail.com> |
151 | |
152 | =head1 LICENSE |
153 | |
154 | This library is free software, you can redistribute it and/or modify |
155 | it under the same terms as Perl itself. |
156 | |
157 | =cut |
158 | |
dce0dfe8 |
159 | 1; |
160 | |
ad91060a |
161 | __DATA__ |
162 | |
5d11d759 |
163 | =begin pod_to_ignore |
164 | |
d89e6c8a |
165 | __schemaclass__ |
166 | package [% schema_class %]; |
167 | |
168 | use strict; |
169 | use base qw/DBIx::Class::Schema::Loader/; |
170 | |
171 | __PACKAGE__->loader_options( |
172 | relationships => 1, |
173 | # debug => 1, |
174 | ); |
175 | |
176 | =head1 NAME |
177 | |
178 | [% schema_class %] - DBIx::Class::Schema::Loader class |
179 | |
180 | =head1 SYNOPSIS |
181 | |
182 | See L<[% app %]> |
183 | |
184 | =head1 DESCRIPTION |
185 | |
186 | Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]> |
187 | |
188 | =head1 AUTHOR |
189 | |
190 | [% author %] |
191 | |
192 | =head1 LICENSE |
193 | |
194 | This library is free software, you can redistribute it and/or modify |
195 | it under the same terms as Perl itself. |
196 | |
197 | =cut |
198 | |
199 | 1; |
200 | |
ad91060a |
201 | __compclass__ |
202 | package [% class %]; |
203 | |
204 | use strict; |
205 | use base 'Catalyst::Model::DBIC::Schema'; |
206 | |
207 | __PACKAGE__->config( |
0f2fd2c0 |
208 | schema_class => '[% schema_class %]', |
0b2a7108 |
209 | [% IF setup_connect_info %]connect_info => [ |
210 | [% FOREACH arg = connect_info %][% arg %], |
211 | [% END %] |
212 | ],[% END %] |
ad91060a |
213 | ); |
214 | |
215 | =head1 NAME |
216 | |
1aeb6e1e |
217 | [% class %] - Catalyst DBIC Schema Model |
ad91060a |
218 | =head1 SYNOPSIS |
219 | |
220 | See L<[% app %]> |
221 | |
222 | =head1 DESCRIPTION |
223 | |
d89e6c8a |
224 | L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]> |
ad91060a |
225 | |
226 | =head1 AUTHOR |
227 | |
228 | [% author %] |
229 | |
230 | =head1 LICENSE |
231 | |
232 | This library is free software, you can redistribute it and/or modify |
233 | it under the same terms as Perl itself. |
234 | |
235 | =cut |
236 | |
237 | 1; |