use warnings;
use Carp::Clan qw/^DBIx::Class/;
+use Scalar::Util qw/weaken/;
use base qw/DBIx::Class/;
package Library::Schema;
use base qw/DBIx::Class::Schema/;
-
+
# load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD
__PACKAGE__->load_classes(qw/CD Book DVD/);
$password,
{ AutoCommit => 0 },
);
-
+
my $schema2 = Library::Schema->connect($coderef_returning_dbh);
# fetch objects using Library::Schema::DVD
$reg{$moniker} = $source;
$self->source_registrations(\%reg);
$source->schema($self);
+ weaken($source->{schema}) if ref($self);
if ($source->result_class) {
my %map = %{$self->class_mappings};
$map{$source->result_class} = $moniker;
sub load_classes {
my ($class, @params) = @_;
-
+
my %comps_for;
-
+
if (@params) {
foreach my $param (@params) {
if (ref $param eq 'ARRAY') {
# filter out commented entries
my @modules = grep { $_ !~ /^#/ } @$param;
-
+
push (@{$comps_for{$class}}, @modules);
}
elsif (ref $param eq 'HASH') {
foreach my $prefix (keys %comps_for) {
foreach my $comp (@{$comps_for{$prefix}||[]}) {
my $comp_class = "${prefix}::${comp}";
- eval "use $comp_class"; # If it fails, assume the user fixed it
- if ($@) {
- $comp_class =~ s/::/\//g;
- die $@ unless $@ =~ /Can't locate.+$comp_class\.pm\sin\s\@INC/;
- warn $@ if $@;
+ $class->ensure_class_loaded($comp_class);
+ $comp_class->source_name($comp) unless $comp_class->source_name;
+
+ push(@to_register, [ $comp_class->source_name, $comp_class ]);
+ }
+ }
+ }
+ Class::C3->reinitialize;
+
+ foreach my $to (@to_register) {
+ $class->register_class(@$to);
+ # if $class->can('result_source_instance');
+ }
+}
+
+=head2 load_namespaces
+
+=over 4
+
+=item Arguments: %options?
+
+=back
+
+This is an alternative to L</load_classes> above which assumes an alternative
+layout for automatic class loading. It assumes that all ResultSource classes
+to be loaded are underneath a sub- namespace of the schema called
+"ResultSource", and any corresponding ResultSet classes to be underneath a
+sub-namespace of the schema called "ResultSet".
+
+You can change the namespaces checked for ResultSources and ResultSets via
+the C<resultsource_namespace> and C<resultset_namespace> options, respectively.
+
+Any source which does not have an explicitly-defined corresponding ResultSet
+will have one created in the appropriate namespace for it, based on
+L<DBIx::Class::ResultSet>. If you wish to change this default ResultSet
+base class, you can do so via the C<default_resultset_base> option. (Your
+custom default should, of course, be based on L<DBIx::Class::ResultSet>
+itself).
+
+This method requires L<Module::Find> to be installed on the system.
+
+Example:
+
+ My::Schema->load_namespaces;
+ # loads My::Schema::ResultSource::CD, My::Schema::ResultSource::Artist,
+ # My::Schema::ResultSet::CD, etc...
+
+ My::Schema->load_namespaces(
+ resultsource_namespace => 'My::Schema::RSources',
+ resultset_namespace => 'My::Schema::RSets',
+ default_resultset_base => 'My::Schema::RSetBase',
+ );
+ # loads My::Schema::RSources::CD, My::Schema::RSources::Artist,
+ # My::Schema::RSets::CD, and if no such class exists on disk,
+ # creates My::Schema::RSets::Artist in memory based on the
+ # class My::Schema::RSetBase
+
+=cut
+
+sub load_namespaces {
+ my ($class, %args) = @_;
+
+ my $resultsource_namespace = $args{resultsource_namespace}
+ || ($class . '::ResultSource');
+ my $resultset_namespace = $args{resultset_namespace}
+ || ($class . '::ResultSet');
+ my $default_resultset_base = $args{default_resultset_base}
+ || 'DBIx::Class::ResultSet';
+
+ eval "require Module::Find";
+ $class->throw_exception("Couldn't load Module::Find ($@)") if $@;
+
+ my %sources = map { (substr($_, length "${resultsource_namespace}::"), $_) }
+ Module::Find::findallmod($resultsource_namespace);
+
+ my %resultsets = map { (substr($_, length "${resultset_namespace}::"), $_) }
+ Module::Find::findallmod($resultset_namespace);
+
+ my @to_register;
+ {
+ no warnings qw/redefine/;
+ no strict qw/refs/;
+ local *Class::C3::reinitialize = sub { };
+ foreach my $source (keys %sources) {
+ my $source_class = $sources{$source};
+ $class->ensure_class_loaded($source_class);
+ $source_class->source_name($source) unless $source_class->source_name;
+ if(!$source_class->resultset_class
+ || $source_class->resultset_class eq 'DBIx::Class::ResultSet') {
+ if(my $rs_class = delete $resultsets{$source}) {
+ $class->ensure_class_loaded($rs_class);
+ $source_class->resultset_class($rs_class);
+ }
+ else {
+ my $rs_class = "$resultset_namespace\::$source";
+ @{"$rs_class\::ISA"} = ($default_resultset_base);
+ $source_class->resultset_class($rs_class);
}
- push(@to_register, [ $comp, $comp_class ]);
}
+
+ push(@to_register, [ $source_class->source_name, $source_class ]);
}
}
+
+ foreach (keys %resultsets) {
+ warn "load_namespaces found ResultSet $_ with no "
+ . 'corresponding ResultSource';
+ }
+
Class::C3->reinitialize;
foreach my $to (@to_register) {
$target_class => $source->result_class, ($base ? $base : ())
);
$source->result_class($target_class);
+ $target_class->result_source_instance($source)
+ if $target_class->can('result_source_instance');
}
}
Class::C3->reinitialize();
For example,
my $author_rs = $schema->resultset('Author')->find(1);
+ my @titles = qw/Night Day It/;
my $coderef = sub {
- my ($author, @titles) = @_;
-
# If any one of these fails, the entire transaction fails
- $author->create_related('books', {
+ $author_rs->create_related('books', {
title => $_
}) foreach (@titles);
my $rs;
eval {
- $rs = $schema->txn_do($coderef, $author_rs, qw/Night Day It/);
+ $rs = $schema->txn_do($coderef);
};
- if ($@) {
- my $error = $@;
- if ($error =~ /Rollback failed/) {
- die "something terrible has happened!";
- } else {
- deal_with_failed_transaction();
- }
+ if ($@) { # Transaction failed
+ die "something terrible has happened!" #
+ if ($@ =~ /Rollback failed/); # Rollback failed
+
+ deal_with_failed_transaction();
}
In a nested transaction (calling txn_do() from within a txn_do() coderef) only
sub txn_do {
my ($self, $coderef, @args) = @_;
- ref $self or $self->throw_exception
- ('Cannot execute txn_do as a class method');
+ $self->storage or $self->throw_exception
+ ('txn_do called on $schema without storage');
ref $coderef eq 'CODE' or $self->throw_exception
('$coderef must be a CODE reference');
Note that this feature is currently EXPERIMENTAL and may not work correctly
across all databases, or fully handle complex relationships.
+See L<SQL::Translator/METHODS> for a list of values for C<$sqlt_args>. The most
+common value for this would be C<< { add_drop_table => 1, } >> to have the SQL
+produced include a DROP TABLE statement for each table created.
+
=cut
sub deploy {
$self->storage->deploy($self, undef, $sqltargs);
}
+=head2 create_ddl_dir (EXPERIMENTAL)
+
+=over 4
+
+=item Arguments: \@databases, $version, $directory, $sqlt_args
+
+=back
+
+Creates an SQL file based on the Schema, for each of the specified
+database types, in the given directory.
+
+Note that this feature is currently EXPERIMENTAL and may not work correctly
+across all databases, or fully handle complex relationships.
+
+=cut
+
+sub create_ddl_dir
+{
+ my $self = shift;
+
+ $self->throw_exception("Can't create_ddl_dir without storage") unless $self->storage;
+ $self->storage->create_ddl_dir($self, @_);
+}
+
+=head2 ddl_filename (EXPERIMENTAL)
+
+ my $filename = $table->ddl_filename($type, $dir, $version)
+
+Creates a filename for a SQL file based on the table class name. Not
+intended for direct end user use.
+
+=cut
+
+sub ddl_filename
+{
+ my ($self, $type, $dir, $version) = @_;
+
+ my $filename = ref($self);
+ $filename =~ s/::/-/;
+ $filename = "$dir$filename-$version-$type.sql";
+
+ return $filename;
+}
+
1;
=head1 AUTHORS