0.14 release, Changes updated, tests fixed
[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
76 or die "Must supply schema class name";
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;
0b2a7108 86 for(@connect_info) {
87 $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
88 }
89 $helper->{connect_info} = \@connect_info;
0f2fd2c0 90 }
91
ad91060a 92 my $file = $helper->{file};
93 $helper->render_file( 'compclass', $file );
d89e6c8a 94
95 if($create eq 'dynamic') {
96 my @schema_parts = split(/\:\:/, $helper->{schema_class});
97 my $schema_file_part = pop @schema_parts;
98
99 my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib', @schema_parts );
100 my $schema_file = File::Spec->catfile( $schema_dir, $schema_file_part . '.pm' );
101
102 $helper->mk_dir($schema_dir);
103 $helper->render_file( 'schemaclass', $schema_file );
104 }
105 elsif($create eq 'static') {
781c6876 106 my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib' );
107 DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at')
108 or die "Cannot load DBIx::Class::Schema::Loader: $@";
109 make_schema_at(
110 $schema_class,
111 { relationships => 1 },
112 \@connect_info,
113 );
d89e6c8a 114 }
ad91060a 115}
116
117=head1 SEE ALSO
118
7b39f3f0 119General Catalyst Stuff:
120
ad91060a 121L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
7b39f3f0 122L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
123
124Stuff related to DBIC and this Model style:
125
126L<DBIx::Class>, L<DBIx::Class::Schema>,
1aeb6e1e 127L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>,
128L<Catalyst::Helper::Model::DBIC::SchemaLoader>
ad91060a 129
130=head1 AUTHOR
131
132Brandon L Black, C<blblack@gmail.com>
133
134=head1 LICENSE
135
136This library is free software, you can redistribute it and/or modify
137it under the same terms as Perl itself.
138
139=cut
140
dce0dfe8 1411;
142
ad91060a 143__DATA__
144
5d11d759 145=begin pod_to_ignore
146
d89e6c8a 147__schemaclass__
148package [% schema_class %];
149
150use strict;
151use base qw/DBIx::Class::Schema::Loader/;
152
153__PACKAGE__->loader_options(
154 relationships => 1,
155 # debug => 1,
156);
157
158=head1 NAME
159
160[% schema_class %] - DBIx::Class::Schema::Loader class
161
162=head1 SYNOPSIS
163
164See L<[% app %]>
165
166=head1 DESCRIPTION
167
168Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
169
170=head1 AUTHOR
171
172[% author %]
173
174=head1 LICENSE
175
176This library is free software, you can redistribute it and/or modify
177it under the same terms as Perl itself.
178
179=cut
180
1811;
182
ad91060a 183__compclass__
184package [% class %];
185
186use strict;
187use base 'Catalyst::Model::DBIC::Schema';
188
189__PACKAGE__->config(
0f2fd2c0 190 schema_class => '[% schema_class %]',
0b2a7108 191 [% IF setup_connect_info %]connect_info => [
192 [% FOREACH arg = connect_info %][% arg %],
193 [% END %]
194 ],[% END %]
ad91060a 195);
196
197=head1 NAME
198
1aeb6e1e 199[% class %] - Catalyst DBIC Schema Model
ad91060a 200=head1 SYNOPSIS
201
202See L<[% app %]>
203
204=head1 DESCRIPTION
205
d89e6c8a 206L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
ad91060a 207
208=head1 AUTHOR
209
210[% author %]
211
212=head1 LICENSE
213
214This library is free software, you can redistribute it and/or modify
215it under the same terms as Perl itself.
216
217=cut
218
2191;