1 package Catalyst::Helper::Model::DBIC::Schema;
9 use UNIVERSAL::require;
13 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
17 script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ]
21 Helper for the DBIC Schema Models.
25 C<CatalystModelName> is the short name for the Catalyst Model class
26 being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>).
28 C<MyApp::SchemaClass> is the fully qualified classname of your Schema,
29 which might or might not yet exist. Note that you should have a good
30 reason to create this under a new global namespace, otherwise use an
31 existing top level namespace for your schema class.
33 C<create=dynamic> instructs this Helper to generate the named Schema
34 class for you, basing it on L<DBIx::Class::Schema::Loader> (which
35 means the table information will always be dynamically loaded at
36 runtime from the database).
38 C<create=static> instructs this Helper to generate the named Schema
39 class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
40 mode to create a standard, manually-defined L<DBIx::Class::Schema>
41 setup, based on what the Loader sees in your database at this moment.
42 A Schema/Model pair generated this way will not require
43 L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
44 adapt itself to changes in your database structure. You can edit
45 the generated classes by hand to refine them.
47 C<connect_info> arguments are the same as what
48 DBIx::Class::Schema::connect expects, and are storage_type-specific.
49 For DBI-based storage, these arguments are the dsn, username,
50 password, and connect options, respectively. These are optional for
51 existing Schemas, but required if you use either of the C<create=>
54 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
56 =head1 TYPICAL EXAMPLES
58 # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
59 # and a Model which references it:
60 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass
62 # Same, but with extra Schema::Loader args (separate multiple values by commas):
63 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar exclude='^wibble|wobble$' dbi:Pg:dbname=foodb myuname mypass
65 # See DBIx::Class::Schema::Loader::Base for list of options
67 # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
68 # and a Model which references it:
69 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
71 # Reference an existing Schema of any kind, and provide some connection information for ->config:
72 script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass dbi:mysql:foodb myuname mypass
74 # Same, but don't supply connect information yet (you'll need to do this
75 # in your app config, or [not recommended] in the schema itself).
76 script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
85 my ( $self, $helper, $schema_class, @connect_info) = @_;
87 $helper->{schema_class} = $schema_class
88 or croak "Must supply schema class name";
91 if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) {
97 while (@connect_info && $connect_info[0] !~ /^dbi:/) {
98 my ($key, $val) = split /=/, shift(@connect_info);
100 if ((my @vals = split /,/ => $val) > 1) {
101 $extra_args{$key} = \@vals;
103 $extra_args{$key} = $val;
108 $helper->{setup_connect_info} = 1;
109 my @helper_connect_info = @connect_info;
110 for(@helper_connect_info) {
111 $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
113 $helper->{connect_info} = \@helper_connect_info;
116 if($create eq 'dynamic') {
117 my @schema_parts = split(/\:\:/, $helper->{schema_class});
118 my $schema_file_part = pop @schema_parts;
120 my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib', @schema_parts );
121 my $schema_file = File::Spec->catfile( $schema_dir, $schema_file_part . '.pm' );
123 $helper->mk_dir($schema_dir);
124 $helper->render_file( 'schemaclass', $schema_file );
126 elsif($create eq 'static') {
127 my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib' );
128 DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at')
129 or croak "Cannot load DBIx::Class::Schema::Loader: $@";
131 my @loader_connect_info = @connect_info;
132 my $num = 6; # argument number on the commandline for "dbi:..."
133 for(@loader_connect_info) {
136 croak "Perl syntax error in commandline argument $num: $@" if $@;
141 # Check if we need to be backward-compatible.
144 my @schema_pm = split '::', $schema_class;
145 $schema_pm[-1] .= '.pm';
146 my $schema_file = File::Spec->catfile($helper->{base}, 'lib', @schema_pm);
148 if (-f $schema_file) {
149 my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
150 $compatible = 1 if $schema_code =~ /->load_classes/;
153 my @components = $compatible ? () : ('InflateColumn::DateTime');
155 if (exists $extra_args{components}) {
156 $extra_args{components} = [ $extra_args{components} ]
157 unless ref $extra_args{components};
159 push @components, @{ delete $extra_args{components} };
162 for my $re_opt (qw/constraint exclude/) {
163 $extra_args{$re_opt} = qr/$extra_args{$re_opt}/
164 if exists $extra_args{$re_opt};
167 if (exists $extra_args{moniker_map}) {
168 die "The moniker_map option is not currently supported by this helper, please write your own DBIx::Class::Schema::Loader script if you need it."
175 (%extra_args ? %extra_args : ()),
180 components => \@components
183 \@loader_connect_info,
187 my $file = $helper->{file};
188 $helper->render_file( 'compclass', $file );
193 General Catalyst Stuff:
195 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
196 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
198 Stuff related to DBIC and this Model style:
200 L<DBIx::Class>, L<DBIx::Class::Schema>,
201 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
205 Brandon L Black, C<blblack@gmail.com>
209 This library is free software, you can redistribute it and/or modify
210 it under the same terms as Perl itself.
221 package [% schema_class %];
224 use base qw/DBIx::Class::Schema::Loader/;
226 __PACKAGE__->loader_options(
233 [% schema_class %] - DBIx::Class::Schema::Loader class
241 Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
249 This library is free software, you can redistribute it and/or modify
250 it under the same terms as Perl itself.
260 use base 'Catalyst::Model::DBIC::Schema';
263 schema_class => '[% schema_class %]',
264 [% IF setup_connect_info %]connect_info => [
265 [% FOREACH arg = connect_info %][% arg %],
272 [% class %] - Catalyst DBIC Schema Model
279 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
287 This library is free software, you can redistribute it and/or modify
288 it under the same terms as Perl itself.