C::Helper::DBIC::Schema -- parse extra Schema::Loader args
[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
34f036a0 62 # Same, but with extra Schema::Loader args such as db_schema:
63 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static db_schema=foodb dbi:Pg:dbname=foodb myuname mypass
64
65 # Likewise, for multiple values such as a components:
66 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static components=Some::Component components=Some::OtherComponent dbi:Pg:dbname=foodb myuname mypass
67
d89e6c8a 68 # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
69 # and a Model which references it:
6116daed 70 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
ad91060a 71
d89e6c8a 72 # Reference an existing Schema of any kind, and provide some connection information for ->config:
6116daed 73 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass dbi:mysql:foodb myuname mypass
ad91060a 74
d89e6c8a 75 # Same, but don't supply connect information yet (you'll need to do this
76 # in your app config, or [not recommended] in the schema itself).
77 script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
ad91060a 78
018eb0e2 79=head1 METHODS
ad91060a 80
018eb0e2 81=head2 mk_compclass
ad91060a 82
83=cut
84
85sub mk_compclass {
0b2a7108 86 my ( $self, $helper, $schema_class, @connect_info) = @_;
0f2fd2c0 87
d89e6c8a 88 $helper->{schema_class} = $schema_class
592cd3ae 89 or croak "Must supply schema class name";
d89e6c8a 90
91 my $create = '';
92 if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) {
93 $create = $1;
94 shift @connect_info;
95 }
0f2fd2c0 96
0b2a7108 97 if(@connect_info) {
0f2fd2c0 98 $helper->{setup_connect_info} = 1;
a0bc9b1d 99 my @helper_connect_info = @connect_info;
100 for(@helper_connect_info) {
0b2a7108 101 $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
102 }
a0bc9b1d 103 $helper->{connect_info} = \@helper_connect_info;
0f2fd2c0 104 }
105
d89e6c8a 106 if($create eq 'dynamic') {
107 my @schema_parts = split(/\:\:/, $helper->{schema_class});
108 my $schema_file_part = pop @schema_parts;
109
110 my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib', @schema_parts );
111 my $schema_file = File::Spec->catfile( $schema_dir, $schema_file_part . '.pm' );
112
113 $helper->mk_dir($schema_dir);
114 $helper->render_file( 'schemaclass', $schema_file );
115 }
116 elsif($create eq 'static') {
592cd3ae 117 my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib' );
118 DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at')
119 or croak "Cannot load DBIx::Class::Schema::Loader: $@";
120
34f036a0 121 my %extra_args;
122 while (@connect_info && $connect_info[0] !~ /^dbi:/) {
123 my ($key, $val) = split /=/, shift(@connect_info);
124
125 if (exists $extra_args{$key}) {
126 $extra_args{$key} = [ $extra_args{$key} ]
127 unless ref $extra_args{$key};
128
129 push @{ $extra_args{$key} }, $val;
130 } else {
131 $extra_args{$key} = $val;
132 }
133 }
134
592cd3ae 135 my @loader_connect_info = @connect_info;
136 my $num = 6; # argument number on the commandline for "dbi:..."
137 for(@loader_connect_info) {
138 if(/^\s*[[{]/) {
139 $_ = eval "$_";
140 croak "Perl syntax error in commandline argument $num: $@" if $@;
141 }
142 $num++;
143 }
144
1d155058 145# Check if we need to be backward-compatible.
146 my $compatible = 0;
147
148 my @schema_pm = split '::', $schema_class;
149 $schema_pm[-1] .= '.pm';
150 my $schema_file = File::Spec->catfile($helper->{base}, 'lib', @schema_pm);
151
152 if (-f $schema_file) {
153 my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
154 $compatible = 1 if $schema_code =~ /->load_classes/;
155 }
156
34f036a0 157 my @components = $compatible ? () : ('InflateColumn::DateTime');
158
159 if (exists $extra_args{components}) {
160 $extra_args{components} = [ $extra_args{components} ]
161 unless ref $extra_args{components};
162
163 push @components, @{ delete $extra_args{components} };
1d155058 164 }
34f036a0 165
166 make_schema_at(
167 $schema_class,
168 {
169 relationships => 1,
170 (%extra_args ? %extra_args : ()),
171 (!$compatible ? (
172 use_namespaces => 1
173 ) : ()),
174 (@components ? (
175 components => \@components
176 ) : ())
177 },
178 \@loader_connect_info,
179 );
d89e6c8a 180 }
202d09c8 181
182 my $file = $helper->{file};
183 $helper->render_file( 'compclass', $file );
ad91060a 184}
185
186=head1 SEE ALSO
187
7b39f3f0 188General Catalyst Stuff:
189
ad91060a 190L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
7b39f3f0 191L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
192
193Stuff related to DBIC and this Model style:
194
195L<DBIx::Class>, L<DBIx::Class::Schema>,
ef91bcf9 196L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
ad91060a 197
198=head1 AUTHOR
199
200Brandon L Black, C<blblack@gmail.com>
201
202=head1 LICENSE
203
204This library is free software, you can redistribute it and/or modify
205it under the same terms as Perl itself.
206
207=cut
208
dce0dfe8 2091;
210
ad91060a 211__DATA__
212
5d11d759 213=begin pod_to_ignore
214
d89e6c8a 215__schemaclass__
216package [% schema_class %];
217
218use strict;
219use base qw/DBIx::Class::Schema::Loader/;
220
221__PACKAGE__->loader_options(
222 relationships => 1,
223 # debug => 1,
224);
225
226=head1 NAME
227
228[% schema_class %] - DBIx::Class::Schema::Loader class
229
230=head1 SYNOPSIS
231
232See L<[% app %]>
233
234=head1 DESCRIPTION
235
236Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
237
238=head1 AUTHOR
239
240[% author %]
241
242=head1 LICENSE
243
244This library is free software, you can redistribute it and/or modify
245it under the same terms as Perl itself.
246
247=cut
248
2491;
250
ad91060a 251__compclass__
252package [% class %];
253
254use strict;
255use base 'Catalyst::Model::DBIC::Schema';
256
257__PACKAGE__->config(
0f2fd2c0 258 schema_class => '[% schema_class %]',
0b2a7108 259 [% IF setup_connect_info %]connect_info => [
260 [% FOREACH arg = connect_info %][% arg %],
261 [% END %]
262 ],[% END %]
ad91060a 263);
264
265=head1 NAME
266
1aeb6e1e 267[% class %] - Catalyst DBIC Schema Model
ad91060a 268=head1 SYNOPSIS
269
270See L<[% app %]>
271
272=head1 DESCRIPTION
273
d89e6c8a 274L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
ad91060a 275
276=head1 AUTHOR
277
278[% author %]
279
280=head1 LICENSE
281
282This library is free software, you can redistribute it and/or modify
283it under the same terms as Perl itself.
284
285=cut
286
2871;