6f4ff9dcf6f37bd70a66fb1011e83e2b3a575221
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
1 package Catalyst::Helper::Model::DBIC::Schema;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 =head1 NAME
8
9 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
10
11 =head1 SYNOPSIS
12
13   script/create.pl model ModelName DBIC::Schema My::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ]
14
15 =head1 DESCRIPTION
16
17 Helper 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       
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,
43       respectively.  These are optional for existing Schemas, but required
44       if you use either of the C<create=> options.
45
46 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
47
48 =head1 TYPICAL EXAMPLES
49
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
53
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
57
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
60
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
64
65 =head2 METHODS
66
67 =head3 mk_compclass
68
69 =cut
70
71 sub mk_compclass {
72     my ( $self, $helper, $schema_class, @connect_info) = @_;
73
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     }
82
83     if(@connect_info) {
84         $helper->{setup_connect_info} = 1;
85         for(@connect_info) {
86             $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
87         }
88         $helper->{connect_info} = \@connect_info;
89     }
90
91     my $file = $helper->{file};
92     $helper->render_file( 'compclass', $file );
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     }
108 }
109
110 =head1 SEE ALSO
111
112 General Catalyst Stuff:
113
114 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
115 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
116
117 Stuff related to DBIC and this Model style:
118
119 L<DBIx::Class>, L<DBIx::Class::Schema>,
120 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>,
121 L<Catalyst::Helper::Model::DBIC::SchemaLoader>
122
123 =head1 AUTHOR
124
125 Brandon L Black, C<blblack@gmail.com>
126
127 =head1 LICENSE
128
129 This library is free software, you can redistribute it and/or modify
130 it under the same terms as Perl itself.
131
132 =cut
133
134 1;
135
136 __DATA__
137
138 =begin pod_to_ignore
139
140 __schemaclass__
141 package [% schema_class %];
142
143 use strict;
144 use 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
157 See L<[% app %]>
158
159 =head1 DESCRIPTION
160
161 Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
162
163 =head1 AUTHOR
164
165 [% author %]
166
167 =head1 LICENSE
168
169 This library is free software, you can redistribute it and/or modify
170 it under the same terms as Perl itself.
171
172 =cut
173
174 1;
175
176 __compclass__
177 package [% class %];
178
179 use strict;
180 use base 'Catalyst::Model::DBIC::Schema';
181
182 __PACKAGE__->config(
183     schema_class => '[% schema_class %]',
184     [% IF setup_connect_info %]connect_info => [
185         [% FOREACH arg = connect_info %][% arg %],
186         [% END %]
187     ],[% END %]
188 );
189
190 =head1 NAME
191
192 [% class %] - Catalyst DBIC Schema Model
193 =head1 SYNOPSIS
194
195 See L<[% app %]>
196
197 =head1 DESCRIPTION
198
199 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
200
201 =head1 AUTHOR
202
203 [% author %]
204
205 =head1 LICENSE
206
207 This library is free software, you can redistribute it and/or modify
208 it under the same terms as Perl itself.
209
210 =cut
211
212 1;