X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSourceProxy%2FTable.pm;h=4cb733fae7c539ff543fa92175cf57a49d6d5f8f;hb=367eaf50970dd3fd223ce5e1f0337703f2a6c70e;hp=4f23097d558e82436d1d251b1a5740b5316e7a09;hpb=5a879106d5ffdd396b40152c2f8f462e93d2c3e4;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/ResultSourceProxy/Table.pm b/lib/DBIx/Class/ResultSourceProxy/Table.pm index 4f23097..4cb733f 100644 --- a/lib/DBIx/Class/ResultSourceProxy/Table.pm +++ b/lib/DBIx/Class/ResultSourceProxy/Table.pm @@ -4,27 +4,69 @@ use strict; use warnings; use base qw/DBIx::Class::ResultSourceProxy/; -use DBIx::Class::ResultSource::Table; - -__PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet! -__PACKAGE__->mk_classdata('table_class' => 'DBIx::Class::ResultSource::Table'); +use DBIx::Class::ResultSource::Table; +use Scalar::Util 'blessed'; +use namespace::clean; + +__PACKAGE__->mk_classaccessor(table_class => 'DBIx::Class::ResultSource::Table'); +# FIXME: Doesn't actually do anything yet! +__PACKAGE__->mk_group_accessors( inherited => 'table_alias' ); + +sub _init_result_source_instance { + my $class = shift; + + $class->mk_group_accessors( inherited => [ result_source_instance => '_result_source' ] ) + unless $class->can('result_source_instance'); + + # might be pre-made for us courtesy of DBIC::DB::result_source_instance() + my $rsrc = $class->result_source_instance; + + return $rsrc + if $rsrc and $rsrc->result_class eq $class; + + my $table_class = $class->table_class; + $class->ensure_class_loaded($table_class); + + if( $rsrc ) { + # + # NOTE! - not using clone() here and *NOT* marking source as derived + # from the one already existing on the class (if any) + # + $rsrc = $table_class->new({ + %$rsrc, + result_class => $class, + source_name => undef, + schema => undef + }); + } + else { + $rsrc = $table_class->new({ + name => undef, + result_class => $class, + source_name => undef, + }); + } + + $class->result_source_instance($rsrc); +} -=head1 NAME +=head1 NAME -DBIx::Class::ResultSourceProxy::Table - provides a classdata table object and method proxies +DBIx::Class::ResultSourceProxy::Table - provides a classdata table +object and method proxies =head1 SYNOPSIS - __PACKAGE__->table('foo'); - __PACKAGE__->add_columns(qw/id bar baz/); - __PACKAGE__->set_primary_key('id'); + __PACKAGE__->table('cd'); + __PACKAGE__->add_columns(qw/cdid artist title year/); + __PACKAGE__->set_primary_key('cdid'); =head1 METHODS =head2 add_columns - __PACKAGE__->add_columns(qw/col1 col2 col3/); + __PACKAGE__->add_columns(qw/cdid artist title year/); Adds columns to the current class and creates accessors for them. @@ -33,59 +75,105 @@ Adds columns to the current class and creates accessors for them. =head2 table __PACKAGE__->table('tbl_name'); - + Gets or sets the table name. =cut sub table { + return $_[0]->result_source->name unless @_ > 1; + my ($class, $table) = @_; - return $class->result_source_instance->name unless $table; - unless (ref $table) { - $table = $class->table_class->new({ - $class->can('result_source_instance') ? %{$class->result_source_instance} : (), + + unless (blessed $table && $table->isa($class->table_class)) { + + my $ancestor = $class->can('result_source_instance') + ? $class->result_source_instance + : undef + ; + + # Folks calling ->table on a class *might* expect the name + # to shift everywhere, but that can't happen + # So what we do is mark the ancestor as "dirty" + # even though it will have no "derived" link to the one we + # will use afterwards + if( + defined $ancestor + and + $ancestor->name ne $table + and + scalar $ancestor->__derived_instances + ) { + # Trigger the "descendants are dirty" logic, without giving + # it an explicit externally-callable interface + # This is ugly as sin, but likely saner in the long run + local $ancestor->{__in_rsrc_setter_callstack} = 1 + unless $ancestor->{__in_rsrc_setter_callstack}; + my $old_name = $ancestor->name; + $ancestor->set_rsrc_instance_specific_attribute( name => "\0" ); + $ancestor->set_rsrc_instance_specific_attribute( name => $old_name ); + } + + + my $table_class = $class->table_class; + $class->ensure_class_loaded($table_class); + + + # NOTE! - not using clone() here and *NOT* marking source as derived + # from the one already existing on the class (if any) + # This is logically sound as we are operating at class-level, and is + # in fact necessary, as otherwise any base-class with a "dummy" table + # will be marked as an ancestor of everything + $table = $table_class->new({ + %{ $ancestor || {} }, name => $table, result_class => $class, }); } - $class->mk_classdata('result_source_instance' => $table); - if ($class->can('schema_instance')) { - $class =~ m/([^:]+)$/; - $class->schema_instance->register_class($class, $class); - } + + $class->mk_group_accessors( inherited => [ result_source_instance => '_result_source' ] ) + unless $class->can('result_source_instance'); + + $class->result_source_instance($table)->name; } -=head2 has_column - - if ($obj->has_column($col)) { ... } - -Returns 1 if the class has a column of this name, 0 otherwise. - -=cut - -=head2 column_info - - my $info = $obj->column_info($col); - -Returns the column metadata hashref for a column. - -=cut +=head2 table_class -=head2 columns + __PACKAGE__->table_class('DBIx::Class::ResultSource::Table'); - my @column_names = $obj->columns; - -=cut +Gets or sets the table class used for construction and validation. -1; +=head2 has_column -=head1 AUTHORS + if ($obj->has_column($col)) { ... } -Matt S. Trout +Returns 1 if the class has a column of this name, 0 otherwise. -=head1 LICENSE +=head2 column_info -You may distribute this code under the same terms as Perl itself. + my $info = $obj->column_info($col); + +Returns the column metadata hashref for a column. For a description of +the various types of column data in this hashref, see +L + +=head2 columns + + my @column_names = $obj->columns; + +=head1 FURTHER QUESTIONS? + +Check the list of L. + +=head1 COPYRIGHT AND LICENSE + +This module is free software L +by the L. You can +redistribute it and/or modify it under the same terms as the +L. =cut +1; + +