Bump version number for dev release
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index b617697..c7a9856 100644 (file)
@@ -14,7 +14,7 @@ use Cwd qw//;
 use Digest::MD5 qw//;
 require DBIx::Class;
 
-our $VERSION = '0.03999_02';
+our $VERSION = '0.04999_02';
 
 __PACKAGE__->mk_ro_accessors(qw/
                                 schema
@@ -34,6 +34,11 @@ __PACKAGE__->mk_ro_accessors(qw/
                                 debug
                                 dump_directory
                                 dump_overwrite
+                                really_erase_my_files
+                                use_namespaces
+                                result_namespace
+                                resultset_namespace
+                                default_resultset_class
 
                                 db_schema
                                 _tables
@@ -140,6 +145,15 @@ classes.  A good example would be C<AlwaysRS>.  Component
 C<ResultSetManager> will be automatically added to the above
 C<components> list if this option is set.
 
+=head2 use_namespaces
+
+Generate result class names suitable for
+L<DBIx::Class::Schema/load_namespaces> and call that instead of
+L<DBIx::Class::Schema/load_classes>. When using this option you can also
+specify any of the options for C<load_namespaces> (i.e. C<result_namespace>,
+C<resultset_namespace>, C<default_resultset_class>), and they will be added
+to the call (and the generated result class names adjusted appropriately).
+
 =head2 dump_directory
 
 This option is designed to be a tool to help you transition from this
@@ -162,6 +176,11 @@ recommended way to access this functionality.
 
 =head2 dump_overwrite
 
+Deprecated.  See L</really_erase_my_files> below, which does *not* mean
+the same thing as the old C<dump_overwrite> setting from previous releases.
+
+=head2 really_erase_my_files
+
 Default false.  If true, Loader will unconditionally delete any existing
 files before creating the new ones from scratch when dumping a schema to disk.
 
@@ -170,11 +189,15 @@ file, up to and including the final stanza which contains
 C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!>
 leaving any customizations you placed after that as they were.
 
-When C<dump_overwrite> is not set, if the output file already exists,
+When C<really_erase_my_files> is not set, if the output file already exists,
 but the aforementioned final stanza is not found, or the checksum
 contained there does not match the generated contents, Loader will
 croak and not touch the file.
 
+You should really be using version control on your schema classes (and all
+of the rest of your code for that matter).  Don't blame me if a bug in this
+code wipes something out when it shouldn't have, you've been warned.
+
 =head1 METHODS
 
 None of these methods are intended for direct invocation by regular
@@ -225,6 +248,10 @@ sub new {
     $self->{schema_class} ||= ( ref $self->{schema} || $self->{schema} );
     $self->{schema} ||= $self->{schema_class};
 
+    croak "dump_overwrite is deprecated.  Please read the"
+        . " DBIx::Class::Schema::Loader::Base documentation"
+            if $self->{dump_overwrite};
+
     $self->{relbuilder} = DBIx::Class::Schema::Loader::RelBuilder->new(
         $self->schema_class, $self->inflect_plural, $self->inflect_singular
     ) if !$self->{skip_relationships};
@@ -292,7 +319,7 @@ sub _load_external {
         $self->_ext_stmt($class, $_);
     }
     $self->_ext_stmt($class,
-        q|# End of lines loaded from '$real_inc_path' |
+        qq|# End of lines loaded from '$real_inc_path' |
     );
     close($fh)
         or croak "Failed to close $real_inc_path: $!";
@@ -338,9 +365,9 @@ sub rescan {
         }
     }
 
-    $self->_load_tables(@created);
+    my $loaded = $self->_load_tables(@created);
 
-    return map { $self->monikers->{$_} } @created;
+    return map { $self->monikers->{$_} } @$loaded;
 }
 
 sub _load_tables {
@@ -385,7 +412,7 @@ sub _load_tables {
     # Drop temporary cache
     delete $self->{_cache};
 
-    1;
+    return \@tables;
 }
 
 sub _get_dump_filename {
@@ -426,8 +453,26 @@ sub _dump_to_dir {
     my $schema_text =
           qq|package $schema_class;\n\n|
         . qq|use strict;\nuse warnings;\n\n|
-        . qq|use base 'DBIx::Class::Schema';\n\n|
-        . qq|__PACKAGE__->load_classes;\n|;
+        . qq|use base 'DBIx::Class::Schema';\n\n|;
+
+    
+    if ($self->use_namespaces) {
+        $schema_text .= qq|__PACKAGE__->load_namespaces|;
+        my $namespace_options;
+        for my $attr (qw(result_namespace
+                         resultset_namespace
+                         default_resultset_class)) {
+            if ($self->$attr) {
+                $namespace_options .= qq|    $attr => '| . $self->$attr . qq|',\n|
+            }
+        }
+        $schema_text .= qq|(\n$namespace_options)| if $namespace_options;
+        $schema_text .= qq|;\n|;
+    }
+    else {
+        $schema_text .= qq|__PACKAGE__->load_classes;\n|;
+
+    }
 
     $self->_write_classfile($schema_class, $schema_text);
 
@@ -449,15 +494,15 @@ sub _write_classfile {
     my $filename = $self->_get_dump_filename($class);
     $self->_ensure_dump_subdirs($class);
 
-    if (-f $filename && $self->dump_overwrite) {
+    if (-f $filename && $self->really_erase_my_files) {
         warn "Deleting existing file '$filename' due to "
-            . "'dump_overwrite' setting\n";
+            . "'really_erase_my_files' setting\n";
         unlink($filename);
     }    
 
     my $custom_content = $self->_get_custom_content($class, $filename);
 
-    $custom_content ||= qq|\n# You can replace this text with custom|
+    $custom_content ||= qq|\n\n# You can replace this text with custom|
         . qq| content, and it will be preserved on regeneration|
         . qq|\n1;\n|;
 
@@ -473,7 +518,7 @@ sub _write_classfile {
         or croak "Cannot open '$filename' for writing: $!";
 
     # Write the top half and its MD5 sum
-    print $fh $text . Digest::MD5::md5_base64($text) . "\n\n";
+    print $fh $text . Digest::MD5::md5_base64($text) . "\n";
 
     # Write out anything loaded via external partial class file in @INC
     print $fh qq|$_\n|
@@ -511,7 +556,7 @@ sub _get_custom_content {
         }
     }
 
-    croak "Cannot not overwrite '$filename' without 'dump_overwrite',"
+    croak "Cannot not overwrite '$filename' without 'really_erase_my_files',"
         . " it does not appear to have been generated by Loader"
             if !$found;
 
@@ -555,7 +600,19 @@ sub _make_src_class {
     my $schema_class = $self->schema_class;
 
     my $table_moniker = $self->_table2moniker($table);
-    my $table_class = $schema_class . q{::} . $table_moniker;
+    my @result_namespace = ($schema_class);
+    if ($self->use_namespaces) {
+        my $result_namespace = $self->result_namespace || 'Result';
+        if ($result_namespace =~ /^\+(.*)/) {
+            # Fully qualified namespace
+            @result_namespace =  ($1)
+        }
+        else {
+            # Relative namespace
+            push @result_namespace, $result_namespace;
+        }
+    }
+    my $table_class = join(q{::}, @result_namespace, $table_moniker);
 
     my $table_normalized = lc $table;
     $self->classes->{$table} = $table_class;
@@ -595,6 +652,12 @@ sub _setup_src_meta {
     }
     else {
         my %col_info_lc = map { lc($_), $col_info->{$_} } keys %$col_info;
+        my $fks = $self->_table_fk_info($table);
+        for my $fkdef (@$fks) {
+            for my $col (@{ $fkdef->{local_columns} }) {
+                $col_info_lc{$col}->{is_foreign_key} = 1;
+            }
+        }
         $self->_dbic_stmt(
             $table_class,
             'add_columns',
@@ -652,9 +715,10 @@ sub _load_relationships {
         $fkdef->{remote_source} =
             $self->monikers->{delete $fkdef->{remote_table}};
     }
+    my $tbl_uniq_info = $self->_table_uniq_info($table);
 
     my $local_moniker = $self->monikers->{$table};
-    my $rel_stmts = $self->{relbuilder}->generate_code($local_moniker, $tbl_fk_info);
+    my $rel_stmts = $self->{relbuilder}->generate_code($local_moniker, $tbl_fk_info, $tbl_uniq_info);
 
     foreach my $src_class (sort keys %$rel_stmts) {
         my $src_stmts = $rel_stmts->{$src_class};