initial merge of Schwern's CDBICompat work, with many thanks
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSourceProxy / Table.pm
CommitLineData
80c90f5d 1package DBIx::Class::ResultSourceProxy::Table;
cda04c3a 2
3use strict;
4use warnings;
5
80c90f5d 6use base qw/DBIx::Class::ResultSourceProxy/;
cda04c3a 7
3e110410 8use DBIx::Class::ResultSource::Table;
9
10__PACKAGE__->mk_classdata(table_class => 'DBIx::Class::ResultSource::Table');
cda04c3a 11
24d67825 12__PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do
75d07914 13 # anything yet!
cda04c3a 14
e60dc79f 15sub _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
75d07914 51=head1 NAME
cda04c3a 52
24d67825 53DBIx::Class::ResultSourceProxy::Table - provides a classdata table
54object and method proxies
cda04c3a 55
56=head1 SYNOPSIS
57
24d67825 58 __PACKAGE__->table('cd');
59 __PACKAGE__->add_columns(qw/cdid artist title year/);
60 __PACKAGE__->set_primary_key('cdid');
cda04c3a 61
62=head1 METHODS
63
cda04c3a 64=head2 add_columns
65
24d67825 66 __PACKAGE__->add_columns(qw/cdid artist title year/);
cda04c3a 67
68Adds columns to the current class and creates accessors for them.
69
70=cut
71
cda04c3a 72=head2 table
73
74 __PACKAGE__->table('tbl_name');
75
76Gets or sets the table name.
77
78=cut
79
80sub table {
81 my ($class, $table) = @_;
b98e75f6 82 return $class->result_source_instance->name unless $table;
cda04c3a 83 unless (ref $table) {
5a879106 84 $table = $class->table_class->new({
24d67825 85 $class->can('result_source_instance') ?
e60dc79f 86 %{$class->result_source_instance||{}} : (),
cda04c3a 87 name => $table,
88 result_class => $class,
b1fb2c94 89 source_name => undef,
5a879106 90 });
cda04c3a 91 }
e87bedbe 92
93 $class->mk_classdata('result_source_instance')
94 unless $class->can('result_source_instance');
95
96 $class->result_source_instance($table);
97
7fb16f1a 98 if ($class->can('schema_instance')) {
99 $class =~ m/([^:]+)$/;
100 $class->schema_instance->register_class($class, $class);
101 }
cda04c3a 102}
103
988bf309 104=head2 has_column
105
106 if ($obj->has_column($col)) { ... }
107
108Returns 1 if the class has a column of this name, 0 otherwise.
109
110=cut
111
112=head2 column_info
113
114 my $info = $obj->column_info($col);
115
116Returns the column metadata hashref for a column. For a description of
117the various types of column data in this hashref, see
75d07914 118L<DBIx::Class::ResultSource/add_column>
988bf309 119
120=cut
cda04c3a 121
d7156e50 122=head2 columns
123
988bf309 124 my @column_names = $obj->columns;
125
126=cut
cda04c3a 127
cda04c3a 1281;
129
130=head1 AUTHORS
131
132Matt S. Trout <mst@shadowcatsystems.co.uk>
133
134=head1 LICENSE
135
136You may distribute this code under the same terms as Perl itself.
137
138=cut
139