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