1 package Catalyst::Helper::Model::DBIC::Schema;
4 no warnings 'uninitialized';
13 use namespace::clean -except => 'meta';
17 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
21 script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \
22 [ create=dynamic | create=static ] [ roles=role1,role2... ] \
23 [ Schema::Loader opts ] [ dsn user pass ] \
24 [ other connect_info args ]
28 Helper for the DBIC Schema Models.
32 C<CatalystModelName> is the short name for the Catalyst Model class
33 being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>).
35 C<MyApp::SchemaClass> is the fully qualified classname of your Schema,
36 which might or might not yet exist. Note that you should have a good
37 reason to create this under a new global namespace, otherwise use an
38 existing top level namespace for your schema class.
40 C<create=dynamic> instructs this Helper to generate the named Schema
41 class for you, basing it on L<DBIx::Class::Schema::Loader> (which
42 means the table information will always be dynamically loaded at
43 runtime from the database).
45 C<create=static> instructs this Helper to generate the named Schema
46 class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
47 mode to create a standard, manually-defined L<DBIx::Class::Schema>
48 setup, based on what the Loader sees in your database at this moment.
49 A Schema/Model pair generated this way will not require
50 L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
51 adapt itself to changes in your database structure. You can edit
52 the generated classes by hand to refine them.
54 C<roles> is the list of roles to apply to the model, see
55 L<Catalyst::Model::DBIC::Schema> for details.
57 C<Schema::Loader opts> are described in L</TYPICAL EXAMPLES> below.
59 C<connect_info> arguments are the same as what
60 DBIx::Class::Schema::connect expects, and are storage_type-specific.
61 For DBI-based storage, these arguments are the dsn, username,
62 password, and connect options, respectively. These are optional for
63 existing Schemas, but required if you use either of the C<create=>
66 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
68 =head1 TYPICAL EXAMPLES
70 # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
71 # and a Model which references it:
72 script/myapp_create.pl model CatalystModelName DBIC::Schema \
73 MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass
75 # Same, with extra connect_info args
76 script/myapp_create.pl model CatalystModelName DBIC::Schema \
77 MyApp::SchemaClass create=static dbi:SQLite:foo.db '' '' \
78 AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
79 on_connect_do='["select 1", "select 2"]'
81 # Same, but with extra Schema::Loader args (separate multiple values by commas):
82 script/myapp_create.pl model CatalystModelName DBIC::Schema \
83 MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \
84 exclude='^wibble|wobble$' moniker_map='{ foo => "FFFFUUUU" }' \
85 dbi:Pg:dbname=foodb myuname mypass
87 # See DBIx::Class::Schema::Loader::Base for list of options
89 # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
90 # and a Model which references it:
91 script/myapp_create.pl model CatalystModelName DBIC::Schema \
92 MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
94 # Reference an existing Schema of any kind, and provide some connection information for ->config:
95 script/myapp_create.pl model CatalystModelName DBIC::Schema \
96 MyApp::SchemaClass dbi:mysql:foodb myuname mypass
98 # Same, but don't supply connect information yet (you'll need to do this
99 # in your app config, or [not recommended] in the schema itself).
100 script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
104 has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1);
106 has schema_class => (is => 'ro', isa => 'Str', required => 1);
108 has loader_args => (is => 'rw', isa => 'HashRef');
109 has connect_info => (is => 'rw', isa => 'HashRef');
111 has old_schema => (is => 'rw', isa => 'Bool', lazy => 1, default => sub {
114 my @schema_pm = split '::', $self->schema_class;
115 $schema_pm[-1] .= '.pm';
117 File::Spec->catfile($self->helper->{base}, 'lib', @schema_pm);
119 if (-f $schema_file) {
120 my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
121 return 1 if $schema_code =~ /->load_classes/;
131 This is called by L<Catalyst::Helper> with the commandline args to generate the
137 my ($package, $helper, $schema_class, @args) = @_;
139 my $self = $package->new(helper => $helper, schema_class => $schema_class);
141 $helper->{schema_class} = $schema_class
142 or croak "Must supply schema class name";
145 if ($args[0] && $args[0] =~ /^create=(dynamic|static)\z/) {
149 if ($args[0] && $args[0] =~ /^roles=(.*)\z/) {
150 $helper->{roles} = '['
151 .(join ',' => map { qq{'$_'} } (split /,/ => $1))
157 $self->_parse_loader_args(\@args);
159 if (List::Util::first { /dbi:/ } @args) {
160 $helper->{setup_connect_info} = 1;
162 $helper->{connect_info} =
163 $self->_build_helper_connect_info(\@args);
165 $self->_parse_connect_info(\@args) if $create eq 'static';
170 $helper->{generator} = ref $self;
171 $helper->{generator_version} = $VERSION;
173 if ($create eq 'dynamic') {
174 $self->helper->{loader_args} = $self->_build_helper_loader_args;
175 $self->_gen_dynamic_schema;
176 } elsif ($create eq 'static') {
177 $self->_gen_static_schema;
183 sub _parse_loader_args {
184 my ($self, $args) = @_;
186 my %loader_args = $self->_read_loader_args($args);
188 while (my ($key, $val) = each %loader_args) {
189 next if $key =~ /^(?:components|constraint|exclude)\z/;
191 $loader_args{$key} = eval $val;
192 croak "syntax error for loader args key '$key' with value '$val': $@"
197 $self->_build_loader_components(delete $loader_args{components});
199 for my $re_opt (qw/constraint exclude/) {
200 $loader_args{$re_opt} = qr/$loader_args{$re_opt}/
201 if exists $loader_args{$re_opt};
204 tie my %result, 'Tie::IxHash';
208 (%loader_args ? %loader_args : ()),
209 (!$self->old_schema ? (
213 components => \@components
217 $self->loader_args(\%result);
219 wantarray ? %result : \%result;
222 sub _read_loader_args {
223 my ($self, $args) = @_;
227 while (@$args && $args->[0] !~ /^dbi:/) {
228 my ($key, $val) = split /=/, shift(@$args), 2;
230 if ((my @vals = split /,/ => $val) > 1) {
231 $loader_args{$key} = \@vals;
233 $loader_args{$key} = $val;
237 wantarray ? %loader_args : \%loader_args;
240 sub _build_helper_loader_args {
243 my $args = $self->loader_args;
245 tie my %loader_args, 'Tie::IxHash';
247 while (my ($arg, $val) = each %$args) {
249 $loader_args{$arg} = $self->_data_struct_to_string($val);
251 $loader_args{$arg} = qq{'$val'};
258 sub _build_loader_components {
259 my ($self, $components) = @_;
261 my @components = $self->old_schema ? () : ('InflateColumn::DateTime');
264 $components = [ $components ] if !ref $components;
265 push @components, @$components;
268 wantarray ? @components : \@components;
271 sub _build_helper_connect_info {
272 my ($self, $connect_info) = @_;
274 my @connect_info = @$connect_info;
276 my ($dsn, $user, $password) = splice @connect_info, 0, 3;
278 tie my %helper_connect_info, 'Tie::IxHash';
280 %helper_connect_info = (
283 password => qq{'$password'}
286 for (@connect_info) {
287 if (/^\s*{.*}\s*\z/) {
289 croak "Syntax errorr in connect_info hash: $_: $@" if $@;
292 for my $key (keys %hash) {
293 my $val = $hash{$key};
296 $val = $self->_data_struct_to_string($val);
301 $helper_connect_info{$key} = $val;
307 my ($key, $val) = split /=/, $_, 2;
309 $helper_connect_info{$key} = $self->_quote_unless_struct($val);
312 \%helper_connect_info
315 sub _data_struct_to_string {
316 my ($self, $data) = @_;
318 local $Data::Dumper::Terse = 1;
319 local $Data::Dumper::Quotekeys = 0;
320 local $Data::Dumper::Indent = 0;
321 local $Data::Dumper::Useqq = 1;
323 return Data::Dumper->Dump([$data]);
326 sub _parse_connect_info {
327 my ($self, $connect_info) = @_;
329 my @connect_info = @$connect_info;
331 my ($dsn, $user, $password) = splice @connect_info, 0, 3;
333 tie my %connect_info, 'Tie::IxHash';
334 @connect_info{qw/dsn user password/} = ($dsn, $user, $password);
336 for (@connect_info) {
337 if (/^\s*{.*}\s*\z/) {
339 croak "Syntax errorr in connect_info hash: $_: $@" if $@;
341 %connect_info = (%connect_info, %$hash);
346 my ($key, $val) = split /=/, $_, 2;
348 $connect_info{$key} = eval $val;
349 croak "syntax error for connect_info key '$key' with value '$val': $@"
353 $self->connect_info(\%connect_info);
358 sub _quote_unless_struct {
359 my ($self, $val) = @_;
361 $val = qq{'$val'} if $val !~ /^\s*[[{]/;
366 sub _gen_dynamic_schema {
369 my $helper = $self->helper;
371 my @schema_parts = split(/\:\:/, $self->schema_class);
372 my $schema_file_part = pop @schema_parts;
374 my $schema_dir = File::Spec->catfile(
375 $helper->{base}, 'lib', @schema_parts
377 my $schema_file = File::Spec->catfile(
378 $schema_dir, $schema_file_part . '.pm'
381 $helper->mk_dir($schema_dir);
382 $helper->render_file('schemaclass', $schema_file);
385 sub _gen_static_schema {
388 croak "cannot load schema without connect info" unless $self->connect_info;
390 my $helper = $self->helper;
392 my $schema_dir = File::Spec->catfile($helper->{base}, 'lib');
394 eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') };
395 croak "Cannot load DBIx::Class::Schema::Loader: $@" if $@;
397 DBIx::Class::Schema::Loader->import(
398 "dump_to_dir:$schema_dir", 'make_schema_at'
404 [$self->connect_info]
410 my $helper = $self->helper;
412 $helper->render_file('compclass', $helper->{file} );
417 General Catalyst Stuff:
419 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
420 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
422 Stuff related to DBIC and this Model style:
424 L<DBIx::Class>, L<DBIx::Class::Schema>,
425 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
429 Brandon L Black, C<blblack@gmail.com>
433 Rafael Kitover, C<<rkitover at cpan.org>>
437 This library is free software, you can redistribute it and/or modify
438 it under the same terms as Perl itself.
449 package [% schema_class %];
452 use base qw/DBIx::Class::Schema::Loader/;
454 __PACKAGE__->loader_options(
455 [%- FOREACH key = loader_args.keys %]
456 [% key %] => [% loader_args.${key} %],
463 [% schema_class %] - L<DBIx::Class::Schema::Loader> class
471 Dynamic L<DBIx::Class::Schema::Loader> schema for use in L<[% class %]>
475 [% generator %] - [% generator_version %]
479 [% author.replace(',+$', '') %]
483 This library is free software, you can redistribute it and/or modify
484 it under the same terms as Perl itself.
494 use base 'Catalyst::Model::DBIC::Schema';
497 schema_class => '[% schema_class %]',
498 [% IF roles %]roles => [% roles %],[% END %]
499 [% IF setup_connect_info %]connect_info => {
500 [%- FOREACH key = connect_info.keys %]
501 [% key %] => [% connect_info.${key} %],
509 [% class %] - Catalyst DBIC Schema Model
517 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
521 [% generator %] - [% generator_version %]
525 [% author.replace(',+$', '') %]
529 This library is free software, you can redistribute it and/or modify
530 it under the same terms as Perl itself.