From: Matt S Trout Date: Mon, 9 Jan 2006 03:27:35 +0000 (+0000) Subject: DB.pm now keeps a Schema Instance X-Git-Tag: v0.05005~117^2~74 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7fb16f1a1bf1e7de4098b4f4ac3d061312f6bac3;p=dbsrgits%2FDBIx-Class.git DB.pm now keeps a Schema Instance --- diff --git a/lib/DBIx/Class/CDBICompat/ColumnCase.pm b/lib/DBIx/Class/CDBICompat/ColumnCase.pm index 3f49f23..ae0a200 100644 --- a/lib/DBIx/Class/CDBICompat/ColumnCase.pm +++ b/lib/DBIx/Class/CDBICompat/ColumnCase.pm @@ -17,7 +17,7 @@ sub _register_columns { sub add_columns { my ($class, @cols) = @_; - $class->table_instance->add_columns(map lc, @cols); + $class->result_source->add_columns(map lc, @cols); $class->_mk_column_accessors(@cols); } diff --git a/lib/DBIx/Class/CDBICompat/ColumnGroups.pm b/lib/DBIx/Class/CDBICompat/ColumnGroups.pm index 60a1e3c..77d6112 100644 --- a/lib/DBIx/Class/CDBICompat/ColumnGroups.pm +++ b/lib/DBIx/Class/CDBICompat/ColumnGroups.pm @@ -48,7 +48,7 @@ sub _register_column_group { $class->_column_groups($groups); } -sub all_columns { return shift->table_instance->columns; } +sub all_columns { return shift->result_source->columns; } sub primary_column { my ($class) = @_; diff --git a/lib/DBIx/Class/DB.pm b/lib/DBIx/Class/DB.pm index 6aaab51..f65375d 100644 --- a/lib/DBIx/Class/DB.pm +++ b/lib/DBIx/Class/DB.pm @@ -1,6 +1,7 @@ package DBIx::Class::DB; use base qw/DBIx::Class/; +use DBIx::Class::Schema; use DBIx::Class::Storage::DBI; use DBIx::Class::ClassResolver::PassThrough; use DBI; @@ -10,22 +11,16 @@ __PACKAGE__->load_components(qw/ResultSetInstance/); *dbi_commit = \&txn_commit; *dbi_rollback = \&txn_rollback; -sub storage { shift->storage_instance(@_); } +sub storage { shift->schema_instance(@_)->storage; } sub resultset_instance { my $class = shift; - my $table = $class->table_instance->new($class->table_instance); - $table->schema($class); - $table->result_class($class); - return $table->resultset; -} - -sub result_source { - my $class = shift; - my $table = $class->table_instance->new($class->table_instance); - $table->schema($class); - $table->result_class($class); - return $table; + my $source = $class->result_source; + if ($source->result_class ne $class) { + $source = $source->new($source); + $source->result_class($class); + } + return $source->resultset; } =head1 NAME @@ -58,7 +53,7 @@ This class provides a simple way of specifying a database connection. Sets or gets the storage backend. Defaults to L. -=head2 class_resolver +=head2 class_resolver ****DEPRECATED**** Sets or gets the class to use for resolving a class. Defaults to L, which returns whatever you give @@ -82,7 +77,8 @@ sub connection { my ($class, @info) = @_; my $storage = DBIx::Class::Storage::DBI->new; $storage->connect_info(\@info); - $class->mk_classdata('storage_instance' => $storage); + my $schema = bless({ storage => $storage }, 'DBIx::Class::Schema'); + $class->mk_classdata('schema_instance' => $schema); } =head2 txn_begin @@ -109,7 +105,10 @@ Rolls back the current transaction. sub txn_rollback { $_[0]->storage->txn_rollback } -sub resolve_class { return shift->class_resolver->class(@_); } +sub resolve_class { + warn "resolve_class deprecated as of 0.04999_02"; + return shift->class_resolver->class(@_); +} 1; diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 2bbbb03..f0e40e9 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -221,7 +221,7 @@ sub search_related { #use Data::Dumper; warn Dumper($query); #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]}); delete $attrs->{_action}; - return $self->resolve_class($rel_obj->{class} + return $self->result_source->schema->resultset($rel_obj->{class} )->search($query, $attrs); } @@ -296,7 +296,7 @@ sub set_from_related { $self->throw( "set_from_related can only handle a hash condition; the " ."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar')) unless ref $cond eq 'HASH'; - my $f_class = $self->resolve_class($rel_obj->{class}); + my $f_class = $self->result_source->schema->class($rel_obj->{class}); $self->throw( "Object $f_obj isn't a ".$f_class ) unless $f_obj->isa($f_class); foreach my $key (keys %$cond) { diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 32bc631..b64711e 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -247,11 +247,13 @@ Called by ResultSet to inflate a result from storage sub inflate_result { my ($class, $me, $prefetch) = @_; #use Data::Dumper; print Dumper(@_); - my $new = bless({ _column_data => $me }, ref $class || $class); - $new->in_storage(1); + my $new = bless({ _column_data => $me, _in_storage => 1 }, + ref $class || $class); + my $schema; PRE: foreach my $pre (keys %{$prefetch||{}}) { my $rel_obj = $class->_relationships->{$pre}; - my $pre_class = $class->resolve_class($rel_obj->{class}); + $schema ||= $new->result_source->schema; + my $pre_class = $schema->class($rel_obj->{class}); my $fetched = $pre_class->inflate_result(@{$prefetch->{$pre}}); $class->throw("No accessor for prefetched $pre") unless defined $rel_obj->{attrs}{accessor}; diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index d900a27..678fe5e 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -67,10 +67,11 @@ compose_connection to create/modify all the existing database classes. =cut sub register_class { - my ($class, $name, $to_register) = @_; - my %reg = %{$class->class_registrations}; + my ($self, $name, $to_register) = @_; + my %reg = %{$self->class_registrations}; $reg{$name} = $to_register; - $class->class_registrations(\%reg); + $self->class_registrations(\%reg); + $to_register->result_source->schema($self); } =head2 registered_classes diff --git a/lib/DBIx/Class/TableInstance.pm b/lib/DBIx/Class/TableInstance.pm index ab81855..4cb8d7f 100644 --- a/lib/DBIx/Class/TableInstance.pm +++ b/lib/DBIx/Class/TableInstance.pm @@ -10,9 +10,9 @@ __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything __PACKAGE__->mk_classdata('table_class' => 'DBIx::Class::Table'); -sub iterator_class { shift->table_instance->resultset_class(@_) } -sub resultset_class { shift->table_instance->resultset_class(@_) } -sub _table_name { shift->table_instance->name } +sub iterator_class { shift->result_source->resultset_class(@_) } +sub resultset_class { shift->result_source->resultset_class(@_) } +sub _table_name { shift->result_source->name } =head1 NAME @@ -43,12 +43,12 @@ Adds columns to the current class and creates accessors for them. sub add_columns { my ($class, @cols) = @_; - $class->table_instance->add_columns(@cols); + $class->result_source->add_columns(@cols); $class->_mk_column_accessors(@cols); } sub _select_columns { - return shift->table_instance->columns; + return shift->result_source->columns; } =head2 table @@ -61,18 +61,22 @@ Gets or sets the table name. sub table { my ($class, $table) = @_; - return $class->table_instance->name unless $table; + return $class->result_source->name unless $table; unless (ref $table) { $table = $class->table_class->new( { name => $table, result_class => $class, }); - if ($class->can('table_instance')) { - $table->{_columns} = { %{$class->table_instance->{_columns}||{}} }; + if ($class->can('result_source')) { + $table->{_columns} = { %{$class->result_source->{_columns}||{}} }; } } - $class->mk_classdata('table_instance' => $table); + $class->mk_classdata('result_source' => $table); + if ($class->can('schema_instance')) { + $class =~ m/([^:]+)$/; + $class->schema_instance->register_class($class, $class); + } } =head2 has_column @@ -85,7 +89,7 @@ Returns 1 if the class has a column of this name, 0 otherwise. sub has_column { my ($self, $column) = @_; - return $self->table_instance->has_column($column); + return $self->result_source->has_column($column); } =head2 column_info @@ -98,7 +102,7 @@ Returns the column metadata hashref for a column. sub column_info { my ($self, $column) = @_; - return $self->table_instance->column_info($column); + return $self->result_source->column_info($column); } =head2 columns @@ -108,11 +112,11 @@ sub column_info { =cut sub columns { - return shift->table_instance->columns(@_); + return shift->result_source->columns(@_); } -sub set_primary_key { shift->table_instance->set_primary_key(@_); } -sub primary_columns { shift->table_instance->primary_columns(@_); } +sub set_primary_key { shift->result_source->set_primary_key(@_); } +sub primary_columns { shift->result_source->primary_columns(@_); } 1; diff --git a/t/testlib/CDBase.pm b/t/testlib/CDBase.pm index 6fdd422..80d8fc5 100644 --- a/t/testlib/CDBase.pm +++ b/t/testlib/CDBase.pm @@ -1,15 +1,6 @@ package CDBase; use strict; -use base qw(DBIx::Class); -__PACKAGE__->load_components(qw/CDBICompat Core DB/); - -use File::Temp qw/tempfile/; -my (undef, $DB) = tempfile(); -my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1 }); - -END { unlink $DB if -e $DB } - -__PACKAGE__->connection(@DSN); +use base qw(DBIx::Class::Test::SQLite); 1;