Merge 'trunk' into 'cdbicompat_integration'
[dbsrgits/DBIx-Class.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     if ($class->can('schema_instance')) {
44         $class =~ m/([^:]+)$/;
45         $class->schema_instance->register_class($class, $class);
46     }
47
48     return $table;
49 }
50
51 =head1 NAME
52
53 DBIx::Class::ResultSourceProxy::Table - provides a classdata table
54 object and method proxies
55
56 =head1 SYNOPSIS
57
58   __PACKAGE__->table('cd');
59   __PACKAGE__->add_columns(qw/cdid artist title year/);
60   __PACKAGE__->set_primary_key('cdid');
61
62 =head1 METHODS
63
64 =head2 add_columns
65
66   __PACKAGE__->add_columns(qw/cdid artist title year/);
67
68 Adds columns to the current class and creates accessors for them.
69
70 =cut
71
72 =head2 table
73
74   __PACKAGE__->table('tbl_name');
75   
76 Gets or sets the table name.
77
78 =cut
79
80 sub table {
81   my ($class, $table) = @_;
82   return $class->result_source_instance->name unless $table;
83   unless (ref $table) {
84     $table = $class->table_class->new({
85         $class->can('result_source_instance') ?
86           %{$class->result_source_instance||{}} : (),
87         name => $table,
88         result_class => $class,
89         source_name => undef,
90     });
91   }
92
93   $class->mk_classdata('result_source_instance')
94     unless $class->can('result_source_instance');
95
96   $class->result_source_instance($table);
97
98   if ($class->can('schema_instance')) {
99     $class =~ m/([^:]+)$/;
100     $class->schema_instance->register_class($class, $class);
101   }
102   return $class->result_source_instance->name;
103 }
104
105 =head2 has_column
106
107   if ($obj->has_column($col)) { ... }
108
109 Returns 1 if the class has a column of this name, 0 otherwise.
110
111 =cut
112
113 =head2 column_info
114
115   my $info = $obj->column_info($col);
116
117 Returns the column metadata hashref for a column. For a description of
118 the various types of column data in this hashref, see
119 L<DBIx::Class::ResultSource/add_column>
120
121 =cut
122
123 =head2 columns
124
125   my @column_names = $obj->columns;
126
127 =cut
128
129 1;
130
131 =head1 AUTHORS
132
133 Matt S. Trout <mst@shadowcatsystems.co.uk>
134
135 =head1 LICENSE
136
137 You may distribute this code under the same terms as Perl itself.
138
139 =cut
140