Resolve $rsrc instance duality on metadata traversal
[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;
6298a324 9use Scalar::Util 'blessed';
10use namespace::clean;
3e110410 11
d46eac43 12# FIXME - both of these *PROBABLY* need to be 'inherited_ro_instance' type
e5053694 13__PACKAGE__->mk_classaccessor(table_class => 'DBIx::Class::ResultSource::Table');
e5053694 14# FIXME: Doesn't actually do anything yet!
15__PACKAGE__->mk_group_accessors( inherited => 'table_alias' );
cda04c3a 16
e60dc79f 17sub _init_result_source_instance {
18 my $class = shift;
19
f064a2ab 20 $class->mk_group_accessors( inherited => [ result_source_instance => '_result_source' ] )
e5053694 21 unless $class->can('result_source_instance');
e60dc79f 22
d46eac43 23 # might be pre-made for us courtesy of DBIC::DB::result_source_instance()
24 my $rsrc = $class->result_source_instance;
25
26 return $rsrc
27 if $rsrc and $rsrc->result_class eq $class;
e60dc79f 28
846e17a6 29 my $table_class = $class->table_class;
30 $class->ensure_class_loaded($table_class);
31
d46eac43 32 if( $rsrc ) {
0ff33686 33 #
34 # NOTE! - not using clone() here and *NOT* marking source as derived
35 # from the one already existing on the class (if any)
36 #
d46eac43 37 $rsrc = $table_class->new({
38 %$rsrc,
e60dc79f 39 result_class => $class,
40 source_name => undef,
41 schema => undef
42 });
43 }
44 else {
d46eac43 45 $rsrc = $table_class->new({
e60dc79f 46 name => undef,
47 result_class => $class,
48 source_name => undef,
49 });
50 }
51
d46eac43 52 $class->result_source_instance($rsrc);
e60dc79f 53}
54
75d07914 55=head1 NAME
cda04c3a 56
24d67825 57DBIx::Class::ResultSourceProxy::Table - provides a classdata table
58object and method proxies
cda04c3a 59
60=head1 SYNOPSIS
61
24d67825 62 __PACKAGE__->table('cd');
63 __PACKAGE__->add_columns(qw/cdid artist title year/);
64 __PACKAGE__->set_primary_key('cdid');
cda04c3a 65
66=head1 METHODS
67
cda04c3a 68=head2 add_columns
69
24d67825 70 __PACKAGE__->add_columns(qw/cdid artist title year/);
cda04c3a 71
72Adds columns to the current class and creates accessors for them.
73
74=cut
75
cda04c3a 76=head2 table
77
78 __PACKAGE__->table('tbl_name');
d4daee7b 79
cda04c3a 80Gets or sets the table name.
81
82=cut
83
84sub table {
d46eac43 85 return $_[0]->result_source_instance->name unless @_ > 1;
86
cda04c3a 87 my ($class, $table) = @_;
c2b7c5dc 88
6298a324 89 unless (blessed $table && $table->isa($class->table_class)) {
846e17a6 90
0ff33686 91 my $ancestor = $class->can('result_source_instance')
92 ? $class->result_source_instance
93 : undef
94 ;
95
846e17a6 96 my $table_class = $class->table_class;
97 $class->ensure_class_loaded($table_class);
98
0ff33686 99
100 # NOTE! - not using clone() here and *NOT* marking source as derived
101 # from the one already existing on the class (if any)
102 # This is logically sound as we are operating at class-level, and is
103 # in fact necessary, as otherwise any base-class with a "dummy" table
104 # will be marked as an ancestor of everything
846e17a6 105 $table = $table_class->new({
0ff33686 106 %{ $ancestor || {} },
cda04c3a 107 name => $table,
108 result_class => $class,
5a879106 109 });
cda04c3a 110 }
e87bedbe 111
f064a2ab 112 $class->mk_group_accessors( inherited => [ result_source_instance => '_result_source' ] )
e87bedbe 113 unless $class->can('result_source_instance');
114
d46eac43 115 $class->result_source_instance($table)->name;
cda04c3a 116}
117
8893ffd0 118=head2 table_class
119
120 __PACKAGE__->table_class('DBIx::Class::ResultSource::Table');
121
122Gets or sets the table class used for construction and validation.
123
988bf309 124=head2 has_column
125
126 if ($obj->has_column($col)) { ... }
127
128Returns 1 if the class has a column of this name, 0 otherwise.
129
988bf309 130=head2 column_info
131
132 my $info = $obj->column_info($col);
133
134Returns the column metadata hashref for a column. For a description of
135the various types of column data in this hashref, see
75d07914 136L<DBIx::Class::ResultSource/add_column>
988bf309 137
d7156e50 138=head2 columns
139
988bf309 140 my @column_names = $obj->columns;
141
a2bd3796 142=head1 FURTHER QUESTIONS?
cda04c3a 143
a2bd3796 144Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
cda04c3a 145
a2bd3796 146=head1 COPYRIGHT AND LICENSE
cda04c3a 147
a2bd3796 148This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
149by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
150redistribute it and/or modify it under the same terms as the
151L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
cda04c3a 152
a2bd3796 153=cut
cda04c3a 154
a2bd3796 1551;
cda04c3a 156
cda04c3a 157