| 1 | package DBIx::Class::Schema::Loader::Dynamic; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw/DBIx::Class::Schema::Loader::DBI/; | ||
| 7 | use mro 'c3'; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | our $VERSION = '1.03'; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($self, %args) = @_; | ||
| 14 | my $class = ref $self || $self; | ||
| 15 | |||
| 16 | $args{dump_directory} ||= '/die/if/I/get/used'; | ||
| 17 | $args{left_base_classes} ||= 'DBIx::Class::Core'; | ||
| 18 | |||
| 19 | 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 | my $isa = $class . "::ISA"; | ||
| 25 | no strict 'refs'; @$isa = (ref $new); | ||
| 26 | } | ||
| 27 | |||
| 28 | eval("require $_") || die for @{$new->left_base_classes}; | ||
| 29 | bless $new, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _load_tables { | ||
| 33 | my ($self, @tables) = @_; | ||
| 34 | |||
| 35 | # Save the new tables to the tables list and compute monikers | ||
| 36 | foreach (@tables) { | ||
| 37 | $self->_tables->{$_->sql_name} = $_; | ||
| 38 | $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 | $self->_make_src_class($_) for @tables; | ||
| 44 | $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 | for my $table (@tables) { | ||
| 52 | my $moniker = $self->monikers->{$table->sql_name}; | ||
| 53 | my $class = $self->classes->{$table->sql_name}; | ||
| 54 | $self->schema->register_class($moniker=>$class); | ||
| 55 | } | ||
| 56 | |||
| 57 | $self->_load_relationships(\@tables); | ||
| 58 | |||
| 59 | # load user-defined customisations as 'mix-ins' if present. | ||
| 60 | for my $class (sort values %{$self->classes}) { | ||
| 61 | if (eval "require $class") { | ||
| 62 | printf STDERR "$class customisations loaded\n" if $self->debug; | ||
| 63 | next | ||
| 64 | } | ||
| 65 | my $err = $@; | ||
| 66 | next if $err =~ /Can't locate/; # It's not a sin.. | ||
| 67 | printf STDERR "WARNING errors loading customisations for $class.. %s\n", $err; | ||
| 68 | } | ||
| 69 | |||
| 70 | # rinse and repeat.. | ||
| 71 | $self->schema->source_registrations({}); | ||
| 72 | for my $table (@tables) { | ||
| 73 | my $moniker = $self->monikers->{$table->sql_name}; | ||
| 74 | my $class = $self->classes->{$table->sql_name}; | ||
| 75 | $self->schema->register_class($moniker=>$class); | ||
| 76 | } | ||
| 77 | |||
| 78 | # all table meta-data including relationships are now has fully 'registered'. | ||
| 79 | return \@tables; | ||
| 80 | } | ||
| 81 | |||
| 82 | # Override existing Loader::Base actions to actually run code rather than generate it.. | ||
| 83 | |||
| 84 | sub _dbic_stmt { | ||
| 85 | my ($self, $class, $method, @args) = @_; | ||
| 86 | printf STDERR "DBIC_STMT %s ( %s )\n", "$class->$method(@args);", Dumper(\@args) if $self->debug; | ||
| 87 | $class->$method(@args); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _inject { | ||
| 91 | my ($self, $class, @parents) = @_; | ||
| 92 | return unless @parents; | ||
| 93 | my $isa = "$class\::ISA"; | ||
| 94 | no strict 'refs'; | ||
| 95 | unshift @$isa, @parents; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _base_class_pod {} | ||
| 99 | sub _make_pod {} | ||
| 100 | sub _make_pod_heading {} | ||
| 101 | sub _pod {} | ||
| 102 | sub _pod_class_list {} | ||
| 103 | sub _pod_cut {} | ||
| 104 | sub _use {} | ||
| 105 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | __END__ |