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