>table(\"table")
[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;
74b8c39f 9use Scalar::Util ();
3e110410 10
11__PACKAGE__->mk_classdata(table_class => 'DBIx::Class::ResultSource::Table');
cda04c3a 12
24d67825 13__PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do
75d07914 14 # anything yet!
cda04c3a 15
e60dc79f 16sub _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
e60dc79f 44 return $table;
45}
46
75d07914 47=head1 NAME
cda04c3a 48
24d67825 49DBIx::Class::ResultSourceProxy::Table - provides a classdata table
50object and method proxies
cda04c3a 51
52=head1 SYNOPSIS
53
24d67825 54 __PACKAGE__->table('cd');
55 __PACKAGE__->add_columns(qw/cdid artist title year/);
56 __PACKAGE__->set_primary_key('cdid');
cda04c3a 57
58=head1 METHODS
59
cda04c3a 60=head2 add_columns
61
24d67825 62 __PACKAGE__->add_columns(qw/cdid artist title year/);
cda04c3a 63
64Adds columns to the current class and creates accessors for them.
65
66=cut
67
cda04c3a 68=head2 table
69
70 __PACKAGE__->table('tbl_name');
71
72Gets or sets the table name.
73
74=cut
75
76sub table {
77 my ($class, $table) = @_;
b98e75f6 78 return $class->result_source_instance->name unless $table;
74b8c39f 79 unless (Scalar::Util::blessed($table)) {
5a879106 80 $table = $class->table_class->new({
24d67825 81 $class->can('result_source_instance') ?
e60dc79f 82 %{$class->result_source_instance||{}} : (),
cda04c3a 83 name => $table,
84 result_class => $class,
b1fb2c94 85 source_name => undef,
5a879106 86 });
cda04c3a 87 }
e87bedbe 88
89 $class->mk_classdata('result_source_instance')
90 unless $class->can('result_source_instance');
91
92 $class->result_source_instance($table);
93
ade8df5b 94 return $class->result_source_instance->name;
cda04c3a 95}
96
988bf309 97=head2 has_column
98
99 if ($obj->has_column($col)) { ... }
100
101Returns 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
109Returns the column metadata hashref for a column. For a description of
110the various types of column data in this hashref, see
75d07914 111L<DBIx::Class::ResultSource/add_column>
988bf309 112
113=cut
cda04c3a 114
d7156e50 115=head2 columns
116
988bf309 117 my @column_names = $obj->columns;
118
119=cut
cda04c3a 120
cda04c3a 1211;
122
123=head1 AUTHORS
124
125Matt S. Trout <mst@shadowcatsystems.co.uk>
126
127=head1 LICENSE
128
129You may distribute this code under the same terms as Perl itself.
130
131=cut
132