switch M::DBIC::Schema to MRO::Compat from NEXT
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
1 package Catalyst::Helper::Model::DBIC::Schema;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.21';
7
8 use Carp;
9 use UNIVERSAL::require;
10
11 =head1 NAME
12
13 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
14
15 =head1 SYNOPSIS
16
17   script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ]
18
19 =head1 DESCRIPTION
20
21 Helper for the DBIC Schema Models.
22
23 =head2 Arguments:
24
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')>).
27
28 C<MyApp::SchemaClass> is the fully qualified classname of your Schema,
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
33 C<create=dynamic> instructs this Helper to generate the named Schema
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
38 C<create=static> instructs this Helper to generate the named Schema
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
47 C<connect_info> arguments are the same as what
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.
53
54 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
55
56 =head1 TYPICAL EXAMPLES
57
58   # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
59   #  and a Model which references it:
60   script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass
61
62   # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
63   #  and a Model which references it:
64   script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
65
66   # Reference an existing Schema of any kind, and provide some connection information for ->config:
67   script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass dbi:mysql:foodb myuname mypass
68
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
72
73 =head1 METHODS
74
75 =head2 mk_compclass
76
77 =cut
78
79 sub mk_compclass {
80     my ( $self, $helper, $schema_class, @connect_info) = @_;
81
82     $helper->{schema_class} = $schema_class
83         or croak "Must supply schema class name";
84
85     my $create = '';
86     if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) {
87         $create = $1;
88         shift @connect_info;
89     }
90
91     if(@connect_info) {
92         $helper->{setup_connect_info} = 1;
93         my @helper_connect_info = @connect_info;
94         for(@helper_connect_info) {
95             $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
96         }
97         $helper->{connect_info} = \@helper_connect_info;
98     }
99
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') {
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 # Check if we need to be backward-compatible.
126         my $compatible = 0;
127
128         my @schema_pm   = split '::', $schema_class;
129         $schema_pm[-1] .= '.pm';
130         my $schema_file = File::Spec->catfile($helper->{base}, 'lib', @schema_pm);
131
132         if (-f $schema_file) {
133             my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
134             $compatible = 1 if $schema_code =~ /->load_classes/;
135         }
136
137         if ($compatible) {
138             make_schema_at(
139                 $schema_class,
140                 { relationships => 1 },
141                 \@loader_connect_info,
142             );
143         } else { # use some saner defaults
144             make_schema_at(
145                 $schema_class,
146                 {
147                     relationships => 1,
148                     use_namespaces => 1,
149                     components => ['InflateColumn::DateTime']
150                 },
151                 \@loader_connect_info,
152             );
153         }
154     }
155
156     my $file = $helper->{file};
157     $helper->render_file( 'compclass', $file );
158 }
159
160 =head1 SEE ALSO
161
162 General Catalyst Stuff:
163
164 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
165 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
166
167 Stuff related to DBIC and this Model style:
168
169 L<DBIx::Class>, L<DBIx::Class::Schema>,
170 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
171
172 =head1 AUTHOR
173
174 Brandon L Black, C<blblack@gmail.com>
175
176 =head1 LICENSE
177
178 This library is free software, you can redistribute it and/or modify
179 it under the same terms as Perl itself.
180
181 =cut
182
183 1;
184
185 __DATA__
186
187 =begin pod_to_ignore
188
189 __schemaclass__
190 package [% schema_class %];
191
192 use strict;
193 use base qw/DBIx::Class::Schema::Loader/;
194
195 __PACKAGE__->loader_options(
196     relationships => 1,
197     # debug => 1,
198 );
199
200 =head1 NAME
201
202 [% schema_class %] - DBIx::Class::Schema::Loader class
203
204 =head1 SYNOPSIS
205
206 See L<[% app %]>
207
208 =head1 DESCRIPTION
209
210 Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
211
212 =head1 AUTHOR
213
214 [% author %]
215
216 =head1 LICENSE
217
218 This library is free software, you can redistribute it and/or modify
219 it under the same terms as Perl itself.
220
221 =cut
222
223 1;
224
225 __compclass__
226 package [% class %];
227
228 use strict;
229 use base 'Catalyst::Model::DBIC::Schema';
230
231 __PACKAGE__->config(
232     schema_class => '[% schema_class %]',
233     [% IF setup_connect_info %]connect_info => [
234         [% FOREACH arg = connect_info %][% arg %],
235         [% END %]
236     ],[% END %]
237 );
238
239 =head1 NAME
240
241 [% class %] - Catalyst DBIC Schema Model
242 =head1 SYNOPSIS
243
244 See L<[% app %]>
245
246 =head1 DESCRIPTION
247
248 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
249
250 =head1 AUTHOR
251
252 [% author %]
253
254 =head1 LICENSE
255
256 This library is free software, you can redistribute it and/or modify
257 it under the same terms as Perl itself.
258
259 =cut
260
261 1;