File Coverage

blib/lib/Catalyst/Helper/Model/DBIC/Schema.pm
Criterion Covered Total %
statement 12 43 27.9
branch 0 18 0.0
condition 0 3 0.0
subroutine 4 5 80.0
pod 1 1 100.0
total 17 70 24.3


line stmt bran cond sub pod time code
1             package Catalyst::Helper::Model::DBIC::Schema;
2              
3 1     1   16 use strict;
  1         14  
  1         106  
4 1     1   15 use warnings;
  1         9  
  1         14  
5 1     1   15 use Carp;
  1         9  
  1         19  
6 1     1   16 use UNIVERSAL::require;
  1         9  
  1         33  
7              
8             =head1 NAME
9            
10             Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
11            
12             =head1 SYNOPSIS
13            
14             script/create.pl model ModelName DBIC::Schema My::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ]
15            
16             =head1 DESCRIPTION
17            
18             Helper 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.
40            
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,
44             respectively. These are optional for existing Schemas, but required
45             if you use either of the C<create=> options.
46            
47             Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
48            
49             =head1 TYPICAL EXAMPLES
50            
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
54            
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
58            
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
61            
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
65            
66             =head2 METHODS
67            
68             =head3 mk_compclass
69            
70             =cut
71              
72             sub mk_compclass {
73 0     0 1       my ( $self, $helper, $schema_class, @connect_info) = @_;
74              
75 0 0             $helper->{schema_class} = $schema_class
76                     or croak "Must supply schema class name";
77              
78 0               my $create = '';
79 0 0 0           if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) {
80 0                   $create = $1;
81 0                   shift @connect_info;
82                 }
83              
84 0 0             if(@connect_info) {
85 0                   $helper->{setup_connect_info} = 1;
86 0                   my @helper_connect_info = @connect_info;
87 0                   for(@helper_connect_info) {
88 0 0                     $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
89                     }
90 0                   $helper->{connect_info} = \@helper_connect_info;
91                 }
92              
93 0 0             if($create eq 'dynamic') {
    0          
94 0                   my @schema_parts = split(/\:\:/, $helper->{schema_class});
95 0                   my $schema_file_part = pop @schema_parts;
96              
97 0                   my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib', @schema_parts );
98 0                   my $schema_file = File::Spec->catfile( $schema_dir, $schema_file_part . '.pm' );
99              
100 0                   $helper->mk_dir($schema_dir);
101 0                   $helper->render_file( 'schemaclass', $schema_file );
102                 }
103                 elsif($create eq 'static') {
104 0                   my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib' );
105 0 0                 DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at')
106                         or croak "Cannot load DBIx::Class::Schema::Loader: $@";
107              
108 0                   my @loader_connect_info = @connect_info;
109 0                   my $num = 6; # argument number on the commandline for "dbi:..."
110 0                   for(@loader_connect_info) {
111 0 0                     if(/^\s*[[{]/) {
112 0                           $_ = eval "$_";
113 0 0                         croak "Perl syntax error in commandline argument $num: $@" if $@;
114                         }
115 0                       $num++;
116                     }
117              
118                     make_schema_at(
119 0                       $schema_class,
120                         { relationships => 1 },
121                         \@loader_connect_info,
122                     );
123                 }
124              
125 0               my $file = $helper->{file};
126 0               $helper->render_file( 'compclass', $file );
127             }
128              
129             =head1 SEE ALSO
130            
131             General Catalyst Stuff:
132            
133             L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
134             L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
135            
136             Stuff related to DBIC and this Model style:
137            
138             L<DBIx::Class>, L<DBIx::Class::Schema>,
139             L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
140            
141             =head1 AUTHOR
142            
143             Brandon L Black, C<blblack@gmail.com>
144            
145             =head1 LICENSE
146            
147             This library is free software, you can redistribute it and/or modify
148             it under the same terms as Perl itself.
149            
150             =cut
151              
152             1;
153              
154             __DATA__
155            
156             =begin pod_to_ignore
157            
158             __schemaclass__
159             package [% schema_class %];
160            
161             use strict;
162             use 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            
175             See L<[% app %]>
176            
177             =head1 DESCRIPTION
178            
179             Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
180            
181             =head1 AUTHOR
182            
183             [% author %]
184            
185             =head1 LICENSE
186            
187             This library is free software, you can redistribute it and/or modify
188             it under the same terms as Perl itself.
189            
190             =cut
191            
192             1;
193            
194             __compclass__
195             package [% class %];
196            
197             use strict;
198             use base 'Catalyst::Model::DBIC::Schema';
199            
200             __PACKAGE__->config(
201             schema_class => '[% schema_class %]',
202             [% IF setup_connect_info %]connect_info => [
203             [% FOREACH arg = connect_info %][% arg %],
204             [% END %]
205             ],[% END %]
206             );
207            
208             =head1 NAME
209            
210             [% class %] - Catalyst DBIC Schema Model
211             =head1 SYNOPSIS
212            
213             See L<[% app %]>
214            
215             =head1 DESCRIPTION
216            
217             L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
218            
219             =head1 AUTHOR
220            
221             [% author %]
222            
223             =head1 LICENSE
224            
225             This library is free software, you can redistribute it and/or modify
226             it under the same terms as Perl itself.
227            
228             =cut
229            
230             1;
231