fix case issues for MSSQL
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index d41f823..e0a42fa 100644 (file)
@@ -61,6 +61,7 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 naming
                                 datetime_timezone
                                 datetime_locale
+                                config_file
 /);
 
 
@@ -418,6 +419,11 @@ columns with the DATE/DATETIME/TIMESTAMP data_types.
 Sets the locale attribute for L<DBIx::Class::InflateColumn::DateTime> for all
 columns with the DATE/DATETIME/TIMESTAMP data_types.
 
+=head1 config_file
+
+File in Perl format, which should return a HASH reference, from which to read
+loader options.
+
 =head1 METHODS
 
 None of these methods are intended for direct invocation by regular
@@ -459,6 +465,18 @@ sub new {
 
     bless $self => $class;
 
+    if (my $config_file = $self->config_file) {
+        my $config_opts = do $config_file;
+
+        croak "Error reading config from $config_file: $@" if $@;
+
+        croak "Config file $config_file must be a hashref" unless ref($config_opts) eq 'HASH';
+
+        while (my ($k, $v) = each %$config_opts) {
+            $self->{$k} = $v unless exists $self->{$k};
+        }
+    }
+
     $self->_ensure_arrayref(qw/additional_classes
                                additional_base_classes
                                left_base_classes
@@ -914,6 +932,34 @@ sub _load_tables {
     }
 
     $self->_make_src_class($_) for @tables;
+
+
+    # sanity-check for moniker clashes
+    my $inverse_moniker_idx;
+    for (keys %{$self->monikers}) {
+      push @{$inverse_moniker_idx->{$self->monikers->{$_}}}, $_;
+    }
+
+    my @clashes;
+    for (keys %$inverse_moniker_idx) {
+      my $tables = $inverse_moniker_idx->{$_};
+      if (@$tables > 1) {
+        push @clashes, sprintf ("tables %s reduced to the same source moniker '%s'",
+          join (', ', map { "'$_'" } @$tables),
+          $_,
+        );
+      }
+    }
+
+    if (@clashes) {
+      die   'Unable to load schema - chosen moniker/class naming style results in moniker clashes. '
+          . 'Either change the naming style, or supply an explicit moniker_map: '
+          . join ('; ', @clashes)
+          . "\n"
+      ;
+    }
+
+
     $self->_setup_src_meta($_) for @tables;
 
     if(!$self->skip_relationships) {
@@ -1352,34 +1398,28 @@ sub _setup_src_meta {
     $self->_dbic_stmt($table_class,'table',$table_name);
 
     my $cols = $self->_table_columns($table);
-    my $col_info;
-    eval { $col_info = $self->__columns_info_for($table) };
-    if($@) {
-        $self->_dbic_stmt($table_class,'add_columns',@$cols);
-    }
-    else {
-        if ($self->_is_case_sensitive) {
-            for my $col (keys %$col_info) {
-                $col_info->{$col}{accessor} = lc $col
-                    if $col ne lc($col);
-            }
-        } else {
-            $col_info = { map { lc($_), $col_info->{$_} } keys %$col_info };
+    my $col_info = $self->__columns_info_for($table);
+    if ($self->_is_case_sensitive) {
+        for my $col (keys %$col_info) {
+            $col_info->{$col}{accessor} = lc $col
+                if $col ne lc($col);
         }
+    } else {
+        $col_info = { map { lc($_), $col_info->{$_} } keys %$col_info };
+    }
 
-        my $fks = $self->_table_fk_info($table);
+    my $fks = $self->_table_fk_info($table);
 
-        for my $fkdef (@$fks) {
-            for my $col (@{ $fkdef->{local_columns} }) {
-                $col_info->{$col}{is_foreign_key} = 1;
-            }
+    for my $fkdef (@$fks) {
+        for my $col (@{ $fkdef->{local_columns} }) {
+            $col_info->{$col}{is_foreign_key} = 1;
         }
-        $self->_dbic_stmt(
-            $table_class,
-            'add_columns',
-            map { $_, ($col_info->{$_}||{}) } @$cols
-        );
     }
+    $self->_dbic_stmt(
+        $table_class,
+        'add_columns',
+        map { $_, ($col_info->{$_}||{}) } @$cols
+    );
 
     my %uniq_tag; # used to eliminate duplicate uniqs