More internals cleanup, separated out ResultSourceInstance from TableInstance
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / TableInstance.pm
index 81c3c39..6a44c84 100644 (file)
@@ -3,16 +3,12 @@ package DBIx::Class::TableInstance;
 use strict;
 use warnings;
 
-use base qw/DBIx::Class/;
+use base qw/DBIx::Class::ResultSourceInstance/;
 use DBIx::Class::Table;
 
 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
 
-__PACKAGE__->mk_classdata('_resultset_class' => 'DBIx::Class::ResultSet');
-
-sub iterator_class { shift->table->resultset_class(@_) }
-sub resultset_class { shift->table->resultset_class(@_) }
-sub _table_name { shift->table->name }
+__PACKAGE__->mk_classdata('table_class' => 'DBIx::Class::Table');
 
 =head1 NAME 
 
@@ -41,22 +37,6 @@ Adds columns to the current class and creates accessors for them.
 
 =cut
 
-sub add_columns {
-  my ($class, @cols) = @_;
-  $class->table->add_columns(@cols);
-  $class->_mk_column_accessors(@cols);
-}
-
-sub resultset_instance {
-  my $class = shift;
-  $class->table->storage($class->storage);
-  $class->next::method($class->table->resultset);
-}
-
-sub _select_columns {
-  return shift->table->columns;
-}
-
 =head2 table
 
   __PACKAGE__->table('tbl_name');
@@ -67,32 +47,22 @@ Gets or sets the table name.
 
 sub table {
   my ($class, $table) = @_;
-  die "$class->table called and no table instance set yet" unless $table;
+  return $class->result_source_instance->name unless $table;
   unless (ref $table) {
-    $table = DBIx::Class::Table->new(
+    $table = $class->table_class->new(
       {
         name => $table,
         result_class => $class,
-        #storage => $class->storage,
       });
+    if ($class->can('result_source_instance')) {
+      $table->{_columns} = { %{$class->result_source_instance->{_columns}||{}} };
+    }
+  }
+  $class->mk_classdata('result_source_instance' => $table);
+  if ($class->can('schema_instance')) {
+    $class =~ m/([^:]+)$/;
+    $class->schema_instance->register_class($class, $class);
   }
-  $class->mk_classdata('table' => $table);
-}
-
-=head2 find_or_create
-
-  $class->find_or_create({ key => $val, ... });
-
-Searches for a record matching the search condition; if it doesn't find one,
-creates one and returns that instead.
-
-=cut
-
-sub find_or_create {
-  my $class    = shift;
-  my $hash     = ref $_[0] eq "HASH" ? shift: {@_};
-  my $exists = $class->find($hash);
-  return defined($exists) ? $exists : $class->create($hash);
 }
 
 =head2 has_column                                                                
@@ -103,11 +73,6 @@ Returns 1 if the class has a column of this name, 0 otherwise.
                                                                                 
 =cut                                                                            
 
-sub has_column {
-  my ($self, $column) = @_;
-  return $self->table->has_column($column);
-}
-
 =head2 column_info                                                               
                                                                                 
   my $info = $obj->column_info($col);                                           
@@ -116,21 +81,12 @@ Returns the column metadata hashref for a column.
                                                                                 
 =cut                                                                            
 
-sub column_info {
-  my ($self, $column) = @_;
-  return $self->table->column_info($column);
-}
+=head2 columns
 
-=head2 columns                                                                   
-                                                                                
   my @column_names = $obj->columns;                                             
                                                                                 
 =cut                                                                            
 
-sub columns {
-  return shift->table->columns(@_);
-}
-
 1;
 
 =head1 AUTHORS