Dynamically load necessary table classes
[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 8__PACKAGE__->mk_classdata(table_class => 'DBIx::Class::ResultSource::Table');
cda04c3a 9
24d67825 10__PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do
75d07914 11 # anything yet!
cda04c3a 12
e60dc79f 13sub _init_result_source_instance {
14 my $class = shift;
15
16 $class->mk_classdata('result_source_instance')
17 unless $class->can('result_source_instance');
18
19 my $table = $class->result_source_instance;
20 my $class_has_table_instance = ($table and $table->result_class eq $class);
21 return $table if $class_has_table_instance;
22
846e17a6 23 my $table_class = $class->table_class;
24 $class->ensure_class_loaded($table_class);
25
e60dc79f 26 if( $table ) {
846e17a6 27 $table = $table_class->new({
e60dc79f 28 %$table,
29 result_class => $class,
30 source_name => undef,
31 schema => undef
32 });
33 }
34 else {
846e17a6 35 $table = $table_class->new({
e60dc79f 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');
d4daee7b 71
cda04c3a 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;
cda04c3a 79 unless (ref $table) {
846e17a6 80
81 my $table_class = $class->table_class;
82 $class->ensure_class_loaded($table_class);
83
84 $table = $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
ade8df5b 98 return $class->result_source_instance->name;
cda04c3a 99}
100
988bf309 101=head2 has_column
102
103 if ($obj->has_column($col)) { ... }
104
105Returns 1 if the class has a column of this name, 0 otherwise.
106
107=cut
108
109=head2 column_info
110
111 my $info = $obj->column_info($col);
112
113Returns the column metadata hashref for a column. For a description of
114the various types of column data in this hashref, see
75d07914 115L<DBIx::Class::ResultSource/add_column>
988bf309 116
117=cut
cda04c3a 118
d7156e50 119=head2 columns
120
988bf309 121 my @column_names = $obj->columns;
122
123=cut
cda04c3a 124
cda04c3a 1251;
126
127=head1 AUTHORS
128
129Matt S. Trout <mst@shadowcatsystems.co.uk>
130
131=head1 LICENSE
132
133You may distribute this code under the same terms as Perl itself.
134
135=cut
136