1 package Catalyst::Helper::Model::DBIC::Schema;
5 no warnings 'uninitialized';
9 use parent 'Class::Accessor::Fast';
12 use UNIVERSAL::require;
17 __PACKAGE__->mk_accessors(qw/
18 helper schema_class loader_args connect_info _old_schema
23 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
27 script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \
28 [ create=dynamic | create=static ] [ Schema::Loader opts ] \
29 [ dsn user pass ] [ other connect_info arguments ]
33 Helper for the DBIC Schema Models.
37 C<CatalystModelName> is the short name for the Catalyst Model class
38 being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>).
40 C<MyApp::SchemaClass> is the fully qualified classname of your Schema,
41 which might or might not yet exist. Note that you should have a good
42 reason to create this under a new global namespace, otherwise use an
43 existing top level namespace for your schema class.
45 C<create=dynamic> instructs this Helper to generate the named Schema
46 class for you, basing it on L<DBIx::Class::Schema::Loader> (which
47 means the table information will always be dynamically loaded at
48 runtime from the database).
50 C<create=static> instructs this Helper to generate the named Schema
51 class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
52 mode to create a standard, manually-defined L<DBIx::Class::Schema>
53 setup, based on what the Loader sees in your database at this moment.
54 A Schema/Model pair generated this way will not require
55 L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
56 adapt itself to changes in your database structure. You can edit
57 the generated classes by hand to refine them.
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
109 my ($package, $helper, $schema_class, @args) = @_;
111 my $self = $package->new;
113 $helper->{schema_class} = $schema_class
114 or croak "Must supply schema class name";
116 $self->schema_class($schema_class);
117 $self->helper($helper);
120 if ($args[0] && $args[0] =~ /^create=(dynamic|static)$/) {
125 $self->_parse_loader_args(\@args);
127 if (List::Util::first { /dbi:/ } @args) {
128 $helper->{setup_connect_info} = 1;
130 $helper->{connect_info} =
131 $self->_build_helper_connect_info(\@args);
133 $self->_parse_connect_info(\@args) if $create eq 'static';
138 $helper->{generator} = ref $self;
139 $helper->{generator_version} = $VERSION;
141 if ($create eq 'dynamic') {
142 $self->helper->{loader_args} = $self->_build_helper_loader_args;
143 $self->_gen_dynamic_schema;
144 } elsif ($create eq 'static') {
145 $self->_gen_static_schema;
151 sub _parse_loader_args {
152 my ($self, $args) = @_;
154 my %loader_args = $self->_read_loader_args($args);
156 while (my ($key, $val) = each %loader_args) {
157 next if $key =~ /^(?:components|constraint|exclude)\z/;
159 $loader_args{$key} = eval $val;
160 die "syntax error for loader args key '$key' with value '$val': $@"
165 $self->_build_loader_components(delete $loader_args{components});
167 for my $re_opt (qw/constraint exclude/) {
168 $loader_args{$re_opt} = qr/$loader_args{$re_opt}/
169 if exists $loader_args{$re_opt};
172 tie my %result, 'Tie::IxHash';
176 (%loader_args ? %loader_args : ()),
177 (!$self->_is_old_schema ? (
181 components => \@components
185 $self->loader_args(\%result);
187 wantarray ? %result : \%result;
190 sub _read_loader_args {
191 my ($self, $args) = @_;
195 while (@$args && $args->[0] !~ /^dbi:/) {
196 my ($key, $val) = split /=/, shift(@$args), 2;
198 if ((my @vals = split /,/ => $val) > 1) {
199 $loader_args{$key} = \@vals;
201 $loader_args{$key} = $val;
205 wantarray ? %loader_args : \%loader_args;
208 sub _build_helper_loader_args {
211 my $args = $self->loader_args;
213 tie my %loader_args, 'Tie::IxHash';
215 while (my ($arg, $val) = each %$args) {
217 $loader_args{$arg} = $self->_data_struct_to_string($val);
219 $loader_args{$arg} = qq{'$val'};
226 sub _build_loader_components {
227 my ($self, $components) = @_;
229 my @components = $self->_is_old_schema ? () : ('InflateColumn::DateTime');
232 $components = [ $components ] if !ref $components;
233 push @components, @$components;
236 wantarray ? @components : \@components;
239 sub _build_helper_connect_info {
240 my ($self, $connect_info) = @_;
242 my @connect_info = @$connect_info;
244 my ($dsn, $user, $password) = splice @connect_info, 0, 3;
246 tie my %helper_connect_info, 'Tie::IxHash';
248 %helper_connect_info = (
251 password => qq{'$password'}
254 for (@connect_info) {
255 if (/^\s*{.*}\s*\z/) {
257 die "Syntax errorr in connect_info hash: $_: $@" if $@;
260 for my $key (keys %hash) {
261 my $val = $hash{$key};
264 $val = $self->_data_struct_to_string($val);
269 $helper_connect_info{$key} = $val;
275 my ($key, $val) = split /=/, $_, 2;
277 $helper_connect_info{$key} = $self->_quote_unless_struct($val);
280 \%helper_connect_info
283 sub _data_struct_to_string {
284 my ($self, $data) = @_;
286 local $Data::Dumper::Terse = 1;
287 local $Data::Dumper::Quotekeys = 0;
288 local $Data::Dumper::Indent = 0;
289 local $Data::Dumper::Useqq = 1;
291 return Data::Dumper->Dump([$data]);
294 sub _parse_connect_info {
295 my ($self, $connect_info) = @_;
297 my @connect_info = @$connect_info;
299 my ($dsn, $user, $password) = splice @connect_info, 0, 3;
301 tie my %connect_info, 'Tie::IxHash';
302 @connect_info{qw/dsn user password/} = ($dsn, $user, $password);
304 for (@connect_info) {
305 if (/^\s*{.*}\s*\z/) {
307 die "Syntax errorr in connect_info hash: $_: $@" if $@;
309 %connect_info = (%connect_info, %$hash);
314 my ($key, $val) = split /=/, $_, 2;
316 $connect_info{$key} = eval $val;
317 die "syntax error for connect_info key '$key' with value '$val': $@"
321 $self->connect_info(\%connect_info);
326 sub _quote_unless_struct {
327 my ($self, $val) = @_;
329 $val = qq{'$val'} if $val !~ /^\s*[[{]/;
334 sub _gen_dynamic_schema {
337 my $helper = $self->helper;
339 my @schema_parts = split(/\:\:/, $self->schema_class);
340 my $schema_file_part = pop @schema_parts;
342 my $schema_dir = File::Spec->catfile(
343 $helper->{base}, 'lib', @schema_parts
345 my $schema_file = File::Spec->catfile(
346 $schema_dir, $schema_file_part . '.pm'
349 $helper->mk_dir($schema_dir);
350 $helper->render_file('schemaclass', $schema_file);
353 sub _gen_static_schema {
356 die "cannot load schema without connect info" unless $self->connect_info;
358 my $helper = $self->helper;
360 my $schema_dir = File::Spec->catfile($helper->{base}, 'lib');
362 DBIx::Class::Schema::Loader->use(
363 "dump_to_dir:$schema_dir", 'make_schema_at'
364 ) or croak "Cannot load DBIx::Class::Schema::Loader: $@";
369 [$self->connect_info]
376 return $self->_old_schema if defined $self->_old_schema;
378 my @schema_pm = split '::', $self->schema_class;
379 $schema_pm[-1] .= '.pm';
381 File::Spec->catfile($self->helper->{base}, 'lib', @schema_pm);
383 if (-f $schema_file) {
384 my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
385 $self->_old_schema(1) if $schema_code =~ /->load_classes/;
387 $self->_old_schema(0);
390 return $self->_old_schema;
395 my $helper = $self->helper;
397 $helper->render_file('compclass', $helper->{file} );
402 General Catalyst Stuff:
404 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
405 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
407 Stuff related to DBIC and this Model style:
409 L<DBIx::Class>, L<DBIx::Class::Schema>,
410 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
414 Brandon L Black, C<blblack@gmail.com>
418 This library is free software, you can redistribute it and/or modify
419 it under the same terms as Perl itself.
430 package [% schema_class %];
433 use base qw/DBIx::Class::Schema::Loader/;
435 __PACKAGE__->loader_options(
436 [%- FOREACH key = loader_args.keys %]
437 [% key %] => [% loader_args.${key} %],
444 [% schema_class %] - L<DBIx::Class::Schema::Loader> class
452 Dynamic L<DBIx::Class::Schema::Loader> schema for use in L<[% class %]>
456 [% generator %] - [% generator_version %]
460 [% author.replace(',+$', '') %]
464 This library is free software, you can redistribute it and/or modify
465 it under the same terms as Perl itself.
475 use base 'Catalyst::Model::DBIC::Schema';
478 schema_class => '[% schema_class %]',
479 [% IF setup_connect_info %]connect_info => {
480 [%- FOREACH key = connect_info.keys %]
481 [% key %] => [% connect_info.${key} %],
489 [% class %] - Catalyst DBIC Schema Model
497 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
501 [% generator %] - [% generator_version %]
505 [% author.replace(',+$', '') %]
509 This library is free software, you can redistribute it and/or modify
510 it under the same terms as Perl itself.