More internals cleanup, separated out ResultSourceInstance from TableInstance
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / TableInstance.pm
1 package DBIx::Class::TableInstance;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::ResultSourceInstance/;
7 use DBIx::Class::Table;
8
9 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet!
10
11 __PACKAGE__->mk_classdata('table_class' => 'DBIx::Class::Table');
12
13 =head1 NAME 
14
15 DBIx::Class::TableInstance - provides a classdata table object and method proxies
16
17 =head1 SYNOPSIS
18
19   __PACKAGE__->table('foo');
20   __PACKAGE__->add_columns(qw/id bar baz/);
21   __PACKAGE__->set_primary_key('id');
22
23 =head1 METHODS
24
25 =cut
26
27 sub _mk_column_accessors {
28   my ($class, @cols) = @_;
29   $class->mk_group_accessors('column' => @cols);
30 }
31
32 =head2 add_columns
33
34   __PACKAGE__->add_columns(qw/col1 col2 col3/);
35
36 Adds columns to the current class and creates accessors for them.
37
38 =cut
39
40 =head2 table
41
42   __PACKAGE__->table('tbl_name');
43   
44 Gets or sets the table name.
45
46 =cut
47
48 sub table {
49   my ($class, $table) = @_;
50   return $class->result_source_instance->name unless $table;
51   unless (ref $table) {
52     $table = $class->table_class->new(
53       {
54         name => $table,
55         result_class => $class,
56       });
57     if ($class->can('result_source_instance')) {
58       $table->{_columns} = { %{$class->result_source_instance->{_columns}||{}} };
59     }
60   }
61   $class->mk_classdata('result_source_instance' => $table);
62   if ($class->can('schema_instance')) {
63     $class =~ m/([^:]+)$/;
64     $class->schema_instance->register_class($class, $class);
65   }
66 }
67
68 =head2 has_column                                                                
69                                                                                 
70   if ($obj->has_column($col)) { ... }                                           
71                                                                                 
72 Returns 1 if the class has a column of this name, 0 otherwise.                  
73                                                                                 
74 =cut                                                                            
75
76 =head2 column_info                                                               
77                                                                                 
78   my $info = $obj->column_info($col);                                           
79                                                                                 
80 Returns the column metadata hashref for a column.
81                                                                                 
82 =cut                                                                            
83
84 =head2 columns
85
86   my @column_names = $obj->columns;                                             
87                                                                                 
88 =cut                                                                            
89
90 1;
91
92 =head1 AUTHORS
93
94 Matt S. Trout <mst@shadowcatsystems.co.uk>
95
96 =head1 LICENSE
97
98 You may distribute this code under the same terms as Perl itself.
99
100 =cut
101