File Coverage

blib/lib/DBIx/Class/Schema/Loader/Dynamic.pm
Criterion Covered Total %
statement 61 64 95.3
branch 5 10 50.0
condition 4 9 44.4
subroutine 13 18 72.2
pod 1 1 100.0
total 84 102 82.3


line stmt bran cond sub pod time code
1             package DBIx::Class::Schema::Loader::Dynamic;
2              
3 1     1   57186 use strict;
  1         3  
  1         27  
4 1     1   6 use warnings;
  1         4  
  1         47  
5              
6 1     1   9 use base qw/DBIx::Class::Schema::Loader::DBI/;
  1         2  
  1         531  
7 1     1   208100 use mro 'c3';
  1         18  
  1         12  
8 1     1   47 use Data::Dumper;
  1         3  
  1         205  
9              
10             our $VERSION = '1.05';
11              
12             sub new {
13 1     1 1 81233 my ($self, %args) = @_;
14 1   33     13 my $class = ref $self || $self;
15              
16 1   50     13 $args{dump_directory} ||= '/die/if/I/get/used';
17 1   50     4 $args{left_base_classes} ||= ['DBIx::Class::Core'];
18              
19 1         10 my $new = $self->next::method(%args);
20              
21             # The loader 'factory' returns a more engine-specific subclass, e.g. DBIC:S:L::DBI::Pg. So,
22             # I'll have what she's having..
23             {
24 1         57561 my $isa = $class . "::ISA";
  1         5  
25 1     1   7 no strict 'refs'; @$isa = (ref $new);
  1         2  
  1         487  
  1         26  
26             }
27              
28 1   50     5 eval("require $_") || die for @{$new->left_base_classes};
  1         11  
29 1         68579 bless $new, $class;
30             }
31              
32             sub _load_tables {
33 1     1   7368 my ($self, @tables) = @_;
34              
35             # Save the new tables to the tables list and compute monikers
36 1         4 foreach (@tables) {
37 3         150553 $self->_tables->{$_->sql_name} = $_;
38 3         321 $self->monikers->{$_->sql_name} = $self->_table2moniker($_);
39             }
40              
41             # "check for moniker clashes": NEED TO FACTOR OUT THIS ALGORITHM FROM ::Base. leave it out for now.
42              
43 1         1698 $self->_make_src_class($_) for @tables;
44 1         11 $self->_setup_src_meta($_) for @tables;
45              
46             # Here's the "Rinse-and-Repeat" Catch-22 that causes so much dbics::loader agony:
47             # - 'register_class' freezes what we know about the class so far.
48             # - relationships cannot be dynamically added until classes are registered.
49             # Solution: register all classes while still unrelated, then build relationships, then wipe-and-reregister.
50              
51 1         87 for my $table (@tables) {
52 3         903 my $moniker = $self->monikers->{$table->sql_name};
53 3         102 my $class = $self->classes->{$table->sql_name};
54 3         68 $self->schema->register_class($moniker=>$class);
55             }
56              
57 1         396 $self->_load_relationships(\@tables);
58              
59             # load user-defined customisations as 'mix-ins' if present.
60 1         435 for my $class (sort values %{$self->classes}) {
  1         10  
61 3 50       159 if (eval "require $class") {
62 0 0       0 printf STDERR "$class customisations loaded\n" if $self->debug;
63             next
64 0         0 }
65 3         17 my $err = $@;
66 3 50       15 next if $err =~ /Can't locate/; # It's not a sin..
67 0         0 printf STDERR "WARNING errors loading customisations for $class.. %s\n", $err;
68             }
69              
70             # rinse and repeat..
71 1         28 $self->schema->source_registrations({});
72 1         25 for my $table (@tables) {
73 3         875 my $moniker = $self->monikers->{$table->sql_name};
74 3         75 my $class = $self->classes->{$table->sql_name};
75 3         59 $self->schema->register_class($moniker=>$class);
76             }
77              
78             # all table meta-data including relationships are now has fully 'registered'.
79 1         398 return \@tables;
80             }
81              
82             # Override existing Loader::Base actions to actually run code rather than generate it..
83              
84             sub _dbic_stmt {
85 16     16   81976 my ($self, $class, $method, @args) = @_;
86 16 50       86 printf STDERR "DBIC_STMT %s ( %s )\n", "$class->$method(@args);", Dumper(\@args) if $self->debug;
87 16         672 $class->$method(@args);
88             }
89              
90             sub _inject {
91 6     6   34 my ($self, $class, @parents) = @_;
92 6 100       23 return unless @parents;
93 3         10 my $isa = "$class\::ISA";
94 1     1   8 no strict 'refs';
  1         2  
  1         114  
95 3         100 unshift @$isa, @parents;
96             }
97              
98       0     sub _base_class_pod {}
99       0     sub _make_pod {}
100       0     sub _make_pod_heading {}
101       0     sub _pod {}
102       12     sub _pod_class_list {}
103       0     sub _pod_cut {}
104       3     sub _use {}
105              
106             1;
107              
108             __END__