a1a0ce3a8e2bc38a60b9c0d58aa5d2c47a35b8fb
[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 use Scalar::Util 'blessed';
10 use namespace::clean;
11
12 # FIXME - both of these *PROBABLY* need to be 'inherited_ro_instance' type
13 __PACKAGE__->mk_classaccessor(table_class => 'DBIx::Class::ResultSource::Table');
14 # FIXME: Doesn't actually do anything yet!
15 __PACKAGE__->mk_group_accessors( inherited => 'table_alias' );
16
17 sub _init_result_source_instance {
18     my $class = shift;
19
20     $class->mk_group_accessors( inherited => 'result_source_instance' )
21       unless $class->can('result_source_instance');
22
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;
28
29     my $table_class = $class->table_class;
30     $class->ensure_class_loaded($table_class);
31
32     if( $rsrc ) {
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         #
37         $rsrc = $table_class->new({
38             %$rsrc,
39             result_class => $class,
40             source_name => undef,
41             schema => undef
42         });
43     }
44     else {
45         $rsrc = $table_class->new({
46             name            => undef,
47             result_class    => $class,
48             source_name     => undef,
49         });
50     }
51
52     $class->result_source_instance($rsrc);
53 }
54
55 =head1 NAME
56
57 DBIx::Class::ResultSourceProxy::Table - provides a classdata table
58 object and method proxies
59
60 =head1 SYNOPSIS
61
62   __PACKAGE__->table('cd');
63   __PACKAGE__->add_columns(qw/cdid artist title year/);
64   __PACKAGE__->set_primary_key('cdid');
65
66 =head1 METHODS
67
68 =head2 add_columns
69
70   __PACKAGE__->add_columns(qw/cdid artist title year/);
71
72 Adds columns to the current class and creates accessors for them.
73
74 =cut
75
76 =head2 table
77
78   __PACKAGE__->table('tbl_name');
79
80 Gets or sets the table name.
81
82 =cut
83
84 sub table {
85   return $_[0]->result_source_instance->name unless @_ > 1;
86
87   my ($class, $table) = @_;
88
89   unless (blessed $table && $table->isa($class->table_class)) {
90
91     my $ancestor = $class->can('result_source_instance')
92       ? $class->result_source_instance
93       : undef
94     ;
95
96     my $table_class = $class->table_class;
97     $class->ensure_class_loaded($table_class);
98
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
105     $table = $table_class->new({
106         %{ $ancestor || {} },
107         name => $table,
108         result_class => $class,
109     });
110   }
111
112   $class->mk_group_accessors(inherited => 'result_source_instance')
113     unless $class->can('result_source_instance');
114
115   $class->result_source_instance($table)->name;
116 }
117
118 =head2 table_class
119
120   __PACKAGE__->table_class('DBIx::Class::ResultSource::Table');
121
122 Gets or sets the table class used for construction and validation.
123
124 =head2 has_column
125
126   if ($obj->has_column($col)) { ... }
127
128 Returns 1 if the class has a column of this name, 0 otherwise.
129
130 =head2 column_info
131
132   my $info = $obj->column_info($col);
133
134 Returns the column metadata hashref for a column. For a description of
135 the various types of column data in this hashref, see
136 L<DBIx::Class::ResultSource/add_column>
137
138 =head2 columns
139
140   my @column_names = $obj->columns;
141
142 =head1 FURTHER QUESTIONS?
143
144 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
145
146 =head1 COPYRIGHT AND LICENSE
147
148 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
149 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
150 redistribute it and/or modify it under the same terms as the
151 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
152
153 =cut
154
155 1;
156
157