1 package Catalyst::Helper::Model::DBIC::Schema;
6 use UNIVERSAL::require;
10 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
14 script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ]
18 Helper for the DBIC Schema Models.
22 C< CatalystModelName > is the short name for the Catalyst Model class
23 being generated (i.e. callable with C< $c->model >
25 C< MyApp::SchemaClass > is the fully qualified classname of your Schema,
26 which might or might not yet exist. Note that you should have a good
27 reason to create this under a new global namespace, otherwise use an
28 existing top level namespace for your schema class.
30 C< create=dynamic > instructs this Helper to generate the named Schema
31 class for you, basing it on L<DBIx::Class::Schema::Loader> (which
32 means the table information will always be dynamically loaded at
33 runtime from the database).
35 C< create=static > instructs this Helper to generate the named Schema
36 class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
37 mode to create a standard, manually-defined L<DBIx::Class::Schema>
38 setup, based on what the Loader sees in your database at this moment.
39 A Schema/Model pair generated this way will not require
40 L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
41 adapt itself to changes in your database structure. You can edit
42 the generated classes by hand to refine them.
44 C< connect_info > arguments are the same as what
45 DBIx::Class::Schema::connect expects, and are storage_type-specific.
46 For DBI-based storage, these arguments are the dsn, username,
47 password, and connect options, respectively. These are optional for
48 existing Schemas, but required if you use either of the C<create=>
51 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
53 =head1 TYPICAL EXAMPLES
55 # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
56 # and a Model which references it:
57 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass
59 # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
60 # and a Model which references it:
61 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
63 # Reference an existing Schema of any kind, and provide some connection information for ->config:
64 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass dbi:mysql:foodb myuname mypass
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
77 my ( $self, $helper, $schema_class, @connect_info) = @_;
79 $helper->{schema_class} = $schema_class
80 or croak "Must supply schema class name";
83 if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) {
89 $helper->{setup_connect_info} = 1;
90 my @helper_connect_info = @connect_info;
91 for(@helper_connect_info) {
92 $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
94 $helper->{connect_info} = \@helper_connect_info;
97 if($create eq 'dynamic') {
98 my @schema_parts = split(/\:\:/, $helper->{schema_class});
99 my $schema_file_part = pop @schema_parts;
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' );
104 $helper->mk_dir($schema_dir);
105 $helper->render_file( 'schemaclass', $schema_file );
107 elsif($create eq 'static') {
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: $@";
112 my @loader_connect_info = @connect_info;
113 my $num = 6; # argument number on the commandline for "dbi:..."
114 for(@loader_connect_info) {
117 croak "Perl syntax error in commandline argument $num: $@" if $@;
124 { relationships => 1 },
125 \@loader_connect_info,
129 my $file = $helper->{file};
130 $helper->render_file( 'compclass', $file );
135 General Catalyst Stuff:
137 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
138 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
140 Stuff related to DBIC and this Model style:
142 L<DBIx::Class>, L<DBIx::Class::Schema>,
143 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
147 Brandon L Black, C<blblack@gmail.com>
151 This library is free software, you can redistribute it and/or modify
152 it under the same terms as Perl itself.
163 package [% schema_class %];
166 use base qw/DBIx::Class::Schema::Loader/;
168 __PACKAGE__->loader_options(
175 [% schema_class %] - DBIx::Class::Schema::Loader class
183 Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
191 This library is free software, you can redistribute it and/or modify
192 it under the same terms as Perl itself.
202 use base 'Catalyst::Model::DBIC::Schema';
205 schema_class => '[% schema_class %]',
206 [% IF setup_connect_info %]connect_info => [
207 [% FOREACH arg = connect_info %][% arg %],
214 [% class %] - Catalyst DBIC Schema Model
221 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
229 This library is free software, you can redistribute it and/or modify
230 it under the same terms as Perl itself.