C::H::M::DBIC::Schema - convert to Moose
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
CommitLineData
ad91060a 1package Catalyst::Helper::Model::DBIC::Schema;
2
f090a149 3use Moose;
ffbf1967 4no warnings 'uninitialized';
018eb0e2 5
2201c2e4 6our $VERSION = '0.24';
7
0f2fd2c0 8use Carp;
2201c2e4 9use Tie::IxHash ();
10use Data::Dumper ();
11use List::Util ();
12
f090a149 13use namespace::clean -except => 'meta';
ad91060a 14
15=head1 NAME
16
17Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
18
19=head1 SYNOPSIS
20
2201c2e4 21 script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \
2ff00e2b 22 [ create=dynamic | create=static ] [ roles=role1,role2... ] \
23 [ Schema::Loader opts ] [ dsn user pass ] \
24 [ other connect_info args ]
0f2fd2c0 25
d89e6c8a 26=head1 DESCRIPTION
27
28Helper for the DBIC Schema Models.
29
30=head2 Arguments:
31
018eb0e2 32C<CatalystModelName> is the short name for the Catalyst Model class
33being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>).
6116daed 34
018eb0e2 35C<MyApp::SchemaClass> is the fully qualified classname of your Schema,
6116daed 36which might or might not yet exist. Note that you should have a good
37reason to create this under a new global namespace, otherwise use an
38existing top level namespace for your schema class.
39
018eb0e2 40C<create=dynamic> instructs this Helper to generate the named Schema
6116daed 41class for you, basing it on L<DBIx::Class::Schema::Loader> (which
42means the table information will always be dynamically loaded at
43runtime from the database).
44
018eb0e2 45C<create=static> instructs this Helper to generate the named Schema
6116daed 46class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
47mode to create a standard, manually-defined L<DBIx::Class::Schema>
48setup, based on what the Loader sees in your database at this moment.
49A Schema/Model pair generated this way will not require
50L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
51adapt itself to changes in your database structure. You can edit
52the generated classes by hand to refine them.
53
2ff00e2b 54C<roles> is the list of roles to apply to the model, see
55L<Catalyst::Model::DBIC::Schema> for details.
56
57C<Schema::Loader opts> are described in L</TYPICAL EXAMPLES> below.
58
018eb0e2 59C<connect_info> arguments are the same as what
6116daed 60DBIx::Class::Schema::connect expects, and are storage_type-specific.
61For DBI-based storage, these arguments are the dsn, username,
62password, and connect options, respectively. These are optional for
63existing Schemas, but required if you use either of the C<create=>
64options.
d89e6c8a 65
66Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
0b2a7108 67
68=head1 TYPICAL EXAMPLES
69
d89e6c8a 70 # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
71 # and a Model which references it:
2201c2e4 72 script/myapp_create.pl model CatalystModelName DBIC::Schema \
73 MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass
74
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"]'
0b2a7108 80
ca14239e 81 # Same, but with extra Schema::Loader args (separate multiple values by commas):
2201c2e4 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
34f036a0 86
ca14239e 87 # See DBIx::Class::Schema::Loader::Base for list of options
34f036a0 88
d89e6c8a 89 # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
90 # and a Model which references it:
2201c2e4 91 script/myapp_create.pl model CatalystModelName DBIC::Schema \
92 MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
ad91060a 93
d89e6c8a 94 # Reference an existing Schema of any kind, and provide some connection information for ->config:
2201c2e4 95 script/myapp_create.pl model CatalystModelName DBIC::Schema \
96 MyApp::SchemaClass dbi:mysql:foodb myuname mypass
ad91060a 97
d89e6c8a 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
ad91060a 101
f090a149 102=cut
103
104has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1);
105
106has schema_class => (is => 'ro', isa => 'Str', required => 1);
107
108has loader_args => (is => 'rw', isa => 'HashRef');
109has connect_info => (is => 'rw', isa => 'HashRef');
110
111has old_schema => (is => 'rw', isa => 'Bool', lazy => 1, default => sub {
112 my $self = shift;
113
114 my @schema_pm = split '::', $self->schema_class;
115 $schema_pm[-1] .= '.pm';
116 my $schema_file =
117 File::Spec->catfile($self->helper->{base}, 'lib', @schema_pm);
118
119 if (-f $schema_file) {
120 my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
121 return 1 if $schema_code =~ /->load_classes/;
122 }
123
124 0;
125});
126
018eb0e2 127=head1 METHODS
ad91060a 128
018eb0e2 129=head2 mk_compclass
ad91060a 130
0fbbc8d5 131This is called by L<Catalyst::Helper> with the commandline args to generate the
132files.
133
ad91060a 134=cut
135
136sub mk_compclass {
2201c2e4 137 my ($package, $helper, $schema_class, @args) = @_;
138
f090a149 139 my $self = $package->new(helper => $helper, schema_class => $schema_class);
0f2fd2c0 140
d89e6c8a 141 $helper->{schema_class} = $schema_class
592cd3ae 142 or croak "Must supply schema class name";
d89e6c8a 143
144 my $create = '';
2ff00e2b 145 if ($args[0] && $args[0] =~ /^create=(dynamic|static)\z/) {
d89e6c8a 146 $create = $1;
2201c2e4 147 shift @args;
148
2ff00e2b 149 if ($args[0] && $args[0] =~ /^roles=(.*)\z/) {
150 $helper->{roles} = '['
151 .(join ',' => map { qq{'$_'} } (split /,/ => $1))
152 .']';
153 shift @args;
154 }
155
2201c2e4 156 if (@args) {
157 $self->_parse_loader_args(\@args);
158
159 if (List::Util::first { /dbi:/ } @args) {
160 $helper->{setup_connect_info} = 1;
161
162 $helper->{connect_info} =
f090a149 163 $self->_build_helper_connect_info(\@args);
2201c2e4 164
165 $self->_parse_connect_info(\@args) if $create eq 'static';
166 }
167 }
d89e6c8a 168 }
0f2fd2c0 169
2201c2e4 170 $helper->{generator} = ref $self;
171 $helper->{generator_version} = $VERSION;
172
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;
178 }
179
180 $self->_gen_model;
181}
182
183sub _parse_loader_args {
184 my ($self, $args) = @_;
185
186 my %loader_args = $self->_read_loader_args($args);
187
188 while (my ($key, $val) = each %loader_args) {
189 next if $key =~ /^(?:components|constraint|exclude)\z/;
190
191 $loader_args{$key} = eval $val;
0fbbc8d5 192 croak "syntax error for loader args key '$key' with value '$val': $@"
2201c2e4 193 if $@;
194 }
195
196 my @components =
197 $self->_build_loader_components(delete $loader_args{components});
198
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};
202 }
203
204 tie my %result, 'Tie::IxHash';
205
206 %result = (
207 relationships => 1,
208 (%loader_args ? %loader_args : ()),
f090a149 209 (!$self->old_schema ? (
2201c2e4 210 use_namespaces => 1
211 ) : ()),
212 (@components ? (
213 components => \@components
214 ) : ())
215 );
216
217 $self->loader_args(\%result);
218
219 wantarray ? %result : \%result;
220}
221
222sub _read_loader_args {
223 my ($self, $args) = @_;
224
225 my %loader_args;
226
227 while (@$args && $args->[0] !~ /^dbi:/) {
228 my ($key, $val) = split /=/, shift(@$args), 2;
ca14239e 229
230 if ((my @vals = split /,/ => $val) > 1) {
2201c2e4 231 $loader_args{$key} = \@vals;
ca14239e 232 } else {
2201c2e4 233 $loader_args{$key} = $val;
ca14239e 234 }
235 }
236
2201c2e4 237 wantarray ? %loader_args : \%loader_args;
238}
239
240sub _build_helper_loader_args {
241 my $self = shift;
242
243 my $args = $self->loader_args;
244
245 tie my %loader_args, 'Tie::IxHash';
246
247 while (my ($arg, $val) = each %$args) {
248 if (ref $val) {
249 $loader_args{$arg} = $self->_data_struct_to_string($val);
250 } else {
251 $loader_args{$arg} = qq{'$val'};
0b2a7108 252 }
0f2fd2c0 253 }
254
2201c2e4 255 \%loader_args
256}
257
258sub _build_loader_components {
259 my ($self, $components) = @_;
d89e6c8a 260
f090a149 261 my @components = $self->old_schema ? () : ('InflateColumn::DateTime');
d89e6c8a 262
2201c2e4 263 if ($components) {
264 $components = [ $components ] if !ref $components;
265 push @components, @$components;
d89e6c8a 266 }
2201c2e4 267
268 wantarray ? @components : \@components;
269}
270
271sub _build_helper_connect_info {
272 my ($self, $connect_info) = @_;
273
274 my @connect_info = @$connect_info;
275
276 my ($dsn, $user, $password) = splice @connect_info, 0, 3;
277
278 tie my %helper_connect_info, 'Tie::IxHash';
279
280 %helper_connect_info = (
281 dsn => qq{'$dsn'},
282 user => qq{'$user'},
283 password => qq{'$password'}
284 );
285
286 for (@connect_info) {
287 if (/^\s*{.*}\s*\z/) {
288 my $hash = eval $_;
0fbbc8d5 289 croak "Syntax errorr in connect_info hash: $_: $@" if $@;
2201c2e4 290 my %hash = %$hash;
291
292 for my $key (keys %hash) {
293 my $val = $hash{$key};
294
295 if (ref $val) {
296 $val = $self->_data_struct_to_string($val);
297 } else {
298 $val = qq{'$val'};
299 }
300
301 $helper_connect_info{$key} = $val;
592cd3ae 302 }
2201c2e4 303
304 next;
592cd3ae 305 }
306
2201c2e4 307 my ($key, $val) = split /=/, $_, 2;
1d155058 308
2201c2e4 309 $helper_connect_info{$key} = $self->_quote_unless_struct($val);
310 }
1d155058 311
2201c2e4 312 \%helper_connect_info
313}
1d155058 314
2201c2e4 315sub _data_struct_to_string {
316 my ($self, $data) = @_;
34f036a0 317
2201c2e4 318 local $Data::Dumper::Terse = 1;
319 local $Data::Dumper::Quotekeys = 0;
320 local $Data::Dumper::Indent = 0;
321 local $Data::Dumper::Useqq = 1;
34f036a0 322
2201c2e4 323 return Data::Dumper->Dump([$data]);
324}
34f036a0 325
2201c2e4 326sub _parse_connect_info {
327 my ($self, $connect_info) = @_;
328
329 my @connect_info = @$connect_info;
330
331 my ($dsn, $user, $password) = splice @connect_info, 0, 3;
332
333 tie my %connect_info, 'Tie::IxHash';
334 @connect_info{qw/dsn user password/} = ($dsn, $user, $password);
335
336 for (@connect_info) {
337 if (/^\s*{.*}\s*\z/) {
338 my $hash = eval $_;
0fbbc8d5 339 croak "Syntax errorr in connect_info hash: $_: $@" if $@;
2201c2e4 340
341 %connect_info = (%connect_info, %$hash);
ca14239e 342
2201c2e4 343 next;
ca14239e 344 }
345
2201c2e4 346 my ($key, $val) = split /=/, $_, 2;
347
348 $connect_info{$key} = eval $val;
0fbbc8d5 349 croak "syntax error for connect_info key '$key' with value '$val': $@"
2201c2e4 350 if $@;
d89e6c8a 351 }
202d09c8 352
2201c2e4 353 $self->connect_info(\%connect_info);
354
355 \%connect_info
356}
357
358sub _quote_unless_struct {
359 my ($self, $val) = @_;
360
361 $val = qq{'$val'} if $val !~ /^\s*[[{]/;
362
363 $val;
364}
365
366sub _gen_dynamic_schema {
367 my $self = shift;
368
369 my $helper = $self->helper;
370
371 my @schema_parts = split(/\:\:/, $self->schema_class);
372 my $schema_file_part = pop @schema_parts;
373
374 my $schema_dir = File::Spec->catfile(
375 $helper->{base}, 'lib', @schema_parts
376 );
377 my $schema_file = File::Spec->catfile(
378 $schema_dir, $schema_file_part . '.pm'
379 );
380
381 $helper->mk_dir($schema_dir);
382 $helper->render_file('schemaclass', $schema_file);
383}
384
385sub _gen_static_schema {
386 my $self = shift;
387
0fbbc8d5 388 croak "cannot load schema without connect info" unless $self->connect_info;
2201c2e4 389
390 my $helper = $self->helper;
391
392 my $schema_dir = File::Spec->catfile($helper->{base}, 'lib');
393
f090a149 394 eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') };
395 croak "Cannot load DBIx::Class::Schema::Loader: $@" if $@;
396
397 DBIx::Class::Schema::Loader->import(
2201c2e4 398 "dump_to_dir:$schema_dir", 'make_schema_at'
f090a149 399 );
2201c2e4 400
401 make_schema_at(
402 $self->schema_class,
403 $self->loader_args,
404 [$self->connect_info]
405 );
406}
407
2201c2e4 408sub _gen_model {
409 my $self = shift;
410 my $helper = $self->helper;
411
412 $helper->render_file('compclass', $helper->{file} );
ad91060a 413}
414
415=head1 SEE ALSO
416
7b39f3f0 417General Catalyst Stuff:
418
ad91060a 419L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
7b39f3f0 420L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
421
422Stuff related to DBIC and this Model style:
423
424L<DBIx::Class>, L<DBIx::Class::Schema>,
ef91bcf9 425L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
ad91060a 426
427=head1 AUTHOR
428
429Brandon L Black, C<blblack@gmail.com>
430
2ff00e2b 431Contributors:
432
433Rafael Kitover, C<<rkitover at cpan.org>>
434
ad91060a 435=head1 LICENSE
436
437This library is free software, you can redistribute it and/or modify
438it under the same terms as Perl itself.
439
440=cut
441
dce0dfe8 4421;
443
ad91060a 444__DATA__
445
5d11d759 446=begin pod_to_ignore
447
d89e6c8a 448__schemaclass__
449package [% schema_class %];
450
451use strict;
452use base qw/DBIx::Class::Schema::Loader/;
453
454__PACKAGE__->loader_options(
2201c2e4 455 [%- FOREACH key = loader_args.keys %]
456 [% key %] => [% loader_args.${key} %],
457 [%- END -%]
458
d89e6c8a 459);
460
461=head1 NAME
462
2201c2e4 463[% schema_class %] - L<DBIx::Class::Schema::Loader> class
d89e6c8a 464
465=head1 SYNOPSIS
466
467See L<[% app %]>
468
469=head1 DESCRIPTION
470
2201c2e4 471Dynamic L<DBIx::Class::Schema::Loader> schema for use in L<[% class %]>
472
473=head1 GENERATED BY
474
475[% generator %] - [% generator_version %]
d89e6c8a 476
477=head1 AUTHOR
478
2201c2e4 479[% author.replace(',+$', '') %]
d89e6c8a 480
481=head1 LICENSE
482
483This library is free software, you can redistribute it and/or modify
484it under the same terms as Perl itself.
485
486=cut
487
4881;
489
ad91060a 490__compclass__
491package [% class %];
492
493use strict;
494use base 'Catalyst::Model::DBIC::Schema';
495
496__PACKAGE__->config(
0f2fd2c0 497 schema_class => '[% schema_class %]',
2ff00e2b 498 [% IF roles %]roles => [% roles %],[% END %]
2201c2e4 499 [% IF setup_connect_info %]connect_info => {
500 [%- FOREACH key = connect_info.keys %]
501 [% key %] => [% connect_info.${key} %],
502 [%- END -%]
503
504 }[% END %]
ad91060a 505);
506
507=head1 NAME
508
1aeb6e1e 509[% class %] - Catalyst DBIC Schema Model
2201c2e4 510
ad91060a 511=head1 SYNOPSIS
512
513See L<[% app %]>
514
515=head1 DESCRIPTION
516
d89e6c8a 517L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
ad91060a 518
2201c2e4 519=head1 GENERATED BY
520
521[% generator %] - [% generator_version %]
522
ad91060a 523=head1 AUTHOR
524
2201c2e4 525[% author.replace(',+$', '') %]
ad91060a 526
527=head1 LICENSE
528
529This library is free software, you can redistribute it and/or modify
530it under the same terms as Perl itself.
531
532=cut
533
5341;