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