X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema.pm;h=4ec608d0fb7848ba98bdc527b71645f6250646dc;hb=19feb86bd0beb518ffb5bcbfe3cf37d2cc9c1c21;hp=d9971d4939e87eb7cee30bd67ef0078c8f0d5f85;hpb=66d9ef6bf10ccbdc9391e81e5b1ad1b615fb67f7;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index d9971d4..4ec608d 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -2,6 +2,8 @@ package DBIx::Class::Schema; use strict; use warnings; + +use Carp qw/croak/; use UNIVERSAL::require; use base qw/DBIx::Class/; @@ -9,7 +11,7 @@ use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/Exception/); __PACKAGE__->mk_classdata('class_mappings' => {}); __PACKAGE__->mk_classdata('source_registrations' => {}); -__PACKAGE__->mk_classdata('storage_type' => 'DBI'); +__PACKAGE__->mk_classdata('storage_type' => '::DBI'); __PACKAGE__->mk_classdata('storage'); =head1 NAME @@ -18,40 +20,35 @@ DBIx::Class::Schema - composable schemas =head1 SYNOPSIS -in My/Schema.pm - package My::Schema; - use base qw/DBIx::Class::Schema/; - + + # load My::Schema::Foo, My::Schema::Bar, My::Schema::Baz __PACKAGE__->load_classes(qw/Foo Bar Baz/); -in My/Schema/Foo.pm - package My::Schema::Foo; - use base qw/DBIx::Class/; - __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example __PACKAGE__->table('foo'); - ... - -in My/DB.pm - - use My::Schema; - My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs); + my $schema1 = My::Schema->connect( + $dsn, + $user, + $password, + $attrs + ); -then in app code + my $schema2 = My::Schema->connect( ... ); - my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB + # fetch objects using My::Schema::Foo + my $resultset = $schema1->resultset('Foo')->search( ... ); + my @objects = $schema2->resultset('Foo')->search( ... ); =head1 DESCRIPTION -Creates database classes based on a schema. This allows you to have more than -one concurrent connection using the same database classes, by making -subclasses under a new namespace for each connection. If you only need one -class, you should probably use L directly instead. +Creates database classes based on a schema. This is the recommended way to +use L and allows you to use more than one concurrent connection +with your classes. NB: If you're used to L it's worth reading the L carefully as DBIx::Class does things a little differently. Note in @@ -61,7 +58,7 @@ particular which module inherits off which. =head2 register_class -Registers a class which isa ResultSourceInstance; equivalent to calling +Registers a class which isa ResultSourceProxy; equivalent to calling $schema->register_source($moniker, $class->result_source_instance); @@ -119,7 +116,7 @@ sub source { # if we got here, they probably passed a full class name my $mapped = $self->class_mappings->{$moniker}; - die "Can't find source for ${moniker}" + croak "Can't find source for ${moniker}" unless $mapped && exists $sreg->{$mapped}; return $sreg->{$mapped}; } @@ -232,7 +229,7 @@ you expect. sub compose_connection { my ($self, $target, @info) = @_; - my $base = 'DBIx::Class::ResultSetInstance'; + my $base = 'DBIx::Class::ResultSetProxy'; $base->require; my $schema = $self->compose_namespace($target, $base); $schema->connection(@info); @@ -290,14 +287,16 @@ sub setup_connection_class { =head2 connection(@args) Instantiates a new Storage object of type storage_type and passes the -arguments to $storage->connection_info. Sets the connection in-place on +arguments to $storage->connect_info. Sets the connection in-place on the schema. =cut sub connection { my ($self, @info) = @_; - my $storage_class = 'DBIx::Class::Storage::'.$self->storage_type; + my $storage_class = $self->storage_type; + $storage_class = 'DBIx::Class::Storage'.$storage_class + if $storage_class =~ m/^::/; $storage_class->require; my $storage = $storage_class->new; $storage->connect_info(\@info);