C::Helper::M::DBIC::SChema -- better Schema::Loader arg parsing, and a bugfix
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
CommitLineData
ad91060a 1package Catalyst::Helper::Model::DBIC::Schema;
2
3use strict;
0f2fd2c0 4use warnings;
018eb0e2 5
dc3605af 6our $VERSION = '0.21';
018eb0e2 7
0f2fd2c0 8use Carp;
781c6876 9use UNIVERSAL::require;
ad91060a 10
11=head1 NAME
12
13Catalyst::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
21Helper for the DBIC Schema Models.
22
23=head2 Arguments:
24
018eb0e2 25C<CatalystModelName> is the short name for the Catalyst Model class
26being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>).
6116daed 27
018eb0e2 28C<MyApp::SchemaClass> is the fully qualified classname of your Schema,
6116daed 29which might or might not yet exist. Note that you should have a good
30reason to create this under a new global namespace, otherwise use an
31existing top level namespace for your schema class.
32
018eb0e2 33C<create=dynamic> instructs this Helper to generate the named Schema
6116daed 34class for you, basing it on L<DBIx::Class::Schema::Loader> (which
35means the table information will always be dynamically loaded at
36runtime from the database).
37
018eb0e2 38C<create=static> instructs this Helper to generate the named Schema
6116daed 39class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
40mode to create a standard, manually-defined L<DBIx::Class::Schema>
41setup, based on what the Loader sees in your database at this moment.
42A Schema/Model pair generated this way will not require
43L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
44adapt itself to changes in your database structure. You can edit
45the generated classes by hand to refine them.
46
018eb0e2 47C<connect_info> arguments are the same as what
6116daed 48DBIx::Class::Schema::connect expects, and are storage_type-specific.
49For DBI-based storage, these arguments are the dsn, username,
50password, and connect options, respectively. These are optional for
51existing Schemas, but required if you use either of the C<create=>
52options.
d89e6c8a 53
54Use 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
ca14239e 62 # Same, but with extra Schema::Loader args (separate multiple values by commas):
63 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar exclude='^wibble|wobble$' dbi:Pg:dbname=foodb myuname mypass
34f036a0 64
ca14239e 65 # See DBIx::Class::Schema::Loader::Base for list of options
34f036a0 66
d89e6c8a 67 # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
68 # and a Model which references it:
6116daed 69 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
ad91060a 70
d89e6c8a 71 # Reference an existing Schema of any kind, and provide some connection information for ->config:
6116daed 72 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass dbi:mysql:foodb myuname mypass
ad91060a 73
d89e6c8a 74 # Same, but don't supply connect information yet (you'll need to do this
75 # in your app config, or [not recommended] in the schema itself).
76 script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
ad91060a 77
018eb0e2 78=head1 METHODS
ad91060a 79
018eb0e2 80=head2 mk_compclass
ad91060a 81
82=cut
83
84sub mk_compclass {
0b2a7108 85 my ( $self, $helper, $schema_class, @connect_info) = @_;
0f2fd2c0 86
d89e6c8a 87 $helper->{schema_class} = $schema_class
592cd3ae 88 or croak "Must supply schema class name";
d89e6c8a 89
90 my $create = '';
91 if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) {
92 $create = $1;
93 shift @connect_info;
94 }
0f2fd2c0 95
ca14239e 96 my %extra_args;
97 while (@connect_info && $connect_info[0] !~ /^dbi:/) {
98 my ($key, $val) = split /=/, shift(@connect_info);
99
100 if ((my @vals = split /,/ => $val) > 1) {
101 $extra_args{$key} = \@vals;
102 } else {
103 $extra_args{$key} = $val;
104 }
105 }
106
0b2a7108 107 if(@connect_info) {
0f2fd2c0 108 $helper->{setup_connect_info} = 1;
a0bc9b1d 109 my @helper_connect_info = @connect_info;
110 for(@helper_connect_info) {
0b2a7108 111 $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
112 }
a0bc9b1d 113 $helper->{connect_info} = \@helper_connect_info;
0f2fd2c0 114 }
115
d89e6c8a 116 if($create eq 'dynamic') {
117 my @schema_parts = split(/\:\:/, $helper->{schema_class});
118 my $schema_file_part = pop @schema_parts;
119
120 my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib', @schema_parts );
121 my $schema_file = File::Spec->catfile( $schema_dir, $schema_file_part . '.pm' );
122
123 $helper->mk_dir($schema_dir);
124 $helper->render_file( 'schemaclass', $schema_file );
125 }
126 elsif($create eq 'static') {
592cd3ae 127 my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib' );
128 DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at')
129 or croak "Cannot load DBIx::Class::Schema::Loader: $@";
130
131 my @loader_connect_info = @connect_info;
132 my $num = 6; # argument number on the commandline for "dbi:..."
133 for(@loader_connect_info) {
134 if(/^\s*[[{]/) {
135 $_ = eval "$_";
136 croak "Perl syntax error in commandline argument $num: $@" if $@;
137 }
138 $num++;
139 }
140
1d155058 141# Check if we need to be backward-compatible.
142 my $compatible = 0;
143
144 my @schema_pm = split '::', $schema_class;
145 $schema_pm[-1] .= '.pm';
146 my $schema_file = File::Spec->catfile($helper->{base}, 'lib', @schema_pm);
147
148 if (-f $schema_file) {
149 my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
150 $compatible = 1 if $schema_code =~ /->load_classes/;
151 }
152
34f036a0 153 my @components = $compatible ? () : ('InflateColumn::DateTime');
154
155 if (exists $extra_args{components}) {
156 $extra_args{components} = [ $extra_args{components} ]
157 unless ref $extra_args{components};
158
159 push @components, @{ delete $extra_args{components} };
1d155058 160 }
34f036a0 161
ca14239e 162 for my $re_opt (qw/constraint exclude/) {
163 $extra_args{$re_opt} = qr/$extra_args{$re_opt}/
164 if exists $extra_args{$re_opt};
165 }
166
167 if (exists $extra_args{moniker_map}) {
168 die "The moniker_map option is not currently supported by this helper, please write your own DBIx::Class::Schema::Loader script if you need it."
169 }
170
34f036a0 171 make_schema_at(
172 $schema_class,
173 {
174 relationships => 1,
175 (%extra_args ? %extra_args : ()),
176 (!$compatible ? (
177 use_namespaces => 1
178 ) : ()),
179 (@components ? (
180 components => \@components
181 ) : ())
182 },
183 \@loader_connect_info,
184 );
d89e6c8a 185 }
202d09c8 186
187 my $file = $helper->{file};
188 $helper->render_file( 'compclass', $file );
ad91060a 189}
190
191=head1 SEE ALSO
192
7b39f3f0 193General Catalyst Stuff:
194
ad91060a 195L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
7b39f3f0 196L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
197
198Stuff related to DBIC and this Model style:
199
200L<DBIx::Class>, L<DBIx::Class::Schema>,
ef91bcf9 201L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
ad91060a 202
203=head1 AUTHOR
204
205Brandon L Black, C<blblack@gmail.com>
206
207=head1 LICENSE
208
209This library is free software, you can redistribute it and/or modify
210it under the same terms as Perl itself.
211
212=cut
213
dce0dfe8 2141;
215
ad91060a 216__DATA__
217
5d11d759 218=begin pod_to_ignore
219
d89e6c8a 220__schemaclass__
221package [% schema_class %];
222
223use strict;
224use base qw/DBIx::Class::Schema::Loader/;
225
226__PACKAGE__->loader_options(
227 relationships => 1,
228 # debug => 1,
229);
230
231=head1 NAME
232
233[% schema_class %] - DBIx::Class::Schema::Loader class
234
235=head1 SYNOPSIS
236
237See L<[% app %]>
238
239=head1 DESCRIPTION
240
241Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
242
243=head1 AUTHOR
244
245[% author %]
246
247=head1 LICENSE
248
249This library is free software, you can redistribute it and/or modify
250it under the same terms as Perl itself.
251
252=cut
253
2541;
255
ad91060a 256__compclass__
257package [% class %];
258
259use strict;
260use base 'Catalyst::Model::DBIC::Schema';
261
262__PACKAGE__->config(
0f2fd2c0 263 schema_class => '[% schema_class %]',
0b2a7108 264 [% IF setup_connect_info %]connect_info => [
265 [% FOREACH arg = connect_info %][% arg %],
266 [% END %]
267 ],[% END %]
ad91060a 268);
269
270=head1 NAME
271
1aeb6e1e 272[% class %] - Catalyst DBIC Schema Model
ad91060a 273=head1 SYNOPSIS
274
275See L<[% app %]>
276
277=head1 DESCRIPTION
278
d89e6c8a 279L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
ad91060a 280
281=head1 AUTHOR
282
283[% author %]
284
285=head1 LICENSE
286
287This library is free software, you can redistribute it and/or modify
288it under the same terms as Perl itself.
289
290=cut
291
2921;