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 /;
14 our $VERSION = '0.03999_01';
16 __PACKAGE__->mk_ro_accessors(qw/
23 additional_base_classes
35 legacy_default_inflections
45 DBIx::Class::Schema::Loader::Base - Base DBIx::Class::Schema::Loader Implementation.
49 See L<DBIx::Class::Schema::Loader>
53 This is the base class for the storage-specific C<DBIx::Class::Schema::*>
54 classes, and implements the common functionality between them.
56 =head1 CONSTRUCTOR OPTIONS
58 These constructor options are the base options for
59 L<DBIx::Class::Schema::Loader/loader_opts>. Available constructor options are:
61 =head2 skip_relationships
63 Skip setting up relationships. The default is to attempt the loading
68 If set to true, each constructive L<DBIx::Class> statement the loader
69 decides to execute will be C<warn>-ed before execution.
73 Set the name of the schema to load (schema in the sense that your database
74 vendor means it). Does not currently support loading more than one schema
79 Only load tables matching regex. Best specified as a qr// regex.
83 Exclude tables matching regex. Best specified as a qr// regex.
87 Overrides the default table name to moniker translation. Can be either
88 a hashref of table keys and moniker values, or a coderef for a translator
89 function taking a single scalar table name argument and returning
90 a scalar moniker. If the hash entry does not exist, or the function
91 returns a false value, the code falls back to default behavior
94 The default behavior is: C<join '', map ucfirst, split /[\W_]+/, lc $table>,
95 which is to say: lowercase everything, split up the table name into chunks
96 anywhere a non-alpha-numeric character occurs, change the case of first letter
97 of each chunk to upper case, and put the chunks back together. Examples:
99 Table Name | Moniker Name
100 ---------------------------
102 luser_group | LuserGroup
103 luser-opts | LuserOpts
105 =head2 inflect_plural
107 Just like L</moniker_map> above (can be hash/code-ref, falls back to default
108 if hash key does not exist or coderef returns false), but acts as a map
109 for pluralizing relationship names. The default behavior is to utilize
110 L<Lingua::EN::Inflect::Number/to_PL>.
112 =head2 inflect_singular
114 As L</inflect_plural> above, but for singularizing relationship names.
115 Default behavior is to utilize L<Lingua::EN::Inflect::Number/to_S>.
117 =head2 additional_base_classes
119 List of additional base classes all of your table classes will use.
121 =head2 left_base_classes
123 List of additional base classes all of your table classes will use
124 that need to be leftmost.
126 =head2 additional_classes
128 List of additional classes which all of your table classes will use.
132 List of additional components to be loaded into all of your table
133 classes. A good example would be C<ResultSetManager>.
135 =head2 resultset_components
137 List of additional ResultSet components to be loaded into your table
138 classes. A good example would be C<AlwaysRS>. Component
139 C<ResultSetManager> will be automatically added to the above
140 C<components> list if this option is set.
142 =head2 legacy_default_inflections
144 Setting this option changes the default fallback for L</inflect_plural> to
145 utilize L<Lingua::EN::Inflect/PL>, and L</inflect_singular> to a no-op.
146 Those choices produce substandard results, but might be necessary to support
147 your existing code if you started developing on a version prior to 0.03 and
148 don't wish to go around updating all your relationship names to the new
151 This option will continue to be supported until at least version 0.05xxx,
152 but may dissappear sometime thereafter. It is recommended that you update
153 your code to use the newer-style inflections when you have the time.
155 =head2 dump_directory
157 This option is designed to be a tool to help you transition from this
158 loader to a manually-defined schema when you decide it's time to do so.
160 The value of this option is a perl libdir pathname. Within
161 that directory this module will create a baseline manual
162 L<DBIx::Class::Schema> module set, based on what it creates at runtime
165 The created schema class will have the same classname as the one on
166 which you are setting this option (and the ResultSource classes will be
167 based on this name as well). Therefore it is wise to note that if you
168 point the C<dump_directory> option of a schema class at the live libdir
169 where that class is currently located, it will overwrite itself with a
170 manual version of itself. This might be a really good or bad thing
171 depending on your situation and perspective.
173 Normally you wouldn't hard-code this setting in your schema class, as it
174 is meant for one-time manual usage.
176 See L<DBIx::Class::Schema::Loader/dump_to_dir> for examples of the
177 recommended way to access this functionality.
179 =head2 dump_overwrite
181 If set to a true value, the dumping code will overwrite existing files.
182 The default is false, which means the dumping code will skip the already
187 None of these methods are intended for direct invocation by regular
188 users of L<DBIx::Class::Schema::Loader>. Anything you can find here
189 can also be found via standard L<DBIx::Class::Schema> methods somehow.
193 # ensure that a peice of object data is a valid arrayref, creating
194 # an empty one or encapsulating whatever's there.
195 sub _ensure_arrayref {
200 $self->{$_} = [ $self->{$_} ]
201 unless ref $self->{$_} eq 'ARRAY';
207 Constructor for L<DBIx::Class::Schema::Loader::Base>, used internally
208 by L<DBIx::Class::Schema::Loader>.
213 my ( $class, %args ) = @_;
215 my $self = { %args };
217 bless $self => $class;
219 $self->{db_schema} ||= '';
220 $self->_ensure_arrayref(qw/additional_classes
221 additional_base_classes
227 push(@{$self->{components}}, 'ResultSetManager')
228 if @{$self->{resultset_components}};
230 $self->{monikers} = {};
231 $self->{classes} = {};
233 $self->{schema_class} ||= ( ref $self->{schema} || $self->{schema} );
234 $self->{schema} ||= $self->{schema_class};
242 foreach my $table_class (values %{$self->classes}) {
243 $table_class->require;
244 if($@ && $@ !~ /^Can't locate /) {
245 croak "Failed to load external class definition"
246 . " for '$table_class': $@";
248 next if $@; # "Can't locate" error
250 # If we make it to here, we loaded an external definition
251 warn qq/# Loaded external class definition for '$table_class'\n/
254 if($self->dump_directory) {
255 my $class_path = $table_class;
256 $class_path =~ s{::}{/}g;
257 $class_path .= '.pm';
258 my $filename = $INC{$class_path};
259 croak 'Failed to locate actual external module file for '
262 open(my $fh, '<', $filename)
263 or croak "Failed to open $filename for reading: $!";
264 $self->_raw_stmt($table_class,
265 q|# These lines loaded from user-supplied external file: |
269 $self->_raw_stmt($table_class, $_);
271 $self->_raw_stmt($table_class,
272 q|# End of lines loaded from user-supplied external file |
275 or croak "Failed to close $filename: $!";
282 Does the actual schema-construction work.
289 $self->_load_classes;
290 $self->_load_relationships if ! $self->skip_relationships;
291 $self->_load_external;
292 $self->_dump_to_dir if $self->dump_directory;
294 # Drop temporary cache
295 delete $self->{_cache};
300 sub _get_dump_filename {
301 my ($self, $class) = (@_);
304 return $self->dump_directory . q{/} . $class . q{.pm};
307 sub _ensure_dump_subdirs {
308 my ($self, $class) = (@_);
310 my @name_parts = split(/::/, $class);
312 my $dir = $self->dump_directory;
313 foreach (@name_parts) {
316 mkdir($dir) or croak "mkdir('$dir') failed: $!";
324 my $target_dir = $self->dump_directory;
326 my $schema_class = $self->schema_class;
328 croak "Must specify target directory for dumping!" if ! $target_dir;
330 warn "Dumping manual schema for $schema_class to directory $target_dir ...\n";
332 if(! -d $target_dir) {
333 mkdir($target_dir) or croak "mkdir('$target_dir') failed: $!";
336 my $verstr = $DBIx::Class::Schema::Loader::VERSION;
337 my $datestr = POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime);
338 my $tagline = qq|# Created by DBIx::Class::Schema::Loader v$verstr @ $datestr|;
340 $self->_ensure_dump_subdirs($schema_class);
342 my $schema_fn = $self->_get_dump_filename($schema_class);
343 if (-f $schema_fn && !$self->dump_overwrite) {
344 warn "$schema_fn exists, will not overwrite\n";
347 open(my $schema_fh, '>', $schema_fn)
348 or croak "Cannot open $schema_fn for writing: $!";
349 print $schema_fh qq|package $schema_class;\n\n$tagline\n\n|;
350 print $schema_fh qq|use strict;\nuse warnings;\n\n|;
351 print $schema_fh qq|use base 'DBIx::Class::Schema';\n\n|;
352 print $schema_fh qq|__PACKAGE__->load_classes;\n|;
353 print $schema_fh qq|\n1;\n\n|;
355 or croak "Cannot close $schema_fn: $!";
358 foreach my $src_class (sort keys %{$self->{_dump_storage}}) {
359 $self->_ensure_dump_subdirs($src_class);
360 my $src_fn = $self->_get_dump_filename($src_class);
361 if (-f $src_fn && !$self->dump_overwrite) {
362 warn "$src_fn exists, will not overwrite\n";
365 open(my $src_fh, '>', $src_fn)
366 or croak "Cannot open $src_fn for writing: $!";
367 print $src_fh qq|package $src_class;\n\n$tagline\n\n|;
368 print $src_fh qq|use strict;\nuse warnings;\n\n|;
369 print $src_fh qq|use base 'DBIx::Class';\n\n|;
370 print $src_fh qq|$_\n|
371 for @{$self->{_dump_storage}->{$src_class}};
372 print $src_fh qq|\n1;\n\n|;
374 or croak "Cannot close $src_fn: $!";
377 warn "Schema dump completed.\n";
386 warn "$target: use $_;" if $self->debug;
387 $self->_raw_stmt($target, "use $_;");
388 $_->require or croak ($_ . "->require: $@");
389 $evalstr .= "package $target; use $_;";
391 eval $evalstr if $evalstr;
398 my $schema_class = $self->schema_class;
400 my $blist = join(q{ }, @_);
401 warn "$target: use base qw/ $blist /;" if $self->debug && @_;
402 $self->_raw_stmt($target, "use base qw/ $blist /;") if @_;
404 $_->require or croak ($_ . "->require: $@");
405 $schema_class->inject_base($target, $_);
409 # Load and setup classes
413 my $schema = $self->schema;
414 my $schema_class = $self->schema_class;
415 my $constraint = $self->constraint;
416 my $exclude = $self->exclude;
417 my @tables = sort $self->_tables_list;
419 warn "No tables found in database, nothing to load" if !@tables;
422 @tables = grep { /$constraint/ } @tables if $constraint;
423 @tables = grep { ! /$exclude/ } @tables if $exclude;
425 warn "All tables excluded by constraint/exclude, nothing to load"
429 $self->{_tables} = \@tables;
431 foreach my $table (@tables) {
432 my $table_moniker = $self->_table2moniker($table);
433 my $table_class = $schema_class . q{::} . $table_moniker;
435 my $table_normalized = lc $table;
436 $self->classes->{$table} = $table_class;
437 $self->classes->{$table_normalized} = $table_class;
438 $self->monikers->{$table} = $table_moniker;
439 $self->monikers->{$table_normalized} = $table_moniker;
441 no warnings 'redefine';
442 local *Class::C3::reinitialize = sub { };
445 { no strict 'refs'; @{"${table_class}::ISA"} = qw/DBIx::Class/ }
447 $self->_use ($table_class, @{$self->additional_classes});
448 $self->_inject($table_class, @{$self->additional_base_classes});
450 $self->_dbic_stmt($table_class, 'load_components', @{$self->components}, qw/PK::Auto Core/);
452 $self->_dbic_stmt($table_class, 'load_resultset_components', @{$self->resultset_components})
453 if @{$self->resultset_components};
454 $self->_inject($table_class, @{$self->left_base_classes});
457 Class::C3::reinitialize;
459 foreach my $table (@tables) {
460 my $table_class = $self->classes->{$table};
461 my $table_moniker = $self->monikers->{$table};
463 $self->_dbic_stmt($table_class,'table',$table);
465 my $cols = $self->_table_columns($table);
467 eval { $col_info = $schema->storage->columns_info_for($table) };
469 $self->_dbic_stmt($table_class,'add_columns',@$cols);
472 my %col_info_lc = map { lc($_), $col_info->{$_} } keys %$col_info;
476 map { $_, ($col_info_lc{$_}||{}) } @$cols
480 my $pks = $self->_table_pk_info($table) || [];
481 @$pks ? $self->_dbic_stmt($table_class,'set_primary_key',@$pks)
482 : carp("$table has no primary key");
484 my $uniqs = $self->_table_uniq_info($table) || [];
485 $self->_dbic_stmt($table_class,'add_unique_constraint',@$_) for (@$uniqs);
487 $schema_class->register_class($table_moniker, $table_class);
488 $schema->register_class($table_moniker, $table_class) if $schema ne $schema_class;
494 Returns a sorted list of loaded tables, using the original database table
502 return @{$self->_tables};
505 # Make a moniker from a table
507 my ( $self, $table ) = @_;
511 if( ref $self->moniker_map eq 'HASH' ) {
512 $moniker = $self->moniker_map->{$table};
514 elsif( ref $self->moniker_map eq 'CODE' ) {
515 $moniker = $self->moniker_map->($table);
518 $moniker ||= join '', map ucfirst, split /[\W_]+/, lc $table;
523 sub _load_relationships {
526 # Construct the fk_info RelBuilder wants to see, by
527 # translating table names to monikers in the _fk_info output
529 foreach my $table ($self->tables) {
530 my $tbl_fk_info = $self->_table_fk_info($table);
531 foreach my $fkdef (@$tbl_fk_info) {
532 $fkdef->{remote_source} =
533 $self->monikers->{delete $fkdef->{remote_table}};
535 my $moniker = $self->monikers->{$table};
536 $fk_info{$moniker} = $tbl_fk_info;
539 my $relbuilder = DBIx::Class::Schema::Loader::RelBuilder->new(
540 $self->schema_class, \%fk_info, $self->inflect_plural,
541 $self->inflect_singular
544 my $rel_stmts = $relbuilder->generate_code;
545 foreach my $src_class (sort keys %$rel_stmts) {
546 my $src_stmts = $rel_stmts->{$src_class};
547 foreach my $stmt (@$src_stmts) {
548 $self->_dbic_stmt($src_class,$stmt->{method},@{$stmt->{args}});
553 # Overload these in driver class:
555 # Returns an arrayref of column names
556 sub _table_columns { croak "ABSTRACT METHOD" }
558 # Returns arrayref of pk col names
559 sub _table_pk_info { croak "ABSTRACT METHOD" }
561 # Returns an arrayref of uniqs [ [ foo => [ col1, col2 ] ], [ bar => [ ... ] ] ]
562 sub _table_uniq_info { croak "ABSTRACT METHOD" }
564 # Returns an arrayref of foreign key constraints, each
565 # being a hashref with 3 keys:
566 # local_columns (arrayref), remote_columns (arrayref), remote_table
567 sub _table_fk_info { croak "ABSTRACT METHOD" }
569 # Returns an array of lower case table names
570 sub _tables_list { croak "ABSTRACT METHOD" }
572 # Execute a constructive DBIC class method, with debug/dump_to_dir hooks.
578 if(!$self->debug && !$self->dump_directory) {
584 $args = '(' . $args . ')' if @_ < 2;
585 my $stmt = $method . $args . q{;};
587 warn qq|$class\->$stmt\n| if $self->debug;
589 $self->_raw_stmt($class, '__PACKAGE__->' . $stmt);
592 # Store a raw source line for a class (for dumping purposes)
594 my ($self, $class, $stmt) = @_;
595 push(@{$self->{_dump_storage}->{$class}}, $stmt) if $self->dump_directory;
600 Returns a hashref of loaded table to moniker mappings. There will
601 be two entries for each table, the original name and the "normalized"
602 name, in the case that the two are different (such as databases
603 that like uppercase table names, or preserve your original mixed-case
604 definitions, or what-have-you).
608 Returns a hashref of table to class mappings. In some cases it will
609 contain multiple entries per table for the original and normalized table
610 names, as above in L</monikers>.
614 L<DBIx::Class::Schema::Loader>