Simplified column handling code, moved primary key defs to Table.pm
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Table.pm
CommitLineData
ea2e61bf 1package DBIx::Class::Table;
2
3use strict;
4use warnings;
5
223b8fe3 6use DBIx::Class::ResultSet;
95a70f01 7
181d4574 8use Carp qw/croak/;
9
1edd1722 10use base qw/DBIx::Class/;
cda04c3a 11__PACKAGE__->load_components(qw/AccessorGroup/);
ea2e61bf 12
cda04c3a 13__PACKAGE__->mk_group_accessors('simple' =>
d7156e50 14 qw/_columns _primaries name resultset_class result_class storage/);
525035fb 15
34d52be2 16=head1 NAME
17
cda04c3a 18DBIx::Class::Table - Table object
34d52be2 19
20=head1 SYNOPSIS
21
22=head1 DESCRIPTION
23
8091aa91 24This class is responsible for defining and doing table-level operations on
25L<DBIx::Class> classes.
34d52be2 26
27=head1 METHODS
28
39fe0e65 29=cut
30
cda04c3a 31sub new {
32 my ($class, $attrs) = @_;
33 $class = ref $class if ref $class;
ec77fadc 34 my $new = bless({ %{$attrs || {}} }, $class);
cda04c3a 35 $new->{resultset_class} ||= 'DBIx::Class::ResultSet';
36 $new->{_columns} ||= {};
37 $new->{name} ||= "!!NAME NOT SET!!";
38 return $new;
ea2e61bf 39}
40
510ca912 41sub add_columns {
cda04c3a 42 my ($self, @cols) = @_;
43 while (my $col = shift @cols) {
d7156e50 44 $self->_columns->{$col} = (ref $cols[0] ? shift : {});
cda04c3a 45 }
a3018bd3 46}
47
d7156e50 48*add_column = \&add_columns;
8fe001e1 49
cda04c3a 50=head2 add_columns
510ca912 51
cda04c3a 52 $table->add_columns(qw/col1 col2 col3/);
39fe0e65 53
cda04c3a 54 $table->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...);
39fe0e65 55
cda04c3a 56Adds columns to the table object. If supplied key => hashref pairs uses
57the hashref as the column_info for that column.
39fe0e65 58
d7156e50 59=head2 add_column
60
61 $table->add_column('col' => \%info?);
62
63Convenience alias to add_columns
64
39fe0e65 65=cut
66
cda04c3a 67sub resultset {
68 my $self = shift;
69 my $rs_class = $self->resultset_class;
70 eval "use $rs_class;";
71 return $rs_class->new($self);
95a70f01 72}
73
8091aa91 74=head2 has_column
103647d5 75
76 if ($obj->has_column($col)) { ... }
77
cda04c3a 78Returns 1 if the table has a column of this name, 0 otherwise.
103647d5 79
80=cut
81
82sub has_column {
83 my ($self, $column) = @_;
84 return exists $self->_columns->{$column};
85}
86
8091aa91 87=head2 column_info
103647d5 88
89 my $info = $obj->column_info($col);
90
8091aa91 91Returns the column metadata hashref for a column.
103647d5 92
93=cut
94
95sub column_info {
96 my ($self, $column) = @_;
181d4574 97 croak "No such column $column" unless exists $self->_columns->{$column};
103647d5 98 return $self->_columns->{$column};
99}
100
d7156e50 101=head2 columns
102
103647d5 103 my @column_names = $obj->columns;
104
105=cut
106
e7513319 107sub columns {
181d4574 108 croak "columns() is a read-only accessor, did you mean add_columns()?" if (@_ > 1);
e7513319 109 return keys %{shift->_columns};
110}
8b445e33 111
d7156e50 112=head2 set_primary_key(@cols)
113
114Defines one or more columns as primary key for this table. Should be
115called after C<add_columns>.
116
117=cut
118
119sub set_primary_key {
120 my ($self, @cols) = @_;
121 # check if primary key columns are valid columns
122 for (@cols) {
123 $self->throw("No such column $_ on table ".$self->name)
124 unless $self->has_column($_);
125 }
126 $self->_primaries(\@cols);
127}
128
129=head2 primary_columns
130
131Read-only accessor which returns the list of primary keys.
132
133=cut
134
135sub primary_columns {
136 return @{shift->_primaries||[]};
137}
138
139
ea2e61bf 1401;
34d52be2 141
34d52be2 142=head1 AUTHORS
143
daec44b8 144Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 145
146=head1 LICENSE
147
148You may distribute this code under the same terms as Perl itself.
149
150=cut
151