1 package DBIx::Class::Schema::Loader::Base;
5 use base qw/Class::Accessor::Fast/;
7 use Carp::Clan qw/^DBIx::Class/;
8 use UNIVERSAL::require;
9 use DBIx::Class::Schema::Loader::RelBuilder;
10 use Data::Dump qw/ dump /;
17 our $VERSION = '0.03999_01';
19 __PACKAGE__->mk_ro_accessors(qw/
26 additional_base_classes
46 DBIx::Class::Schema::Loader::Base - Base DBIx::Class::Schema::Loader Implementation.
50 See L<DBIx::Class::Schema::Loader>
54 This is the base class for the storage-specific C<DBIx::Class::Schema::*>
55 classes, and implements the common functionality between them.
57 =head1 CONSTRUCTOR OPTIONS
59 These constructor options are the base options for
60 L<DBIx::Class::Schema::Loader/loader_opts>. Available constructor options are:
62 =head2 skip_relationships
64 Skip setting up relationships. The default is to attempt the loading
69 If set to true, each constructive L<DBIx::Class> statement the loader
70 decides to execute will be C<warn>-ed before execution.
74 Set the name of the schema to load (schema in the sense that your database
75 vendor means it). Does not currently support loading more than one schema
80 Only load tables matching regex. Best specified as a qr// regex.
84 Exclude tables matching regex. Best specified as a qr// regex.
88 Overrides the default table name to moniker translation. Can be either
89 a hashref of table keys and moniker values, or a coderef for a translator
90 function taking a single scalar table name argument and returning
91 a scalar moniker. If the hash entry does not exist, or the function
92 returns a false value, the code falls back to default behavior
95 The default behavior is: C<join '', map ucfirst, split /[\W_]+/, lc $table>,
96 which is to say: lowercase everything, split up the table name into chunks
97 anywhere a non-alpha-numeric character occurs, change the case of first letter
98 of each chunk to upper case, and put the chunks back together. Examples:
100 Table Name | Moniker Name
101 ---------------------------
103 luser_group | LuserGroup
104 luser-opts | LuserOpts
106 =head2 inflect_plural
108 Just like L</moniker_map> above (can be hash/code-ref, falls back to default
109 if hash key does not exist or coderef returns false), but acts as a map
110 for pluralizing relationship names. The default behavior is to utilize
111 L<Lingua::EN::Inflect::Number/to_PL>.
113 =head2 inflect_singular
115 As L</inflect_plural> above, but for singularizing relationship names.
116 Default behavior is to utilize L<Lingua::EN::Inflect::Number/to_S>.
118 =head2 additional_base_classes
120 List of additional base classes all of your table classes will use.
122 =head2 left_base_classes
124 List of additional base classes all of your table classes will use
125 that need to be leftmost.
127 =head2 additional_classes
129 List of additional classes which all of your table classes will use.
133 List of additional components to be loaded into all of your table
134 classes. A good example would be C<ResultSetManager>.
136 =head2 resultset_components
138 List of additional ResultSet components to be loaded into your table
139 classes. A good example would be C<AlwaysRS>. Component
140 C<ResultSetManager> will be automatically added to the above
141 C<components> list if this option is set.
143 =head2 dump_directory
145 This option is designed to be a tool to help you transition from this
146 loader to a manually-defined schema when you decide it's time to do so.
148 The value of this option is a perl libdir pathname. Within
149 that directory this module will create a baseline manual
150 L<DBIx::Class::Schema> module set, based on what it creates at runtime
153 The created schema class will have the same classname as the one on
154 which you are setting this option (and the ResultSource classes will be
155 based on this name as well).
157 Normally you wouldn't hard-code this setting in your schema class, as it
158 is meant for one-time manual usage.
160 See L<DBIx::Class::Schema::Loader/dump_to_dir> for examples of the
161 recommended way to access this functionality.
163 =head2 dump_overwrite
165 Default false. If true, Loader will unconditionally delete any existing
166 files before creating the new ones from scratch when dumping a schema to disk.
168 The default behavior is instead to only replace the top portion of the
169 file, up to and including the final stanza which contains
170 C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!>
171 leaving any customizations you placed after that as they were.
173 When C<dump_overwrite> is not set, if the output file already exists,
174 but the aforementioned final stanza is not found, or the checksum
175 contained there does not match the generated contents, Loader will
176 croak and not touch the file.
180 None of these methods are intended for direct invocation by regular
181 users of L<DBIx::Class::Schema::Loader>. Anything you can find here
182 can also be found via standard L<DBIx::Class::Schema> methods somehow.
186 # ensure that a peice of object data is a valid arrayref, creating
187 # an empty one or encapsulating whatever's there.
188 sub _ensure_arrayref {
193 $self->{$_} = [ $self->{$_} ]
194 unless ref $self->{$_} eq 'ARRAY';
200 Constructor for L<DBIx::Class::Schema::Loader::Base>, used internally
201 by L<DBIx::Class::Schema::Loader>.
206 my ( $class, %args ) = @_;
208 my $self = { %args };
210 bless $self => $class;
212 $self->{db_schema} ||= '';
213 $self->_ensure_arrayref(qw/additional_classes
214 additional_base_classes
220 push(@{$self->{components}}, 'ResultSetManager')
221 if @{$self->{resultset_components}};
223 $self->{monikers} = {};
224 $self->{classes} = {};
226 $self->{schema_class} ||= ( ref $self->{schema} || $self->{schema} );
227 $self->{schema} ||= $self->{schema_class};
229 $self->{relbuilder} = DBIx::Class::Schema::Loader::RelBuilder->new(
230 $self->schema_class, $self->inflect_plural, $self->inflect_singular
231 ) if !$self->{skip_relationships};
236 sub _find_file_in_inc {
237 my ($self, $file) = @_;
239 foreach my $prefix (@INC) {
240 my $fullpath = $prefix . '/' . $file;
241 return $fullpath if -f $fullpath;
248 my ($self, $class) = @_;
250 my $class_path = $class;
251 $class_path =~ s{::}{/}g;
252 $class_path .= '.pm';
254 my $inc_path = $self->_find_file_in_inc($class_path);
256 return if !$inc_path;
258 my $real_dump_path = $self->dump_directory
260 File::Spec->catfile($self->dump_directory, $class_path)
263 my $real_inc_path = Cwd::abs_path($inc_path);
264 return if $real_inc_path eq $real_dump_path;
267 croak "Failed to load external class definition"
268 . " for '$class': $@"
271 # If we make it to here, we loaded an external definition
272 warn qq/# Loaded external class definition for '$class'\n/
275 # The rest is only relevant when dumping
276 return if !$self->dump_directory;
278 croak 'Failed to locate actual external module file for '
281 open(my $fh, '<', $real_inc_path)
282 or croak "Failed to open '$real_inc_path' for reading: $!";
283 $self->_ext_stmt($class,
284 qq|# These lines were loaded from '$real_inc_path' found in \@INC.|
285 .q|# They are now part of the custom portion of this file|
286 .q|# for you to hand-edit. If you do not either delete|
287 .q|# this section or remove that file from @INC, this section|
288 .q|# will be repeated redundantly when you re-create this|
289 .q|# file again via Loader!|
293 $self->_ext_stmt($class, $_);
295 $self->_ext_stmt($class,
296 q|# End of lines loaded from '$real_inc_path' |
299 or croak "Failed to close $real_inc_path: $!";
304 Does the actual schema-construction work.
311 # First, use _tables_list with constraint and exclude
312 # to get a list of tables to operate on
314 my $constraint = $self->constraint;
315 my $exclude = $self->exclude;
316 my @tables = sort $self->_tables_list;
319 warn "No tables found in database, nothing to load";
322 @tables = grep { /$constraint/ } @tables if $constraint;
323 @tables = grep { ! /$exclude/ } @tables if $exclude;
325 warn "All tables excluded by constraint/exclude, nothing to load"
329 # Save the tables list
330 $self->{_tables} = \@tables;
332 # Set up classes/monikers
334 no warnings 'redefine';
335 local *Class::C3::reinitialize = sub { };
338 $self->_make_src_class($_) for @tables;
341 Class::C3::reinitialize;
343 $self->_setup_src_meta($_) for @tables;
345 if(!$self->skip_relationships) {
346 $self->_load_relationships($_) for @tables;
349 $self->_load_external($_)
350 for ($self->schema_class, values %{$self->classes});
352 $self->_dump_to_dir if $self->dump_directory;
354 # Drop temporary cache
355 delete $self->{_cache};
360 sub _get_dump_filename {
361 my ($self, $class) = (@_);
364 return $self->dump_directory . q{/} . $class . q{.pm};
367 sub _ensure_dump_subdirs {
368 my ($self, $class) = (@_);
370 my @name_parts = split(/::/, $class);
371 pop @name_parts; # we don't care about the very last element,
372 # which is a filename
374 my $dir = $self->dump_directory;
377 mkdir($dir) or croak "mkdir('$dir') failed: $!";
379 last if !@name_parts;
380 $dir = File::Spec->catdir($dir, shift @name_parts);
387 my $target_dir = $self->dump_directory;
389 my $schema_class = $self->schema_class;
391 croak "Must specify target directory for dumping!" if ! $target_dir;
393 warn "Dumping manual schema for $schema_class to directory $target_dir ...\n";
396 qq|package $schema_class;\n\n|
397 . qq|use strict;\nuse warnings;\n\n|
398 . qq|use base 'DBIx::Class::Schema';\n\n|
399 . qq|__PACKAGE__->load_classes;\n|;
401 $self->_write_classfile($schema_class, $schema_text);
403 foreach my $src_class (sort keys %{$self->{_dump_storage}}) {
405 qq|package $src_class;\n\n|
406 . qq|use strict;\nuse warnings;\n\n|
407 . qq|use base 'DBIx::Class';\n\n|;
409 $self->_write_classfile($src_class, $src_text);
412 warn "Schema dump completed.\n";
415 sub _write_classfile {
416 my ($self, $class, $text) = @_;
418 my $filename = $self->_get_dump_filename($class);
419 $self->_ensure_dump_subdirs($class);
421 if (-f $filename && $self->dump_overwrite) {
422 warn "Deleting existing file '$filename' due to "
423 . "'dump_overwrite' setting\n";
427 my $custom_content = $self->_get_custom_content($class, $filename);
429 $custom_content ||= qq|\n# You can replace this text with custom|
430 . qq| content, and it will be preserved on regeneration|
434 for @{$self->{_dump_storage}->{$class} || []};
436 $text .= qq|\n\n# Created by DBIx::Class::Schema::Loader|
437 . qq| v| . $DBIx::Class::Schema::Loader::VERSION
438 . q| @ | . POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime)
439 . qq|\n# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:|;
441 open(my $fh, '>', $filename)
442 or croak "Cannot open '$filename' for writing: $!";
444 # Write the top half and its MD5 sum
445 print $fh $text . Digest::MD5::md5_base64($text) . "\n\n";
447 # Write out anything loaded via external partial class file in @INC
449 for @{$self->{_ext_storage}->{$class} || []};
451 print $fh $custom_content;
454 or croak "Cannot close '$filename': $!";
457 sub _get_custom_content {
458 my ($self, $class, $filename) = @_;
460 return if ! -f $filename;
461 open(my $fh, '<', $filename)
462 or croak "Cannot open '$filename' for reading: $!";
465 qr{^(# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:)([A-Za-z0-9/+]{22})\n};
470 if(!$found && /$mark_re/) {
473 croak "Checksum mismatch in '$filename'"
474 if Digest::MD5::md5_base64($buffer) ne $2;
483 croak "Cannot not overwrite '$filename' without 'dump_overwrite',"
484 . " it does not appear to have been generated by Loader"
496 warn "$target: use $_;" if $self->debug;
497 $self->_raw_stmt($target, "use $_;");
498 $_->require or croak ($_ . "->require: $@");
499 $evalstr .= "package $target; use $_;";
501 eval $evalstr if $evalstr;
508 my $schema_class = $self->schema_class;
510 my $blist = join(q{ }, @_);
511 warn "$target: use base qw/ $blist /;" if $self->debug && @_;
512 $self->_raw_stmt($target, "use base qw/ $blist /;") if @_;
514 $_->require or croak ($_ . "->require: $@");
515 $schema_class->inject_base($target, $_);
519 # Create class with applicable bases, setup monikers, etc
520 sub _make_src_class {
521 my ($self, $table) = @_;
523 my $schema = $self->schema;
524 my $schema_class = $self->schema_class;
526 my $table_moniker = $self->_table2moniker($table);
527 my $table_class = $schema_class . q{::} . $table_moniker;
529 my $table_normalized = lc $table;
530 $self->classes->{$table} = $table_class;
531 $self->classes->{$table_normalized} = $table_class;
532 $self->monikers->{$table} = $table_moniker;
533 $self->monikers->{$table_normalized} = $table_moniker;
535 { no strict 'refs'; @{"${table_class}::ISA"} = qw/DBIx::Class/ }
537 $self->_use ($table_class, @{$self->additional_classes});
538 $self->_inject($table_class, @{$self->additional_base_classes});
540 $self->_dbic_stmt($table_class, 'load_components', @{$self->components}, qw/PK::Auto Core/);
542 $self->_dbic_stmt($table_class, 'load_resultset_components', @{$self->resultset_components})
543 if @{$self->resultset_components};
544 $self->_inject($table_class, @{$self->left_base_classes});
547 # Set up metadata (cols, pks, etc) and register the class with the schema
548 sub _setup_src_meta {
549 my ($self, $table) = @_;
551 my $schema = $self->schema;
552 my $schema_class = $self->schema_class;
554 my $table_class = $self->classes->{$table};
555 my $table_moniker = $self->monikers->{$table};
557 $self->_dbic_stmt($table_class,'table',$table);
559 my $cols = $self->_table_columns($table);
561 eval { $col_info = $self->_columns_info_for($table) };
563 $self->_dbic_stmt($table_class,'add_columns',@$cols);
566 my %col_info_lc = map { lc($_), $col_info->{$_} } keys %$col_info;
570 map { $_, ($col_info_lc{$_}||{}) } @$cols
574 my $pks = $self->_table_pk_info($table) || [];
575 @$pks ? $self->_dbic_stmt($table_class,'set_primary_key',@$pks)
576 : carp("$table has no primary key");
578 my $uniqs = $self->_table_uniq_info($table) || [];
579 $self->_dbic_stmt($table_class,'add_unique_constraint',@$_) for (@$uniqs);
581 $schema_class->register_class($table_moniker, $table_class);
582 $schema->register_class($table_moniker, $table_class) if $schema ne $schema_class;
587 Returns a sorted list of loaded tables, using the original database table
595 return @{$self->_tables};
598 # Make a moniker from a table
600 my ( $self, $table ) = @_;
604 if( ref $self->moniker_map eq 'HASH' ) {
605 $moniker = $self->moniker_map->{$table};
607 elsif( ref $self->moniker_map eq 'CODE' ) {
608 $moniker = $self->moniker_map->($table);
611 $moniker ||= join '', map ucfirst, split /[\W_]+/, lc $table;
616 sub _load_relationships {
617 my ($self, $table) = @_;
619 my $tbl_fk_info = $self->_table_fk_info($table);
620 foreach my $fkdef (@$tbl_fk_info) {
621 $fkdef->{remote_source} =
622 $self->monikers->{delete $fkdef->{remote_table}};
625 my $local_moniker = $self->monikers->{$table};
626 my $rel_stmts = $self->{relbuilder}->generate_code($local_moniker, $tbl_fk_info);
628 foreach my $src_class (sort keys %$rel_stmts) {
629 my $src_stmts = $rel_stmts->{$src_class};
630 foreach my $stmt (@$src_stmts) {
631 $self->_dbic_stmt($src_class,$stmt->{method},@{$stmt->{args}});
636 # Overload these in driver class:
638 # Returns an arrayref of column names
639 sub _table_columns { croak "ABSTRACT METHOD" }
641 # Returns arrayref of pk col names
642 sub _table_pk_info { croak "ABSTRACT METHOD" }
644 # Returns an arrayref of uniqs [ [ foo => [ col1, col2 ] ], [ bar => [ ... ] ] ]
645 sub _table_uniq_info { croak "ABSTRACT METHOD" }
647 # Returns an arrayref of foreign key constraints, each
648 # being a hashref with 3 keys:
649 # local_columns (arrayref), remote_columns (arrayref), remote_table
650 sub _table_fk_info { croak "ABSTRACT METHOD" }
652 # Returns an array of lower case table names
653 sub _tables_list { croak "ABSTRACT METHOD" }
655 # Execute a constructive DBIC class method, with debug/dump_to_dir hooks.
661 if(!$self->debug && !$self->dump_directory) {
667 $args = '(' . $args . ')' if @_ < 2;
668 my $stmt = $method . $args . q{;};
670 warn qq|$class\->$stmt\n| if $self->debug;
672 $self->_raw_stmt($class, '__PACKAGE__->' . $stmt);
675 # Store a raw source line for a class (for dumping purposes)
677 my ($self, $class, $stmt) = @_;
678 push(@{$self->{_dump_storage}->{$class}}, $stmt) if $self->dump_directory;
681 # Like above, but separately for the externally loaded stuff
683 my ($self, $class, $stmt) = @_;
684 push(@{$self->{_ext_storage}->{$class}}, $stmt) if $self->dump_directory;
689 Returns a hashref of loaded table to moniker mappings. There will
690 be two entries for each table, the original name and the "normalized"
691 name, in the case that the two are different (such as databases
692 that like uppercase table names, or preserve your original mixed-case
693 definitions, or what-have-you).
697 Returns a hashref of table to class mappings. In some cases it will
698 contain multiple entries per table for the original and normalized table
699 names, as above in L</monikers>.
703 L<DBIx::Class::Schema::Loader>