>table(\"table")
[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   unless (Scalar::Util::blessed($table)) {
80     $table = $class->table_class->new({
81         $class->can('result_source_instance') ?
82           %{$class->result_source_instance||{}} : (),
83         name => $table,
84         result_class => $class,
85         source_name => undef,
86     });
87   }
88
89   $class->mk_classdata('result_source_instance')
90     unless $class->can('result_source_instance');
91
92   $class->result_source_instance($table);
93
94   return $class->result_source_instance->name;
95 }
96
97 =head2 has_column
98
99   if ($obj->has_column($col)) { ... }
100
101 Returns 1 if the class has a column of this name, 0 otherwise.
102
103 =cut
104
105 =head2 column_info
106
107   my $info = $obj->column_info($col);
108
109 Returns the column metadata hashref for a column. For a description of
110 the various types of column data in this hashref, see
111 L<DBIx::Class::ResultSource/add_column>
112
113 =cut
114
115 =head2 columns
116
117   my @column_names = $obj->columns;
118
119 =cut
120
121 1;
122
123 =head1 AUTHORS
124
125 Matt S. Trout <mst@shadowcatsystems.co.uk>
126
127 =head1 LICENSE
128
129 You may distribute this code under the same terms as Perl itself.
130
131 =cut
132