Merge 'subclassed_rsset' into 'trunk'
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / ResultSourceProxy / Table.pm
1 package DBIx::Class::ResultSourceProxy::Table;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::ResultSourceProxy/;
7
8 use DBIx::Class::ResultSource::Table;
9
10 __PACKAGE__->mk_classdata(table_class => 'DBIx::Class::ResultSource::Table');
11
12 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do
13                                           # anything yet!
14
15 sub _init_result_source_instance {
16     my $class = shift;
17
18     $class->mk_classdata('result_source_instance')
19         unless $class->can('result_source_instance');
20
21     my $table = $class->result_source_instance;
22     my $class_has_table_instance = ($table and $table->result_class eq $class);
23     return $table if $class_has_table_instance;
24
25     if( $table ) {
26         $table = $class->table_class->new({
27             %$table,
28             result_class => $class,
29             source_name => undef,
30             schema => undef
31         });
32     }
33     else {
34         $table = $class->table_class->new({
35             name            => undef,
36             result_class    => $class,
37             source_name     => undef,
38         });
39     }
40
41     $class->result_source_instance($table);
42
43     return $table;
44 }
45
46 =head1 NAME
47
48 DBIx::Class::ResultSourceProxy::Table - provides a classdata table
49 object and method proxies
50
51 =head1 SYNOPSIS
52
53   __PACKAGE__->table('cd');
54   __PACKAGE__->add_columns(qw/cdid artist title year/);
55   __PACKAGE__->set_primary_key('cdid');
56
57 =head1 METHODS
58
59 =head2 add_columns
60
61   __PACKAGE__->add_columns(qw/cdid artist title year/);
62
63 Adds columns to the current class and creates accessors for them.
64
65 =cut
66
67 =head2 table
68
69   __PACKAGE__->table('tbl_name');
70   
71 Gets or sets the table name.
72
73 =cut
74
75 sub table {
76   my ($class, $table) = @_;
77   return $class->result_source_instance->name unless $table;
78   unless (ref $table) {
79     $table = $class->table_class->new({
80         $class->can('result_source_instance') ?
81           %{$class->result_source_instance||{}} : (),
82         name => $table,
83         result_class => $class,
84         source_name => undef,
85     });
86   }
87
88   $class->mk_classdata('result_source_instance')
89     unless $class->can('result_source_instance');
90
91   $class->result_source_instance($table);
92
93   return $class->result_source_instance->name;
94 }
95
96 =head2 has_column
97
98   if ($obj->has_column($col)) { ... }
99
100 Returns 1 if the class has a column of this name, 0 otherwise.
101
102 =cut
103
104 =head2 column_info
105
106   my $info = $obj->column_info($col);
107
108 Returns the column metadata hashref for a column. For a description of
109 the various types of column data in this hashref, see
110 L<DBIx::Class::ResultSource/add_column>
111
112 =cut
113
114 =head2 columns
115
116   my @column_names = $obj->columns;
117
118 =cut
119
120 1;
121
122 =head1 AUTHORS
123
124 Matt S. Trout <mst@shadowcatsystems.co.uk>
125
126 =head1 LICENSE
127
128 You may distribute this code under the same terms as Perl itself.
129
130 =cut
131