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